如何在 C# 中以编程方式将 xlsx 文件转换为 2003 xls 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/806771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert xlsx files into 2003 xls files programatically in C#?
提问by Victor Rodrigues
I've found ExcelPackage, a better library than Excel Interop API to create and mantain programatically excel sheets, but they are generated in .xlsx. Most of people that will see the files have only office 2003 installed, so I need to convert, in my C# code, the final result into a .xls file.
我发现ExcelPackage是一个比 Excel Interop API 更好的库,可以以编程方式创建和维护 Excel 工作表,但它们是在 .xlsx 中生成的。大多数会看到这些文件的人只安装了 office 2003,所以我需要在我的 C# 代码中将最终结果转换为 .xls 文件。
Do you know any way to do it in C# code?
你知道在 C# 代码中有什么方法吗?
** UPDATE I'm trying to use SaveAs method, but it doesn't work, it just doesn't do anything, or return the error 0x800A03EC .
**更新我正在尝试使用 SaveAs 方法,但它不起作用,它什么也不做,或者返回错误 0x800A03EC 。
回答by Scott Lance
You can try to use Microsoft.Office.Interop.Excel. You will need to have Excel installed on the machine that is trying to do the conversion. You can add the reference from the COM tab and use the Microsoft Excel 12.0 Object Library component.
您可以尝试使用 Microsoft.Office.Interop.Excel。您需要在尝试进行转换的计算机上安装 Excel。您可以从 COM 选项卡添加引用并使用 Microsoft Excel 12.0 对象库组件。
Basically, you will open up the existing workbook using Workbook.Open(), create a new Worksheet and copy over the existing data. You can then use the Workbook.SaveAs() method, this will let you set the file format in the 2nd parameter.
基本上,您将使用 Workbook.Open() 打开现有工作簿,创建一个新工作表并复制现有数据。然后您可以使用 Workbook.SaveAs() 方法,这将让您在第二个参数中设置文件格式。
回答by Murph
I suspect this won't be a popular answer, but I don't believe that its desirable to convert the files to .xls from .xlsx (I was going to suggest that it wasn't necessary, but, unfortunately, that's a generalisation too far).
我怀疑这不会是一个流行的答案,但我不认为将文件从 .xlsx 转换为 .xls 是可取的(我打算建议它没有必要,但不幸的是,这是一个概括太远)。
The "Microsoft Office Compatibility Pack" is free to download and adds support for the new formats to Office XP and Office 2003 - far better therefore, at least in the general instance, to persuade your users to bring their systems up to spec than to mire yourself in having to deal with office interop (which is basically going to cause you, and quite possibly your users, a lot of pain). Similarly I believe there is support for the new formats in Open Office 3.
“Microsoft Office 兼容包”可免费下载,并为 Office XP 和 Office 2003 添加了对新格式的支持 - 因此,至少在一般情况下,说服您的用户使他们的系统符合规范要好得多,而不是陷入困境你自己不得不处理办公室互操作(这基本上会给你,很可能你的用户带来很多痛苦)。同样,我相信 Open Office 3 支持新格式。
I do appreciate that there are circumstances where people will not be allowed to add this capability to their system but for the most part adding the tools as above will make people's lives easier since it will reduce the friction between those using Office 2007 and those using older versions.
我确实理解在某些情况下不允许人们将这种功能添加到他们的系统中,但在大多数情况下,添加上述工具将使人们的生活更轻松,因为它将减少使用 Office 2007 的人和使用旧版的人之间的摩擦版本。
回答by foson
Try this code:
试试这个代码:
try
{
Microsoft.Office.Interop.Word.ApplicationClass oWord = new ApplicationClass();
object oMissing = Type.Missing;
object fileName = @"c:\test.docx";
Document oDoc = oWord.Application.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object fileName2 = @"c:\test2.doc";
object fileFormat = WdSaveFormat.wdFormatDocument97;
oDoc.SaveAs(ref fileName2, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
oWord = null;
Console.WriteLine("Done");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.Read();
回答by sam
Here is a piece of code from my project with IBM iSeries. It will convert ANY Excel file to Excel 2003:
这是我的 IBM iSeries 项目中的一段代码。它会将任何 Excel 文件转换为 Excel 2003:
string MOVE_DOWNLOADED(string FILENAME)
{
string Path = FILENAME;
Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(Path,
0,
true,
5,
"",
"",
true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t",
false,
false,
0,
true,
1,
0);
string retval = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + "_tmp_" + ".xlsx";
try
{
workBook.SaveAs(retval,
Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795,
null,
null,
false,
false,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,
false,
false,
null,
null,
null);
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
workBook.Close(null, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook = null;
GC.Collect(); // force final cleanup!
return retval;
}