aspose.word生成word文档
public class WordUtil { private DocumentBuilder oWordApplic; // a reference to Word application private Aspose.Words.Document oDoc; // a reference to the document public void OpenWithTemplate(string strFileName) { if (!string.IsNullOrEmpty(strFileName)) { oDoc = new Aspose.Words.Document(strFileName); } } public void Open() { oDoc = new Aspose.Words.Document(); } public void Builder() { oWordApplic = new DocumentBuilder(oDoc); } /// <summary> /// 保存文件 /// </summary> /// <param name="strFileName"></param> public void SaveAs(string strFileName) { if (this.Docversion == 2007) { oDoc.Save(strFileName, SaveFormat.Docx); } else { oDoc.Save(strFileName, SaveFormat.Doc); } } /// <summary> /// 添加内容 /// </summary> /// <param name="strText"></param> public void InsertText(string strText, double conSize, bool conBold, string conAlign) { oWordApplic.Bold = conBold; oWordApplic.Font.Size = conSize; switch (conAlign) { case "left": oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left; break; case "center": oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center; break; case "right": oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Right; break; default: oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left; break; } oWordApplic.Writeln(strText); } /// <summary> /// 添加内容 /// </summary> /// <param name="strText"></param> public void WriteText(string strText, double conSize, bool conBold, string conAlign) { oWordApplic.Bold = conBold; oWordApplic.Font.Size = conSize; switch (conAlign) { case "left": oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left; break; case "center": oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center; break; case "right": oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Right; break; default: oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left; break; } oWordApplic.Write(strText); } #region 设置纸张 public void setPaperSize(string papersize) { switch (papersize) { case "A4": foreach (Aspose.Words.Section section in oDoc) { section.PageSetup.PaperSize = PaperSize.A4; section.PageSetup.Orientation = Orientation.Portrait; section.PageSetup.VerticalAlignment = Aspose.Words.PageVerticalAlignment.Top; } break; case "A4H"://A4横向 foreach (Aspose.Words.Section section in oDoc) { section.PageSetup.PaperSize = PaperSize.A4; section.PageSetup.Orientation = Orientation.Landscape; section.PageSetup.TextColumns.SetCount(2); section.PageSetup.TextColumns.EvenlySpaced = true; section.PageSetup.TextColumns.LineBetween = true; //section.PageSetup.LeftMargin = double.Parse("3.35"); //section.PageSetup.RightMargin =double.Parse("0.99"); } break; case "A3": foreach (Aspose.Words.Section section in oDoc) { section.PageSetup.PaperSize = PaperSize.A3; section.PageSetup.Orientation = Orientation.Portrait; } break; case "A3H"://A3横向 foreach (Aspose.Words.Section section in oDoc) { section.PageSetup.PaperSize = PaperSize.A3; section.PageSetup.Orientation = Orientation.Landscape; section.PageSetup.TextColumns.SetCount(2); section.PageSetup.TextColumns.EvenlySpaced = true; section.PageSetup.TextColumns.LineBetween = true; } break; case "16K": foreach (Aspose.Words.Section section in oDoc) { section.PageSetup.PaperSize = PaperSize.B5; section.PageSetup.Orientation = Orientation.Portrait; } break; case "8KH": foreach (Aspose.Words.Section section in oDoc) { section.PageSetup.PageWidth = double.Parse("36.4 ");//纸张宽度 section.PageSetup.PageHeight = double.Parse("25.7");//纸张高度 section.PageSetup.Orientation = Orientation.Landscape; section.PageSetup.TextColumns.SetCount(2); section.PageSetup.TextColumns.EvenlySpaced = true; section.PageSetup.TextColumns.LineBetween = true; //section.PageSetup.LeftMargin = double.Parse("3.35"); //section.PageSetup.RightMargin = double.Parse("0.99"); } break; } } #endregion public void SetHeade(string strBookmarkName, string text) { if (oDoc.Range.Bookmarks[strBookmarkName] != null) { Aspose.Words.Bookmark mark = oDoc.Range.Bookmarks[strBookmarkName]; mark.Text = text; } } public void InsertFile(string vfilename) { Aspose.Words.Document srcDoc = new Aspose.Words.Document(vfilename); Node insertAfterNode = oWordApplic.CurrentParagraph.PreviousSibling; InsertDocument(insertAfterNode, oDoc, srcDoc); } public void InsertFile(string vfilename, string strBookmarkName, int pNum) { //Aspose.Words.Document srcDoc = new Aspose.Words.Document(vfilename); //Aspose.Words.Bookmark bookmark = oDoc.Range.Bookmarks[strBookmarkName]; //InsertDocument(bookmark.BookmarkStart.ParentNode, srcDoc); //替换插入word内容 oWordApplic.Document.Range.Replace(new System.Text.RegularExpressions.Regex(strBookmarkName), new InsertDocumentAtReplaceHandler(vfilename, pNum), false); } /// <summary> /// 插入word内容 /// </summary> /// <param name="insertAfterNode"></param> /// <param name="mainDoc"></param> /// <param name="srcDoc"></param> public static void InsertDocument(Node insertAfterNode, Aspose.Words.Document mainDoc, Aspose.Words.Document srcDoc) { // Make sure that the node is either a pargraph or table. if ((insertAfterNode.NodeType != NodeType.Paragraph) & (insertAfterNode.NodeType != NodeType.Table)) throw new Exception("The destination node should be either a paragraph or table."); //We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode; //Remove empty paragraphs from the end of document while (null != srcDoc.LastSection.Body.LastParagraph && !srcDoc.LastSection.Body.LastParagraph.HasChildNodes) { srcDoc.LastSection.Body.LastParagraph.Remove(); } NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KeepSourceFormatting); //Loop through all sections in the source document. int sectCount = srcDoc.Sections.Count; for (int sectIndex = 0; sectIndex < sectCount; sectIndex++) { Aspose.Words.Section srcSection = srcDoc.Sections[sectIndex]; //Loop through all block level nodes (paragraphs and tables) in the body of the section. int nodeCount = srcSection.Body.ChildNodes.Count; for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++) { Node srcNode = srcSection.Body.ChildNodes[nodeIndex]; Node newNode = importer.ImportNode(srcNode, true); dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } } static void InsertDocument(Node insertAfterNode, Aspose.Words.Document srcDoc) { // Make sure that the node is either a paragraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) throw new ArgumentException("The destination node should be either a paragraph or table."); // We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode; // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Aspose.Words.Section srcSection in srcDoc.Sections) { // Loop through all block level nodes (paragraphs and tables) in the body of the section. foreach (Node srcNode in srcSection.Body) { // Let's skip the node if it is a last empty paragraph in a section. if (srcNode.NodeType.Equals(NodeType.Paragraph)) { Aspose.Words.Paragraph para = (Aspose.Words.Paragraph)srcNode; if (para.IsEndOfSection && !para.HasChildNodes) continue; } // This creates a clone of the node, suitable for insertion into the destination document. Node newNode = importer.ImportNode(srcNode, true); // Insert new node after the reference node. dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } } /// <summary> /// 换行 /// </summary> public void InsertLineBreak() { oWordApplic.InsertBreak(BreakType.LineBreak); } /// <summary> /// 换多行 /// </summary> /// <param name="nline"></param> public void InsertLineBreak(int nline) { for (int i = 0; i < nline; i++) oWordApplic.InsertBreak(BreakType.LineBreak); } #region InsertScoreTable public bool InsertScoreTable(bool dishand, bool distab, string handText) { try { oWordApplic.StartTable();//开始画Table oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left; //添加Word表格 oWordApplic.InsertCell(); oWordApplic.CellFormat.Width = 115.0; oWordApplic.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(115); oWordApplic.CellFormat.Borders.LineStyle = LineStyle.None; oWordApplic.StartTable();//开始画Table oWordApplic.RowFormat.Height = 20.2; oWordApplic.InsertCell(); oWordApplic.CellFormat.Borders.LineStyle = LineStyle.Single; oWordApplic.Font.Size = 10.5; oWordApplic.Bold = false; oWordApplic.Write("评卷人"); oWordApplic.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center; oWordApplic.CellFormat.Width = 50.0; oWordApplic.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50); oWordApplic.RowFormat.Height = 20.0; oWordApplic.InsertCell(); oWordApplic.CellFormat.Borders.LineStyle = LineStyle.Single; oWordApplic.Font.Size = 10.5; oWordApplic.Bold = false; oWordApplic.Write("得分"); oWordApplic.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center; oWordApplic.CellFormat.Width = 50.0; oWordApplic.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50); oWordApplic.EndRow(); oWordApplic.RowFormat.Height = 25.0; oWordApplic.InsertCell(); oWordApplic.RowFormat.Height = 25.0; oWordApplic.InsertCell(); oWordApplic.EndRow(); oWordApplic.EndTable(); oWordApplic.InsertCell(); oWordApplic.CellFormat.Width = 300.0; oWordApplic.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto; oWordApplic.CellFormat.Borders.LineStyle = LineStyle.None; oWordApplic.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left; oWordApplic.Font.Size = 11; oWordApplic.Bold = true; oWordApplic.Write(handText); oWordApplic.EndRow(); oWordApplic.RowFormat.Height = 28; oWordApplic.EndTable(); return true; } catch { return false; } } #endregion #region 插入表格 public bool InsertTable(System.Data.DataTable dt, bool haveBorder) { Aspose.Words.Tables.Table table = oWordApplic.StartTable();//开始画Table ParagraphAlignment paragraphAlignmentValue = oWordApplic.ParagraphFormat.Alignment; oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center; //添加Word表格 for (int row = 0; row < dt.Rows.Count; row++) { oWordApplic.RowFormat.Height = 25; for (int col = 0; col < dt.Columns.Count; col++) { oWordApplic.InsertCell(); oWordApplic.Font.Size = 10.5; oWordApplic.Font.Name = "宋体"; oWordApplic.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐 oWordApplic.CellFormat.Width = 50.0; oWordApplic.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50); if (haveBorder == true) { //设置外框样式 oWordApplic.CellFormat.Borders.LineStyle = LineStyle.Single; //样式设置结束 } oWordApplic.Write(dt.Rows[row][col].ToString()); } oWordApplic.EndRow(); } oWordApplic.EndTable(); oWordApplic.ParagraphFormat.Alignment = paragraphAlignmentValue; table.Alignment = Aspose.Words.Tables.TableAlignment.Center; table.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto; return true; } #endregion public void InsertPagebreak() { oWordApplic.InsertBreak(BreakType.PageBreak); } public void InsertBookMark(string BookMark) { oWordApplic.StartBookmark(BookMark); oWordApplic.EndBookmark(BookMark); } public void GotoBookMark(string strBookMarkName) { oWordApplic.MoveToBookmark(strBookMarkName); } public void ClearBookMark() { oDoc.Range.Bookmarks.Clear(); } public void ReplaceText(string oleText, string newText) { //System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(oleText); oDoc.Range.Replace(oleText, newText, false, false); } private class InsertDocumentAtReplaceHandler : IReplacingCallback { private string vfilename; private int pNum; public InsertDocumentAtReplaceHandler(string filename, int _pNum) { this.vfilename = filename; this.pNum = _pNum; } ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) { Document subDoc = new Document(this.vfilename); subDoc.FirstSection.Body.FirstParagraph.InsertAfter(new Run(subDoc, this.pNum + "."), null); // Insert a document after the paragraph, containing the match text. Node currentNode = e.MatchNode; Paragraph para = (Paragraph)e.MatchNode.ParentNode; InsertDocument(para, subDoc); // Remove the paragraph with the match text. e.MatchNode.Remove(); e.MatchNode.Range.Delete(); return ReplaceAction.Skip; } } }
相关推荐
在.NET项目中,引入此DLL即可开始利用Aspose.Words提供的API来处理Word和Excel文档。开发者可以使用C#、VB.NET或其他.NET兼容语言进行编程,调用Aspose.Words的类和方法,实现各种文档处理需求。 总的来说,Aspose....
Aspose.Words是一个强大的文档处理库,能够处理Microsoft Word文档的各种操作,而ZedGraph则是一个用于.NET平台的开源图表库,能够生成各种高质量的2D图表。 首先,Aspose.Words提供了丰富的API,可以让我们在Word...
本文将深入探讨如何利用Aspose.Words将Word文档中的表格转换为图片,这对于在网页、移动应用或者需要离线查看表格内容的场景中尤其有用。 首先,我们需要理解Aspose.Words的基本概念。Aspose.Words是一个.NET组件,...
在IT行业中,Aspose.Words是一款非常强大的文档处理组件,它允许开发者在不依赖Microsoft Office的情况下,进行Word文档的创建、阅读、修改和转换。这个资源聚焦于利用Aspose.Words进行一系列的关键操作,包括插入...
Aspose.Words是一款强大的.NET库,专为处理Microsoft Word文档而设计。它允许开发者在不依赖Microsoft Office的情况下,进行创建、编辑、转换和显示Word文档的各种操作。在.NET环境中,Aspose.Words提供了丰富的API...
在C#(.NET 5)中,可以利用Aspose.Words的Document类来加载和操作Word文档。例如,通过`Document doc = new Document("path_to_file.docx");`可以创建一个Document对象,从而访问和修改文档内容。为了给文档添加...
6. 邮件合并:通过邮件合并功能,Aspose.Words可以自动处理大量个性化的文档,如批量生成信函、报告或证书,极大地提高了工作效率。 7. PDF处理:除了基本的创建和转换,Aspose.Words还支持对PDF文档的高级操作,如...
在“Aspose.Words for .NET根据word模板创建文档Demo源码”中,我们可以通过分析源代码来学习如何利用Aspose.Words的功能根据模板文件生成新的Word文档。 1. **模板文件的理解与使用** 模板文件通常是一个预先设计...
总之,Aspose.Words提供了一种灵活且高效的方式,通过Word模板和编程来生成包含动态数据和图表的文档。无论是简单的文本替换还是复杂的图表插入,Aspose.Words都能帮助开发者实现自动化和个性化文档生成。
下面我们将详细讲解如何利用Aspose.Words将Word文件转换为PDF。 首先,你需要在你的项目中引入Aspose.Words的NuGet包。在Visual Studio中,可以通过右键点击解决方案资源管理器中的项目,选择“管理NuGet程序包”,...
总的来说,Aspose.Words for .NET 提供了全面的文档处理能力,使开发者能够在.NET环境中高效地处理Word和PDF文件,大大简化了文档自动化生成和格式化的工作流程。无论是简单的文本替换还是复杂的图片插入,Aspose....
Aspose.Words是一款强大的文档处理库,主要用于在C++编程环境中创建、操作和转换Microsoft Word文档。这个"Aspose.Words.Cpp_18.11.zip"压缩包包含的资源显然是Aspose.Words库的C++版本,版本号为18.11,允许开发者...
在这个“aspose.words导出word文件demo”中,包含了一个名为`aspose.words.dll`的库文件,它是Aspose.Words的核心组件,提供了丰富的API来处理Word文档的各种操作。 首先,`aspose.words.dll`文件库是Aspose.Words...
3. **模板处理**:利用MailMerge功能,Aspose.Words可以从数据源(如数据库、CSV文件或XML)填充模板,生成批量定制的文档,例如信函、发票或证书。 4. **编程接口**:提供直观的API,便于.NET开发者集成到他们的...
Aspose.Words.dll是Aspose公司开发的一个强大的文档处理组件,专门用于处理Microsoft Word文档。版本22.4包含了对.NET框架2.0到6.0的兼容性,这使得它能适应多种开发环境和项目需求。值得注意的是,这个版本还支持...
- 企业级应用:对于需要处理大量Word文档的企业来说,Aspose.Words 18.7与SkiaSharp的结合提供了更稳定的解决方案,能够满足企业对于文档自动化处理、报告生成、格式转换等需求。 - 开发工具:开发者可以利用这一...
4个版本的Aspose.words.dll,包括 Aspose.Words.V16.7,Aspose.Words.V17.12,Aspose.Words.V18.4,AsposeWords.V18.7,修复一些低版本bug
Aspose.Words是一款强大的文档处理库,主要用于在.NET Core环境下创建、编辑和转换Microsoft Word文档。这个19.11版本特别引人注目,因为它兼容.NET Core 3.1框架,这意味着开发者可以在跨平台环境中利用这个库的...
本主题将探讨如何利用Aspose.Words和System.Drawing这两个组件在Unity中创建丰富的Word文档,包括插入表格和图片。 Aspose.Words是一款强大的.NET库,它允许程序员在不依赖Microsoft Word的情况下操作Word文档。在...