日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yí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ù)公司,擁有項(xiàng)目網(wǎng)站制作、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元潼南做網(wǎng)站,已為上家服務(wù),為潼南各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

將現(xiàn)有的C#中using word勞動(dòng)成果放在這里。有時(shí)間在加以完善!

一、添加頁(yè)眉

 
 
 
  1. view plaincopy to clipboardprint?
  2. using System;   
  3. using System.Collections.Generic;   
  4. using System.ComponentModel;   
  5. using System.Data;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using Word = Microsoft.Office.Interop.Word;   
  9. using System.IO;   
  10. using System.Reflection;   
  11. using Microsoft.Office.Interop.Word;   
  12.   
  13.   
  14. namespace WordCreateDLL   
  15. {   
  16.    public class AddHeader   
  17.     {   
  18.         public static void AddSimpleHeader(Application WordApp,string HeaderText)   
  19.         {   
  20.             //添加頁(yè)眉   
  21.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;   
  22.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;   
  23.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);   
  24.             WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設(shè)置左對(duì)齊   
  25.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;   
  26.         }   
  27.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)   
  28.         {   
  29.             //添加頁(yè)眉   
  30.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;   
  31.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;   
  32.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);   
  33.             //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//設(shè)置字體顏色   
  34.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置左對(duì)齊   
  35.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;   
  36.         }   
  37.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)   
  38.         {   
  39.             //添加頁(yè)眉   
  40.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;   
  41.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;   
  42.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);   
  43.             WordApp.Selection.Font.Color =fontcolor;//設(shè)置字體顏色   
  44.             WordApp.Selection.Font.Size = fontsize;//設(shè)置字體大小   
  45.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置對(duì)齊方式   
  46.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;   
  47.         }   
  48.   
  49.   
  50.     }   
  51. }  
  52. using System;
  53. using System.Collections.Generic;
  54. using System.ComponentModel;
  55. using System.Data;
  56. using System.Linq;
  57. using System.Text;
  58. using Word = Microsoft.Office.Interop.Word;
  59. using System.IO;
  60. using System.Reflection;
  61. using Microsoft.Office.Interop.Word;
  62. namespace WordCreateDLL
  63. {
  64.    public class AddHeader
  65.     {
  66.         public static void AddSimpleHeader(Application WordApp,string HeaderText)
  67.         {
  68.             //添加頁(yè)眉
  69.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
  70.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
  71.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
  72.             WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設(shè)置左對(duì)齊
  73.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
  74.         }
  75.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)
  76.         {
  77.             //添加頁(yè)眉
  78.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
  79.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
  80.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
  81.             //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//設(shè)置字體顏色
  82.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置左對(duì)齊
  83.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
  84.         }
  85.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)
  86.         {
  87.             //添加頁(yè)眉
  88.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
  89.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
  90.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);
  91.             WordApp.Selection.Font.Color =fontcolor;//設(shè)置字體顏色
  92.             WordApp.Selection.Font.Size = fontsize;//設(shè)置字體大小
  93.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設(shè)置對(duì)齊方式
  94.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
  95.         }
  96.     }
  97. }

二、插入圖片

 
 
 
  1. view plaincopy to clipboardprint?
  2. using System;   
  3. using System.Collections.Generic;   
  4. using System.ComponentModel;   
  5. using System.Data;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using Word = Microsoft.Office.Interop.Word;   
  9. using System.IO;   
  10. using System.Reflection;   
  11. using Microsoft.Office.Interop.Word;   
  12.   
  13. namespace WordCreateDLL   
  14. {   
  15.   public class AddPic   
  16.     {   
  17.       public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)   
  18.       {   
  19.           //插入圖片   
  20.           string FileName = @FName;//圖片所在路徑   
  21.           object LinkToFile = false;   
  22.           object SaveWithDocument = true;   
  23.           object Anchor = An;   
  24.           WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);   
  25.           WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//圖片寬度   
  26.           WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//圖片高度   
  27.           //將圖片設(shè)置為四周環(huán)繞型   
  28.           Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();   
  29.           s.WrapFormat.Type = wdWrapType;   
  30.       }   
  31.   
  32.     }   
  33. }  
  34. using System;
  35. using System.Collections.Generic;
  36. using System.ComponentModel;
  37. using System.Data;
  38. using System.Linq;
  39. using System.Text;
  40. using Word = Microsoft.Office.Interop.Word;
  41. using System.IO;
  42. using System.Reflection;
  43. using Microsoft.Office.Interop.Word;
  44. namespace WordCreateDLL
  45. {
  46.   public class AddPic
  47.     {
  48.       public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)
  49.       {
  50.           //插入圖片
  51.           string FileName = @FName;//圖片所在路徑
  52.           object LinkToFile = false;
  53.           object SaveWithDocument = true;
  54.           object Anchor = An;
  55.           WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
  56.           WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//圖片寬度
  57.           WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//圖片高度
  58.           //將圖片設(shè)置為四周環(huán)繞型
  59.           Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
  60.           s.WrapFormat.Type = wdWrapType;
  61.       }
  62.     }
  63. }

