Npoi导出非模板
最近使用NPOI做了个导出Word文档的功能,因为之前都是导出Excel很方便(不用模板),所以导出Word也选用了Npoi(也没有用模板,??),开始之前没有查阅资料,非模板简直费劲的狠,有的方法查阅资料搜索出来的都不沾边,后来通过查阅资料与自己研究,终于把需求实现了,话不多说上代码,希望以后能够帮助到有需要的人
一 、首先引入NPOI NuGet:
版本说明
NPOI 2.5.1 本次用到了最新版本的Npoi跟以前旧版本对比 有的方法没有找到 通过Nuget搜索Npoi下载安装就好
二、创建页眉页脚(显示第几页共几页):
使用方式,如下所示:
如果我们没有特殊要求使用 只显示页眉页脚
CT_Ftr m_ftr = new CT_Ftr(); m_ftr.AddNewP().AddNewR().AddNewT().Value = "我是页脚"; XWPFRelation Frelation = XWPFRelation.FOOTER; XWPFFooter m_f = (XWPFFooter)document.CreateRelationship(Frelation, XWPFFactory.GetInstance(), document.FooterList.Count + 1); //设置页脚 m_f.SetHeaderFooter(m_ftr); m_f.SetXWPFDocument(document); CT_HdrFtrRef m_HdrFtr1 = m_SectPr.AddNewFooterReference(); m_HdrFtr1.type = ST_HdrFtr.@default; m_HdrFtr1.id = m_f.GetPackageRelationship().Id;
这里我需要显示第几页共几页 还会显示页脚有背景色 页眉有字体颜色等,如下:
/// <summary> /// 创建页眉 /// </summary> /// <param name="document"></param> /// <param name="m_SectPr"></param> /// <param name="jo"></param> public static void CreateHeader(XWPFDocument document, CT_SectPr m_SectPr, QuotationExportEntity.QuotationDetailInfoRequest Entity) { int FontSize = 17; //创建页眉 CT_Hdr m_Hdr = new CT_Hdr(); m_Hdr.Items = new System.Collections.ArrayList();//添加多个头信息 CT_SdtBlock m_Sdt = new CT_SdtBlock(); CT_SdtContentBlock m_SdtContent = m_Sdt.AddNewSdtContent(); CT_P m_SdtContentP = m_SdtContent.AddNewP(); CT_PPr m_SdtContentPpPr = m_SdtContentP.AddNewPPr(); m_SdtContentPpPr.AddNewJc().val = ST_Jc.left; m_SdtContentP.Items = new System.Collections.ArrayList(); CT_R m_SdtR = m_SdtContentP.AddNewR(); CT_RPr m_ctrpr = m_SdtR.AddNewRPr(); CT_Fonts rfonts = m_ctrpr.AddNewRFonts(); //添加新的字体 CT_Color rColor = m_ctrpr.AddNewColor(); //改变页字体颜色 rColor.val = "red"; rfonts.ascii = "新微软雅黑"; rfonts.eastAsia = "新微软雅黑"; m_ctrpr.AddNewSz().val = (ulong)FontSize; m_SdtR.AddNewT().Value = "联想报价单 "; m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); m_ctrpr.AddNewSz().val = (ulong)FontSize; m_SdtR.AddNewT().Value = "" + Entity.quotationNumber.ToString() + "-买方名称:" + Entity.customerName + " "; m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); rColor = m_ctrpr.AddNewColor(); rColor.val = "red"; if (!string.IsNullOrEmpty(Entity.lenovoLinkName) && string.IsNullOrEmpty(Entity.customerPhone)) { m_ctrpr.AddNewSz().val = (ulong)FontSize; m_SdtR.AddNewT().Value = " 客户经理:"; m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); m_ctrpr.AddNewSz().val = (ulong)FontSize; m_SdtR.AddNewT().Value = "" + Entity.lenovoLinkName + " (" + Entity.customerPhone + ") "; } m_Hdr.Items.Add(m_Sdt); XWPFRelation Hrelation = XWPFRelation.HEADER; XWPFHeader m_h = (XWPFHeader)document.CreateRelationship(Hrelation, XWPFFactory.GetInstance()); //设置页眉 m_h.SetHeaderFooter(m_Hdr); CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference(); m_HdrFtr2.type = ST_HdrFtr.@default; m_HdrFtr2.id = m_h.GetPackageRelationship().Id; } /// <summary> /// 创建页脚 /// </summary> /// <param name="document"></param> /// <param name="m_SectPr"></param> /// <param name="jo"></param> public static void CreateFooter(XWPFDocument document, CT_SectPr m_SectPr, QuotationExportEntity.QuotationDetailInfoRequest Entity) { int FontSize = 17; CT_Ftr m_ftr = new CT_Ftr(); m_ftr.Items = new System.Collections.ArrayList(); CT_SdtBlock m_Sdt = new CT_SdtBlock(); CT_SdtPr m_SdtPr = m_Sdt.AddNewSdtPr(); CT_SdtDocPart m_SdtDocPartObj = m_SdtPr.AddNewDocPartObj(); m_SdtDocPartObj.AddNewDocPartGallery().val = "Page Numbers (Bottom of Page)"; m_SdtDocPartObj.docPartUnique = new CT_OnOff(); m_Sdt.AddNewSdtEndPr(); CT_SdtContentBlock m_SdtContent = m_Sdt.AddNewSdtContent(); CT_P m_SdtContentP = m_SdtContent.AddNewP(); CT_PPr m_SdtContentPpPr = m_SdtContentP.AddNewPPr(); m_SdtContentPpPr.AddNewJc().val = ST_Jc.left; m_SdtContentP.Items = new System.Collections.ArrayList(); CT_SimpleField m_fldSimple = new CT_SimpleField(); CT_R m_SdtR = m_SdtContentP.AddNewR(); CT_RPr m_ctrpr = m_SdtR.AddNewRPr(); CT_Fonts rfonts = m_ctrpr.AddNewRFonts(); //添加新的字体 CT_Color rColor = m_ctrpr.AddNewColor(); //改变页字体颜色 CT_OnOff bBold = m_ctrpr.AddNewB(); //字体加粗 CT_OnOff bcsCold = m_ctrpr.AddNewBCs(); rColor.val = "white"; rfonts.ascii = "新微软雅黑"; rfonts.eastAsia = "新微软雅黑"; m_ctrpr.AddNewSz().val = (ulong)FontSize; m_ctrpr.AddNewSzCs().val = (ulong)FontSize; m_SdtR.AddNewRPr().AddNewLang().val = "zh-CN"; //代表的是语言文化代码和国家地区 CT_Highlight rHighlight = new CT_Highlight(); rHighlight.val = ST_HighlightColor.black; m_ctrpr.highlight = rHighlight; bBold.val = true; bcsCold.val = true; m_SdtR.AddNewT().Value = " 联想商用PC "; m_ftr.Items.Add(m_Sdt); m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); rfonts = m_ctrpr.AddNewRFonts(); rfonts.ascii = "新微软雅黑"; rfonts.eastAsia = "新微软雅黑"; m_ctrpr.AddNewSz().val = (ulong)FontSize; m_ctrpr.AddNewSzCs().val = (ulong)FontSize; m_SdtR.AddNewRPr().AddNewLang().val = "zh-CN"; //代表的是语言文化代码和国家地区 m_SdtR.AddNewT().Value = " "; m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); rfonts = m_ctrpr.AddNewRFonts(); rfonts.ascii = "新微软雅黑"; rfonts.eastAsia = "新微软雅黑"; m_ctrpr.AddNewSz().val = (ulong)FontSize; m_ctrpr.AddNewSzCs().val = (ulong)FontSize; m_SdtR.AddNewRPr().AddNewLang().val = "zh-CN"; //代表的是语言文化代码和国家地区 rHighlight = new CT_Highlight(); rHighlight.val = ST_HighlightColor.red; m_ctrpr.highlight = rHighlight; bBold = m_ctrpr.AddNewB(); //字体加粗 bcsCold = m_ctrpr.AddNewBCs(); bBold.val = true; bcsCold.val = true; rColor = m_ctrpr.AddNewColor(); rColor.val = "white"; m_SdtR.AddNewT().Value = " 专业,只为更好 "; m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); rfonts = m_ctrpr.AddNewRFonts(); rfonts.ascii = "新微软雅黑"; rfonts.eastAsia = "新微软雅黑"; m_ctrpr.AddNewSz().val = (ulong)FontSize; m_ctrpr.AddNewSzCs().val = (ulong)FontSize; m_SdtR.AddNewRPr().AddNewLang().val = "zh-CN"; //代表的是语言文化代码和国家地区 m_SdtR.AddNewT().Value = " 第"; m_fldSimple.instr = " PAGE \\* Arabic \\* MERGEFORMAT "; m_SdtContentP.Items.Add(m_fldSimple); m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); rfonts = m_ctrpr.AddNewRFonts(); rfonts.ascii = "新微软雅黑"; rfonts.eastAsia = "新微软雅黑"; m_ctrpr.AddNewSz().val = (ulong)FontSize; m_ctrpr.AddNewSzCs().val = (ulong)FontSize; m_ctrpr.AddNewLang().val = "zh-CN"; m_SdtR.AddNewT().Value = "页 共";// / m_fldSimple = new CT_SimpleField(); m_fldSimple.instr = " NUMPAGES \\* Arabic \\* MERGEFORMAT "; m_SdtContentP.Items.Add(m_fldSimple); m_SdtR = m_SdtContentP.AddNewR(); m_ctrpr = m_SdtR.AddNewRPr(); rfonts = m_ctrpr.AddNewRFonts(); rfonts.ascii = "新微软雅黑"; rfonts.eastAsia = "新微软雅黑"; m_ctrpr.AddNewSz().val = (ulong)FontSize; m_ctrpr.AddNewSzCs().val = (ulong)FontSize; m_ctrpr.AddNewLang().val = "zh-CN"; m_SdtR.AddNewT().Value = "页"; //创建页脚关系 //m_ftr.AddNewP().AddNewR().AddNewT().Value = "我是页脚"; XWPFRelation Frelation = XWPFRelation.FOOTER; XWPFFooter m_f = (XWPFFooter)document.CreateRelationship(Frelation, XWPFFactory.GetInstance(), document.FooterList.Count + 1); //设置页脚 m_f.SetHeaderFooter(m_ftr); m_f.SetXWPFDocument(document); CT_HdrFtrRef m_HdrFtr1 = m_SectPr.AddNewFooterReference(); m_HdrFtr1.type = ST_HdrFtr.@default; m_HdrFtr1.id = m_f.GetPackageRelationship().Id; }
三、创建段落 文本 表格 插入图片 垂直合并单元格:
合并单元格查阅资料很多都是边创建表格边合并,这里是创建完表格在合并
/// <summary> /// 创建word文档中的段落对象和设置段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置) /// </summary> /// <param name="document">document文档对象</param> /// <param name="fillContent">段落第一个文本对象填充的内容</param> /// <param name="isBold">是否加粗</param> /// <param name="fontSize">字体大小</param> /// <param name="fontFamily">字体</param> /// <param name="paragraphAlign">段落排列(左对齐,居中,右对齐)</param> /// <param name="isStatement">是否在同一段落创建第二个文本对象(解决同一段落里面需要填充两个或者多个文本值的情况,多个文本需要自己拓展,现在最多支持两个)</param> /// <param name="secondFillContent">第二次声明的文本对象填充的内容,样式与第一次的一致</param> /// <returns></returns> private static XWPFParagraph ParagraphInstanceSetting(XWPFDocument document, string fillContent, bool isBold, int fontSize, string fontFamily, string rgbStr, UnderlinePatterns value, ParagraphAlignment paragraphAlign, ParagraphAlignment secondParagraphAlign, int secondFontSize = 0, bool isStatement = false, string secondFillContent = "") { XWPFParagraph paragraph = document.CreateParagraph();//创建段落对象 paragraph.Alignment = paragraphAlign;//文字显示位置,段落排列(左对齐,居中,右对齐) XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象 xwpfRun.IsBold = isBold;//文字加粗 xwpfRun.SetText(fillContent);//填充内容 xwpfRun.FontSize = fontSize;//设置文字大小 xwpfRun.SetFontFamily(fontFamily, FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定 xwpfRun.SetColor(rgbStr);//设置字体颜色--十六进制 xwpfRun.SetUnderline(value);//设置下划线,枚举类型 xwpfRun.AddTab(); //第二个文本的样式 if (isStatement) { XWPFRun secondxwpfRun = paragraph.CreateRun();//创建段落文本对象 secondxwpfRun.IsBold = isBold;//文字加粗 secondxwpfRun.SetText(secondFillContent);//填充内容 secondxwpfRun.FontSize = secondFontSize;//设置文字大小 secondxwpfRun.SetFontFamily(fontFamily, FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定 } return paragraph; } /// <summary> /// 创建Word文档中表格段落实例和设置表格段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置) /// </summary> /// <param name="document">document文档对象</param> /// <param name="table">表格对象</param> /// <param name="fillContent">要填充的文字</param> /// <param name="paragraphAlign">段落排列(左对齐,居中,右对齐)</param> /// <param name="textPosition">设置文本位置(设置两行之间的行间,从而实现表格文字垂直居中的效果),从而实现table的高度设置效果 </param> /// <param name="isBold">是否加粗(true加粗,false不加粗)</param> /// <param name="fontSize">字体大小</param> /// <param name="fontColor">字体颜色--十六进制</param> /// <param name="isItalic">是否设置斜体(字体倾斜)</param> /// <returns></returns> public static XWPFParagraph SetTableParagraphInstanceSetting(XWPFDocument document, XWPFTable table, string fillContent, ParagraphAlignment paragraphAlign, int textPosition = 24, bool isBold = false, int fontSize = 10, string fontColor = "000000", bool isItalic = false, UnderlinePatterns value = UnderlinePatterns.None, int secondFontSize = 0, bool isStatement = false, string secondFillContent = "") { var para = new CT_P(); //设置单元格文本对齐 para.AddNewPPr().AddNewTextAlignment(); XWPFParagraph paragraph = new XWPFParagraph(para, table.Body);//创建表格中的段落对象 paragraph.Alignment = paragraphAlign; XWPFRun xwpfRun = paragraph.CreateRun();// xwpfRun.SetText(fillContent); xwpfRun.FontSize = fontSize;//字体大小 xwpfRun.SetColor(fontColor);//设置字体颜色--十六进制 xwpfRun.IsItalic = isItalic;//是否设置斜体(字体倾斜) xwpfRun.IsBold = isBold;//是否加粗 xwpfRun.SetFontFamily("宋体", FontCharRange.None);//设置字体(如:微软雅黑,华文楷体,宋体) xwpfRun.TextPosition = textPosition;//设置文本位置(设置两行之间的行间),从而实现table的高度设置效果 xwpfRun.SetUnderline(value);//设置下划线,枚举类型 //第二个文本的样式 if (isStatement) { XWPFRun secondxwpfRun = paragraph.CreateRun();//创建段落文本对象 secondxwpfRun.SetText(secondFillContent); secondxwpfRun.FontSize = secondFontSize;//字体大小 secondxwpfRun.SetColor(fontColor);//设置字体颜色--十六进制 secondxwpfRun.IsItalic = isItalic;//是否设置斜体(字体倾斜) secondxwpfRun.IsBold = isBold;//是否加粗 secondxwpfRun.SetFontFamily("宋体", FontCharRange.None);//设置字体(如:微软雅黑,华文楷体,宋体) secondxwpfRun.TextPosition = textPosition;//设置文本位置(设置两行之间的行间),从而实现table的高度设置效果 secondxwpfRun.SetUnderline(value);//设置下划线,枚举类型 } return paragraph; } /// <summary> /// Table中插入图片 /// </summary> /// <param name="document">document文档对象</param> /// <param name="table">表格对象</param> /// <param name="path">路径</param> private static XWPFParagraph SetWritePicture(XWPFDocument document, XWPFTable table, string path, ParagraphAlignment paragraphAlign, NPOI.XWPF.UserModel.PictureType type, int widthEmus, int heightEmus) { var para = new CT_P(); XWPFParagraph xwpfPicture = new XWPFParagraph(para, table.Body);//创建表格中的段落对象 xwpfPicture.Alignment = paragraphAlign; XWPFRun runPicture = xwpfPicture.CreateRun(); using (FileStream picData = new FileStream(path, FileMode.Open, FileAccess.Read)) { runPicture.AddPicture(picData, (int)type, "11.png", widthEmus, heightEmus); } //为了解决Npoi版本Bug CT_Inline inline = runPicture.GetCTR().GetDrawingList()[0].inline[0]; inline.docPr.id = 1; return xwpfPicture; } /// <summary> /// 垂直合并单元格(先创建表格后合并) /// </summary> /// <param name="table"></param> /// <param name="fromCol"></param> /// <param name="toCol"></param> /// <param name="fromRow"></param> /// <param name="toRow"></param> /// <returns></returns> public static void MYMergeCells(XWPFTable table, int fromCol, int toCol, int fromRow, int toRow) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { if (fromCol < toCol) { table.GetRow(rowIndex).MergeCells(fromCol, toCol); } XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(fromCol); CT_Tc cttc = rowcell.GetCTTc(); if (cttc.tcPr == null) { cttc.AddNewTcPr(); } if (rowIndex == fromRow) { // The first merged cell is set with RESTART merge value rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.restart; } else { // Cells which join (merge) the first one, are set with CONTINUE rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.@continue; } } }
/// <summary> /// 创建word文档中的段落对象和设置段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置) /// </summary> /// <param name="document">document文档对象</param> /// <param name="fillContent">段落第一个文本对象填充的内容</param> /// <param name="isBold">是否加粗</param> /// <param name="fontSize">字体大小</param> /// <param name="fontFamily">字体</param> /// <param name="paragraphAlign">段落排列(左对齐,居中,右对齐)</param> /// <param name="isStatement">是否在同一段落创建第二个文本对象(解决同一段落里面需要填充两个或者多个文本值的情况,多个文本需要自己拓展,现在最多支持两个)</param> /// <param name="secondFillContent">第二次声明的文本对象填充的内容,样式与第一次的一致</param> /// <returns></returns> private static XWPFParagraph ParagraphInstanceSetting(XWPFDocument document, string fillContent, bool isBold, int fontSize, string fontFamily, string rgbStr, UnderlinePatterns value, ParagraphAlignment paragraphAlign, ParagraphAlignment secondParagraphAlign, int secondFontSize = 0, bool isStatement = false, string secondFillContent = "") { XWPFParagraph paragraph = document.CreateParagraph();//创建段落对象 paragraph.Alignment = paragraphAlign;//文字显示位置,段落排列(左对齐,居中,右对齐) XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象 xwpfRun.IsBold = isBold;//文字加粗 xwpfRun.SetText(fillContent);//填充内容 xwpfRun.FontSize = fontSize;//设置文字大小 xwpfRun.SetFontFamily(fontFamily, FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定 xwpfRun.SetColor(rgbStr);//设置字体颜色--十六进制 xwpfRun.SetUnderline(value);//设置下划线,枚举类型 xwpfRun.AddTab(); //第二个文本的样式 if (isStatement) { XWPFRun secondxwpfRun = paragraph.CreateRun();//创建段落文本对象 secondxwpfRun.IsBold = isBold;//文字加粗 secondxwpfRun.SetText(secondFillContent);//填充内容 secondxwpfRun.FontSize = secondFontSize;//设置文字大小 secondxwpfRun.SetFontFamily(fontFamily, FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定 } return paragraph; } /// <summary> /// 创建Word文档中表格段落实例和设置表格段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置) /// </summary> /// <param name="document">document文档对象</param> /// <param name="table">表格对象</param> /// <param name="fillContent">要填充的文字</param> /// <param name="paragraphAlign">段落排列(左对齐,居中,右对齐)</param> /// <param name="textPosition">设置文本位置(设置两行之间的行间,从而实现表格文字垂直居中的效果),从而实现table的高度设置效果 </param> /// <param name="isBold">是否加粗(true加粗,false不加粗)</param> /// <param name="fontSize">字体大小</param> /// <param name="fontColor">字体颜色--十六进制</param> /// <param name="isItalic">是否设置斜体(字体倾斜)</param> /// <returns></returns> public static XWPFParagraph SetTableParagraphInstanceSetting(XWPFDocument document, XWPFTable table, string fillContent, ParagraphAlignment paragraphAlign, int textPosition = 24, bool isBold = false, int fontSize = 10, string fontColor = "000000", bool isItalic = false, UnderlinePatterns value = UnderlinePatterns.None, int secondFontSize = 0, bool isStatement = false, string secondFillContent = "") { var para = new CT_P(); //设置单元格文本对齐 para.AddNewPPr().AddNewTextAlignment(); XWPFParagraph paragraph = new XWPFParagraph(para, table.Body);//创建表格中的段落对象 paragraph.Alignment = paragraphAlign; XWPFRun xwpfRun = paragraph.CreateRun();// xwpfRun.SetText(fillContent); xwpfRun.FontSize = fontSize;//字体大小 xwpfRun.SetColor(fontColor);//设置字体颜色--十六进制 xwpfRun.IsItalic = isItalic;//是否设置斜体(字体倾斜) xwpfRun.IsBold = isBold;//是否加粗 xwpfRun.SetFontFamily("宋体", FontCharRange.None);//设置字体(如:微软雅黑,华文楷体,宋体) xwpfRun.TextPosition = textPosition;//设置文本位置(设置两行之间的行间),从而实现table的高度设置效果 xwpfRun.SetUnderline(value);//设置下划线,枚举类型 //第二个文本的样式 if (isStatement) { XWPFRun secondxwpfRun = paragraph.CreateRun();//创建段落文本对象 secondxwpfRun.SetText(secondFillContent); secondxwpfRun.FontSize = secondFontSize;//字体大小 secondxwpfRun.SetColor(fontColor);//设置字体颜色--十六进制 secondxwpfRun.IsItalic = isItalic;//是否设置斜体(字体倾斜) secondxwpfRun.IsBold = isBold;//是否加粗 secondxwpfRun.SetFontFamily("宋体", FontCharRange.None);//设置字体(如:微软雅黑,华文楷体,宋体) secondxwpfRun.TextPosition = textPosition;//设置文本位置(设置两行之间的行间),从而实现table的高度设置效果 secondxwpfRun.SetUnderline(value);//设置下划线,枚举类型 } return paragraph; } /// <summary> /// Table中插入图片 /// </summary> /// <param name="document">document文档对象</param> /// <param name="table">表格对象</param> /// <param name="path">路径</param> private static XWPFParagraph SetWritePicture(XWPFDocument document, XWPFTable table, string path, ParagraphAlignment paragraphAlign, NPOI.XWPF.UserModel.PictureType type, int widthEmus, int heightEmus) { var para = new CT_P(); XWPFParagraph xwpfPicture = new XWPFParagraph(para, table.Body);//创建表格中的段落对象 xwpfPicture.Alignment = paragraphAlign; XWPFRun runPicture = xwpfPicture.CreateRun(); using (FileStream picData = new FileStream(path, FileMode.Open, FileAccess.Read)) { runPicture.AddPicture(picData, (int)type, "11.png", widthEmus, heightEmus); } //为了解决Npoi版本Bug CT_Inline inline = runPicture.GetCTR().GetDrawingList()[0].inline[0]; inline.docPr.id = 1; return xwpfPicture; } /// <summary> /// 垂直合并单元格(先创建表格后合并) /// </summary> /// <param name="table"></param> /// <param name="fromCol"></param> /// <param name="toCol"></param> /// <param name="fromRow"></param> /// <param name="toRow"></param> /// <returns></returns> public static void MYMergeCells(XWPFTable table, int fromCol, int toCol, int fromRow, int toRow) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { if (fromCol < toCol) { table.GetRow(rowIndex).MergeCells(fromCol, toCol); } XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(fromCol); CT_Tc cttc = rowcell.GetCTTc(); if (cttc.tcPr == null) { cttc.AddNewTcPr(); } if (rowIndex == fromRow) { // The first merged cell is set with RESTART merge value rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.restart; } else { // Cells which join (merge) the first one, are set with CONTINUE rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.@continue; } } }
四、 生成Word,RowIndex(根据数据判断达到XX行或它的倍数就直接换页)
int TableIndex = 0; int DocIndex = 0; int RowIndex = 5; //通过使用文件流,创建文件流对象,向文件流中写入内容,并保存为Word文档格式 using (var stream = new FileStream(Path.Combine(uploadPath, fileName), FileMode.Create, FileAccess.Write)) { //创建document文档对象对象实例 XWPFDocument document = new XWPFDocument(); document.Document.body.sectPr = new CT_SectPr(); CT_SectPr m_SectPr = document.Document.body.sectPr; m_SectPr.pgSz.h = (ulong)16838; m_SectPr.pgSz.w = (ulong)11906; m_SectPr.pgMar.left = (ulong)1000;//左边距 m_SectPr.pgMar.right = (ulong)1000;//右边距 m_SectPr.pgMar.top ="20"; m_SectPr.pgMar.bottom = "20"; //创建页脚 带页码(第几页 共几页) CreateFooter(document, m_SectPr, Entity); //创建页眉 CreateHeader(document, m_SectPr, Entity); ////文本标题 document.SetParagraph(ParagraphInstanceSetting(document, "", true, 15, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.CENTER, ParagraphAlignment.RIGHT), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, "", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.RIGHT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); #region Table0 XWPFTable zeroXwpfTable = document.CreateTable(2, 3);//显示的行列数rows:3行,cols:4列 CT_Tbl zeroTable = document.Document.body.GetTblArray()[TableIndex++]; zeroTable.AddNewTblPr().jc = new CT_Jc(); zeroTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 zeroTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 zeroTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr zeroPr = zeroXwpfTable.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); zeroPr.tcW = new CT_TblWidth(); zeroPr.tcW.w = "3500";//单元格宽 zeroPr.tcW.type = ST_TblWidth.dxa; zeroPr = zeroXwpfTable.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); zeroPr.tcW = new CT_TblWidth(); zeroPr.tcW.w = "1000";//单元格宽 zeroPr.tcW.type = ST_TblWidth.dxa; zeroPr = zeroXwpfTable.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr(); zeroPr.tcW = new CT_TblWidth(); zeroPr.tcW.w = "4000";//单元格宽 zeroPr.tcW.type = ST_TblWidth.dxa; zeroXwpfTable.SetBottomBorder(XWPFBorderType.NONE, 1, 0, null); zeroXwpfTable.SetLeftBorder(XWPFBorderType.NONE, 1, 0, null); zeroXwpfTable.SetTopBorder(XWPFBorderType.NONE, 1, 0, null); zeroXwpfTable.SetRightBorder(XWPFBorderType.NONE, 1, 0, null); zeroXwpfTable.SetInsideHBorder(XWPFBorderType.NONE, 1, 0, null); zeroXwpfTable.SetInsideVBorder(XWPFBorderType.NONE, 1, 0, null); MYMergeCells(zeroXwpfTable, 2, 2, 0, 1); zeroXwpfTable.GetRow(0).GetCell(0).SetVerticalAlignment(XWPFVertAlign.CENTER); zeroXwpfTable.GetRow(1).GetCell(0).SetVerticalAlignment(XWPFVertAlign.CENTER); zeroXwpfTable.GetRow(1).GetCell(2).SetVerticalAlignment(XWPFVertAlign.CENTER); zeroXwpfTable.GetRow(0).GetCell(0).SetColor("#E12826"); zeroXwpfTable.GetRow(1).GetCell(0).SetColor("#E12826"); zeroXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, zeroXwpfTable, " 报价单编号", ParagraphAlignment.LEFT, 0, true, 8, "white", false, UnderlinePatterns.None, 7, true, Entity.quotationNumber.ToString())); zeroXwpfTable.GetRow(0).GetCell(2).SetParagraph(SetWritePicture(document, zeroXwpfTable, "F:\\2.PNG".ToString(), ParagraphAlignment.RIGHT, NPOI.XWPF.UserModel.PictureType.PNG, 130 * 9525, 37 * 9525)); zeroXwpfTable.GetRow(1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, zeroXwpfTable, " 客户名称 ", ParagraphAlignment.LEFT, 10, true, 8, "white", false, UnderlinePatterns.None, 7, true, Entity.customerName.ToString())); #endregion document.SetParagraph(ParagraphInstanceSetting(document, "", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.RIGHT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); if (Entity.quoteExport == 1) { #region Table1 XWPFTable firstXwpfTable = document.CreateTable(4, 2); CT_Tbl firstTable = document.Document.body.GetTblArray()[TableIndex++]; firstTable.AddNewTblPr().jc = new CT_Jc(); firstTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 firstTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 firstTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr firstPr = firstXwpfTable.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); firstPr.tcW = new CT_TblWidth(); firstPr.tcW.w = "5000";//单元格宽 firstPr.tcW.type = ST_TblWidth.dxa; firstPr = firstXwpfTable.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); firstPr.tcW = new CT_TblWidth(); firstPr.tcW.w = "3000";//单元格宽 firstPr.tcW.type = ST_TblWidth.dxa; firstXwpfTable.SetBottomBorder(XWPFBorderType.NONE, 1, 0, null); firstXwpfTable.SetLeftBorder(XWPFBorderType.NONE, 1, 0, null); firstXwpfTable.SetTopBorder(XWPFBorderType.NONE, 1, 0, null); firstXwpfTable.SetRightBorder(XWPFBorderType.NONE, 1, 0, null); firstXwpfTable.SetInsideHBorder(XWPFBorderType.NONE, 1, 0, null); firstXwpfTable.SetInsideVBorder(XWPFBorderType.NONE, 1, 0, null); firstXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, "报价单生成日期", ParagraphAlignment.LEFT, 10, false, 10, "#ff0000")); firstXwpfTable.GetRow(0).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, "付款方式", ParagraphAlignment.LEFT, 10, false, 10, "#ff0000")); firstXwpfTable.GetRow(1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, Entity.createTime.ToString(), ParagraphAlignment.LEFT, 40, false, 10)); firstXwpfTable.GetRow(1).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, Entity.payTypeName.ToString(), ParagraphAlignment.LEFT, 40, false, 10)); firstXwpfTable.GetRow(2).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, "报价单有效期至", ParagraphAlignment.LEFT, 10, false, 10, "#ff0000")); firstXwpfTable.GetRow(2).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, "预计供货周期", ParagraphAlignment.LEFT, 10, false, 10, "#ff0000")); firstXwpfTable.GetRow(3).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, Entity.createTime.ToString(), ParagraphAlignment.LEFT, 0, false, 10)); firstXwpfTable.GetRow(3).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, firstXwpfTable, Entity.supplyTime.ToString(), ParagraphAlignment.LEFT, 0, false, 10)); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "#ff0000", UnderlinePatterns.Single, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); #endregion RowIndex += 6; } if (Entity.customerExport == 1) { #region Table2 XWPFTable SecondXwpfTable = document.CreateTable(4, 2); CT_Tbl secondTable = document.Document.body.GetTblArray()[TableIndex++]; secondTable.AddNewTblPr().jc = new CT_Jc(); secondTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 secondTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 secondTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr secondPr = SecondXwpfTable.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); secondPr.tcW = new CT_TblWidth(); secondPr.tcW.w = "6000";//单元格宽 secondPr.tcW.type = ST_TblWidth.dxa; secondPr = SecondXwpfTable.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); secondPr.tcW = new CT_TblWidth(); secondPr.tcW.w = "3000";//单元格宽 secondPr.tcW.type = ST_TblWidth.dxa; SecondXwpfTable.SetBottomBorder(XWPFBorderType.NONE, 1, 0, null); SecondXwpfTable.SetLeftBorder(XWPFBorderType.NONE, 1, 0, null); SecondXwpfTable.SetTopBorder(XWPFBorderType.NONE, 1, 0, null); SecondXwpfTable.SetRightBorder(XWPFBorderType.NONE, 1, 0, null); SecondXwpfTable.SetInsideHBorder(XWPFBorderType.NONE, 1, 0, null); SecondXwpfTable.SetInsideVBorder(XWPFBorderType.NONE, 1, 0, null); SecondXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, SecondXwpfTable, "收货地址", ParagraphAlignment.LEFT, 20, false, 9, "#ff0000")); SecondXwpfTable.GetRow(1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, SecondXwpfTable, "客户名称:" + Entity.customerName.ToString() + "", ParagraphAlignment.LEFT, 30, false, 10, "")); SecondXwpfTable.GetRow(2).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, SecondXwpfTable, "项目地址:" + Entity.boName.ToString() + "", ParagraphAlignment.LEFT, 30, false, 10, "")); SecondXwpfTable.GetRow(2).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, SecondXwpfTable, "联系人:" + Entity.customerLinkName.ToString() + "", ParagraphAlignment.LEFT, 30, false, 10, "")); SecondXwpfTable.GetRow(3).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, SecondXwpfTable, "地 址:" + Entity.customerAddress.ToString() + "", ParagraphAlignment.LEFT, 0, false, 10, "")); SecondXwpfTable.GetRow(3).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, SecondXwpfTable, "电话:" + Entity.customerPhone.ToString() + "", ParagraphAlignment.LEFT, 0, false, 10)); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "#ff0000", UnderlinePatterns.Single, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); RowIndex += 6; #endregion } if (Entity.lenovoExport == 1) { #region Table3 XWPFTable ThreeXwpfTable = document.CreateTable(2, 3); CT_Tbl threeTable = document.Document.body.GetTblArray()[TableIndex++]; threeTable.AddNewTblPr().jc = new CT_Jc(); threeTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 threeTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 threeTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr threePr = ThreeXwpfTable.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); threePr.tcW = new CT_TblWidth(); threePr.tcW.w = "3000";//单元格宽 threePr.tcW.type = ST_TblWidth.dxa; threePr = ThreeXwpfTable.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); threePr.tcW = new CT_TblWidth(); threePr.tcW.w = "3000";//单元格宽 threePr.tcW.type = ST_TblWidth.dxa; threePr = ThreeXwpfTable.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr(); threePr.tcW = new CT_TblWidth(); threePr.tcW.w = "3000";//单元格宽 threePr.tcW.type = ST_TblWidth.dxa; ThreeXwpfTable.SetBottomBorder(XWPFBorderType.NONE, 1, 0, null); ThreeXwpfTable.SetLeftBorder(XWPFBorderType.NONE, 1, 0, null); ThreeXwpfTable.SetTopBorder(XWPFBorderType.NONE, 1, 0, null); ThreeXwpfTable.SetRightBorder(XWPFBorderType.NONE, 1, 0, null); ThreeXwpfTable.SetInsideHBorder(XWPFBorderType.NONE, 1, 0, null); ThreeXwpfTable.SetInsideVBorder(XWPFBorderType.NONE, 1, 0, null); ThreeXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, ThreeXwpfTable, "客户经理", ParagraphAlignment.LEFT, 0, false, 10, "#ff0000")); ThreeXwpfTable.GetRow(0).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, ThreeXwpfTable, "联系电话", ParagraphAlignment.LEFT, 0, false, 10, "#ff0000")); ThreeXwpfTable.GetRow(0).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, ThreeXwpfTable, "客户经理邮箱", ParagraphAlignment.LEFT, 0, false, 10, "#ff0000")); ThreeXwpfTable.GetRow(1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, ThreeXwpfTable, Entity.lenovoLinkName.ToString(), ParagraphAlignment.LEFT, 20, false, 10, "")); ThreeXwpfTable.GetRow(1).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, ThreeXwpfTable, Entity.lenovoPhone.ToString(), ParagraphAlignment.LEFT, 20, false, 10, "")); ThreeXwpfTable.GetRow(1).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, ThreeXwpfTable, Entity.lenovoEmail.ToString(), ParagraphAlignment.LEFT, 20, false, 10, "")); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "#ff0000", UnderlinePatterns.Single, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); RowIndex += 3; #endregion } if (Entity.invoiceExport == 1) { #region Table XWPFTable Table = document.CreateTable(2, 3); CT_Tbl CtTable = document.Document.body.GetTblArray()[TableIndex++]; CtTable.AddNewTblPr().jc = new CT_Jc(); CtTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 CtTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 CtTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr CtPr = Table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); CtPr.tcW = new CT_TblWidth(); CtPr.tcW.w = "3000";//单元格宽 CtPr.tcW.type = ST_TblWidth.dxa; CtPr = Table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); CtPr.tcW = new CT_TblWidth(); CtPr.tcW.w = "3000";//单元格宽 CtPr.tcW.type = ST_TblWidth.dxa; CtPr = Table.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr(); CtPr.tcW = new CT_TblWidth(); CtPr.tcW.w = "3000";//单元格宽 CtPr.tcW.type = ST_TblWidth.dxa; Table.SetBottomBorder(XWPFBorderType.NONE, 1, 0, null); Table.SetLeftBorder(XWPFBorderType.NONE, 1, 0, null); Table.SetTopBorder(XWPFBorderType.NONE, 1, 0, null); Table.SetRightBorder(XWPFBorderType.NONE, 1, 0, null); Table.SetInsideHBorder(XWPFBorderType.NONE, 1, 0, null); Table.SetInsideVBorder(XWPFBorderType.NONE, 1, 0, null); Table.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, Table, "发票形式", ParagraphAlignment.LEFT, 0, false, 10, "#ff0000")); Table.GetRow(0).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, Table, "发票收取方式", ParagraphAlignment.LEFT, 0, false, 10, "#ff0000")); Table.GetRow(1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, Table, Entity.InvoiceType.ToString(), ParagraphAlignment.LEFT, 20, false, 10, "")); Table.GetRow(1).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, Table, Entity.InvoiceReceiveTypeName.ToString(), ParagraphAlignment.LEFT, 20, false, 10, "")); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "#ff0000", UnderlinePatterns.Single, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); RowIndex += 3; #endregion } #region Table4 价格汇总 document.SetParagraph(ParagraphInstanceSetting(document, "|价格汇总", true, 15, "微软雅黑", "#ff0000", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); XWPFTable FourXwpfTable = document.CreateTable(product.Count + 2, 5); CT_Tbl FourTable = document.Document.body.GetTblArray()[TableIndex++]; FourTable.AddNewTblPr().jc = new CT_Jc(); FourTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 FourTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 FourTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr fourPr = FourXwpfTable.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr(); fourPr.tcW = new CT_TblWidth(); fourPr.tcW.w = "1000";//单元格宽 fourPr.tcW.type = ST_TblWidth.dxa; fourPr = FourXwpfTable.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr(); fourPr.tcW = new CT_TblWidth(); fourPr.tcW.w = "1000";//单元格宽 fourPr.tcW.type = ST_TblWidth.dxa; fourPr = FourXwpfTable.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr(); fourPr.tcW = new CT_TblWidth(); fourPr.tcW.w = "1000";//单元格宽 fourPr.tcW.type = ST_TblWidth.dxa; #region 设置样式 FourXwpfTable.SetBottomBorder(XWPFBorderType.NONE, 13, 0, "pink"); FourXwpfTable.SetLeftBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.SetTopBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.SetRightBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.SetInsideHBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.SetInsideVBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "产品名称", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(0).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "配置", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(0).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "数量", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(0).GetCell(3).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "单价(含税)", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(0).GetCell(4).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "金额(含税)", ParagraphAlignment.LEFT, 30, false, 10, "")); RowIndex += 3; #endregion int Num = 0; int TotalPrice = 0; int RowNum = 0; for (int i = 0; i < product.Count(); i++) { Num += product[i].quantity; TotalPrice += product[i].quantity * product[i].productPrice; FourXwpfTable.GetRow(i + 1).GetCell(4).SetColor("#EEEEEE"); for (int w = 18; w <= product[i].productName.ToString().Length; w++) { if (w % 18 == 0) { product[i].productName= product[i].productName.ToString().Insert(w, " "); } } for (int t = 18; t <= product[i].majorParts.ToString().Length; t++) { if (t % 18 == 0) { product[i].majorParts = product[i].majorParts.ToString().Insert(t," "); } } if (product[i].productName.ToString().Length >= product[i].majorParts.ToString().Length) { RowIndex += Convert.ToInt32(product[i].productName.ToString().Length / 18) + ((product[i].productName.ToString().Length % 18) > 0 ? 1 : 0); RowNum = Convert.ToInt32(product[i].productName.ToString().Length / 18) + ((product[i].productName.ToString().Length % 18) > 0 ? 1 : 0); } else { RowIndex += Convert.ToInt32(product[i].majorParts.ToString().Length / 18) + ((product[i].productName.ToString().Length % 18) > 0 ? 1 : 0); RowNum = Convert.ToInt32(product[i].majorParts.ToString().Length / 18) + ((product[i].productName.ToString().Length % 18) > 0 ? 1 : 0); } if (RowIndex > 40 && RowIndex <= 45) { RowIndex = 0; document.SetParagraph(BreakPage(document), DocIndex++); } else if (RowIndex > 45) { RowIndex = RowIndex - RowNum; document.SetParagraph(BreakPage(document), DocIndex++); RowIndex = 0; RowIndex += RowNum; } FourXwpfTable.GetRow(i + 1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, product[i].productName.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); FourXwpfTable.GetRow(i + 1).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, product[i].majorParts.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); FourXwpfTable.GetRow(i + 1).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, product[i].quantity.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); FourXwpfTable.GetRow(i + 1).GetCell(3).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, product[i].productPrice.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); FourXwpfTable.GetRow(i + 1).GetCell(4).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "¥" + product[i].productTotalPrice.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); } FourXwpfTable.GetRow(product.Count() + 1).GetCell(0).SetBorderRight(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(1).SetBorderRight(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(2).SetBorderRight(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(3).SetBorderRight(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(4).SetBorderRight(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(0).SetBorderLeft(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(1).SetBorderLeft(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(2).SetBorderLeft(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(3).SetBorderLeft(XWPFBorderType.NONE, 1, 0, null); FourXwpfTable.GetRow(product.Count() + 1).GetCell(4).SetBorderLeft(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(product.Count() + 1).GetCell(4).SetBorderRight(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(product.Count() + 1).GetCell(4).SetBorderBottom(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(product.Count() + 1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(product.Count() + 1).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(product.Count() + 1).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(product.Count() + 1).GetCell(3).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "总计(含税)", ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(product.Count() + 1).GetCell(4).SetParagraph(SetTableParagraphInstanceSetting(document, FourXwpfTable, "¥" + TotalPrice.ToString(), ParagraphAlignment.LEFT, 30, false, 10, "")); FourXwpfTable.GetRow(product.Count() + 1).GetCell(4).SetColor("#EEEEEE"); FourXwpfTable.GetRow(product.Count()).GetCell(0).SetBorderBottom(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(product.Count()).GetCell(1).SetBorderBottom(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(product.Count()).GetCell(2).SetBorderBottom(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(product.Count()).GetCell(3).SetBorderBottom(XWPFBorderType.SINGLE, 13, 0, "pink"); FourXwpfTable.GetRow(product.Count()).GetCell(4).SetBorderBottom(XWPFBorderType.SINGLE, 13, 0, "pink"); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); #endregion #region Table5 温馨提示 document.SetParagraph(ParagraphInstanceSetting(document, "|温馨提示", true, 15, "微软雅黑", "#ff0000", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); XWPFTable FiveXwpfTable = document.CreateTable(3, 1); CT_Tbl fiveTable = document.Document.body.GetTblArray()[TableIndex++]; fiveTable.AddNewTblPr().jc = new CT_Jc(); fiveTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 fiveTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 fiveTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr fivePr = FiveXwpfTable.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); fivePr.tcW = new CT_TblWidth(); fivePr.tcW.w = "9000";//单元格宽 fivePr.tcW.type = ST_TblWidth.dxa; FiveXwpfTable.SetBottomBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FiveXwpfTable.SetLeftBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FiveXwpfTable.SetTopBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FiveXwpfTable.SetRightBorder(XWPFBorderType.SINGLE, 13, 0, "pink"); FiveXwpfTable.SetInsideHBorder(XWPFBorderType.SINGLE, 1, 0, "pink"); FiveXwpfTable.SetInsideVBorder(XWPFBorderType.SINGLE, 1, 0, "pink"); FiveXwpfTable.GetRow(1).GetCell(0).SetBorderTop(XWPFBorderType.NONE, 1, 0, null); FiveXwpfTable.GetRow(2).GetCell(0).SetBorderTop(XWPFBorderType.NONE, 1, 0, null); FiveXwpfTable.GetRow(1).GetCell(0).SetBorderBottom(XWPFBorderType.NONE, 1, 0, null); FiveXwpfTable.GetRow(0).GetCell(0).SetBorderBottom(XWPFBorderType.NONE, 1, 0, null); FiveXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, FiveXwpfTable, "", ParagraphAlignment.LEFT, -10, false, 9, "")); FiveXwpfTable.GetRow(1).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, FiveXwpfTable, "", ParagraphAlignment.LEFT, 9, false, 10, "")); FiveXwpfTable.GetRow(2).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, FiveXwpfTable, "", ParagraphAlignment.LEFT, 30, false, 9, "")); document.SetParagraph(BreakPage(document), DocIndex++); #endregion #region Table6 document.SetParagraph(ParagraphInstanceSetting(document, "|报价单描述", true, 15, "微软雅黑", "#ff0000", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); for (int k = 0; k < Entity.projects.Count(); k++) { for (int i = 0; i < Entity.projects[k].products.Count(); i++) { if (Entity.projects[k].products[i].parts.Count() == 0) { continue; } XWPFTable SixXwpfTable = document.CreateTable(Entity.projects[k].products[i].parts.Count() + 3, 5);//显示的行列数rows:3行,cols:4列 CT_Tbl sixTable = document.Document.body.GetTblArray()[TableIndex++]; sixTable.AddNewTblPr().jc = new CT_Jc(); sixTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 sixTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 sixTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; CT_TcPr sixPr = SixXwpfTable.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); sixPr.tcW = new CT_TblWidth(); sixPr.tcW.w = "3000";//单元格宽 sixPr.tcW.type = ST_TblWidth.dxa; sixPr = SixXwpfTable.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); sixPr.tcW = new CT_TblWidth(); sixPr.tcW.w = "2000";//单元格宽 sixPr.tcW.type = ST_TblWidth.dxa; sixPr = SixXwpfTable.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr(); sixPr.tcW = new CT_TblWidth(); sixPr.tcW.w = "1500";//单元格宽 sixPr.tcW.type = ST_TblWidth.dxa; sixPr = SixXwpfTable.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr(); sixPr.tcW = new CT_TblWidth(); sixPr.tcW.w = "1500";//单元格宽 sixPr.tcW.type = ST_TblWidth.dxa; sixPr = SixXwpfTable.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr(); sixPr.tcW = new CT_TblWidth(); sixPr.tcW.w = "1000";//单元格宽 sixPr.tcW.type = ST_TblWidth.dxa; SixXwpfTable.SetBottomBorder(XWPFBorderType.NONE, 1, 0, null); SixXwpfTable.SetLeftBorder(XWPFBorderType.NONE, 1, 0, null); SixXwpfTable.SetTopBorder(XWPFBorderType.NONE, 1, 0, null); SixXwpfTable.SetRightBorder(XWPFBorderType.NONE, 1, 0, null); SixXwpfTable.SetInsideHBorder(XWPFBorderType.NONE, 1, 0, null); SixXwpfTable.SetInsideVBorder(XWPFBorderType.NONE, 1, 0, null); MYMergeCells(SixXwpfTable, 0, 0, 0, Entity.projects[k].products[i].parts.Count() + 2); MYMergeCells(SixXwpfTable, 1, 1, 0, 1); //if (fileJArray.fsp.bomPic.Where(p => p.bomCode == Entity.projects[k].products[i].bomCode).Count() > 0) //{ // SixXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetWritePicture(document, SixXwpfTable, fileJArray.fsp.bomPic.Where(p => p.bomCode == Entity.projects[k].products[i].bomCode).FirstOrDefault().bomPic, ParagraphAlignment.CENTER, NPOI.XWPF.UserModel.PictureType.JPEG, 170 * 9525, 170 * 9525)); //} //else //{ SixXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, "", ParagraphAlignment.LEFT, -10, false, 10, "#ff0000")); //} SixXwpfTable.GetRow(0).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, "数量", ParagraphAlignment.LEFT, -10, false, 10, "")); SixXwpfTable.GetRow(0).GetCell(3).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, "单价", ParagraphAlignment.LEFT, -10, false, 10, "")); SixXwpfTable.GetRow(0).GetCell(4).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, "金额", ParagraphAlignment.LEFT, -10, false, 10, "")); SixXwpfTable.GetRow(0).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, Entity.projects[k].products[i].productName.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "#ff0000")); SixXwpfTable.GetRow(1).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, Entity.projects[k].products[i].quantity.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); SixXwpfTable.GetRow(1).GetCell(3).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, Entity.projects[k].products[i].productPrice.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); SixXwpfTable.GetRow(1).GetCell(4).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, Entity.projects[k].products[i].productTotalPrice.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); // SixXwpfTable.GetRow(1).GetCell(1).AddParagraph().CreateRun(); SixXwpfTable.GetRow(2).MergeCells(1, 2); SixXwpfTable.GetRow(2).MergeCells(2, 3); SixXwpfTable.GetRow(2).GetCell(1).SetColor("#EEEEEE"); SixXwpfTable.GetRow(2).GetCell(2).SetColor("#EEEEEE"); SixXwpfTable.GetRow(2).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, "标签", ParagraphAlignment.LEFT, -10, true, 10, "")); SixXwpfTable.GetRow(2).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, "描述", ParagraphAlignment.LEFT, -10, true, 10, "")); for (int s = 0; s < Entity.projects[k].products[i].parts.Count(); s++) { SixXwpfTable.GetRow(3 + s).MergeCells(1, 2); SixXwpfTable.GetRow(3 + s).MergeCells(2, 3); SixXwpfTable.GetRow(3 + s).GetCell(1).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, Entity.projects[k].products[i].parts[s].r3Name.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); SixXwpfTable.GetRow(3 + s).GetCell(2).SetParagraph(SetTableParagraphInstanceSetting(document, SixXwpfTable, Entity.projects[k].products[i].parts[s].r3VName.ToString(), ParagraphAlignment.LEFT, -10, false, 10, "")); } document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "#ff0000", UnderlinePatterns.Single, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); } } #endregion //#region 广告位 //document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); //document.SetParagraph(ParagraphInstanceSetting(document, "|增值服务 ", true, 15, "微软雅黑", "#ff0000", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); //document.SetParagraph(ParagraphInstanceSetting(document, " ", false, 14, "微软雅黑", "", UnderlinePatterns.None, ParagraphAlignment.LEFT, ParagraphAlignment.RIGHT, 10, false, ""), DocIndex++); //XWPFTable ImageXwpfTable = document.CreateTable(1, 1); //CT_Tbl ImageTable = document.Document.body.GetTblArray()[TableIndex++]; //ImageTable.AddNewTblPr().jc = new CT_Jc(); //ImageTable.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 //ImageTable.AddNewTblPr().AddNewTblW().w = "9000";//表宽度 //ImageTable.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; //CT_TcPr ImagePr = ImageXwpfTable.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); //ImagePr.tcW = new CT_TblWidth(); //ImagePr.tcW.w = "9000";//单元格宽 //ImagePr.tcW.type = ST_TblWidth.dxa; //ImageXwpfTable.SetBottomBorder(XWPFBorderType.NONE, 13, 0, "pink"); //ImageXwpfTable.SetLeftBorder(XWPFBorderType.NONE, 13, 0, "pink"); //ImageXwpfTable.SetTopBorder(XWPFBorderType.NONE, 13, 0, "pink"); //ImageXwpfTable.SetRightBorder(XWPFBorderType.NONE, 13, 0, "pink"); //ImageXwpfTable.SetInsideHBorder(XWPFBorderType.NONE, 1, 0, "pink"); //ImageXwpfTable.SetInsideVBorder(XWPFBorderType.NONE, 1, 0, "pink"); //ImageXwpfTable.GetRow(0).GetCell(0).SetParagraph(SetWritePicture(document, ImageXwpfTable, fileJArray.fsp.adImage.Count() > 0 ? fileJArray.fsp.adImage : "", ParagraphAlignment.RIGHT, NPOI.XWPF.UserModel.PictureType.PNG, 600 * 9525, 1000 * 9525)); #endregion document.Write(stream); filePath = Path.Combine(uploadPath, fileName); }
总结:
经历了小一周的摸索,为了实现想要的样子,通过不断查阅与自己的理解终于达到了想要的效果,感谢那些写资料的人让我少走一些弯路,在这里记录下自己的一些收获,心得分享给大家,希望会帮助到大家
RowIndex