1.总体说明:操作主要涉及两个对象Document及DocumentBuilder
Document主要用来获取文档中的节点,DocumentBuilder主要用于实现文档内容的写入
doc_Operate = new Document(blankTemplatePth);
doc_template = new Document(ToCopytemplatePth);
builder_template = new DocumentBuilder(doc_template);
builder_operate = new DocumentBuilder(doc_Operate);
2.内容写入,样式设置
builder_operate.ParagraphFormat.Style = doc_Operate.Styles["标题"];
builder_operate.Writeln("XXX报告");
builder_operate.ParagraphFormat.Style = doc_Operate.Styles["正文"];
builder_operate.Writeln("(征求意见稿)");
3.关于分节,分节之后,需要将当前插入光标移动至新插入的Section中,否则容易出错
builder_operate.InsertBreak(BreakType.SectionBreakNewPage);
Section lstSection2 = doc_Operate.LastSection;
int idx2 = doc_Operate.Sections.IndexOf(lstSection2);
builder_operate.MoveToSection(idx2);
builder_operate.MoveToDocumentEnd();
4.复制模板文档中的表格,插入到当前文档中
//获取Table对象
Table tbN=doc.ImportNode(tb, true, ImportFormatMode.KeepSourceFormatting) as Table;
doc.LastSection.Body.AppendChild(tbN);
Paragraph p = new Paragraph(doc);
tbN.ParentNode.AppendChild(p); //添加paragraph,以打断表格
将当前文档中的表格,插入到指定表格之后
Paragraph pp = new Paragraph(doc_operate);
jzxBSTable.ParentNode.InsertAfter(pp, jzxBSTable);
var tb = jzxBSTable.Clone(true) as Aspose.Words.Tables.Table;
pp.ParentNode.InsertAfter(tb, pp);
5.当前表格插入新行
Row rN = table_N.Rows[1].Clone(true) as Row;
table_N.InsertAfter(rN, table_N.LastRow);
6.单元格插入图片
builder_operate.MoveToCell(tableindex, 0, 0, 0);
Aspose.Words.Drawing.Shape spa = builder_operate.InsertImage(jietuA);
此处插入图片时,容易出现异常,提示tableindex超出索引,TableIndex是通过indexOf对象获取到的.
查阅大量资料发现,如果一个word文档中出现了多个Section,需要采用本篇第3小节的内容,移动当前的光标
7.设置单元格内容
public static void setCellText(this Cell cell, string txt)
{
Run run;
if (cell.FirstParagraph.Runs.Count == 0)
{
run = new Run(cell.Document);
}
else
{
run = (Run)cell.FirstParagraph.Runs[0].Clone(true);
}
run.Text = txt;
for (int i = 1; i < cell.Paragraphs.Count; i++)
{
Node np=cell.GetChild(NodeType.Paragraph, i, false);
cell.RemoveChild(np);
}
cell.FirstParagraph.RemoveAllChildren();
cell.EnsureMinimum();
if(cell.Paragraphs.Count!=0)
cell.Paragraphs[0].AppendChild(run);
}
8.合并单元格
public static void HorizontallyMergeCells(Cell c1, Cell c2,bool SaveAllVal=false)
{
c1.CellFormat.HorizontalMerge = CellMerge.First;
//Move all content from next cell to previous
if (SaveAllVal)
{
foreach (Node child in c2.ChildNodes)
c1.AppendChild(child);
}
c2.CellFormat.HorizontalMerge = CellMerge.Previous;
}
public static void VerticallyMergeCells(Cell c1, Cell c2,bool SaveAllVal)
{
c1.CellFormat.VerticalMerge = CellMerge.First;
//Move all content from bottom cell to top
if (SaveAllVal)
{
foreach (Node child in c2.ChildNodes)
c1.AppendChild(child);
}
c2.CellFormat.VerticalMerge = CellMerge.Previous;
}
public static void MergeCell(this Table tb, int startrowid, int endrowid, int startColId, int endColId)
{
for (int i = startrowid; i <= endrowid; i++)
{
for (int j = startColId+1; j <= endColId; j++)
{
//每行进行横向合并
HorizontallyMergeCells(tb.Rows[i].Cells[startColId], tb.Rows[i].Cells[j]);
}
}
//首行进行纵向合并
for (int i = startrowid+1; i <= endrowid; i++)
{
VerticallyMergeCells(tb.Rows[startrowid].Cells[startColId], tb.Rows[i].Cells[startColId], false);
}
}
9.插入另一个Word文档
Document docShuoMing = new Document(summaryTemplatePth);
//docShuoMing.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
//docShuoMing.FirstSection.PageSetup.RestartPageNumbering = true;
doc_Operate.LastSection.AppendContent(docShuoMing.FirstSection);
//doc_Operate.AppendDocument(docShuoMing, ImportFormatMode.KeepSourceFormatting);
builder_operate.MoveToDocumentEnd();
10.插入页码
public static void InsertHeaderFooter(Section sect, HeaderFooterType headerType)
{
HeaderFooter header = sect.HeadersFooters[headerType];
if (header == null)
{
header = new HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
}
public static void CancelHeaderFotter(Section sect)
{
for (int i = sect.HeadersFooters.Count-1; i >=0; i--)
{
sect.HeadersFooters.RemoveAt(i);
}
}
/// <summary>
/// 插入页码
/// </summary>
/// <param name="builder_operate"></param>
/// <param name="sec"></param>
/// <param name="startNumber"></param>
public static void InsertYeMa(DocumentBuilder builder_operate,Section sec,NumberStyle ns=NumberStyle.Arabic,int startNumber=1)
{
//添加页码
ASPWHelper.InsertHeaderFooter(sec, HeaderFooterType.HeaderPrimary);
builder_operate.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
//设置开始页码
builder_operate.PageSetup.PageStartingNumber = startNumber;
builder_operate.PageSetup.PageNumberStyle = ns;
//页码在每个section会重新开始
builder_operate.PageSetup.RestartPageNumbering = true;
//页码位置
builder_operate.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder_operate.InsertField("PAGE", "");
builder_operate.MoveToDocumentEnd();
}
11.移除最后的分页符
public static void RemoveLastPageBreak(Document doc)
{
NodeCollection runs = doc.LastSection.GetChildNodes(NodeType.Run, true);
for (int i = runs.Count - 1; i >= 0; i--)
{
Run run = (Run)runs[i];
if (run.Text.IndexOf(ControlChar.PageBreakChar) >= 0)
{
run.Text = run.Text.Remove(run.Text.IndexOf(ControlChar.PageBreakChar), 1);
break;
}
}
}
12.插入目录
/// <summary>
/// 插入目录
/// </summary>
/// <param name="bulider_blank"></param>
public static void InsertTOC(DocumentBuilder bulider_blank)
{
//设置"目录"格式
bulider_blank.ParagraphFormat.Alignment = ParagraphAlignment.Center;
bulider_blank.Bold = true;
bulider_blank.Font.Name = "SONG";
bulider_blank.Writeln("目录");
bulider_blank.ParagraphFormat.ClearFormatting();//清除所有样式
bulider_blank.InsertTableOfContents("\\o\"1-3\"\\h\\z\\u");
bulider_blank.InsertBreak(BreakType.SectionBreakNewPage);
}
13.获取某一Section的页数
public static int getLastSectionPageCount(Document doc)
{
int idx = doc.Sections.IndexOf(doc.LastSection);
Document tmp = doc.Clone();
for (int i = tmp.Sections.Count - 1; i >= 0; i--)
{
if (i != idx)
{
tmp.Sections.RemoveAt(i);
}
}
return tmp.PageCount;
}
14. 获取word中的表格 如果表格中有多个表格,通过索引获取不到,是测试许可的问题
public static Table GetTable(this Document doc,int idx)
{
var table0 = doc.GetChildNodes(NodeType.Table, true)[idx] as Aspose.Words.Tables.Table;
return table0;
}
15.在表格中找到关键字的行列号,方便开发时索引用
/// <summary>
/// 找到表格关键字的行列号
/// </summary>
/// <param name="table"></param>
/// <param name="flag"></param>
/// <returns></returns>
public static RC GetRowColumn(this Table table,string flag)
{
RC r = new RC();
for (int i = 0; i < table.Rows.Count; i++)
{
var row = table.Rows[i];
for (int j = 0; j < row.Cells.Count; j++)
{
var val = row.Cells[j].GetText().Trim();
val = val.removeTableSpecial();
if (val == flag.Trim())
{
r.row = i;
r.col = j;
break;
}
System.Diagnostics.Debug.WriteLine(i + "-" + j + ":" + val);
}
}
return r;
}
public class RC
{
public int row;
public int col;
public RC()
{
row = -1;
col = -1;
}
}
15.移除表格字符中特殊转义符号
public static string removeTableSpecial(this string str)
{
str = str.Replace("\a", "");
str = str.Replace("\t","");
return str;
}
16.通过字符串的形式实现换行
关键 ControlChar对象,参考帖子 https://reference.aspose.com/words/net/aspose.words/controlchar/linebreak/
其他特殊字符也可以使用该对象进行添加
StringBuilder sbjzd = new StringBuilder();
sbjzd.Append(jzddesc+";"+ControlChar.LineBreak);
17.checkbox的勾选与否
参考该帖子
https://www.cnblogs.com/zhmlxx/p/14549189.html
核心是使用Wingdings 2字符集,代码如下:
builder_operate.Font.Name = "Wingdings 2";
builder_operate.Write(char.ConvertFromUtf32(82)); //选中样式
builder_operate.Write(char.ConvertFromUtf32(163)); //未选中样式
另外,可以通过InsertCheckBox的方式插入可以点选的文本框,只不过样式容易与文档不搭配。大致代码如下:
builder_operate.InsertCheckBox("name", true, 10);