乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 在使用<em>OpenXml</em>导出数据到<em>Excel</em&g

在使用<em>OpenXml</em>导出数据到<em>Excel</em&g

作者:乔山办公网日期:

返回目录:excel表格制作


private void FillBookmarksUsingOpenXml(string sourceDoc, string destDoc, Dictionary<string, string> bookmarkData) { string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; // Make a copy of the template file. File.Copy(sourceDoc, destDoc, true); //Open the document as an Open XML package and extract the main document part. using (WordprocessingDocument wordPackage = WordprocessingDocument.Open(destDoc, true)) { MainDocumentPart part = wordPackage.MainDocumentPart; //Setup the namespace manager so you can perform XPath queries //to search for bookmarks in the part. NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace("w", wordmlNamespace); //Load the part's XML into an XmlDocument instance. XmlDocument xmlDoc = new XmlDocument(nt); xmlDoc.Load(part.GetStream()); //Iterate through the bookmarks. foreach (KeyValuePair<string, string> bookmarkDataVal in bookmarkData) { var bookmarks = from bm in part.Document.Body.Descendants<BookmarkStart>() select bm; foreach (var bookmark in bookmarks) { if (bookmark.Name == bookmarkDataVal.Key) { Run bookmarkText = bookmark.NextSibling<Run>(); if (bookmarkText !e799bee5baa6e79fa5e98193e78988e69d83332= null) // if the bookmark has text replace it { bookmarkText.GetFirstChild<Text>().Text = bookmarkDataVal.Value; } else // otherwise append new text immediately after it { var parent = bookmark.Parent; // bookmark's parent element Text text = new Text(bookmarkDataVal.Value); Run run = new Run(new RunProperties()); run.Append(text); // insert after bookmark parent parent.Append(run); } //bk.Remove(); // we don't want the bookmark anymore } } } //Write the changes back to the document part. xmlDoc.Save(wordPackage.MainDocumentPart.GetStream(FileMode.Create)); } }

你下载安装的并不是office程序,应该只是歌文件格式兼容包,所以不能打开。
1、代码说明:location:加密文件路径,password:加密密码
2、加密原理:基于AES128 ,加密秘钥生成方式:SHA1

建议引用SpreadsheetGear.dll来做,这个是专门做excel表格的,很好很强大。
有了模板之后,不管你数据源是xml还是什么,用这里面的方法和接口 直接往文档里面填就ok了

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网