日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#動(dòng)態(tài)生成Word文檔并填充數(shù)據(jù)

C#動(dòng)態(tài)生成Word文檔步驟之一:添加引用->COM->Microsoft Word 11.0 Object Library

創(chuàng)新互聯(lián)公司是一家專業(yè)提供來(lái)賓企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為來(lái)賓眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。

C#動(dòng)態(tài)生成Word文檔步驟之二:在.cs文件中添加 using  Word;

下面的例子中包括C#對(duì)Word文檔的創(chuàng)建、插入表格、設(shè)置樣式等操作: (例子中代碼有些涉及數(shù)據(jù)信息部分被省略,重要是介紹一些C#操作word文檔的方法)

 
 
 
  1. public   string  CreateWordFile( string  CheckedInfo)
  2.          ... {
  3.             string  message  =   "" ;
  4.             try 
  5.              ... {
  6.                Object Nothing  =  System.Reflection.Missing.Value;
  7.                Directory.CreateDirectory( " C:/CNSI " );   // 創(chuàng)建文件所在目錄 
  8.                 string  name  =   " CNSI_ "   +  DateTime.Now.ToShortString() + " .doc " ;
  9.                 object  filename  =   " C://CNSI// "   +  name;   // 文件保存路徑
  10.                 // 創(chuàng)建Word文檔 
  11.                Word.Application WordApp  =   new  Word.ApplicationClass();
  12.                Word.Document WordDoc  =  WordApp.Documents.Add( ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing);
  13.                 // 添加頁(yè)眉 
  14.                WordApp.ActiveWindow.View.Type  =  WdViewType.wdOutlineView;
  15.                WordApp.ActiveWindow.View.SeekView  =  WdSeekView.wdSeekPrimaryHeader;
  16.                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( " [頁(yè)眉內(nèi)容] " );
  17.                WordApp.Selection.ParagraphFormat.Alignment  =  Word.WdParagraphAlignment.wdAlignParagraphRight; // 設(shè)置右對(duì)齊 
  18.                WordApp.ActiveWindow.View.SeekView  =  WdSeekView.wdSeekMainDocument; // 跳出頁(yè)眉設(shè)置 
  19.                WordApp.Selection.ParagraphFormat.LineSpacing  =  15f; // 設(shè)置文檔的行間距
  20.                 // 移動(dòng)焦點(diǎn)并換行 
  21.                 object  count  =   14 ;
  22.                 object  WdLine  =  Word.WdUnits.wdLine; // 換一行; 
  23.                 WordApp.Selection.MoveDown( ref  WdLine,  ref  count,  ref  Nothing); // 移動(dòng)焦點(diǎn) 
  24.                 WordApp.Selection.TypeParagraph(); // 插入段落
  25.                  // 文檔中創(chuàng)建表格 
  26.                 Word.Table newTable  =  WordDoc.Tables.Add(WordApp.Selection.Range,  12 ,  3 ,  ref  Nothing,  ref  Nothing);
  27.                  // 設(shè)置表格樣式 
  28.                 newTable.Borders.OutsideLineStyle  =  Word.WdLineStyle.wdLineStyleThickThinLargeGap;
  29.                 newTable.Borders.InsideLineStyle  =  Word.WdLineStyle.wdLineStyleSingle;
  30.                 newTable.Columns[ 1 ].Width  =  100f;
  31.                 newTable.Columns[ 2 ].Width  =  220f;
  32.                 newTable.Columns[ 3 ].Width  =  105f;
  33.                  // 填充表格內(nèi)容 
  34.                 newTable.Cell( 1 ,  1 ).Range.Text  =   " 產(chǎn)品詳細(xì)信息表 " ;
  35.                 newTable.Cell( 1 ,  1 ).Range.Bold  =   2 ; // 設(shè)置單元格中字體為粗體
  36.                  // 合并單元格 
  37.                 newTable.Cell( 1 ,  1 ).Merge(newTable.Cell( 1 ,  3 ));
  38.                 WordApp.Selection.Cells.VerticalAlignment  =  Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; // 垂直居中 
  39.                 WordApp.Selection.ParagraphFormat.Alignment  =  Word.WdParagraphAlignment.wdAlignParagraphCenter; // 水平居中
  40.                        
  41.                  // 填充表格內(nèi)容 
  42.                 newTable.Cell( 2 ,  1 ).Range.Text  =   " 產(chǎn)品基本信息 " ;
  43.                 newTable.Cell( 2 ,  1 ).Range.Font.Color  =  Word.WdColor.wdColorDarkBlue; // 設(shè)置單元格內(nèi)字體顏色
  44.                  // 合并單元格 
  45.                 newTable.Cell( 2 ,  1 ).Merge(newTable.Cell( 2 ,  3 ));
  46.                 WordApp.Selection.Cells.VerticalAlignment  =  Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  47.                   // 填充表格內(nèi)容 
  48.                  newTable.Cell( 3 ,  1 ).Range.Text  =   " 品牌名稱: " ;
  49.                  newTable.Cell( 3 ,  2 ).Range.Text  =  BrandName;
  50.                   // 縱向合并單元格 
  51.                  newTable.Cell( 3 ,  3 ).Select(); // 選中一行 
  52.                   object  moveUnit  =  Word.WdUnits.wdLine;
  53.                   object  moveCount  =   5 ;
  54.                   object  moveExtend  =  Word.WdMovementType.wdExtend;
  55.                   WordApp.Selection.MoveDown( ref  moveUnit,  ref  moveCount,  ref  moveExtend);
  56.                   WordApp.Selection.Cells.Merge();
  57.                    // 插入圖片 
  58.                    string  FileName  =  Picture; // 圖片所在路徑 
  59.                    object  LinkToFile  =   false ;
  60.                    object  SaveWithDocument  =   true ;
  61.                    object  Anchor  =  WordDoc.Application.Selection.Range;
  62.                   WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName,  ref  LinkToFile,  ref  SaveWithDocument,  ref  Anchor);
  63.                    WordDoc.Application.ActiveDocument.InlineShapes[ 1 ].Width  =  100f; // 圖片寬度 
  64.                    WordDoc.Application.ActiveDocument.InlineShapes[ 1 ].Height  =  100f; // 圖片高度
  65.                     // 將圖片設(shè)置為四周環(huán)繞型 
  66.                    Word.Shape s  =  WordDoc.Application.ActiveDocument.InlineShapes[ 1 ].ConvertToShape();
  67.                    s.WrapFormat.Type  =  Word.WdWrapType.wdWrapSquare;
  68.                        
  69.                    newTable.Cell( 12 ,  1 ).Range.Text  =   " 產(chǎn)品特殊屬性 " ;
  70.                    newTable.Cell( 12 ,  1 ).Merge(newTable.Cell( 12 ,  3 ));
  71.                      // 在表格中增加行 
  72.                     WordDoc.Content.Tables[ 1 ].Rows.Add( ref  Nothing);
  73.                      
  74.                     WordDoc.Paragraphs.Last.Range.Text  =   " 文檔創(chuàng)建時(shí)間: "   +  DateTime.Now.ToString(); // “落款” 
  75.                     WordDoc.Paragraphs.Last.Alignment  =  Word.WdParagraphAlignment.wdAlignParagraphRight;
  76.                     // 文件保存 
  77.                    WordDoc.SaveAs( ref  filename,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing,  ref  Nothing);
  78.                    WordDoc.Close( ref  Nothing,  ref  Nothing,  ref  Nothing);
  79.                    WordApp.Quit( ref  Nothing,  ref  Nothing,  ref  Nothing);
  80.                    message = name + " 文檔生成成功,以保存到C:CNSI下 " ;
  81.            } 
  82.             catch 
  83.              ... {
  84.                message  =   " 文件導(dǎo)出異常! " ;
  85.            } 
  86.             return  message;
  87.        } 

這樣,就實(shí)現(xiàn)了 C#動(dòng)態(tài)生成Word文檔。


網(wǎng)站名稱:C#動(dòng)態(tài)生成Word文檔并填充數(shù)據(jù)
瀏覽地址:http://www.dlmjj.cn/article/ccoesod.html