JS代码可参考http://www.cnblogs.com/mu1516633121/p/7753423.html
同样是winform架构下应用到Aspose.Words来读写Word文档
其中Section是对页面进行设置,在Aspose.Words中,在什么地方导出什么内容前要先将光标移至相应位置,然后再Writeln写入对应的内容。
/// <summary>
/// 导出Word文档
/// </summary>
/// <param name="data">要导出的数据</param>
/// <param name="title">标题</param>
/// <param name="colWidth">宽度列表</param>
public void exportWord(string data,string title, string colWidth)
{
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "docx";
saveDialog.Filter = "Word文件|*.docx";
saveDialog.FileName = DateTime.Now.Ticks.ToString();
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < ) return; //被点了取消 try
{
DataTable products = JsonConvert.DeserializeObject<DataTable>(data);
//导出的每列的宽度
double[] colWidthList = JsonConvert.DeserializeObject<double[]>(colWidth); Document doc = new Document();
DocumentBuilder docWriter = new DocumentBuilder(doc);
foreach (Aspose.Words.Section section in doc)
{
//设置纸张方向为横向
section.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
//设置纸张大小为A4纸大小
section.PageSetup.PaperSize = Aspose.Words.PaperSize.A4;
section.PageSetup.HeaderDistance = ;
//设置页眉页脚
section.PageSetup.DifferentFirstPageHeaderFooter = true;
section.PageSetup.OddAndEvenPagesHeaderFooter = true;
section.PageSetup.LeftMargin = ; //页边距
section.PageSetup.RightMargin = ;
section.PageSetup.TopMargin = ;
section.PageSetup.BottomMargin = ;
}
#region 设置页眉页脚
//移动光标至页眉设置属性(第一页)
docWriter.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
docWriter.ParagraphFormat.Alignment = ParagraphAlignment.Center;
docWriter.Font.Name = "黑体";
docWriter.Font.Bold = true;
docWriter.Font.Size = ;
docWriter.Writeln(title);
//移动光标至页眉设置属性(偶数页)
docWriter.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
docWriter.ParagraphFormat.Alignment = ParagraphAlignment.Center;
docWriter.Font.Name = "黑体";
docWriter.Font.Bold = true;
docWriter.Font.Size = ;
docWriter.Writeln(title);
//移动光标至页眉设置属性(奇数页)
docWriter.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
docWriter.ParagraphFormat.Alignment = ParagraphAlignment.Center;
docWriter.Font.Name = "黑体";
docWriter.Font.Bold = true;
docWriter.Font.Size = ;
docWriter.Writeln(title);
//移动光标至页脚设置属性(第一页)
docWriter.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
docWriter.Font.Name = "Arial";
docWriter.Font.Size = ;
docWriter.Writeln("导出时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//移动光标至页脚设置属性(偶数页)
docWriter.MoveToHeaderFooter(HeaderFooterType.FooterEven);
docWriter.Font.Name = "Arial";
docWriter.Font.Size = ;
docWriter.Writeln("导出时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//移动光标至页脚设置属性(奇数页) docWriter.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
docWriter.Font.Name = "Arial";
docWriter.Font.Size = ;
docWriter.Writeln("导出时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
#endregion //光标移至文档内容
docWriter.MoveToDocumentStart();
for (int i = ; i < products.Rows.Count; i++)
{
for (int j = ; j < products.Columns.Count; j++)
{
//创建内容
AsposeCreateCell(docWriter, colWidthList[j], products.Rows[i][j].ToString());
}
docWriter.EndRow();
}//end for doc.Save(saveFileName, Aspose.Words.SaveFormat.Docx); mainForm.Invoke(new MethodInvoker(delegate
{
MessageBox.Show(mainForm, "导出成功");
}));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
/// <summary>
/// 画表格
/// </summary>
/// <param name="builder">文档</param>
/// <param name="width">宽度</param>
/// <param name="text">文本</param>
public void AsposeCreateCell(DocumentBuilder builder, double width, string text)
{
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.Width = width;//单元格的宽度
builder.CellFormat.LeftPadding = ;//单元格的左内边距
builder.CellFormat.RightPadding = ;//单元格的右内边距
builder.RowFormat.Height = ;//行高
builder.CellFormat.VerticalMerge = CellMerge.None;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐
builder.Font.Size = ;
builder.Font.Name = "Arial";
builder.Write(text);
}