在C#中向Word 中现有的表格中加行一行,并给每个单元格一个值,要怎么写呀? - 大海

时间:2024-03-05 14:58:40

问题描述:
      在
C#中向Word 中现有的表格中加行一行,并给每个单元格一个值,要怎么写呀?


解决办法:

       
        
1.       首先,该问题涉及到Office开发。


2.      
引用Office组件,添加References,在弹出的窗口中,选择Com选项卡,然后找到Micorsoft Word 11.0 Object Library组件,点击确定,可以看到References目录下增加了WordMicrosoft.Office.CoreVBIDE三个Assembly


注:我引用时
Word Assembly一直无法正常引用,因为之前用的是中文系统没发现这个问题,现在用的操作系统、Visual studio 2005是英文的,而Office是中文的,可能是这个原因导致的吧。


3.      
添加引用using Microsoft.Office.Core


4.      
几种常见的Word以及表格操作


 1bool saveChange = false;
 2            object missing = System.Reflection.Missing.Value;
 3            object template = (object)templateFilePath;
 4            object filename = (object)saveFilePath;   
 5            object isVisible = missing;
 6            object readOnly = missing;
 7            object breakType = Word.WdBreakType.wdSectionBreakNextPage;
 8            object isSaveChange = (object)saveChange;
 9            Word.Document doc = null;
10
11            //定义一个Word.Application 对象
12            Word.Application WordApp =  new  Word.ApplicationClass();
13//打开文档
14                doc = WordApp.Documents.Open(
15                    ref template, ref missing,ref readOnly,ref missing, ref missing, ref missing, 
16                    ref missing, ref missing, ref missing,ref missing, ref missing, ref isVisible, 
17                    ref missing, ref missing, ref missing, ref missing);
18
19                //设置页眉文本
20                WordApp.ActiveWindow.ActivePane.View.SeekView 
21                    = Word.WdSeekView.wdSeekCurrentPageHeader;
22                WordApp.Selection.WholeStory();
23                WordApp.Selection.TypeText( this.m_titleText );
24                WordApp.ActiveWindow.ActivePane.View.SeekView 
25                    = Word.WdSeekView.wdSeekMainDocument;
26
27                //页面设置,设置页面为纵向布局,设置纸张类型为A4纸
28                doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
29                doc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);
30                doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
31
32                //创建表格及设置表格和单元格属性
33                object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;
34                doc.Content.Tables.Add(
35                    WordApp.Selection.Range, 
36                    totalCount + 1
37                    totalField - keyCount_1, 
38                    ref missing, 
39                    ref autoFitBehavior);
40                //合并单元隔
41                doc.Content.Tables[1].Cell(i+1,j).Select();
42                                    object moveUnit = Word.WdUnits.wdLine;
43                                    object moveCount = 1;
44                                    object moveExtend = Word.WdMovementType.wdExtend;
45                                    WordApp.Selection.MoveUp(ref moveUnit, ref moveCount, ref moveExtend);
46                                    WordApp.Selection.Cells.Merge();
47                                    WordApp.Selection.Cells.VerticalAlignment =  Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
48
49                    doc.Content.Tables[1].Cell(i+1,1).Range.Text = “单元格内容填充”
50
51//添加表格行
52doc.Content.Tables[0].Rows.Add(ref beforeRow);
53//添加表格列
54doc.Content.Tables[0].Columns.Add(ref beforeColumn);
55//文本居中
56WordApp.Selection.ParagraphFormat.Alignment =
57      Word.WdParagraphAlignment.wdAlignParagraphCenter;
58WordApp.Selection.Cells.VerticalAlignment =  
59      Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//选中单元格文字垂直居中
60



 

5.       总结

其实有关Office开发,比如Word,可以通过打开Word,然后通过宏录制,把你所想要做的事情进行录制,然后查看VBA脚本,对VB熟悉的朋友就很容易了。在C#中的操作也大同小异,只是语法上不同,对于初学Office开发的朋友很容易入门的。