三、插入表格

 
 
 
  1. view plaincopy to clipboardprint?
  2. using System;   
  3. using System.Collections.Generic;   
  4. using System.ComponentModel;   
  5. using System.Data;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using Word = Microsoft.Office.Interop.Word;   
  9. using System.IO;   
  10. using System.Reflection;   
  11. using Microsoft.Office.Interop.Word;   
  12.   
  13. namespace WordCreateDLL   
  14. {   
  15.    public class AddTable   
  16.     {   
  17.        public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)   
  18.        {   
  19.            Object Nothing = System.Reflection.Missing.Value;   
  20.            //文檔中創(chuàng)建表格   
  21.            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);   
  22.            //設(shè)置表格樣式   
  23.            newTable.Borders.OutsideLineStyle = outStyle;   
  24.            newTable.Borders.InsideLineStyle = intStyle;   
  25.            newTable.Columns[1].Width = 100f;   
  26.            newTable.Columns[2].Width = 220f;   
  27.            newTable.Columns[3].Width = 105f;   
  28.   
  29.            //填充表格內(nèi)容   
  30.            newTable.Cell(1, 1).Range.Text = "產(chǎn)品詳細(xì)信息表";   
  31.            newTable.Cell(1, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體   
  32.            //合并單元格   
  33.            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));   
  34.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中   
  35.            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中   
  36.   
  37.            //填充表格內(nèi)容   
  38.            newTable.Cell(2, 1).Range.Text = "產(chǎn)品基本信息";   
  39.            newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色   
  40.            //合并單元格   
  41.            newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));   
  42.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;   
  43.   
  44.            //填充表格內(nèi)容   
  45.            newTable.Cell(3, 1).Range.Text = "品牌名稱:";   
  46.            newTable.Cell(3, 2).Range.Text = "品牌名稱:";   
  47.            //縱向合并單元格   
  48.            newTable.Cell(3, 3).Select();//選中一行   
  49.            object moveUnit = WdUnits.wdLine;   
  50.            object moveCount = 5;   
  51.            object moveExtend = WdMovementType.wdExtend;   
  52.            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);   
  53.            WordApp.Selection.Cells.Merge();   
  54.   
  55.   
  56.            //插入圖片   
  57.            string FileName = @"C:\1.jpg";//圖片所在路徑   
  58.            object Anchor = WordDoc.Application.Selection.Range;   
  59.            float Width = 200f;//圖片寬度   
  60.            float Height = 200f;//圖片高度   
  61.   
  62.            //將圖片設(shè)置為四周環(huán)繞型   
  63.            WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;   
  64.            AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);   
  65.   
  66.            newTable.Cell(12, 1).Range.Text = "產(chǎn)品特殊屬性";   
  67.            newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));   
  68.            //在表格中增加行   
  69.            WordDoc.Content.Tables[1].Rows.Add(ref Nothing);   
  70.        }   
  71.   
  72.   
  73.     }   
  74. }  
  75. using System;
  76. using System.Collections.Generic;
  77. using System.ComponentModel;
  78. using System.Data;
  79. using System.Linq;
  80. using System.Text;
  81. using Word = Microsoft.Office.Interop.Word;
  82. using System.IO;
  83. using System.Reflection;
  84. using Microsoft.Office.Interop.Word;
  85. namespace WordCreateDLL
  86. {
  87.    public class AddTable
  88.     {
  89.        public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)
  90.        {
  91.            Object Nothing = System.Reflection.Missing.Value;
  92.            //文檔中創(chuàng)建表格
  93.            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);
  94.            //設(shè)置表格樣式
  95.            newTable.Borders.OutsideLineStyle = outStyle;
  96.            newTable.Borders.InsideLineStyle = intStyle;
  97.            newTable.Columns[1].Width = 100f;
  98.            newTable.Columns[2].Width = 220f;
  99.            newTable.Columns[3].Width = 105f;
  100.            //填充表格內(nèi)容
  101.            newTable.Cell(1, 1).Range.Text = "產(chǎn)品詳細(xì)信息表";
  102.            newTable.Cell(1, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體
  103.            //合并單元格
  104.            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
  105.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
  106.            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
  107.            //填充表格內(nèi)容
  108.            newTable.Cell(2, 1).Range.Text = "產(chǎn)品基本信息";
  109.            newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色
  110.            //合并單元格
  111.            newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
  112.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  113.            //填充表格內(nèi)容
  114.            newTable.Cell(3, 1).Range.Text = "品牌名稱:";
  115.            newTable.Cell(3, 2).Range.Text = "品牌名稱:";
  116.            //縱向合并單元格
  117.            newTable.Cell(3, 3).Select();//選中一行
  118.            object moveUnit = WdUnits.wdLine;
  119.            object moveCount = 5;
  120.            object moveExtend = WdMovementType.wdExtend;
  121.            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
  122.            WordApp.Selection.Cells.Merge();
  123.            //插入圖片
  124.            string FileName = @"C:\1.jpg";//圖片所在路徑
  125.            object Anchor = WordDoc.Application.Selection.Range;
  126.            float Width = 200f;//圖片寬度
  127.            float Height = 200f;//圖片高度
  128.            //將圖片設(shè)置為四周環(huán)繞型
  129.            WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
  130.            AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);
  131.            newTable.Cell(12, 1).Range.Text = "產(chǎn)品特殊屬性";
  132.            newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
  133.            //在表格中增加行
  134.            WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
  135.        }
  136.     }
  137. }

四、插入chart

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