Tag: openxml

OpenXML库保存excel文件

我有以下代码,它没有将值保存到单元格和文件中。 它在cell.cellvalue字段中显示值,但不将其写入excel。 我不知道如何保存文件。 我使用OpenXml-SDK ,我正在为创建的电子表格文档的每个cell/row写入数据表值。 using (SpreadsheetDocument ssd=SpreadsheetDocument.Open(Server.MapPath(@”\ExcelPackageTemplate.xlsx”),true)) { WorkbookPart wbPart = ssd.WorkbookPart; WorksheetPart worksheetPart = wbPart.WorksheetParts.First(); SheetData sheetdata = worksheetPart.Worksheet.GetFirstChild(); string[] headerColumns = new string[] { dt.Columns[0].ColumnName, dt.Columns[1].ColumnName,dt.Columns[2].ColumnName }; DocumentFormat.OpenXml.Spreadsheet.Row r = new DocumentFormat.OpenXml.Spreadsheet.Row(); int RowIndexer = 1; //int colInd=0; r.RowIndex = (UInt32)RowIndexer; string test = ColumnName(RowIndexer); foreach (DataColumn dc in dt.Columns) { DocumentFormat.OpenXml.Spreadsheet.Cell […]

OOXML SDK非法字符替换

我在使用MS的Open XML SDK 2.0创建XLSX文档时遇到问题。 我的问题是我需要在Excel工作表中显示一些这些非法字符,但如果我只是将它们添加到工作表中,则不会加载该文档。 我正在使用这个function private static string ProcessString(string str) { return System.Security.SecurityElement.Escape(str); } 哪个会给我汤姆的球而不是汤姆的球。 (好吧,我还没弄明白如何获得后者,因为生成的excel不会打开。) 有人知道如何在Excel工作表中使用OOXML显示非法XML字符吗? 编辑: 在我用来创建文本单元格的函数中: private static Cell CreateTextCell(string header, UInt32 index, string text) { var c = new Cell { DataType = CellValues.String, CellReference = header + index }; var cellValue = new CellValue(text); c.Append(cellValue); return c; } 我知道它与非法字符有关,因为当我没有在我的文本中包含特定字段时它起作用,然后当我包含它时,Excel会给我一个解析器错误和一个空白文档。 […]

使用Moq模拟OpenXML

我应该如何测试以下GetWorksheetPart方法: public class ExcelDocument : IExcelDocument { private readonly string _filePath; public ExcelDocument(string filePath) { _filePath = filePath; } public WorksheetPart GetWorksheetPart(ISpreadsheetDocument excelDoc, string sheetName) { Sheet sheet = excelDoc.GetSheet(sheetName); if (sheet == null) { throw new ArgumentException( String.Format(“No sheet named {0} found in spreadsheet {1}”, sheetName, _filePath), “sheetName”); } return excelDoc.GetPartById(sheet.Id); } } IExcelDocument和包装器的SpreadsheetDocumentWrapper接口是: […]

如何替换注释的InnerText

我尝试过以下方法: comment.InnerText=comment.InnerText.Replace(comment.InnerText,new_text); 哪个不起作用,因为我们只能读取InnerText属性。 如何有效地更改InnerText值,以便我可以将修改保存到MainDocumentPart.Document和MainDocumentPart.Document ? 编辑: DocumentFormat.OpenXml.Wordprocessing.Comment是评论的类。 编辑2:方法: public void updateCommentInnerTextNewWorkItem(List<Tuple> list){ //DOCX.CDOC.Comments -> WordProcessingCommentsPart.Comments //DOCX._CIT -> Dictionary foreach (var comm in DOCX.CDOC.Comments) { foreach (var item in list) { foreach (var item_cit in DOCX._CIT) { if (((Comment)comm).InnerText.Contains(“”) && item.Item3.Contains(item_cit.Value)) { comm.InnerXml = comm.InnerXml.Replace(comm.InnerText, item.Item1 + “”); //comm.InnerText.Replace(comm.InnerText,item.Item1+””); //DOCX.CDOC.Comments.Save(); //DOCX.DOC.MainDocumentPart.Document.Save(); } if (((Comment)comm).InnerText.Contains(“<tag class") && […]

使用XML文件中的数据/基于模板将XML转换为Word文档生成Word文档(docx)

我有一个XML文件,其中包含我需要在Word文档中填充的数据。 我需要找到一种方法,定义一个模板,该模板可以用作从XML文件填充数据并创建输出文档的基线。 我相信有两种方法可以做到这一点。 创建一个将成为“模板”的XSLT文件,并使用它来与XML文件一起使用它生成Word文档。 使用Word中的内容控件来创建模板文档,并以某种方式映射到XML文件。 我只是不知道如何实现这两种方式的细节。 或者不确定是否有另一种更简单的方法来完成此任务。 有人可以举例说明如何实现这一点。 只是一个简单的例子就足够了。 对于任何编码,我更喜欢C#。 我正在使用Word 2016,但希望它从Word 2007到Word 2016兼容,如果可能的话,因为用户将使用这些版本。 谢谢!

以编程方式将OpenXml文档保存为以前的版本(Word 2007)

我有一些代码可以创建一个要在MS Word中查看的文档。 一些XML通过XSLT运行,然后添加到OpenXML文档中。 生成的文档在Word 2010中看起来很好,但不会在Word 2007中打开,我需要能够支持。 如何创建有效的Word 2007文档? 使用Open XML Format SDK 2.0(版本2.0.5022.0) using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; … using (WordprocessingDocument package = WordprocessingDocument.Create(outputPath, WordprocessingDocumentType.Document)) { package.AddMainDocumentPart(); MainDocumentPart mainPart = package.MainDocumentPart; mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document(); // code … XML translation and append to document package.MainDocumentPart.Document.Save(); } 。 这篇文章有一些有用的信息: Word 2010和Word 2007之间存在差异,它们遵循OOXML标准的不同版本。 您很可能已包含在Word 2010中有效但Word 2007无法处理的内容。 […]

使用OpenXML 2.5将数据写入docx文档中的TextInput元素

我有一些docx文件。 我用OpenXML 2.5 SDK阅读它们,并在每个doc中搜索TextInput 。 byte[] filebytes = System.IO.File.ReadAllBytes(“Test.docx”); using (MemoryStream stream = new MemoryStream(filebytes)) using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true)) { IEnumerable fields = wordDocument.MainDocumentPart.Document.Descendants(); foreach (var field in fields) { IEnumerable textInputs = field.Descendants(); foreach (var ti in textInputs) { <> } } wordDocument.MainDocumentPart.Document.Save(); stream.Flush(); ETC… } 我怎么能在每个TextInput写一个值? 谢谢!

如何使用带有c#的OpenXML Format SDK从格式的单词中读取数据?

我使用下面的代码行从docx文件中获取字符串。 我能够读取字符串但不能格式化。 如何用格式读取docx数据。 using (WordprocessingDocument myDocument = WordprocessingDocument.Open(docxFilePath, true)) { Body body = myDocument.MainDocumentPart.Document.Body; string content = body.InnerText; } 谢谢你的快速回复。

示例使用openxml创建powerpoint

在哪里可以找到带有OpenXML的ppt文件中的插入文本和图像的“Hello World”? 我无法生成简单的模板。

OpenXML在写元素时挂起

我有一个程序,它基本上从数据库中提取数据,将其缓存到一个文件,然后将该数据导出为多种格式(Excel,Excel 2003,CSV)。 我正在使用OpenXML SDK 2.0来完成Excel工作。 这些导出过程并行运行(使用Parallel.ForEach ),数据量可能非常大 – 例如,某些CSV为800MB。 在这些较大的导出期间,我注意到XML文档的编写将会挂起。 例如,如果我有8个并行导出,那么在某些时候它们都会“暂停”。 它们都围绕着同一点: //this.Writer is an OpenXmlWriter which was created from a WorksheetPart. this.Writer.WriteElement(new Cell() { CellValue = new CellValue(value), DataType = CellValues.String }); 当发生这种情况时,我暂停调试器(在这种情况下是VS2013)并注意到所有线程都在代码的相同部分阻塞 – 有些在OpenXML SDK中更深一些 – 但它们都源于对OpenXmlWriter.WriteElement的调用。 我使用JustDecompile挖掘了源代码,但没有找到任何答案。 似乎正在使用中间流写入隔离存储,这由于某种原因阻塞。 每个基础流都是FileStream 。 这是一个屏幕截图,显示在OpenXmlWriter.WriteElement方法中或内部阻止的所有(在本例中为8个)并行任务: 其中一个挂起线程的完整堆栈 – 带注释。 WindowsBase.dll!MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName Normal WindowsBase.dll!MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(int retryCount, out string fileName) WindowsBase.dll!MS.Internal.IO.Packaging.SparseMemoryStream.EnsureIsolatedStoreStream() […]