public static void Word_FillModelForReceipt(string modelPath, string savePath, Dictionary<string, string> content, Dictionary<string, string> price = default, Dictionary<int, Models.PosItem> posItem = default)
{
System.IO.FileStream fs = new System.IO.FileStream(modelPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
NPOI.XWPF.UserModel.XWPFDocument docx = new NPOI.XWPF.UserModel.XWPFDocument(fs);//打开07(.docx)以上的版本的文档
//遍历word中的段落
foreach (var para in docx.Paragraphs)
{
foreach (var be in para.Body.BodyElements)
{
if (be.GetType() == typeof(NPOI.XWPF.UserModel.XWPFParagraph))
{
var be1 = (NPOI.XWPF.UserModel.XWPFParagraph)be;
foreach (var kv in content)
{
if (be1.ParagraphText.Contains(kv.Key))
{
be1.ReplaceText(kv.Key, kv.Value);
}
}
}
}
}
//插入表格
if (price?.Count > 0)
{
if (docx.Tables?.Count > 0)
{
NPOI.XWPF.UserModel.XWPFTable table = docx.Tables[0];
if (table.Rows?.Count > 0)
{
int i = 0;
foreach (var item in price)
{
var row = table.InsertNewTableRow(table.Rows.Count - 1);
var cell = row.GetCell(0) ?? row.CreateCell();
SetCellText(cell, table, item.Key);
cell = row.GetCell(1) ?? row.CreateCell();
cell = row.GetCell(2) ?? row.CreateCell();
//SetCellText(cell, table, item.Value.CountTitle);
cell = row.GetCell(3) ?? row.CreateCell();
//SetCellText(cell, table, item.Value.Count);
cell = row.GetCell(4) ?? row.CreateCell();
cell = row.GetCell(5) ?? row.CreateCell();
SetCellText(cell, table, "HK$", NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
cell = row.GetCell(6) ?? row.CreateCell();
SetCellText(cell, table, item.Value, NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
i++;
}
}
}
}
//插入表格
if (posItem?.Count > 0)
{
if (docx.Tables?.Count > 0)
{
NPOI.XWPF.UserModel.XWPFTable table = docx.Tables[0];
if (table.Rows?.Count > 0)
{
int i = 0;
foreach (var item in posItem)
{
var row = table.InsertNewTableRow(table.Rows.Count - 1);
var cell = row.GetCell(0) ?? row.CreateCell();
SetCellText(cell, table, item.Value.Name);
cell = row.GetCell(1) ?? row.CreateCell();
cell = row.GetCell(2) ?? row.CreateCell();
SetCellText(cell, table, item.Value.CountTitle);
cell = row.GetCell(3) ?? row.CreateCell();
SetCellText(cell, table, item.Value.Count);
cell = row.GetCell(4) ?? row.CreateCell();
cell = row.GetCell(5) ?? row.CreateCell();
SetCellText(cell, table, "HK$", NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
cell = row.GetCell(6) ?? row.CreateCell();
SetCellText(cell, table, item.Value.Price, NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
i++;
}
}
}
}
var tr = docx.Tables[0].Rows.Last();
var tc = tr.GetCell(5);
//SetCellStyle(tc, docx.Tables[0]);
tc.SetBorderBottom(NPOI.XWPF.UserModel.XWPFTable.XWPFBorderType.DOUBLE, 1, 0, NPOI.HSSF.Util.HSSFColor.Black.HexString);
tc.Paragraphs.First().Runs.First().IsBold = true;
tc.Paragraphs.First().Runs.First().FontSize = 9;
tc = tr.GetCell(6);
//SetCellStyle(tc, docx.Tables[0]);
tc.SetBorderBottom(NPOI.XWPF.UserModel.XWPFTable.XWPFBorderType.DOUBLE, 1, 0, NPOI.HSSF.Util.HSSFColor.Black.HexString);
tc.Paragraphs.First().Runs.First().IsBold = true;
tc.Paragraphs.First().Runs.First().FontSize = 9;
//遍历表格
foreach (var table in docx.Tables)
{
foreach (var row in table.Rows)
{
foreach (var cell in row.GetTableCells())
{
foreach (var para in cell.Paragraphs)
{
foreach (var be in para.Body.BodyElements)
{
if (be.GetType() == typeof(NPOI.XWPF.UserModel.XWPFParagraph))
{
var be1 = (NPOI.XWPF.UserModel.XWPFParagraph)be;
foreach (var kv in content)
{
if (be1.ParagraphText.Contains(kv.Key))
{
be1.ReplaceText(kv.Key, kv.Value);
}
}
}
}
}
}
}
}
System.IO.FileStream output = new System.IO.FileStream(savePath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
docx.Write(output);
fs.Close();
fs.Dispose();
output.Close();
output.Dispose();
}
03-27
3643
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-27
1435
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-01