//地政函档案封面及收件回执 public static void DZHDAFMJSJHZ(Dictionary <string, string> dic, string tplFilePath, string outputPrintFilePath) { using (FileStream tplFileStream = new FileStream(tplFilePath, FileMode.Open, FileAccess.Read)) using (FileStream outputFileStream = File.Create(outputPrintFilePath)) { List <RespDZHSJHZ> list = new DbHelper().QueryRespDZHSJHZ(SharpDbPrinter.Program.globalYwid); XWPFDocument tplDoc = new XWPFDocument(tplFileStream); var printDoc = DocxBaseRelace(tplDoc, dic); var firstTable = printDoc.Tables[0]; for (int i = 0; i < list.Count; i++) { XWPFTableRow newRow = new XWPFTableRow(new NPOI.OpenXmlFormats.Wordprocessing.CT_Row(), firstTable); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.MergeCells(0, 1); if (list[i].MTR_NAME == null && list[i].OLD_NUM == null && list[i].DUPL_NUM == null) { } else { newRow.GetCell(0).SetParagraph(SetCellText(printDoc, firstTable, list[i].MTR_NAME ?? "0")); newRow.GetCell(1).SetParagraph(SetCellText(printDoc, firstTable, list[i].OLD_NUM ?? "0")); newRow.GetCell(2).SetParagraph(SetCellText(printDoc, firstTable, list[i].DUPL_NUM ?? "0")); } firstTable.AddRow(newRow, 2 + i); } firstTable.RemoveRow(1); printDoc.Write(outputFileStream); } }
//地政函审批表(改功能) /// <summary> /// 将模板文档的源关键字替换成目标文本 /// </summary> /// <param name="dic">基础替换文本字典</param> /// <param name="formatParaDic">包含换行符的替换目标字典</param> /// <param name="tplFilePath">模板文件的路径</param> /// <param name="outputPrintFilePath">输出文件路径</param> public static void DZHSPBGGN(Dictionary <string, string> dic, Dictionary <string, string> formatParaDic, string tplFilePath, string outputPrintFilePath) { using (FileStream tplFileStream = new FileStream(tplFilePath, FileMode.Open, FileAccess.Read)) using (FileStream outputFileStream = File.Create(outputPrintFilePath)) { List <RespDZHSPBChild> list = new DbHelper().QueryDZHSPB1_Child(SharpDbPrinter.Program.globalYwid); XWPFDocument tplDoc = new XWPFDocument(tplFileStream); var printDoc = DocxRepalceWithParaFormat(tplDoc, formatParaDic); printDoc = DocxBaseRelace(printDoc, dic); int row1 = 9; var firstTable = printDoc.Tables[0]; for (int i = 0; i < list.Count; i++) { XWPFTableRow newRow = new XWPFTableRow(new NPOI.OpenXmlFormats.Wordprocessing.CT_Row(), firstTable); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.MergeCells(5, 9); newRow.MergeCells(0, 4); newRow.GetCell(0).SetParagraph(SetCellText(printDoc, firstTable, list[i].th ?? "")); newRow.GetCell(1).SetParagraph(SetCellText(printDoc, firstTable, list[i].YTDSYZH ?? "")); newRow.GetCell(2).SetParagraph(SetCellText(printDoc, firstTable, list[i].PZMJ ?? "")); firstTable.AddRow(newRow, row1 + 1 + i); } firstTable.RemoveRow(row1); printDoc.Write(outputFileStream); } }
private XWPFDocument InsertTable(XWPFDocument doc, Table t) { var maxColCount = t.Rows.Max(x => x.Cells.Count); if (t == null) { return(doc); } var table = doc.CreateTable(); table.Width = t.Width; int index = 0; t.Rows?.ForEach(r => { XWPFTableRow tableRow = index == 0 ? table.GetRow(0) : table.CreateRow(); for (int i = 0; i < r.Cells.Count; i++) { var cell = r.Cells[i]; var xwpfCell = i == 0 ? tableRow.GetCell(0) : tableRow.AddNewTableCell(); foreach (var para in cell.Paragraphs) { xwpfCell.AddParagraph().Set(para); } if (!string.IsNullOrWhiteSpace(cell.Color)) { tableRow.GetCell(i).SetColor(cell.Color); } } //补全单元格,并合并 var rowColsCount = tableRow.GetTableICells().Count; if (rowColsCount < maxColCount) { for (int i = rowColsCount - 1; i < maxColCount; i++) { tableRow.CreateCell(); } tableRow.MergeCells(rowColsCount - 1, maxColCount); } index++; }); return(doc); }
/// <summary> /// 应用命令 /// </summary> /// <param name="command"></param> /// <param name="colIndex"></param> /// <param name="row"></param> private void ApplyCommand(string command, int colIndex, XWPFTableRow row) { string[] commands = command.Split(':'); if (commands.Length != 2) { return; } string type = commands[0]; string typeValue = commands[1]; if (type == "RowSpan") { int endColIndex = colIndex + typeValue.ConvertTo <int>() - 1; int cellCount = row.GetTableCells().Count; for (int j = cellCount; j <= endColIndex; j++) { row.CreateCell(); } row.MergeCells(colIndex, endColIndex); } }
/// <summary> /// 将行拷贝到目标表格 /// 只拷贝了基本的样式 /// </summary> /// <param name="targetTable">目标表格</param> /// <param name="sourceRow">源表格行</param> /// <param name="rowIndex">行索引</param> /// <param name="maxCol">表格最大列数</param> public static void CopyRowToTable(XWPFTable targetTable, XWPFTableRow sourceRow, int rowIndex, int maxCol) { //在表格指定位置新增一行 XWPFTableRow targetRow = rowIndex == 0 ? targetTable.GetRow(0) : targetTable.CreateRow(); //复制行属性 targetRow.GetCTRow().trPr = sourceRow.GetCTRow().trPr; List <XWPFTableCell> cellList = sourceRow.GetTableCells(); if (null == cellList) { return; } //复制列及其属性和内容 int colIndex = 0; foreach (XWPFTableCell sourceCell in cellList) { XWPFTableCell targetCell = null; //新增行会默认有一个单元格,因此直接获取就好 try { targetCell = targetRow.GetCell(colIndex); } catch (Exception) { } if (targetCell == null) { targetCell = targetRow.CreateCell(); } //列属性 targetCell.GetCTTc().tcPr = sourceCell.GetCTTc().tcPr; //段落属性 if (sourceCell.Paragraphs != null && sourceCell.Paragraphs.Count > 0) { var paragraph = targetCell.Paragraphs[0]; var ctp = (CT_P)typeof(XWPFParagraph).GetField("paragraph", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(paragraph); var sourceParagraph = sourceCell.Paragraphs[0]; var sourceCtp = (CT_P)typeof(XWPFParagraph).GetField("paragraph", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sourceParagraph); paragraph.Alignment = sourceParagraph.Alignment; ctp.pPr = sourceCtp.pPr; if (sourceCell.Paragraphs[0].Runs != null && sourceCell.Paragraphs[0].Runs.Count > 0) { XWPFRun cellR = targetCell.Paragraphs[0].CreateRun(); var srcRun = sourceCell.Paragraphs[0].Runs[0]; cellR.SetText(sourceCell.GetText()); cellR.IsBold = srcRun.IsBold; cellR.SetColor(srcRun.GetColor()); cellR.FontFamily = srcRun.FontFamily; cellR.IsCapitalized = srcRun.IsCapitalized; cellR.IsDoubleStrikeThrough = srcRun.IsDoubleStrikeThrough; cellR.IsEmbossed = srcRun.IsEmbossed; cellR.IsImprinted = srcRun.IsImprinted; cellR.IsItalic = srcRun.IsItalic; cellR.IsShadowed = srcRun.IsShadowed; } else { targetCell.SetText(sourceCell.GetText()); } } else { targetCell.SetText(sourceCell.GetText()); } colIndex++; } if (cellList.Count < maxCol) { try { targetRow.MergeCells(cellList.Count - 1, maxCol - 1); } catch { } } }