C# 使用NPOI生成Word文档(按照模板)


当前第2页 返回上一页

三、 表格处理(XWPFTable table)
doc.Tables 获取文档里的所有的表格对象;
这里有必要多一嘴,doc.Tables获取的只是Word中最外层的表格,不包含嵌套内层的。
获取嵌套单元格可使用cell.Tables;
(一) 表格行处理(XWPFTableRow row)
row.Rows 获取表格所有行;
(二) 表格单元格处理(XWPFTableCell cell)
row.GetTableICells() ;获取表格行的所有单元格;
获取到单元格之后就可以获取单元格里的文本段落(Paragraphs)并且进行文本替换
(三) 水平合并行单元格

1

2

3

4

CT_Tc cttcofRowThird = cell.GetCTTc();

CT_TcPr ctProfRowThird = cttcofRowThird.AddNewTcPr();

ctProfRowThird.gridSpan = new CT_DecimalNumber();

ctProfRowThird.gridSpan.val = num.ToString();//合并num列

(四) 垂直合并列单元格

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

List<XWPFTableRow> rows所有要合并的行的XWPFTableRow对象集合。

 

XWPFTableCell cellFirstofThird = 第一行要合并的单元格对象;

CT_Tc cttcFirstofThird = cellFirstofThird.GetCTTc();

CT_TcPr ctPrFirstofThird = cttcFirstofThird.AddNewTcPr();

ctPrFirstofThird.AddNewVMerge().val = ST_Merge.restart;//开始合并行

ctPrFirstofThird.AddNewVAlign().val = ST_VerticalJc.center;//垂直

cttcFirstofThird.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;

for (int i = 1; i < rows.Count; i++)

{

    XWPFTableCell cellfirstofRow = 第i行要合并的单元格对象;

    CT_Tc cttcfirstofRow = cellfirstofRow.GetCTTc();

    CT_TcPr ctPrfirstofRow = cttcfirstofRow.AddNewTcPr();

    ctPrfirstofRow.AddNewVMerge().val = ST_Merge.@continue;//继续合并行

    ctPrfirstofRow.AddNewVAlign().val = ST_VerticalJc.center;//垂直

}

四、 图片处理
2.3.0版本的NPOI的图片插入没有集成xml文件的修改所以需要手写代码编入(当然,我是拷过来的)。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

using (FileStream fsImg = new FileStream(图片路径, FileMode.Open, FileAccess.Read, FileShare.None))

{

var picID = doc.AddPictureData(fsImg, (int)NPOI.XWPF.UserModel.PictureType.JPEG);

string picXml = ""

+ "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"

                    + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\""

                    + "0"

                    + "\" name=\"Generated\"/>"

                    + "            <pic:cNvPicPr/>"

                    + "         </pic:nvPicPr>"

                    + "         <pic:blipFill>"

                    + "            <a:blip r:embed=\""

                    + id

                    + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"

                    + "            <a:stretch>"

                    + "               <a:fillRect/>"

                    + "            </a:stretch>"

                    + "         </pic:blipFill>"

                    + "         <pic:spPr>"

                    + "            <a:xfrm>"

                    + "               <a:off x=\"0\" y=\"0\"/>"

                    + "               <a:ext cx=\""

                    + width

                    + "\" cy=\""

                    + height

                    + "\"/>"

                    + "            </a:xfrm>"

                    + "            <a:prstGeom prst=\"rect\">"

                    + "               <a:avLst/>"

                    + "            </a:prstGeom>"

                    + "         </pic:spPr>"

                    + "      </pic:pic>";

                XWPFParagraph par = cell.AddParagraph();//创建段落对象(可以在doc加也可在cell加)

                par.Alignment = ParagraphAlignment.CENTER;//居中

                XWPFRun run = par.CreateRun();

                CT_Inline inline = run.GetCTR().AddNewDrawing().AddNewInline();

                inline.graphic = new CT_GraphicalObject

                {

                    graphicData = new CT_GraphicalObjectData

                    {

                        uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"

                    }

                };

                try

                {

                    inline.graphic.graphicData.AddPicElement(picXml);

                }

                catch (XmlException xe)

                {

                    throw xe;

                }

                NPOI.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D extent = inline.AddNewExtent();

                extent.cx = width;

                extent.cy = height;

                NPOI.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();

                docPr.id = 1;

                docPr.name = "Image" + id;

}

今天先到这了,以后有收获再补充。

相关推荐:

使用C/C++编写PHP Extension

【c#教程】C# 数据类型

以上就是C# 使用NPOI生成Word文档(按照模板)的详细内容!

返回前面的内容

相关阅读 >>

C#如何使用浏览按钮获得文件路径和文件夹路径的实现方法

C#如何使用?C#的基本语法

C# windowsapi应用之flashwindowex -实现窗口闪烁的方法详解

C#语言为什么用的人很少

.net和C#有什么区别

深入理解C#rx的主要接口

C#教程】C# 枚举(enum)

详细介绍C#数学运算表达式解释器的示例代码

详细介绍asp.net中的C#基础知识

C#日期格式转换的公共方法类的实现详解

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...