新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#中usingword相關(guān)用法及代碼示例
在C#中using word的命名空間,大多是利用Microsoft.Office.Interop.Word來生成word的方法。以下是一些C#中using word的不同用法

創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站制作、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元潼南做網(wǎng)站,已為上家服務(wù),為潼南各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
將現(xiàn)有的C#中using word勞動成果放在這里。有時間在加以完善!
一、添加頁眉
- view plaincopy to clipboardprint?
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- using System.IO;
- using System.Reflection;
- using Microsoft.Office.Interop.Word;
- namespace WordCreateDLL
- {
- public class AddHeader
- {
- public static void AddSimpleHeader(Application WordApp,string HeaderText)
- {
- //添加頁眉
- WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
- WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
- WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設(shè)置左對齊
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
- }
- public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)
- {
- //添加頁眉
- WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
- WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
- //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//設(shè)置字體顏色
- WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置左對齊
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
- }
- public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)
- {
- //添加頁眉
- WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
- WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
- WordApp.Selection.Font.Color =fontcolor;//設(shè)置字體顏色
- WordApp.Selection.Font.Size = fontsize;//設(shè)置字體大小
- WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置對齊方式
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- using System.IO;
- using System.Reflection;
- using Microsoft.Office.Interop.Word;
- namespace WordCreateDLL
- {
- public class AddHeader
- {
- public static void AddSimpleHeader(Application WordApp,string HeaderText)
- {
- //添加頁眉
- WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
- WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
- WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設(shè)置左對齊
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
- }
- public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)
- {
- //添加頁眉
- WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
- WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
- //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//設(shè)置字體顏色
- WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置左對齊
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
- }
- public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)
- {
- //添加頁眉
- WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
- WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
- WordApp.Selection.Font.Color =fontcolor;//設(shè)置字體顏色
- WordApp.Selection.Font.Size = fontsize;//設(shè)置字體大小
- WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置對齊方式
- WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
- }
- }
- }
二、插入圖片
- view plaincopy to clipboardprint?
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- using System.IO;
- using System.Reflection;
- using Microsoft.Office.Interop.Word;
- namespace WordCreateDLL
- {
- public class AddPic
- {
- public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)
- {
- //插入圖片
- string FileName = @FName;//圖片所在路徑
- object LinkToFile = false;
- object SaveWithDocument = true;
- object Anchor = An;
- WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
- WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//圖片寬度
- WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//圖片高度
- //將圖片設(shè)置為四周環(huán)繞型
- Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
- s.WrapFormat.Type = wdWrapType;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- using System.IO;
- using System.Reflection;
- using Microsoft.Office.Interop.Word;
- namespace WordCreateDLL
- {
- public class AddPic
- {
- public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)
- {
- //插入圖片
- string FileName = @FName;//圖片所在路徑
- object LinkToFile = false;
- object SaveWithDocument = true;
- object Anchor = An;
- WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
- WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//圖片寬度
- WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//圖片高度
- //將圖片設(shè)置為四周環(huán)繞型
- Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
- s.WrapFormat.Type = wdWrapType;
- }
- }
- }
三、插入表格
- view plaincopy to clipboardprint?
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- using System.IO;
- using System.Reflection;
- using Microsoft.Office.Interop.Word;
- namespace WordCreateDLL
- {
- public class AddTable
- {
- public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)
- {
- Object Nothing = System.Reflection.Missing.Value;
- //文檔中創(chuàng)建表格
- Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);
- //設(shè)置表格樣式
- newTable.Borders.OutsideLineStyle = outStyle;
- newTable.Borders.InsideLineStyle = intStyle;
- newTable.Columns[1].Width = 100f;
- newTable.Columns[2].Width = 220f;
- newTable.Columns[3].Width = 105f;
- //填充表格內(nèi)容
- newTable.Cell(1, 1).Range.Text = "產(chǎn)品詳細信息表";
- newTable.Cell(1, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體
- //合并單元格
- newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
- WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
- WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
- //填充表格內(nèi)容
- newTable.Cell(2, 1).Range.Text = "產(chǎn)品基本信息";
- newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色
- //合并單元格
- newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
- WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;
- //填充表格內(nèi)容
- newTable.Cell(3, 1).Range.Text = "品牌名稱:";
- newTable.Cell(3, 2).Range.Text = "品牌名稱:";
- //縱向合并單元格
- newTable.Cell(3, 3).Select();//選中一行
- object moveUnit = WdUnits.wdLine;
- object moveCount = 5;
- object moveExtend = WdMovementType.wdExtend;
- WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
- WordApp.Selection.Cells.Merge();
- //插入圖片
- string FileName = @"C:\1.jpg";//圖片所在路徑
- object Anchor = WordDoc.Application.Selection.Range;
- float Width = 200f;//圖片寬度
- float Height = 200f;//圖片高度
- //將圖片設(shè)置為四周環(huán)繞型
- WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
- AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);
- newTable.Cell(12, 1).Range.Text = "產(chǎn)品特殊屬性";
- newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
- //在表格中增加行
- WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- using System.IO;
- using System.Reflection;
- using Microsoft.Office.Interop.Word;
- namespace WordCreateDLL
- {
- public class AddTable
- {
- public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)
- {
- Object Nothing = System.Reflection.Missing.Value;
- //文檔中創(chuàng)建表格
- Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);
- //設(shè)置表格樣式
- newTable.Borders.OutsideLineStyle = outStyle;
- newTable.Borders.InsideLineStyle = intStyle;
- newTable.Columns[1].Width = 100f;
- newTable.Columns[2].Width = 220f;
- newTable.Columns[3].Width = 105f;
- //填充表格內(nèi)容
- newTable.Cell(1, 1).Range.Text = "產(chǎn)品詳細信息表";
- newTable.Cell(1, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體
- //合并單元格
- newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
- WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
- WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
- //填充表格內(nèi)容
- newTable.Cell(2, 1).Range.Text = "產(chǎn)品基本信息";
- newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色
- //合并單元格
- newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
- WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;
- //填充表格內(nèi)容
- newTable.Cell(3, 1).Range.Text = "品牌名稱:";
- newTable.Cell(3, 2).Range.Text = "品牌名稱:";
- //縱向合并單元格
- newTable.Cell(3, 3).Select();//選中一行
- object moveUnit = WdUnits.wdLine;
- object moveCount = 5;
- object moveExtend = WdMovementType.wdExtend;
- WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
- WordApp.Selection.Cells.Merge();
- //插入圖片
- string FileName = @"C:\1.jpg";//圖片所在路徑
- object Anchor = WordDoc.Application.Selection.Range;
- float Width = 200f;//圖片寬度
- float Height = 200f;//圖片高度
- //將圖片設(shè)置為四周環(huán)繞型
- WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
- AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);
- newTable.Cell(12, 1).Range.Text = "產(chǎn)品特殊屬性";
- newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
- //在表格中增加行
- WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
- }
- }
- }
四、插入chart
- view plaincopy to clipboardprint?
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- using System.IO;
- using System.Reflection;
- using Microsoft.Office.Interop.Word;
- using Microsoft.Office.Interop.Graph;
- using System.Windows.Forms;
- using System.Drawing;
- namespace WordCreateDLL
- {
- public class AddChart
- {
- public static void AddSimpleChart(Document WordDoc, Word.Application WordApp, Object oEndOfDoc, string [,]data)
- {
- //插入chart
- object oMissing = System.Reflection.Missing.Value;
- Word.InlineShape oShape;
- object oClassType = "MSGraph.Chart.8";
- Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
- oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,
- ref oMissing, ref oMissing, ref oMissing,
- ref oMissing, ref oMissing, ref oMissing);
- //Demonstrate use of late bound oChart and oChartApp objects to
- //manipulate the chart object with MSGraph.
- object oChart;
- object oChartApp;
- oChart = oShape.OLEFormat.Object;
- oChartApp = oChart.GetType().InvokeMember("Application",BindingFlags.GetProperty, null, oChart, null);
- //Change the chart type to Line.
- object[] Parameters = new Object[1];
- Parameters[0] = 4; //xlLine = 4
- oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,
- null, oChart, Parameters);
- Chart objChart = (Chart)oShape.OLEFormat.Object;
- objChart.ChartType = XlChartType.xlColumnClustered;
- //綁定數(shù)據(jù)
- DataSheet dataSheet;
- dataSheet = objChart.Application.DataSheet;
- int rownum=data.GetLength(0);
- int columnnum=data.GetLength(1);
- for(int i=1;i<=rownum;i++ )
- for (int j = 1; j <= columnnum; j++)
- {
- dataSheet.Cells[i,j] =data[i-1,j-1];
- }
- objChart.Application.Update();
- oChartApp.GetType().InvokeMember("Update",
- BindingFlags.InvokeMethod, null, oChartApp, null);
- oChartApp.GetType().InvokeMember("Quit",
- BindingFlags.InvokeMethod, null, oChartApp, null);
- //設(shè)置大小
- oShape.Width = WordApp.InchesToPoints(6.25f)
分享文章:C#中usingword相關(guān)用法及代碼示例
轉(zhuǎn)載源于:http://m.fisionsoft.com.cn/article/dhpdeoo.html


咨詢
建站咨詢
