/// <summary> /// 创建文档 /// </summary> /// <param name="setting"></param> public static void ExportDocument(DocumentSetting setting) { XWPFDocument docx = new XWPFDocument(); MemoryStream ms = new MemoryStream(); //设置文档 docx.Document.body.sectPr = new CT_SectPr(); CT_SectPr setPr = docx.Document.body.sectPr; //获取页面大小 Tuple <int, int> size = GetPaperSize(setting.PaperType); setPr.pgSz.w = (ulong)size.Item1; setPr.pgSz.h = (ulong)size.Item2; //创建一个段落 CT_P p = docx.Document.body.AddNewP(); //段落水平居中 p.AddNewPPr().AddNewJc().val = ST_Jc.center; XWPFParagraph gp = new XWPFParagraph(p, docx); XWPFRun gr = gp.CreateRun(); //创建标题 if (!string.IsNullOrEmpty(setting.TitleSetting.Title)) { gr.GetCTR().AddNewRPr().AddNewRFonts().ascii = setting.TitleSetting.FontName; gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = setting.TitleSetting.FontName; gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia; gr.GetCTR().AddNewRPr().AddNewSz().val = (ulong)setting.TitleSetting.FontSize; //2号字体 gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)setting.TitleSetting.FontSize; gr.GetCTR().AddNewRPr().AddNewB().val = setting.TitleSetting.HasBold; //加粗 gr.GetCTR().AddNewRPr().AddNewColor().val = "black"; //字体颜色 gr.SetText(setting.TitleSetting.Title); } //创建文档主要内容 if (!string.IsNullOrEmpty(setting.MainContentSetting.MainContent)) { p = docx.Document.body.AddNewP(); p.AddNewPPr().AddNewJc().val = ST_Jc.both; gp = new XWPFParagraph(p, docx) { IndentationFirstLine = 2 }; //单倍为默认值(240)不需设置,1.5倍=240X1.5=360,2倍=240X2=480 p.AddNewPPr().AddNewSpacing().line = "400";//固定20磅 p.AddNewPPr().AddNewSpacing().lineRule = ST_LineSpacingRule.exact; gr = gp.CreateRun(); CT_RPr rpr = gr.GetCTR().AddNewRPr(); CT_Fonts rfonts = rpr.AddNewRFonts(); rfonts.ascii = setting.MainContentSetting.FontName; rfonts.eastAsia = setting.MainContentSetting.FontName; rpr.AddNewSz().val = (ulong)setting.MainContentSetting.FontSize;//5号字体-21 rpr.AddNewSzCs().val = (ulong)setting.MainContentSetting.FontSize; rpr.AddNewB().val = setting.MainContentSetting.HasBold; gr.SetText(setting.MainContentSetting.MainContent); } //开始写入 docx.Write(ms); using (FileStream fs = new FileStream(setting.SavePath, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } ms.Close(); }
/// <summary> /// 创建文档 /// </summary> /// <param name="setting"></param> public static void ExportDocument(List <Table> listTable, List <TableMapping> listMapping, string fileName) { XWPFDocument doc = new XWPFDocument(NPOI.OpenXml4Net.OPC.OPCPackage.Open("12345.docx")); MemoryStream ms = new MemoryStream(); DocumentSetting setting = new DocumentSetting(); int docItemsCount = doc.BodyElements.Count; for (int i = 0; i < docItemsCount; i++) { doc.RemoveBodyElement(0); } //设置文档 doc.Document.body.sectPr = new CT_SectPr(); CT_SectPr setPr = doc.Document.body.sectPr; //获取页面大小 Tuple <int, int> size = GetPaperSize(setting.PaperType); setPr.pgSz.w = (ulong)size.Item1; setPr.pgSz.h = (ulong)size.Item2; int tableNum = 0; foreach (var table in listTable) { tableNum++; string tableTitle = table.TableName; if (!string.IsNullOrEmpty(table.TableComment)) { tableTitle = $"{table.TableComment} {tableTitle}"; } Console.WriteLine($"正在处理 {tableNum}.{tableTitle}"); //创建一个段落 XWPFParagraph gp = doc.CreateParagraph(); XWPFStyles styles = doc.CreateStyles(); //表格段落标题 gp.SetNumID($"2.1"); gp.Style = "Heading2"; XWPFRun gr = gp.CreateRun(); gr.SetText($"{tableTitle}"); //获取表字段信息 var listTableSchema = GetTableSchema <TableSchema>(MysqlGetTableSchemaSQL(table.TableName)); //创建表格 XWPFTable docTable = doc.CreateTable(listTableSchema.Count + 1, listMapping.Count); int i = 0; foreach (var tableMapping in listMapping) { var currentCell = docTable.GetRow(0).GetCell(i); currentCell = SetCell(currentCell, tableMapping.DocumentColumn, tableMapping.ColumnWidth, "CCCCCC", true, 10, true); //var ppr = currentCell.GetCTTc().AddNewP().AddNewPPr(); //space.line="0"; //space.lineRule = ST_LineSpacingRule.auto; i++; } int rowIndex = 1; foreach (var tableSchema in listTableSchema) { int cellIndex = 0; foreach (var tableMapping in listMapping) { var currentCell = docTable.GetRow(rowIndex).GetCell(cellIndex); string color = string.Empty; string text = string.Empty; if (rowIndex % 2 == 0) { color = "DDDDDD"; } if (string.IsNullOrEmpty(tableMapping.DBColumn)) { text = rowIndex.ToString(); } else { text = GetPropValue(tableSchema, tableMapping.DBColumn); } currentCell = SetCell(currentCell, text, tableMapping.ColumnWidth, color, tableMapping.IsCenter, 10); cellIndex++; } rowIndex++; } } //开始写入 doc.Write(ms); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } ms.Close(); }