How to create Excel file using OpenXML without creating a local file?

You could use the overload of SpreadsheetDocument.Create that takes a Stream and pass it a MemoryStream: MemoryStream memoryStream = new MemoryStream(); SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook); //add the excel contents… //reset the position to the start of the stream memoryStream.Seek(0, SeekOrigin.Begin); return new FileStreamResult(memoryStream, “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”); Note that as per this StackOverflow question you don’t need … Read more

openxml spreadsheat save-as

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()) { … Read more

Add HTML String to OpenXML (*.docx) Document

I can reproduce the error “… there is a problem with the content” by using an incomplete HTML document as the content of the alternative format import part. For example if you use the following HTML snippet <h1>HELLO</h1> MS Word is unable to open the document. The code below shows how to add an AlternativeFormatImportPart … Read more

Applying % number format to a cell value using OpenXML

WorkbookStylesPart sp = workbookPart.AddNewPart<WorkbookStylesPart>(); Create a stylesheet, sp.Stylesheet = new Stylesheet(); Create a numberingformat, sp.Stylesheet.NumberingFormats = new NumberingFormats(); // #.##% is also Excel style index 1 NumberingFormat nf2decimal = new NumberingFormat(); nf2decimal.NumberFormatId = UInt32Value.FromUInt32(3453); nf2decimal.FormatCode = StringValue.FromString(“0.0%”); sp.Stylesheet.NumberingFormat.Append(nf2decimal); Create a cell format and apply the numbering format id cellFormat = new CellFormat(); cellFormat.FontId = 0; … Read more

Optimal way to Read an Excel file (.xls/.xlsx)

Take a look at Linq-to-Excel. It’s pretty neat. var book = new LinqToExcel.ExcelQueryFactory(@”File.xlsx”); var query = from row in book.Worksheet(“Stock Entry”) let item = new { Code = row[“Code”].Cast<string>(), Supplier = row[“Supplier”].Cast<string>(), Ref = row[“Ref”].Cast<string>(), } where item.Supplier == “Walmart” select item; It also allows for strongly-typed row access too.

open xml reading from excel file

Your approach seemed to work ok for me – in that it did “enter the loop”. Nevertheless you could also try something like the following: void Main() { string fileName = @”c:\path\to\my\file.xlsx”; using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false)) { WorkbookPart workbookPart = doc.WorkbookPart; SharedStringTablePart sstpart … Read more

Using OpenXmlReader

I think you took the wrong WorksheetPart for reading the rows. The line workbookPart.WorksheetParts.First(); gets the first WorksheetPart of the collection which must not necessarily be the first worksheet as you see it in Microsoft Excel. So, iterate through all WorksheetParts and you should see some output on your console window. static void ReadExcelFileSAX(string fileName) … Read more