class Program
{
static void Main(string[] args)
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
//页面设置
PageSetup ps = builder.PageSetup;
ps.PaperSize = Aspose.Words.PaperSize.A4;
ps.Orientation = Orientation.Portrait;
ps.TopMargin = ConvertUtil.InchToPoint(1.0);
ps.BottomMargin = ConvertUtil.InchToPoint(1.0);
ps.LeftMargin = ConvertUtil.InchToPoint(0.5);
ps.RightMargin = ConvertUtil.InchToPoint(0.5);
ps.HeaderDistance = ConvertUtil.InchToPoint(0.2);
ps.FooterDistance = ConvertUtil.InchToPoint(0.2);
//ps.DifferentFirstPageHeaderFooter = true; //页眉页脚首页不同
ps.HeaderDistance = 50;
ps.FooterDistance = 60;
//数据准备
DataTable data = new DataTable();
data.Columns.Add("Column1");
data.Columns.Add("Column2");
data.Columns.Add("Column3");
data.Columns.Add("Column4");
data.Columns.Add("Column5");
Random rand = new Random();
for (int i = 0; i < 20; i++)
{
DataRow row = data.NewRow();
foreach (DataColumn column in data.Columns)
{
row[column.ColumnName] = rand.Next();
}
data.Rows.Add(row);
}
//使用 DocumentBuilder创建表格
CreateTable(builder, data, "This is table title");
builder.InsertBreak(BreakType.PageBreak);
//使用Table创建表格
Table table = CreateTable(doc, data, "This is table title");
builder.CurrentSection.Body.AppendChild(table);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
#region 段落格式 签名添加
//插入一段话(非表格)
builder.Font.ClearFormatting();
builder.Font.Bold = true;
builder.Font.Name = "黑体";
builder.Font.Size = 14;
builder.ParagraphFormat.ClearFormatting();
builder.ParagraphFormat.LineSpacing = 19;
builder.Writeln("一、新段落标题");
builder.Font.ClearFormatting();
builder.Font.Name = "宋体";
builder.Font.Size = 12;
builder.Write("签发:");
builder.Font.ClearFormatting();
builder.Font.Underline = Underline.Single;
builder.Write(" ");
builder.InsertImage("签名.png", RelativeHorizontalPosition.LeftMargin, 80, RelativeVerticalPosition.Line, -10, 50, 20, WrapType.None);
builder.Font.ClearFormatting();
builder.Font.Name = "宋体";
builder.Font.Size = 12;
builder.Write(" 项目负责/所属人:");
builder.Font.ClearFormatting();
builder.Font.Underline = Underline.Single;
builder.Write(" ");
builder.InsertImage("签名.png", RelativeHorizontalPosition.LeftMargin, 270, RelativeVerticalPosition.Line, -10, 50, 20, WrapType.None);
builder.Font.ClearFormatting();
builder.Font.Name = "宋体";
builder.Font.Size = 12;
builder.Write(" 承接部门负责人:");
builder.Font.ClearFormatting();
builder.Font.Underline = Underline.Single;
builder.Write(" ."); //bug: 如果最后面全部是空格,下划线出不来, 加一个点
builder.InsertImage("签名.png", RelativeHorizontalPosition.LeftMargin, 460, RelativeVerticalPosition.Line, -10, 50, 20, WrapType.None);
builder.Font.ClearFormatting();
builder.MoveToDocumentEnd();
#endregion
#region 页眉页脚设置
// Create the headers.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Borders.Bottom.LineStyle = LineStyle.Single;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("Header Text goes here...");
//add footer having current date
//builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
////builder.InsertField("Date", ""); //日期
//builder.InsertField("Page");
// Get the primary footer
//页码设置 格式 page/numpages
foreach (Section section in doc.Sections)
{
HeaderFooter hf = new HeaderFooter(section.Document, HeaderFooterType.FooterEven);
section.HeadersFooters.Add(hf);
// Get the first paragraph of footer
hf.AppendChild(new Paragraph(section.Document));
Paragraph para = hf.FirstParagraph;
// Add current page number field
para.AppendField("PAGE");
HeaderFooter hf2 = new HeaderFooter(section.Document, HeaderFooterType.FooterPrimary);
section.HeadersFooters.Add(hf2);
// Get the first paragraph of footer
hf2.AppendChild(new Paragraph(section.Document));
Paragraph para2 = hf2.FirstParagraph;
// Add current page number field
para2.AppendField("PAGE");
// Add a / character
para2.AppendChild(new Run(doc, "/"));
// Add total page numbers field
para2.AppendField("NUMPAGES");
para2.ParagraphFormat.Borders.Top.LineStyle = LineStyle.Single;
}
#endregion
doc.UpdateFields();
//常用保存格式
doc.Save("text.doc", SaveFormat.Doc);
doc.Save("text.pdf", SaveFormat.Pdf);
doc.Save("text.rtf", SaveFormat.Rtf);
}
//使用 DocumentBuilder创建表格
static void CreateTable(DocumentBuilder builder, DataTable data, string title)
{
if (data == null || data.Rows.Count <= 0 || string.IsNullOrWhiteSpace(title))
{
return;
}
//开始创建表格
builder.StartTable();
//创建一行 标题行
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.SetPaddings(0, 10, 0, 10);
builder.CellFormat.FitText = false; //列宽根据内容自适应
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //文字水平居中
builder.Font.ClearFormatting();
builder.Font.Spacing = 0;
builder.Font.Bold = true;
builder.Write(title);
for (int i = 0; i < data.Columns.Count - 1; i++)
{
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.Previous; //和前一个单元格合并
}
//builder.CellFormat.VerticalMerge = CellMerge.First;
builder.EndRow();//结束一行
//标题
foreach (DataColumn column in data.Columns)
{
//开始一行
builder.InsertCell();
//格式设置
builder.CellFormat.ClearFormatting(); //清除样式 以便于重新开始设置样式
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Font.ClearFormatting();
builder.Font.Size = 12;
builder.Font.Bold = true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//写入内容
builder.Write(column.ColumnName);
}
builder.EndRow();
foreach (DataRow dataRow in data.Rows)
{
foreach (DataColumn column in data.Columns)
{
//开始一行
builder.InsertCell();
//格式设置
builder.CellFormat.ClearFormatting(); //清除样式 以便于重新开始设置样式
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Font.ClearFormatting();
builder.Font.Size = 12;
//写入内容
builder.Write(dataRow[column.ColumnName].ToString());
}
//结束一行
builder.EndRow();
}
//结束表格
builder.EndTable();
}
//使用Table创建表格
static Table CreateTable(Document doc, DataTable data, string title)
{
Table table = new Table(doc);
Row row;
Cell cell;
Run run;
row = table.NewRow();
cell = row.NewCell();
cell.CellFormat.HorizontalMerge = CellMerge.First;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.FirstParagraph.ParagraphFormat.LineSpacing = 30;
run = new Run(doc);
run.Font.Bold = true;
run.Text = title;
cell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center; //居中
cell.FirstParagraph.AppendChild(run);
for (int i = 0; i < data.Columns.Count - 1; i++)
{
cell = row.NewCell();
cell.CellFormat.ClearFormatting();
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
}
if (data != null && data.Rows.Count > 0 && data.Columns.Count > 0)
{
//标题
row = table.NewRow();
//row.RowFormat.ClearFormatting();
row.RowFormat.HeightRule = HeightRule.AtLeast; //行高规则:最小行高
row.RowFormat.Height = 20;
foreach (DataColumn column in data.Columns)
{
//cell=row.NewCell(column.ColumnName);
cell = row.NewCell();
cell.CellFormat.ClearFormatting();
cell.CellFormat.HorizontalMerge = CellMerge.None;
run = new Run(doc);
run.Font.Bold = true;
run.Text = column.ColumnName;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.FirstParagraph.AppendChild(run);
}
foreach (DataRow dr in data.Rows)
{
row = table.NewRow();
//row.RowFormat.ClearFormatting();
row.RowFormat.HeightRule = HeightRule.AtLeast; //行高规则:最小行高
row.RowFormat.Height = 20;
foreach (DataColumn dataColumn in data.Columns)
{
cell = row.NewCell(dr[dataColumn.ColumnName].ToString());
cell.CellFormat.ClearFormatting();
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
}
}
}
//table.ClearBorders(); //清除表格边框
//table.AutoFit(AutoFitBehavior.FixedColumnWidths); //使用指定的列宽固定列宽,禁止自适应列宽
return table;
}
}
另外几个扩展方法的代码如下:
public static class MyExtendMethod
{
public static Row NewRow(this Table table)
{
Row row = new Row(table.Document);
table.AppendChild(row);
return row;
}
public static Cell NewCell(this Row row)
{
Cell cell = new Cell(row.Document);
row.AppendChild(cell);
cell.AppendChild(new Paragraph(row.Document));
return cell;
}
public static Cell NewCell(this Row row,string cellText)
{
Cell cell = new Cell(row.Document);
cell.AppendChild(new Paragraph(row.Document));
row.AppendChild(cell);
cell.FirstParagraph.AppendChild(new Run(row.Document, cellText));
return cell;
}
}