以XLS格式导出文件或将xls文件转换为c#.net中的xlsx格式 [英] Export the file in XLS format or convert the xls file to xlsx format in c#.net

查看:463
本文介绍了以XLS格式导出文件或将xls文件转换为c#.net中的xlsx格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用XML格式生成xls文件的代码(在celles中使用样式和着色)。现在没有更改整个代码我需要将excel导出为xlsx格式。我不能使用互操作。我可以使用第三方dll,但期望任何解决方案,而不使用第三方DLL。



生成xls文件的当前代码如下,

 StringBuilder xmlStrBuilder =  new  StringBuilder(); 
xmlStrBuilder.Append( );
xmlStrBuilder.Append( <?mso-application progid = \Excel.Sheet \ ????>中);
xmlStrBuilder.Append( < workbook xmlns = \ hold = /> xmlStrBuilder.Append( < span class =code-string> 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 ;





我尝试了什么:



我试图使用未按预期转换文件的第三方dll。将mime类型更改为openxmlformat无效。任何使用officeopenxml转换文件的选项。

解决方案

你不能在这样的代码中进行转换。 .xlsx文件不仅仅是一个XML文件。它是一个包含大量不同XML文件的.ZIP文件。



您别无选择,只能使用OpenXML SDK重写代码以支持导出.xlsx文件,或者通过使用支持以任一格式导出的第三方库。



如果没有对代码进行大规模的重写,你将无法做到这一点。

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,

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.

解决方案

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.


这篇关于以XLS格式导出文件或将xls文件转换为c#.net中的xlsx格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