導出

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using Word = Microsoft.Office.Interop.Word;  
  8. using System.IO;  
  9. using System.Reflection;  
  10. using Microsoft.Office.Interop.Word;  
  11.   
  12.   
  13. namespace WordCreateDLL  
  14. {  
  15.    public class AddHeader  
  16.     {  
  17.         public static void AddSimpleHeader(Application WordApp,string HeaderText)  
  18.         {  
  19.             //添加頁眉   
  20.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;  
  21.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;  
  22.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);  
  23.             WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設置左對齊   
  24.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;  
  25.         }  
  26.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)  
  27.         {  
  28.             //添加頁眉   
  29.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;  
  30.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;  
  31.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);  
  32.             //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//設置字體顏色   
  33.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設置左對齊   
  34.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;  
  35.         }  
  36.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)  
  37.         {  
  38.             //添加頁眉   
  39.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;  
  40.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;  
  41.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);  
  42.             WordApp.Selection.Font.Color =fontcolor;//設置字體顏色   
  43.             WordApp.Selection.Font.Size = fontsize;//設置字體大小   
  44.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設置對齊方式   
  45.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;  
  46.         }  
  47.   
  48.   
  49.     }  
  50. }  

 

二、插入圖片

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using Word = Microsoft.Office.Interop.Word;  
  8. using System.IO;  
  9. using System.Reflection;  
  10. using Microsoft.Office.Interop.Word;  
  11.   
  12. namespace WordCreateDLL  
  13. {  
  14.   public class AddPic  
  15.     {  
  16.       public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)  
  17.       {  
  18.           //插入圖片   
  19.           string FileName = @FName;//圖片所在路徑   
  20.           object LinkToFile = false;  
  21.           object SaveWithDocument = true;  
  22.           object Anchor = An;  
  23.           WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);  
  24.           WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//圖片寬度   
  25.           WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//圖片高度   
  26.           //將圖片設置爲四周環繞型   
  27.           Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();  
  28.           s.WrapFormat.Type = wdWrapType;  
  29.       }  
  30.   
  31.     }  
  32. }  

 

三、插入表格

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using Word = Microsoft.Office.Interop.Word;  
  8. using System.IO;  
  9. using System.Reflection;  
  10. using Microsoft.Office.Interop.Word;  
  11.   
  12. namespace WordCreateDLL  
  13. {  
  14.    public class AddTable  
  15.     {  
  16.        public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)  
  17.        {  
  18.            Object Nothing = System.Reflection.Missing.Value;  
  19.            //文檔中創建表格   
  20.            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);  
  21.            //設置表格樣式   
  22.            newTable.Borders.OutsideLineStyle = outStyle;  
  23.            newTable.Borders.InsideLineStyle = intStyle;  
  24.            newTable.Columns[1].Width = 100f;  
  25.            newTable.Columns[2].Width = 220f;  
  26.            newTable.Columns[3].Width = 105f;  
  27.   
  28.            //填充表格內容   
  29.            newTable.Cell(1, 1).Range.Text = "產品詳細信息表";  
  30.            newTable.Cell(1, 1).Range.Bold = 2;//設置單元格中字體爲粗體   
  31.            //合併單元格   
  32.            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));  
  33.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中   
  34.            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中   
  35.   
  36.            //填充表格內容   
  37.            newTable.Cell(2, 1).Range.Text = "產品基本信息";  
  38.            newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//設置單元格內字體顏色   
  39.            //合併單元格   
  40.            newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));  
  41.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;  
  42.   
  43.            //填充表格內容   
  44.            newTable.Cell(3, 1).Range.Text = "品牌名稱:";  
  45.            newTable.Cell(3, 2).Range.Text = "品牌名稱:";  
  46.            //縱向合併單元格   
  47.            newTable.Cell(3, 3).Select();//選中一行   
  48.            object moveUnit = WdUnits.wdLine;  
  49.            object moveCount = 5;  
  50.            object moveExtend = WdMovementType.wdExtend;  
  51.            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);  
  52.            WordApp.Selection.Cells.Merge();  
  53.   
  54.   
  55.            //插入圖片   
  56.            string FileName = @"C:/1.jpg";//圖片所在路徑   
  57.            object Anchor = WordDoc.Application.Selection.Range;  
  58.            float Width = 200f;//圖片寬度   
  59.            float Height = 200f;//圖片高度   
  60.   
  61.            //將圖片設置爲四周環繞型   
  62.            WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;  
  63.            AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);  
  64.   
  65.            newTable.Cell(12, 1).Range.Text = "產品特殊屬性";  
  66.            newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));  
  67.            //在表格中增加行   
  68.            WordDoc.Content.Tables[1].Rows.Add(ref Nothing);  
  69.        }  
  70.   
  71.   
  72.     }  
  73. }  

 

