Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code that generate xls file using XML formatting (with styles and coloring in celles). Now with out changing the entire code I need to export the excel to xlsx format. I cant use interop. I can use third party dll but expecting any solution without using the third party dll.

Current code that generate xls file is given below,
C#
StringBuilder xmlStrBuilder = new StringBuilder();
xmlStrBuilder.Append("");
xmlStrBuilder.Append("<?mso-application progid=\"Excel.Sheet\"????>");
xmlStrBuilder.Append("<workbook xmlns="\" hold=" />            xmlStrBuilder.Append(" xmlns:o="\" xmlns:x="\"urn:schemas-microsoft-com:office:excel\"");<br" mode="hold" />            xmlStrBuilder.Append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
xmlStrBuilder.Append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
-------------
-------------
xmlStrBuilder.Append("<worksheet>");

response.Clear();
response.AppendHeader("Content-Type", "application/vnd.ms-excel");
response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
response.Write(xmlStrBuilder);
response.Flush();
response.End();
excelXml.Length = 0;
excelXml.Capacity = 0;


What I have tried:

I tried to use the third party dll which is not converting the file as expected. Changing the mime type to openxmlformat is not working. Any option to use officeopenxml to convert the file.
Posted
v5

1 solution

You cannot do the conversion in your code like this. A .xlsx file is not just one XML file. It's a .ZIP file containing a bunch of different XML files.

You have no choice but to rewrite the code to support exporting .xlsx files, using the OpenXML SDK, OR by using a third party library that supports exporting in either format.

There is no way you're going to do this without a good sized rewrite of your code.
 
Share this answer
 
Comments
Member 12520912 13-May-16 9:47am    
I am applying colors and styles to my xls file. Also openXML SDK will only work with datatable. I dont thing i can create a datatable to generate my excel. Here we are exporting the UI in excel by creating templates. is this possible in OpenXML SDK
Dave Kreskowiak 13-May-16 9:49am    
OpenXML SDK does NOT work with datatables. Where did you get that from?

It'll create an Excel workbook from scratch and you can do anything in it to arrange data however you want and format it.
Member 12520912 15-May-16 10:50am    
thanks Dave. started to rewrite the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900