1. 姓名" />

      亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

      <bdo id="k3c88"><span id="k3c88"><del id="k3c88"></del></span></bdo>
    2. 【吉光片羽】MVC 導(dǎo)出Word的兩種方式

      系統(tǒng) 1862 0

      ? ? 1.直接將Html轉(zhuǎn)成Word。MVC自帶FileResult很好用。Html中我們也可以嵌入自己的樣式。

      ? ? html:

              
                <
              
              
                div 
              
              
                id
              
              
                ="target"
              
              
                >
              
              
                <
              
              
                style
              
              
                >
              
              
                
      
              table
              
              
                {
              
              
                border-collapse
              
              
                :
              
              
                 collapse
              
              
                ;
              
              
                border
              
              
                :
              
              
                 1px solid  black
              
              
                ;
              
              
                }
              
              
                
      
              td
              
              
                {
              
              
                border
              
              
                :
              
              
                 1px royalblue solid
              
              
                }
              
              
                </
              
              
                style
              
              
                >
              
              
                <
              
              
                img 
              
              
                src
              
              
                ="../../Content/eman_sm.png"
              
              
                 alt
              
              
                =""
              
              
                />
              
              
                <
              
              
                table
              
              
                >
              
              
                <
              
              
                tr
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              姓名
              
                </
              
              
                td
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              成績(jī)
              
                </
              
              
                td
              
              
                >
              
              
                </
              
              
                tr
              
              
                >
              
              
                <
              
              
                tr
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              張山
              
                </
              
              
                td
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              80分
              
                </
              
              
                td
              
              
                >
              
              
                </
              
              
                tr
              
              
                >
              
              
                <
              
              
                tr
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              李四
              
                </
              
              
                td
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              90分
              
                </
              
              
                td
              
              
                >
              
              
                </
              
              
                tr
              
              
                >
              
              
                </
              
              
                table
              
              
                >
              
              
                </
              
              
                div
              
              
                >
              
              
                <
              
              
                div 
              
              
                style
              
              
                ="color: red"
              
              
                ></
              
              
                div
              
              
                >
              
              
                <
              
              
                a 
              
              
                id
              
              
                ="ea"
              
              
                 href
              
              
                ="#"
              
              
                >
              
              導(dǎo)出word
              
                </
              
              
                a
              
              
                >
              
              
                <
              
              
                a 
              
              
                href
              
              
                ="@Url.Action("
              
              
                ExportWords")"
              
              
                >
              
              ExportWords
              
                </
              
              
                a
              
              
                >
              
            
      View Code
            <script>
            
              
      
          $(
            
            "#ea").click(
            
              function
            
            
               () {
      
              
            
            
              var
            
             html = $("#target"
            
              ).html();
      
              window.open(
            
            "/Home/ExportWord?html=" +
            
               html);
      
          })
      
      
            
            </script>
          

      get方法會(huì)受到url長(zhǎng)度的影響,可以換成隱藏的form提交。

              
                <
              
              
                form 
              
              
                id
              
              
                ="form1"
              
              
                 action
              
              
                ="/Home/ExportWord"
              
              
                >
              
              
                <
              
              
                input 
              
              
                type
              
              
                ="hidden"
              
              
                 value
              
              
                =""
              
              
                  name
              
              
                ="html"
              
              
                 id
              
              
                ="cc"
              
              
                />
              
              
                <
              
              
                input 
              
              
                type
              
              
                ="submit"
              
              
                 id
              
              
                ="st"
              
              
                />
              
              
                </
              
              
                form
              
              
                >
              
              
                <
              
              
                div 
              
              
                id
              
              
                ="target"
              
              
                >
              
              
                <
              
              
                style
              
              
                >
              
              
                
      
              table
              
              
                {
              
              
                border-collapse
              
              
                :
              
              
                 collapse
              
              
                ;
              
              
                border
              
              
                :
              
              
                 1px solid  black
              
              
                ;
              
              
                }
              
              
                
      
              td
              
              
                {
              
              
                border
              
              
                :
              
              
                 1px royalblue solid
              
              
                }
              
              
                </
              
              
                style
              
              
                >
              
              
                <
              
              
                table
              
              
                >
              
              
                <
              
              
                tr
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              姓名
              
                </
              
              
                td
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              成績(jī)
              
                </
              
              
                td
              
              
                >
              
              
                </
              
              
                tr
              
              
                >
              
              
                <
              
              
                tr
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              張山
              
                </
              
              
                td
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              80分
              
                </
              
              
                td
              
              
                >
              
              
                </
              
              
                tr
              
              
                >
              
              
                <
              
              
                tr
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              李四
              
                </
              
              
                td
              
              
                >
              
              
                <
              
              
                td
              
              
                >
              
              90分
              
                </
              
              
                td
              
              
                >
              
              
                </
              
              
                tr
              
              
                >
              
              
                </
              
              
                table
              
              
                >
              
              
                </
              
              
                div
              
              
                >
              
              
                <
              
              
                div 
              
              
                style
              
              
                ="color: red"
              
              
                ></
              
              
                div
              
              
                >
              
              
                <
              
              
                a 
              
              
                id
              
              
                ="ea"
              
              
                 href
              
              
                ="#"
              
              
                >
              
              導(dǎo)出word
              
                </
              
              
                a
              
              
                >
              
              
                <
              
              
                a 
              
              
                href
              
              
                ="@Url.Action("
              
              
                ExportWords")"
              
              
                >
              
              ExportWords
              
                </
              
              
                a
              
              
                >
              
              
                <
              
              
                script
              
              
                >
              
              
                
      
          $(
              
              
                "
              
              
                #ea
              
              
                "
              
              
                ).click(
              
              
                function
              
              
                 () {
      
              
              
              
                var
              
              
                 html 
              
              
                =
              
              
                 $(
              
              
                "
              
              
                #target
              
              
                "
              
              
                ).html();
      
              $(
              
              
                "
              
              
                #cc
              
              
                "
              
              
                ).val(html);
      
              $(
              
              
                "
              
              
                #st
              
              
                "
              
              
                ).click();
      
          })
      
      
              
              
                </
              
              
                script
              
              
                >
              
            
      View Code

      ?

      Action:

             [ValidateInput(
            
              false
            
            
              )]
      
              
            
            
              public
            
             FileResult ExportWord(
            
              string
            
            
               html)
      
              {
      
                  StringBuilder sb 
            
            = 
            
              new
            
            
               StringBuilder();
      
                  sb.Append(
            
            
              "
            
            
              <!DOCTYPE html>
            
            
              "
            
            
              );
      
                  sb.Append(
            
            
              "
            
            
              <body>
            
            
              "
            
            
              );
      
                  sb.Append(html);
      
                  sb.Append(
            
            
              "
            
            
              </body>
            
            
              "
            
            
              );
      
                  
            
            
              var
            
             byteArray =
            
               System.Text.Encoding.Default.GetBytes(sb.ToString());
      
                  Response.ContentEncoding 
            
            = System.Text.Encoding.GetEncoding(
            
              "
            
            
              gb2312
            
            
              "
            
            
              );
      
                  
            
            
              return
            
             File(byteArray, 
            
              "
            
            
              application/ms-word
            
            
              "
            
            , 
            
              "
            
            
              wordtest
            
            
              "
            
             + 
            
              "
            
            
              .doc
            
            
              "
            
            
              );
      
              }
            
          

      這樣的問(wèn)題是導(dǎo)出的圖片是引用型的,路徑一變化就不會(huì)顯示了。優(yōu)點(diǎn)就是速度很快。

      ? 2.使用Microsoft.Office.Interop.Word 提供的方法創(chuàng)建一個(gè)word。

              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 創(chuàng)建一個(gè)word
      
              
              
              
                ///
              
              
                </summary>
              
              
                ///
              
              
                <returns></returns>
              
              
                public
              
              
                 ActionResult ExportWords()
      
              {
      
                  CreateAWord();
      
                  SetPageHeader(
              
              
                "
              
              
                測(cè)試創(chuàng)建一個(gè)Word文檔
              
              
                "
              
              
                );
      
                  InsertText(
              
              
                "
              
              
                這是一個(gè)測(cè)試文檔的內(nèi)容
              
              
                "
              
              , 
              
                14
              
              , WdColor.wdColorGray10, 
              
                1
              
              
                , WdParagraphAlignment.wdAlignParagraphLeft);
      
                  NewLine();
      
                  InsertPicture(
              
              
                @"
              
              
                C:\Users\Administrator\Desktop\duoceshi.png
              
              
                "
              
              
                );
      
                  InsertTable();
      
                  SaveWord(
              
              
                @"
              
              
                C:\Users\Administrator\Desktop\\test.doc
              
              
                "
              
              
                );
      
                  CloseDocument(
              
              
                @"
              
              
                C:\Users\Administrator\Desktop\\test.doc
              
              
                "
              
              
                );
      
                  
              
              
                return
              
              
                null
              
              
                ;
      
              }
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 Word應(yīng)用對(duì)象  
      
              
              
              
                ///
              
              
                </summary>
              
              
                private
              
              
                 Microsoft.Office.Interop.Word.Application _wordApplication;
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 word 文件對(duì)象 
      
              
              
              
                ///
              
              
                </summary>
              
              
                private
              
              
                 Microsoft.Office.Interop.Word.Document _wordDocument;
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                  創(chuàng)建文檔  如果報(bào)錯(cuò):類型“Microsoft.Office.Interop.Word.ApplicationClass”未定義構(gòu)造函數(shù) ; 解決方法:在其中點(diǎn)開(kāi)“引用”文件夾,在"Microsoft.Office.Interop.Word" 上點(diǎn)擊鼠標(biāo)右鍵,選擇“屬性”,將屬性中的“嵌入互操作類型”的值改為“false”即可
      
              
              
              
                ///
              
              
                </summary>
              
              
                public
              
              
                void
              
              
                 CreateAWord()
      
              {
      
                  
              
              
                //
              
              
                實(shí)例化word應(yīng)用對(duì)象 
              
              
                this
              
              ._wordApplication = 
              
                new
              
              
                 Microsoft.Office.Interop.Word.ApplicationClass();
      
                  Object myNothing 
              
              =
              
                 System.Reflection.Missing.Value;
      
                  
              
              
                this
              
              ._wordDocument = 
              
                this
              
              ._wordApplication.Documents.Add(
              
                ref
              
               myNothing, 
              
                ref
              
               myNothing, 
              
                ref
              
               myNothing, 
              
                ref
              
              
                 myNothing);
      
              }
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 添加頁(yè)眉 
      
              
              
              
                ///
              
              
                </summary>
              
              
                ///
              
              
                <param name="pPageHeader"></param>
              
              
                public
              
              
                void
              
               SetPageHeader(
              
                string
              
              
                 pPageHeader)
      
              {
      
                  
              
              
                //
              
              
                添加頁(yè)眉 
              
              
                this
              
              ._wordApplication.ActiveWindow.View.Type =
              
                 Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
      
                  
              
              
                this
              
              ._wordApplication.ActiveWindow.View.SeekView =
              
                 Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
      
                  
              
              
                this
              
              
                ._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
      
                  
              
              
                //
              
              
                設(shè)置中間對(duì)齊 
              
              
                this
              
              ._wordApplication.Selection.ParagraphFormat.Alignment =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
      
                  
              
              
                //
              
              
                跳出頁(yè)眉設(shè)置 
              
              
                this
              
              ._wordApplication.ActiveWindow.View.SeekView =
              
                 Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
      
              }
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 插入文字
      
              
              
              
                ///
              
              
                </summary>
              
              
                ///
              
              
                <param name="pText">
              
              
                文本信息
              
              
                </param>
              
              
                ///
              
              
                <param name="pFontSize">
              
              
                字體大小
              
              
                </param>
              
              
                ///
              
              
                <param name="pFontColor">
              
              
                字體顏色
              
              
                </param>
              
              
                ///
              
              
                <param name="pFontBold">
              
              
                字體粗體
              
              
                </param>
              
              
                ///
              
              
                <param name="ptextAlignment">
              
              
                方向
              
              
                </param>
              
              
                public
              
              
                void
              
               InsertText(
              
                string
              
               pText, 
              
                int
              
               pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, 
              
                int
              
              
                 pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
      
              {
      
                  
              
              
                //
              
              
                設(shè)置字體樣式以及方向 
              
              
                this
              
              ._wordApplication.Application.Selection.Font.Size =
              
                 pFontSize;
      
                  
              
              
                this
              
              ._wordApplication.Application.Selection.Font.Bold =
              
                 pFontBold;
      
                  
              
              
                this
              
              ._wordApplication.Application.Selection.Font.Color =
              
                 pFontColor;
      
                  
              
              
                this
              
              ._wordApplication.Application.Selection.ParagraphFormat.Alignment =
              
                 ptextAlignment;
      
                  
              
              
                this
              
              
                ._wordApplication.Application.Selection.TypeText(pText);
      
              }
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 換行
      
              
              
              
                ///
              
              
                </summary>
              
              
                public
              
              
                void
              
              
                 NewLine()
      
              {
      
                  
              
              
                //
              
              
                換行 
              
              
                this
              
              
                ._wordApplication.Application.Selection.TypeParagraph();
      
              }
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 插入一個(gè)圖片 
      
              
              
              
                ///
              
              
                </summary>
              
              
                ///
              
              
                <param name="pPictureFileName"></param>
              
              
                public
              
              
                void
              
               InsertPicture(
              
                string
              
              
                 pPictureFileName)
      
              {
      
                  
              
              
                object
              
               myNothing =
              
                 System.Reflection.Missing.Value;
      
                  
              
              
                //
              
              
                圖片居中顯示 
              
              
                this
              
              ._wordApplication.Selection.ParagraphFormat.Alignment =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
      
                  
              
              
                this
              
              ._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, 
              
                ref
              
               myNothing, 
              
                ref
              
               myNothing, 
              
                ref
              
              
                 myNothing);
      
              }
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 插入表格
      
              
              
              
                ///
              
              
                </summary>
              
              
                public
              
              
                void
              
              
                 InsertTable()
      
              {
      
                  
              
              
                object
              
               myNothing =
              
                 System.Reflection.Missing.Value;
      
                  Microsoft.Office.Interop.Word.Table table1 
              
              = _wordDocument.Tables.Add(_wordApplication.Selection.Range, 
              
                4
              
              , 
              
                3
              
              , 
              
                ref
              
               myNothing, 
              
                ref
              
              
                 myNothing);
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                1
              
              , 
              
                1
              
              ).Range.Text = 
              
                "
              
              
                產(chǎn)品\n項(xiàng)目
              
              
                "
              
              
                ;
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                1
              
              , 
              
                2
              
              ).Range.Text = 
              
                "
              
              
                電腦
              
              
                "
              
              
                ;
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                1
              
              , 
              
                3
              
              ).Range.Text = 
              
                "
              
              
                手機(jī)
              
              
                "
              
              
                ;
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                2
              
              , 
              
                1
              
              ).Range.Text = 
              
                "
              
              
                重量(kg)
              
              
                "
              
              
                ;
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                3
              
              , 
              
                1
              
              ).Range.Text = 
              
                "
              
              
                價(jià)格(元)
              
              
                "
              
              
                ;
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                4
              
              , 
              
                1
              
              ).Range.Text = 
              
                "
              
              
                共同信息
              
              
                "
              
              
                ;
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                4
              
              , 
              
                2
              
              ).Range.Text = 
              
                "
              
              
                信息A
              
              
                "
              
              
                ;
      
                  _wordDocument.Tables[
              
              
                1
              
              ].Cell(
              
                4
              
              , 
              
                3
              
              ).Range.Text = 
              
                "
              
              
                信息B
              
              
                "
              
              
                ;
      
                  table1.Select();
      
                  table1.Rows.Alignment 
              
              = Microsoft.Office.Interop.Word.WdRowAlignment.wdAlignRowCenter;
              
                //
              
              
                整個(gè)表格居中
              
              
                
      
                  _wordApplication.Selection.Cells.VerticalAlignment 
              
              =
              
                 Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
      
                  _wordApplication.Selection.ParagraphFormat.Alignment 
              
              =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
      
                  _wordApplication.Selection.Cells.HeightRule 
              
              =
              
                 Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightExactly;
      
                  _wordApplication.Selection.Cells.Height 
              
              = 
              
                40
              
              
                ;
      
                  table1.Rows[
              
              
                2
              
              ].Height = 
              
                20
              
              
                ;
      
                  table1.Rows[
              
              
                3
              
              ].Height = 
              
                20
              
              
                ;
      
                  table1.Rows[
              
              
                4
              
              ].Height = 
              
                20
              
              
                ;
      
                  table1.Range.ParagraphFormat.Alignment 
              
              =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
      
                  _wordApplication.Selection.Cells.Width 
              
              = 
              
                150
              
              
                ;
      
                  table1.Columns[
              
              
                1
              
              ].Width = 
              
                75
              
              
                ;
      
                  table1.Cell(
              
              
                1
              
              , 
              
                1
              
              ).Range.ParagraphFormat.Alignment =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
      
                  table1.Cell(
              
              
                1
              
              , 
              
                1
              
              ).Range.Paragraphs[
              
                2
              
              ].Format.Alignment =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
      
      
      
      
      
                  _wordApplication.Selection.Cells.VerticalAlignment 
              
              =
              
                 Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
      
                  _wordApplication.Selection.ParagraphFormat.Alignment 
              
              =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
      
                  _wordApplication.Selection.Cells.HeightRule 
              
              =
              
                 Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightExactly;
      
                  _wordApplication.Selection.Cells.Height 
              
              = 
              
                40
              
              
                ;
      
                  table1.Rows[
              
              
                2
              
              ].Height = 
              
                20
              
              ; table1.Rows[
              
                3
              
              ].Height = 
              
                20
              
              
                ;
      
                  table1.Rows[
              
              
                4
              
              ].Height = 
              
                20
              
              
                ;
      
                  table1.Range.ParagraphFormat.Alignment 
              
              =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
      
                  _wordApplication.Selection.Cells.Width 
              
              = 
              
                150
              
              
                ;
      
                  table1.Columns[
              
              
                1
              
              ].Width = 
              
                75
              
              
                ;
      
                  table1.Cell(
              
              
                1
              
              , 
              
                1
              
              ).Range.ParagraphFormat.Alignment =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
      
                  table1.Cell(
              
              
                1
              
              , 
              
                1
              
              ).Range.Paragraphs[
              
                2
              
              ].Format.Alignment =
              
                 Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
      
      
      
                  
              
              
                //
              
              
                表頭斜線 
              
              
      
                  table1.Cell(
              
                1
              
              , 
              
                1
              
              ).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Visible = 
              
                true
              
              
                ;
      
                  table1.Cell(
              
              
                1
              
              , 
              
                1
              
              ).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Color =
              
                 Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
      
                  table1.Cell(
              
              
                1
              
              , 
              
                1
              
              ).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].LineWidth =
              
                 Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
      
      
      
      
      
                  
              
              
                //
              
              
                表格邊框             
              
              
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].Visible = 
              
                true
              
              
                ;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].Color 
              
              =
              
                 Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].LineWidth 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
      
      
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].Visible 
              
              = 
              
                true
              
              
                ;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].Color 
              
              =
              
                 Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].LineWidth 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
      
      
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Visible 
              
              = 
              
                true
              
              
                ;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Color 
              
              =
              
                 Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
      
      
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineWidth 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineStyle 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDoubleWavy;
      
      
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].Visible 
              
              = 
              
                true
              
              
                ;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].Color 
              
              =
              
                 Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineWidth 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineStyle 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDoubleWavy;
      
      
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible 
              
              = 
              
                true
              
              
                ;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Color 
              
              =
              
                 Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineWidth 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDouble;
      
      
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].Visible 
              
              = 
              
                true
              
              
                ;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].Color 
              
              =
              
                 Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineWidth 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
      
                  table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineStyle 
              
              =
              
                 Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDouble;
      
      
      
      
      
                  
              
              
                //
              
              
                合并單元格 
      
                  
              
              
                //
              
              
                _wordDocument.Tables[1].Cell(4, 2).Merge(table1.Cell(4, 3)); 
              
              
                        }
      
              [DllImport(
              
              
                "
              
              
                shell32.dll 
              
              
                "
              
              
                )]
      
              
              
              
                public
              
              
                static
              
              
                extern
              
              
                int
              
               ShellExecute(IntPtr hwnd, String lpszOp, String lpszFile, String lpszParams, String lpszDir, 
              
                int
              
              
                 FsShowCmd);
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 關(guān)閉文檔 
      
              
              
              
                ///
              
              
                </summary>
              
              
                public
              
              
                void
              
               CloseDocument(
              
                string
              
              
                 fileName)
      
              {
      
                  
              
              
                object
              
               myNothing =
              
                 System.Reflection.Missing.Value;
      
                  
              
              
                //
              
              
                關(guān)閉文檔 
              
              
                object
              
               saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; _wordDocument.Close(
              
                ref
              
               myNothing, 
              
                ref
              
               myNothing, 
              
                ref
              
              
                 myNothing);
      
                  _wordApplication.Application.Quit(
              
              
                ref
              
               saveOption, 
              
                ref
              
               myNothing, 
              
                ref
              
               myNothing); _wordDocument = 
              
                null
              
              ; _wordApplication = 
              
                null
              
              
                ;
      
                  
              
              
                //
              
              
                ShellExecute(IntPtr.Zero, "open", fileName, "", "", 3);
              
              
                        }
      
              
              
              
                ///
              
              
                <summary>
              
              
                ///
              
              
                 保存文件 
      
              
              
              
                ///
              
              
                </summary>
              
              
                ///
              
              
                <param name="pFileName">
              
              
                文件名
              
              
                </param>
              
              
                public
              
              
                void
              
               SaveWord(
              
                string
              
              
                 pFileName)
      
              {
      
                  
              
              
                object
              
               myNothing =
              
                 System.Reflection.Missing.Value;
      
                  
              
              
                object
              
               myFileName =
              
                 pFileName;
      
                  
              
              
                object
              
               myWordFormatDocument =
              
                 Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
      
                  
              
              
                object
              
               myLockd = 
              
                false
              
              
                ;
      
                  
              
              
                object
              
               myPassword = 
              
                ""
              
              
                ;
      
                  
              
              
                object
              
               myAddto = 
              
                true
              
              
                ;
      
                  
              
              
                try
              
              
                
      
                  {
      
                      
              
              
                this
              
              ._wordDocument.SaveAs(
              
                ref
              
               myFileName, 
              
                ref
              
               myWordFormatDocument, 
              
                ref
              
               myLockd, 
              
                ref
              
              
                 myPassword,
      
                          
              
              
                ref
              
               myAddto, 
              
                ref
              
              
                 myPassword,
      
                          
              
              
                ref
              
               myLockd, 
              
                ref
              
               myLockd, 
              
                ref
              
               myLockd, 
              
                ref
              
               myLockd, 
              
                ref
              
               myNothing, 
              
                ref
              
               myNothing, 
              
                ref
              
              
                 myNothing,
      
                          
              
              
                ref
              
               myNothing, 
              
                ref
              
               myNothing, 
              
                ref
              
              
                 myNothing);
      
                  }
      
                  
              
              
                catch
              
              
                 (Exception exception)
      
                  {
      
                      
              
              
                throw
              
              
                new
              
               Exception(
              
                "
              
              
                保存word文檔失敗!
              
              
                "
              
              
                );
      
                  }
      
              }
              
            
      View Code

      ?圖片倒是保存住了,可配置的地方很多,速度有些慢,代碼量頗多。

      ?3.如果要導(dǎo)出excel 還是用NPOI最好。? http://www.cnblogs.com/lwme/archive/2011/11/18/npoi_excel_import_export.html

      ? 調(diào)用該博主ExcelRender中的方法,返回File。 不然網(wǎng)上的其他辦法都很有問(wèn)題,要么打不開(kāi),要么服務(wù)器沒(méi)裝office就不行了。

            
              public
            
            
               FileResult ExportExcel()
      
              {
      
                  DataTable table 
            
            = 
            
              new
            
            
               DataTable();
      
                  table.Columns.Add(
            
            
              "
            
            
              aa
            
            
              "
            
            , 
            
              typeof
            
            (
            
              string
            
            
              ));
      
                  table.Columns.Add(
            
            
              "
            
            
              bb
            
            
              "
            
            , 
            
              typeof
            
            (
            
              string
            
            
              ));
      
                  table.Columns.Add(
            
            
              "
            
            
              cc
            
            
              "
            
            , 
            
              typeof
            
            (
            
              string
            
            
              ));
      
                  
            
            
              for
            
             (
            
              int
            
             i = 
            
              0
            
            ; i < 
            
              10
            
            ; i++
            
              )
      
                  {
      
                      
            
            
              string
            
             a =
            
               DateTime.Now.Ticks.ToString();
      
                      Thread.Sleep(
            
            
              1
            
            
              );
      
                      
            
            
              string
            
             b =
            
               DateTime.Now.Ticks.ToString();
      
                      Thread.Sleep(
            
            
              1
            
            
              );
      
                      
            
            
              string
            
             c =
            
               DateTime.Now.Ticks.ToString();
      
                      Thread.Sleep(
            
            
              1
            
            
              );
      
                      table.Rows.Add(a, b, c);
      
                  }
      
      
      
                 
            
            
              var
            
             ms=
            
               ExcelRender.RenderToExcel(table);
      
      
      
                  
            
            
              return
            
             File(ms, 
            
              "
            
            
              application/vnd.ms-excel
            
            
              "
            
            , 
            
              "
            
            
              考試成績(jī).xls
            
            
              "
            
            
              );
      
              }
            
          

      ?4. Excel導(dǎo)入。

      ? file就是一個(gè)HttpPostedFileBase 對(duì)象。即上傳的excel文件。用ExcelRender 轉(zhuǎn)換為table 再進(jìn)行插入數(shù)據(jù)庫(kù)的操作。

            
              byte
            
            [] filebBytes =
            
              new
            
            
              byte
            
            
              [file.ContentLength];
      
         file.InputStream.Read(filebBytes, 
            
            
              0
            
            
              , file.ContentLength);
      
      
      
         
            
            
              var
            
             table = ExcelRender.RenderFromExcel(
            
              new
            
             MemoryStream(filebBytes),
            
              0
            
            ,
            
              0
            
            );
          

      ?

      【吉光片羽】MVC 導(dǎo)出Word的兩種方式


      更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

      微信掃碼或搜索:z360901061

      微信掃一掃加我為好友

      QQ號(hào)聯(lián)系: 360901061

      您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

      【本文對(duì)您有幫助就好】

      您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

      發(fā)表我的評(píng)論
      最新評(píng)論 總共0條評(píng)論
      主站蜘蛛池模板: 色国产视频 | 国产一级精品毛片 | 亚洲香蕉视频 | 神马我不卡在线观看 | 草莓视频在线观看精品最新 | 久久中文字幕视频 | 久久精品动漫 | 一级毛片免费视频 | 色综合91久久精品中文字幕 | 亚洲精品自产拍在线观看 | 五月激情婷婷综合 | 成人特黄午夜性a一级毛片 成人特级毛片69免费观看 | 久久伊人色综合 | 日本精品99 | 青青青免费视频精品99 | 在线亚洲欧美性天天影院 | 成人美女黄网站色大色费 | 香蕉综合网 | 国产一级久久久久久毛片 | 国产精品久久久久影院色 | 一级女性全黄生活片免费看 | 不卡影院 | 午夜伊人 | 久久91这里精品国产2020 | 第一福利影院 | h视频在线观看免费网站 | 天天做天天做天天综合网 | 成人禁在线观看午夜亚洲 | 热久久国产欧美一区二区精品 | 欧美乱xxxxx强 | 国产免费无遮挡精品视频 | 国产精品视频公开费视频 | 欧美日韩国产成人综合在线 | 九九热在线观看视频 | 亚洲成 人a影院青久在线观看 | 久久99热久久精品在线6 | 久久青草免费免费91线频观看 | 欧美777精品久久久久网 | 久久久久国产精品免费看 | 国产精品视频福利一区二区 | 91精品国产91久久久久久 |