四、插入chart

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using Word = Microsoft.Office.Interop.Word;  
  8. using System.IO;  
  9. using System.Reflection;  
  10. using Microsoft.Office.Interop.Word;  
  11. using Microsoft.Office.Interop.Graph;  
  12. using System.Windows.Forms;  
  13. using System.Drawing;  
  14.   
  15.   
  16. namespace WordCreateDLL  
  17. {  
  18.     public class AddChart  
  19.     {  
  20.         public static void AddSimpleChart(Document WordDoc, Word.Application WordApp, Object oEndOfDoc, string [,]data)  
  21.         {  
  22.             //插入chart     
  23.             object oMissing = System.Reflection.Missing.Value;  
  24.             Word.InlineShape oShape;  
  25.             object oClassType = "MSGraph.Chart.8";  
  26.             Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;  
  27.             oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,  
  28.                 ref oMissing, ref oMissing, ref oMissing,  
  29.                 ref oMissing, ref oMissing, ref oMissing);  
  30.   
  31.             //Demonstrate use of late bound oChart and oChartApp objects to   
  32.             //manipulate the chart object with MSGraph.   
  33.             object oChart;  
  34.             object oChartApp;  
  35.             oChart = oShape.OLEFormat.Object;  
  36.             oChartApp = oChart.GetType().InvokeMember("Application",BindingFlags.GetProperty, null, oChart, null);  
  37.   
  38.             //Change the chart type to Line.   
  39.             object[] Parameters = new Object[1];  
  40.             Parameters[0] = 4; //xlLine = 4   
  41.             oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,  
  42.                 null, oChart, Parameters);  
  43.   
  44.   
  45.             Chart objChart = (Chart)oShape.OLEFormat.Object;  
  46.             objChart.ChartType = XlChartType.xlColumnClustered;  
  47.   
  48.             //綁定數據   
  49.             DataSheet dataSheet;  
  50.             dataSheet = objChart.Application.DataSheet;  
  51.             int rownum=data.GetLength(0);  
  52.             int columnnum=data.GetLength(1);  
  53.             for(int i=1;i<=rownum;i++ )  
  54.                 for (int j = 1; j <= columnnum; j++)  
  55.                 {   
  56.                    dataSheet.Cells[i,j] =data[i-1,j-1];  
  57.        
  58.                 }  
  59.             
  60.             objChart.Application.Update();  
  61.             oChartApp.GetType().InvokeMember("Update",  
  62.                 BindingFlags.InvokeMethod, null, oChartApp, null);  
  63.             oChartApp.GetType().InvokeMember("Quit",  
  64.                 BindingFlags.InvokeMethod, null, oChartApp, null);  
  65.   
  66.             //設置大小   
  67.             oShape.Width = WordApp.InchesToPoints(6.25f);  
  68.             oShape.Height = WordApp.InchesToPoints(3.57f);  
  69.   
  70.         }  
  71.     }  
  72. }  

 

