Tag: openxml sdk

使用OpenXml创建分页符

我使用OpenXml创建Word文档,其中包含简单文本和本文下的一些表格。 如何强制使用此文本的Paragraph始终显示在新页面上? 也许这一段应该是一些标题,但我不知道该怎么做。 谢谢

使用Open XML创建word文档

我正在创建一个示例处理程序来生成简单的Word文档。 本文档将包含文本Hello world 这是我使用的代码(C#.NET 3.5), 我创建了Word文档,但没有文本,大小为0。 我该如何解决? (我使用CopyStream方法,因为CopyTo仅在.NET 4.0及更高版本中可用。) public class HandlerCreateDocx : IHttpHandler { public void ProcessRequest(HttpContext context) { using (MemoryStream mem = new MemoryStream()) { // Create Document using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true)) { // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure and add […]

使用open xml sdk从xlsx读取日期

我在其中一个单元格的xlsx文件中有一个格式为“4/5/2011”(月/日/年)的日期。 我试图解析文件并在某些类中加载这些数据。 到目前为止,我解析单元格的部分如下所示: string cellValue = cell.InnerText; if (cell.DataType != null) { switch (cell.DataType.Value) { case CellValues.SharedString: // get string from shared string table cellValue = this.GetStringFromSharedStringTable(int.Parse(cellValue)); break; } } 我希望date可以是cell.DataType。 事实是,在使用日期“4/5/2011”解析单元格时,cell.DataType的值为null,单元格的值为“40638”,并且它不是共享字符串表的索引。 (我之前尝试过,结果是exception。) 有任何想法吗? 谢谢

如何按页码访问OpenXML内容?

使用OpenXML,我可以按页码阅读文档内容吗? wordDocument.MainDocumentPart.Document.Body提供完整文档的内容。 public void OpenWordprocessingDocumentReadonly() { string filepath = @”C:\…\test.docx”; // Open a WordprocessingDocument based on a filepath. using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filepath, false)) { // Assign a reference to the existing document body. Body body = wordDocument.MainDocumentPart.Document.Body; int pageCount = 0; if (wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text != null) { pageCount = Convert.ToInt32(wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text); } for (int i = […]

使用OpenXML SDK用换行符替换docx文件上的文本(换行符)

我正在尝试使用C#替换整个 DOCX文件上的特定字符串,并使用换行符(换行符)。 我要搜索的文本字符串可以位于文件的段落或表格中。 我目前正在使用下面的代码替换文本。 using (WordprocessingDocument doc = WordprocessingDocument.Open(“yourdoc.docx”, true)) { var body = doc.MainDocumentPart.Document.Body; foreach (var text in body.Descendants()) { if (text.Text.Contains(“##Text1##”)) { text.Text = text.Text.Replace(“##Text1##”, Environment.NewLine); } } } 问题:当我运行此代码时,输​​出DOCX文件将文本替换为空格(即“”)而不是换行符。 如何更改此代码才能使其正常工作?

无法在Open Xml中使用现有段落样式

我正在将HTML文件导出到Open XML wordfile。 如果在HTML 中使用,我想在该部分添加Heading1样式。 但不知何故,当我在Microsoft Word 2010中打开文档时,未应用样式。 如果我在Libre Office中打开创建的文档,则会应用一些样式。 我自己也定义了一些样式,如果我使用其中一种样式,Word和Libre Office中的一切都很顺利。 我打开了适用于Microsoft Office的Open XML SDK 2.5生产力工具 ,当我查看它提供的示例代码时,它建议: ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = “Kop1” }; Kop1(而不是Heading1)是因为我的Word是荷兰语所以我的模板是荷兰语,但这并没有解决问题。 在此代码示例中,我创建了段落并向其添加样式和文本: using (wordDocument = WordprocessingDocument.Open(documentStream, true)) { MainDocumentPart mainPart = wordDocument.MainDocumentPart; WP.Body body = wordDocument.MainDocumentPart.Document.Body; WP.Paragraph para = body.AppendChild(new WP.Paragraph()); StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart; if (part != […]

使用OpenXmlReader

我讨厌借助StackOverflow这样(看似)基本的东西,但我在过去的几个小时里一直在和微软打架,似乎已经走到了死胡同。 我正在尝试阅读(大)Excel 2007+电子表格,谷歌已经告诉我,使用OpenXml SDK是一个非常受欢迎的选择。 所以我给了这个东西一个镜头,阅读了一些教程,检查了微软自己的库页面,并且得到了很少的东西。 我正在使用一个小测试电子表格,只有一列数字和一个字符串 – 大规模测试将在稍后进行。 我尝试了几种与我即将发布的实现相似的实现,但没有一种实现读取数据。 下面的代码主要来自另一个StackOverflow线程,它似乎已经工作 – 对我来说不是这样。 我想我会让你们检查/调试/帮助这个版本,因为它可能比我今天写的任何内容都要少。 static void ReadExcelFileSAX(string fileName) { using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, true)) { WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); OpenXmlPartReader reader = new OpenXmlPartReader(worksheetPart); string text; string rowNum; while (reader.Read()) { if (reader.ElementType == typeof(Row)) { do { if (reader.HasAttributes) { […]

如何使用OpenXml 2.0将任何文件类型嵌入到Microsoft Word中

我花了很多时间试图找出一种使用OpenXml 2.0将任何文件嵌入Microsoft Word的好方法; Office文档相当容易,但其他文件类型如PDF,TXT,GIF,JPG,HTML等等。 在C#中使用任何文件类型的好方法是什么?

将多个word文档合并为一个Open Xml

我有大约10个word文档,我使用open xml和其他东西生成。 现在我想创建另一个word文档,我想逐个加入到这个新创建的文档中。 我想使用open xml,任何提示都会很明显。 以下是我的代码: private void CreateSampleWordDocument() { //string sourceFile = Path.Combine(“D:\\GeneralLetter.dot”); //string destinationFile = Path.Combine(“D:\\New.doc”); string sourceFile = Path.Combine(“D:\\GeneralWelcomeLetter.docx”); string destinationFile = Path.Combine(“D:\\New.docx”); try { // Create a copy of the template file and open the copy //File.Copy(sourceFile, destinationFile, true); using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true)) { // Change the document type […]

使用Open XML SDK替换Word文件中的书签文本

我假设v2.0更好……他们有一些很好的“如何:……”的例子,但是书签看起来并不像表格那么明显……书签是由两个 XML元素BookmarkStart定义的书签结束 。 我们有一些带有书签的模板作为书签,我们只想用其他文本替换书签……没有奇怪的格式化,但如何选择/替换书签文字?