读取word --->填充数据 --->保存word

SpireWordHelper:

public class SpireWordHelper
{
public void CreateWord(CreatePaymentData data)
{
Document doc = new Document(data.TempWord);
doc.Properties.FormFieldShading = false;
var sections = doc.Sections[0];
var paraList = sections.Paragraphs;
paraList[2].Text = "至:" + data.sName;
paraList[17].Text = "至:" + data.sName;

var nowDate = DateTime.Now;

paraList[13].Text = nowDate.ToString("yyyy年MM月");
paraList[28].Text = nowDate.ToString("yyyy年MM月");

Table table1 = doc.Sections[0].Tables[0] as Table;
Table table2 = doc.Sections[0].Tables[1] as Table;
var printer = data.BillingPrinters;
decimal serviceTotal = 0, fixedTotal = 0;
for (int i = 0; i < printer.Count; i++)
{
table1.AddRow();
table2.AddRow();
table1 = GetRange(table1, printer[i].AssetModel, i + 1, 0);
table2 = GetRange(table2, printer[i].AssetModel, i + 1, 0);

table1 = GetRange(table1, printer[i].SerialNo, i + 1, 1);
table2 = GetRange(table2, printer[i].SerialNo, i + 1, 1);

table1 = GetRange(table1, printer[i].Address, i + 1, 2);
table2 = GetRange(table2, printer[i].Address, i + 1, 2);

table1 = GetRange(table1, "XXX", i + 1, 3);
table2 = GetRange(table2, "XXX", i + 1, 3);

table1 = GetRange(table1, printer[i].ServicePeriod1, i + 1, 4, 0);
table1 = GetRange(table1, printer[i].ServicePeriod2, i + 1, 4, 1);

table2 = GetRange(table2, printer[i].ServicePeriod1, i + 1, 4, 0);
table2 = GetRange(table2, printer[i].ServicePeriod2, i + 1, 4, 1);

table1 = GetRange(table1,printer[i].ServiceTotalAmount.ToString(), i + 1, 5);
table2 = GetRange(table2, printer[i].FixedTotalAmount.ToString(), i + 1, 5);
serviceTotal += printer[i].ServiceTotalAmount;
fixedTotal += printer[i].FixedTotalAmount;
}
//汇总数据
table1.AddRow();
table1 = GetRange(table1, "XXXX", printer.Count + 1, 4);
table1 = GetRange(table1, serviceTotal.ToString(), printer.Count + 1, 5);
table2.AddRow();
table2 = GetRange(table2, "XXXX", printer.Count + 1, 4);
table2 = GetRange(table2, fixedTotal.ToString(), printer.Count + 1, 5);

string filePath = data.SavePath + "/" + data.FileNameWord;
doc.SaveToFile(filePath, FileFormat.Docx);
WordHelper wordHelper = new WordHelper();
wordHelper.RemoveWatermark(filePath);
}

private Table GetRange(Table table, string text, int i, int j, int k = 0)
{
var range = table[i, j].AddParagraph().AppendText(text);
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 9;
range.CharacterFormat.TextColor = Color.Black;
range.CharacterFormat.Bold = false;
table[i, j].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
table[i, j].Paragraphs[k].Format.HorizontalAlignment = HorizontalAlignment.Center;
return table;
}
}

NPOI版:

public bool CreatePayment(CreatePaymentData data)
{
using (FileStream stream = File.OpenRead(data.TempPlatePath))
{
XWPFDocument doc = new XWPFDocument(stream);
var paraList = doc.Paragraphs;

var toTitle1 = paraList[2].Runs[1];
toTitle1.SetText(data.sName);

var toTitle2 = paraList[17].Runs[1];
toTitle2.SetText(data.sName);

var nowDate = DateTime.Now;
var year1 = paraList[13].Runs[0];
year1.SetText(nowDate.ToString("yyyy"));
var month1 = paraList[13].Runs[2];
month1.SetText(nowDate.ToString("MM"));

var year2 = paraList[28].Runs[0];
year2.SetText(nowDate.ToString("yyyy"));
var month2 = paraList[28].Runs[2];
month2.SetText(nowDate.ToString("MM"));

#region 填充table1中的数据
var table = doc.Tables[0];
//table.RemoveRow(0);
for (int i = 0; i < 5; i++)
{
CT_Row m_NewRow = new CT_Row();
XWPFTableRow m_Row = new XWPFTableRow(m_NewRow, table);

var c0 = m_Row.CreateCell();
c0.SetBorderBottom(XWPFTable.XWPFBorderType.THICK, 1, 4, "#000000");
c0.SetParagraph(SetCellText(table, JoinN(data.AssetModel + i)));

var c1 = m_Row.CreateCell();
c1.SetParagraph(SetCellText(table, JoinN(data.SerialNo)));

var c2 = m_Row.CreateCell();
c2.SetParagraph(SetCellText(table, JoinN(data.Address)));

var c3 = m_Row.CreateCell();
c3.SetParagraph(SetCellText(table, "XXXX"));

var c4 = m_Row.CreateCell();
c4.SetParagraph(SetCellText(table, data.ServicePeriod1 + "\n" + data.ServicePeriod2));

var c5 = m_Row.CreateCell();
c5.SetParagraph(SetCellText(table, JoinN("1234")));

table.AddRow(m_Row);
}
#endregion
if (!Directory.Exists(data.SavePath))
{
Directory.CreateDirectory(data.SavePath);
}

FileStream outFile = new FileStream(data.SavePath + "/" + data.FileName, FileMode.Create);
doc.Write(outFile);
outFile.Close();
return true;
}
}

private string JoinN(string address)
{
for (int i = 10; i < address.Length; i+=10)
{
address = address.Insert(i,"\n");
}
return address;
}

/// <summary>
/// 设置字体格式
/// </summary>
/// <param name="doc"></param>
/// <param name="table"></param>
/// <param name="setText"></param>
/// <returns></returns>
public XWPFParagraph SetCellText(XWPFTable table, string setText)
{
CT_P para = new CT_P();
XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
pCell.Alignment = ParagraphAlignment.CENTER;//字体居中
pCell.VerticalAlignment = TextAlignment.CENTER;//字体居中
pCell.BorderLeft = Borders.Thick;
pCell.BorderRight = Borders.Thick;
pCell.BorderTop = Borders.Thick;
pCell.BorderBottom = Borders.Thick;
XWPFRun r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = 10;
//r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
r1c1.SetTextPosition(15);//设置高度
return pCell;
}

public void RemoveWatermark(string filePath)
{
using (FileStream stream = File.OpenRead(filePath))
{
XWPFDocument doc = new XWPFDocument(stream);
var paraList = doc.Paragraphs;
paraList[0].RemoveRun(0);

FileStream outFile = new FileStream(filePath, FileMode.Create);
doc.Write(outFile);
outFile.Close();
}
}
}