測試程序

  1. private void button3_Click(object sender, EventArgs e)  
  2.        {  
  3.   
  4.   
  5.            object oMissing = System.Reflection.Missing.Value;  
  6.            object oEndOfDoc = "//endofdoc"/* /endofdoc is a predefined bookmark */  
  7.   
  8.            //Start Word and create a new document.   
  9.            Word.Application oWord;  
  10.            Word.Document oDoc;  
  11.            oWord = new Word.Application();  
  12.            oWord.Visible = true;  
  13.            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,  
  14.                ref oMissing, ref oMissing);  
  15.   
  16.            ////添加頁眉   
  17.            String HeaderText = "石化盈科";  
  18.           // AddHeader.AddSimpleHeader(oWord, HeaderText);   
  19.            WdParagraphAlignment wdAlign = WdParagraphAlignment.wdAlignParagraphCenter;  
  20.            WdColor fontcolor = WdColor.wdColorBlue;  
  21.            float fontsize = 10;  
  22.            //AddHeader.AddSimpleHeader(oWord, HeaderText, wdAlign);   
  23.            AddHeader.AddSimpleHeader(oWord, HeaderText, wdAlign,fontcolor,fontsize);  
  24.   
  25.            //Insert a paragraph at the beginning of the document.   
  26.            Word.Paragraph oPara1;  
  27.            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);  
  28.            oPara1.Range.Text = "Heading 1";  
  29.            oPara1.Range.Font.Bold = 1;  
  30.            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.   
  31.            oPara1.Range.InsertParagraphAfter();  
  32.   
  33.            //Insert a paragraph at the end of the document.   
  34.            Word.Paragraph oPara2;  
  35.            object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;  
  36.            oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);  
  37.            oPara2.Range.Text = "Heading 2";  
  38.            oPara2.Format.SpaceAfter = 6;  
  39.            oPara2.Range.InsertParagraphAfter();  
  40.   
  41.            //插入文字   
  42.            Word.Paragraph oPara3;  
  43.            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;  
  44.            oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);  
  45.            oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";  
  46.            oPara3.Range.Font.Bold = 0;  
  47.            oPara3.Format.SpaceAfter = 24;  
  48.            oPara3.Range.InsertParagraphAfter();  
  49.   
  50.            string text = "zhangruichao ";  
  51.            WdColor textcolor = fontcolor;  
  52.            float textsize = 12;  
  53.            AddLine.AddSimpLine(oDoc, oEndOfDoc, oRng, text, textcolor, textsize);  
  54.   
  55.   
  56.            //插入表格   
  57.            WdLineStyle OutStyle = WdLineStyle.wdLineStyleThickThinLargeGap;  
  58.            WdLineStyle InStyle = WdLineStyle.wdLineStyleSingle;  
  59.            AddTable.AddSimpleTable(oWord, oDoc, 12, 3, OutStyle, InStyle);  
  60.   
  61.            //插入分頁   
  62.            Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;  
  63.            object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;  
  64.            object oPageBreak = Word.WdBreakType.wdPageBreak;  
  65.            wrdRng.Collapse(ref oCollapseEnd);  
  66.            wrdRng.InsertBreak(ref oPageBreak);  
  67.            wrdRng.Collapse(ref oCollapseEnd);  
  68.            wrdRng.InsertAfter("We're now on page 2. Here's my chart:");  
  69.            wrdRng.InsertParagraphAfter();  
  70.   
  71.            //Insert a chart.   
  72.            string[,] data=new string[4,5];  
  73.   
  74.            data[0,1] = "第一月";  
  75.            data[0, 2] = "第二月";  
  76.            data[0, 3] = "第三月";  
  77.            data[0, 4] = "第四月";  
  78.   
  79.            data[1,0] = "東部";  
  80.            data[1,1] = "50";  
  81.            data[1,2] = "50";  
  82.            data[1,3] = "40";  
  83.            data[1,4] = "50";  
  84.   
  85.   
  86.            data[2,0] = "西部";  
  87.            data[2,1] = "60";  
  88.            data[2,2] = "60";  
  89.            data[2,3] = "70";  
  90.            data[2,4] = "80";  
  91.   
  92.            //data[3,6] = "0";   
  93.            data[3,0] = "中部";  
  94.            data[3,1] = "50";  
  95.            data[3,2] = "50";  
  96.            data[3,3] = "40";  
  97.            data[3,4] = "50";  
  98.   
  99.   
  100.   
  101.   
  102.            AddChart.AddSimpleChart(oDoc, oWord, oEndOfDoc, data);  
  103.   
  104.            wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;  
  105.            wrdRng.InsertParagraphAfter();  
  106.            wrdRng.InsertAfter("THE END.");  
  107.   
  108.            //Close this form.   
  109.            this.Close();  
  110.   
  111.        }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章