I want to generate .xlsx with my application.
the reason I don't want to do this by loading .xml are :
1. my application will then depend on those .xml, I have to maintain my code and also those .xml
2. if I load from .xml, maybe it is even better to have a empty .xlsx in my server, open it, do some modification to it, write it out. This doesn't make sense to me. and I don't want to have a bunch of .xlsx or .xml in my server.
I believe we are capable of doing this, but just don't have a walk-through here. I have been reading many documents just trying to do this simple things. I can't find many information on this one.
Here is what I have gone so far, hope that someone can complete this puzzle together.
//create a excel 2007 package, .xlsx
SpreadsheetDocument excelDoc = SpreadsheetDocument.Create(@"C:\sample\ooxmlSDKnewSS.xlsx", SpreadsheetDocumentType.Workbook);
//add a workbook to the package
WorkbookPart wbPart = excelDoc.AddWorkbookPart();
//add a worksheet to the workbook
WorksheetPart wsPart = wbPart.AddNewPart<WorksheetPart>();
//generate the id for the worksheet
string relId = wbPart.GetIdOfPart(wsPart);
This should handle the relationship thing of the package with one workbook and one worksheet only.
We now need to create the real .xml document for them.
//create a xml document for workbook.xml
XmlDocument xwb = new XmlDocument
//I believe this is what every .xml must have
xwb.AppendChild(xwb.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
//add the root element,
//I Believe there are better ways to handle namespace
//if you know, please tell me
XmlElement eleWorkbook = xwb.CreateElement("workbook", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
eleWorkbook.SetAttribute(@"xmlns:r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
xwb.AppendChild(eleWorkbook);
eleWorkbook.AppendChild(xwb.CreateElement("sheets"));
XmlElement eleSheet = xwb.CreateElement("sheet");
eleSheet.SetAttribute("name", "Sheet1");
eleSheet.SetAttribute("sheetId", "1");
eleSheet.SetAttribute(@"r:id", relId);
eleWorkbook.FirstChild.AppendChild(eleSheet);
//write this .xml to workbookpart
Stream wbstream = wbPart.GetStream();
xwb.Save(wbstream);
//do the same for worksheet .xml
XmlDocument xws = new XmlDocument();
xws.AppendChild(xws.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
XmlElement eleWorksheet = xws.CreateElement("worksheet", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
eleWorksheet.SetAttribute(@"xmlns:r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
xws.AppendChild(eleWorksheet);
XmlElement eleSheetData = xws.CreateElement("sheetData");
eleWorksheet.AppendChild(eleSheetData);
Stream wsstream = wsPart.GetStream();
xws.Save(wsstream);
//close it
excelDoc.Close();
This code can generate a package, with files that are needed in place, but cannot be opened by Excel 2007. I believe the problem is caused by namespace or relationship in-consistency. probably relationship. I am still strugling on how to make my hand-made .xml to be consistent with the automated relationship by the SDK, those Id sort of things.
Please help me out if you have done this before.
Thank you