之前在一篇博客中,介绍了关于Aspose.word的用法,建议初次使用该组件的朋友,先去了解下,
点击查看Aspose.Word详细介绍。
软件开发过程中,对Office的读写总是难免的,尽管.NET平台,微软提供了丰富的API,但是,我们大都不想,为了读写Office而给服务器装上Office,即使安装上,也可能在读写时碰到配置权限的问题,更不用提,应用移植时带来的问题。
了解Aspose.Words,有段时间了,近些时间,使用的很少。借此闲暇之余,又想捡起这些点点滴滴。
Aspose.Words的设计思想,非常符合DOM的结构思想,因此,对一般的软件开发者来说,使用它,也是分分钟的事儿,顷刻间便可融会贯通哈。首先,要声明的一点是,Aspose.Word是一款收费的工具,所幸, 我这里有特定版本的证书,可以放心使用,而无需担心过期限制问题。
早前在一片博客中,介绍了如何使用Spire.Doc来在Word中实现下拉列表和复选框,感兴趣的朋友可以了解下:C#操作word,读取并插入combox/checkbox等【Spire.doc示例程序】。Spire.Doc功能强大,唯一美中不足的是,它像Aspose.Words那样,是收费项目,没有证书的情况下使用,你会看到”Evaluation Warning : The document was created with Spire.Doc for .NET.“
这样的红色信息,这只是其一,还不知道它还会有什么限制,所幸,就放弃了。今天在这里,主要给大家介绍下关于Aspose.Words用一些用法。
上图,只是简单的在一个Word文件中,插入DropDownList及Table对象,下面具体看下代码,
这里我以控制台作为示例。
1、首先我们来看下,Aspose.Words怎样导出下来框:
#region 1、导出一个下拉列表
Aspose.Words.Markup.StructuredDocumentTag sdt = new StructuredDocumentTag(document, SdtType.DropDownList, MarkupLevel.Inline);
sdt.Title = "请点击选择章节考点";
for (int i = 0; i < 10; i++)
{
sdt.ListItems.Add(new SdtListItem("选项-" + i, i.ToString()));
}
if (sdt.FirstChild.NodeType == NodeType.Run) ((Run)sdt.FirstChild).Text = "请选择章节考点";//设置默认显示文本
para.AppendChild(sdt);
body.AppendChild(para);
#endregion 导出一个下拉列表
2、同理,Aspose.Words导出checkbox。
#region 2、导出复选框
Aspose.Words.Paragraph paragraph = new Aspose.Words.Paragraph(document);
Aspose.Words.Run run = new Aspose.Words.Run(document);
run.Text = "是否启用";
paragraph.AppendChild(run);
Aspose.Words.Markup.StructuredDocumentTag sdtCheckbox = new StructuredDocumentTag(document, SdtType.Checkbox, MarkupLevel.Inline);
paragraph.AppendChild(sdtCheckbox);
body.AppendChild(paragraph);
#endregion
3、Aspose.Words导出表格
#region 3、导出表格
Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc: document);
for (int i = 0; i < 6; i++)
{
Aspose.Words.Tables.Row row = new Aspose.Words.Tables.Row(document);
for (int j = 0; j < 8; j++)
{
Aspose.Words.Tables.Cell cell = new Aspose.Words.Tables.Cell(document);
Aspose.Words.Paragraph paragraphCell = new Aspose.Words.Paragraph(document);
Run runCell = new Run(document);
runCell.Text = string.Format("{0}-{1}", i, j);
paragraphCell.AppendChild(runCell);
cell.AppendChild(paragraphCell);
//设置单元格样式
cell.CellFormat.Borders.Bottom.Color = cell.CellFormat.Borders.Top.Color = System.Drawing.Color.DarkGreen;
cell.CellFormat.Borders.Left.Color = cell.CellFormat.Borders.Right.Color = System.Drawing.Color.DarkGreen;
cell.CellFormat.Borders.Top.LineStyle = cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.Thick;
cell.CellFormat.Borders.Left.LineStyle = cell.CellFormat.Borders.Right.LineStyle = LineStyle.Thick;
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
body.AppendChild(table);
#endregion 3、导出表格
4、Aspose.Words导出页眉页脚
#region 4、导出页眉页脚
Aspose.Words.HeaderFooter hf = new Aspose.Words.HeaderFooter(document, HeaderFooterType.HeaderPrimary);
Aspose.Words.Paragraph paragraphHeader = new Aspose.Words.Paragraph(doc: document);
Aspose.Words.Run runTitle = new Aspose.Words.Run(doc: document);
runTitle.Text = "点题库";
runTitle.Font.Bold = true;
runTitle.Font.Size = 16D;
paragraphHeader.AppendChild(runTitle);
paragraphHeader.ParagraphFormat.Alignment = ParagraphAlignment.Center;
hf.AppendChild(paragraphHeader);
section.AppendChild(hf);
#endregion 4、导出页眉页脚
最后,保存文件即可。
//保存文档
document.Save(strfilename, SaveFormat.Docx);
让我们来看看完整的代码:
/// <summary>
/// 入口程序
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
new Aspose.Words.License().SetLicense(License.LStream);//证书,需要的朋友可以加我QQ:1058736170
string strfilename = @"G:\demo.docx";
//声明文档
Aspose.Words.Document document = new Aspose.Words.Document();
var section = document.Sections[0];//获取第一节
var body = section.Body;//获取第一节内容
Aspose.Words.Paragraph para = new Aspose.Words.Paragraph(document);//声明一个段落
#region 1、导出一个下拉列表
Aspose.Words.Markup.StructuredDocumentTag sdt = new StructuredDocumentTag(document, SdtType.DropDownList, MarkupLevel.Inline);
sdt.Title = "请点击选择章节考点";
for (int i = 0; i < 10; i++)
{
sdt.ListItems.Add(new SdtListItem("选项-" + i, i.ToString()));
}
if (sdt.FirstChild.NodeType == NodeType.Run) ((Run)sdt.FirstChild).Text = "请选择章节考点";//设置默认显示文本
para.AppendChild(sdt);
body.AppendChild(para);
#endregion 导出一个下拉列表
#region 2、导出复选框
Aspose.Words.Paragraph paragraph = new Aspose.Words.Paragraph(document);
Aspose.Words.Run run = new Aspose.Words.Run(document);
run.Text = "是否启用";
paragraph.AppendChild(run);
Aspose.Words.Markup.StructuredDocumentTag sdtCheckbox = new StructuredDocumentTag(document, SdtType.Checkbox, MarkupLevel.Inline);
paragraph.AppendChild(sdtCheckbox);
body.AppendChild(paragraph);
#endregion
#region 3、导出表格
Aspose.Words.Tables.Table table = new Aspose.Words.Tables.Table(doc: document);
for (int i = 0; i < 6; i++)
{
Aspose.Words.Tables.Row row = new Aspose.Words.Tables.Row(document);
for (int j = 0; j < 8; j++)
{
Aspose.Words.Tables.Cell cell = new Aspose.Words.Tables.Cell(document);
Aspose.Words.Paragraph paragraphCell = new Aspose.Words.Paragraph(document);
Run runCell = new Run(document);
runCell.Text = string.Format("{0}-{1}", i, j);
paragraphCell.AppendChild(runCell);
cell.AppendChild(paragraphCell);
//设置单元格样式
cell.CellFormat.Borders.Bottom.Color = cell.CellFormat.Borders.Top.Color = System.Drawing.Color.DarkGreen;
cell.CellFormat.Borders.Left.Color = cell.CellFormat.Borders.Right.Color = System.Drawing.Color.DarkGreen;
cell.CellFormat.Borders.Top.LineStyle = cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.Thick;
cell.CellFormat.Borders.Left.LineStyle = cell.CellFormat.Borders.Right.LineStyle = LineStyle.Thick;
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
body.AppendChild(table);
#endregion 3、导出表格
#region 4、导出页眉页脚
Aspose.Words.HeaderFooter hf = new Aspose.Words.HeaderFooter(document, HeaderFooterType.HeaderPrimary);
Aspose.Words.Paragraph paragraphHeader = new Aspose.Words.Paragraph(doc: document);
Aspose.Words.Run runTitle = new Aspose.Words.Run(doc: document);
runTitle.Text = "李朝强的博客http://www.lichaoqiang.com/";
runTitle.Font.Bold = true;
runTitle.Font.Size = 16D;
paragraphHeader.AppendChild(runTitle);
paragraphHeader.ParagraphFormat.Alignment = ParagraphAlignment.Center;
hf.AppendChild(paragraphHeader);
section.AppendChild(hf);
#endregion 4、导出页眉页脚
//保存文档
document.Save(strfilename, SaveFormat.Docx);
Console.WriteLine("保存成功!");
Console.Read();
}
附加一点的是,怎样调整样式?这里以设置表格的样式为例。
//设置单元格样式
cell.CellFormat.Borders.Bottom.Color = cell.CellFormat.Borders.Top.Color = System.Drawing.Color.DarkGreen;
cell.CellFormat.Borders.Left.Color = cell.CellFormat.Borders.Right.Color = System.Drawing.Color.DarkGreen;
cell.CellFormat.Borders.Top.LineStyle = cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.Thick;
cell.CellFormat.Borders.Left.LineStyle = cell.CellFormat.Borders.Right.LineStyle = LineStyle.Thick;