openxml-sdk

How to get cell value with applied formatting (formatted cell value) with OpenXML SDK

早过忘川 提交于 2019-11-27 21:15:16
问题 I've been googling and searching on the site for the answer, but I couldn't find a solution - everywhere people mostly discuss how to add new number format to the document and apply it. What I need is to get the cell value as a string with applied formatting - i.e. same string as would be displayed by Excel. I already figured that there's no easy way or built-in function which would return the readymade formatted value for a cell. So it seems to me that to get the value I need to do two

Using OpenXML SDK to replace text on a docx file with a line break (newline)

Deadly 提交于 2019-11-27 19:26:16
问题 I am trying to use C# to replace a specific string of text on an entire DOCX file with a line break (newline). The string of text that I am searching for could be in a paragraph or in a table in the file. I am currently using the code below to replace text. using (WordprocessingDocument doc = WordprocessingDocument.Open("yourdoc.docx", true)) { var body = doc.MainDocumentPart.Document.Body; foreach (var text in body.Descendants<Text>()) { if (text.Text.Contains("##Text1##")) { text.Text =

Unable to use existing paragraph styles in Open Xml

不羁的心 提交于 2019-11-27 16:19:17
I'm working on an export of an HTML file to a Open XML wordfile. If in the HTML <h1> is used, I want to add a Heading1 style to that part. But somehow when I open the document in Microsoft Word 2010, the Style isn't applied. If I open the created document in Libre Office, there is some style applied. I also defined some styles myself, and if I use one of those styles, everything went well in Word and Libre Office. I opened the Open XML SDK 2.5 Productivity Tool for Microsoft Office and when I look in the example code it provides, it suggests: ParagraphStyleId paragraphStyleId1 = new

Why appending AutoFilter corrupts my excel file in this example?

房东的猫 提交于 2019-11-27 15:42:52
Hi I use the below method to apply an AutoFilter : public static void ApplyAutofilter(string fileName, string sheetName, string reference) { using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, true)) { IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName); var arrSheets = sheets as Sheet[] ?? sheets.ToArray(); string relationshipId = arrSheets.First().Id.Value; var worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId); var autoFilter = new AutoFilter() { Reference =

openxml spreadsheat save-as

孤街浪徒 提交于 2019-11-27 15:02:37
I have an Excel 2007 spreadsheet that I edit with the OpenXML SDK 2. I remove some rows etc. I would like to know how to save that Spreadsheetdocument to another filename. To my knowledge there is no built in way to change the filename, but since one way to edit the file is to use streams you can easily give the name of the file you want when writing out the contents of a stream: byte[] byteArray = File.ReadAllBytes("C:\\temp\\oldName.xltx"); using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); using (SpreadsheetDocument spreadsheetDoc =

How can I embed any file type into Microsoft Word using OpenXml 2.0

拜拜、爱过 提交于 2019-11-27 13:21:37
I spent a lot of time trying to figure out a good way to embed any file into Microsoft Word using OpenXml 2.0; Office documents are fairly easy but what about other file types such as PDF, TXT, GIF, JPG, HTML, etc.... What is a good way to get this to work for any file type, in C#? Embedding Foreign Objects (PDF, TXT, GIF, etc…) into Microsoft Word using OpenXml 2.0 (Well, in collaboration with COM) I got a lot from this site, so here I asked and answered my own question in order to give back a little on a topic in which I had difficulty finding answers on, hope it helps people. There are

Reading a date from xlsx using open xml sdk

╄→гoц情女王★ 提交于 2019-11-27 12:04:15
问题 I have a date in format "4/5/2011" (month/day/year) in a xlsx file in one of the cells. Im trying to parse the file and load those data in some classes. So far the part where I parse the cell looks like this: 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; } } I hoped that date would be a cell.DataType

How do I have Open XML spreadsheet “uncollapse” cells in a spreadsheet?

人盡茶涼 提交于 2019-11-27 08:01:19
问题 I'm working with xslx Excel file on the server side in C#. In a spreadsheet, say there are 15 columns (cells) total. In the rows of cells, some values are missing. So the first row is my header will properly have the 15 cells. But my data rows, some cells might have empty values, so Open XML has a "jagged" set of cells values. Row 1 will have the full 15 cells, Row 2 might have 13 cells since two of the values are empty. What! How do I map this data properly? It basically shifts everything to

How to access OpenXML content by page number?

你说的曾经没有我的故事 提交于 2019-11-27 07:54:52
问题 Using OpenXML, can I read the document content by page number? wordDocument.MainDocumentPart.Document.Body gives content of full document. 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

Replace bookmark text in Word file using Open XML SDK

可紊 提交于 2019-11-27 07:39:01
I assume v2.0 is better... they have some nice "how to:..." examples but bookmarks don't seem to act as obviously as say a Table... a bookmark is defined by two XML elements BookmarkStart & BookmarkEnd . We have some templates with text in as bookmarks and we simply want to replace bookmarks with some other text... no weird formatting is going on but how do I select/replace bookmark text? Here's my approach after using you guys as inspiration: IDictionary<String, BookmarkStart> bookmarkMap = new Dictionary<String, BookmarkStart>(); foreach (BookmarkStart bookmarkStart in file.MainDocumentPart