如何在C#中以编程方式将xlsx文件转换为2003 xls文件?

我发现ExcelPackage是一个比Excel Interop API更好的库来创建和保存编程excel表,但它们是在.xlsx中生成的。 大多数会看到文件的人只安装了office 2003,所以我需要在我的C#代码中将最终结果转换为.xls文件。

你知道用C#代码做任何事吗?

** UPDATE我正在尝试使用SaveAs方法,但它不起作用,它只是没有做任何事情,或返回错误0x800A03EC。

我怀疑这不会是一个流行的答案,但我不认为将文件从.xlsx转换为.xls是合乎需要的(我原本认为没有必要,但不幸的是,这是一个概括太远)。

“Microsoft Office兼容包”可以免费下载并添加对Office XP和Office 2003的新格式的支持 – 因此,至少在一般情况下,说服用户将其系统升级到规范而不是泥潭你自己必须处理办公室互操作(这基本上会导致你,很可能是你的用户,很多痛苦)。 同样,我相信Open Office 3中的新格式也得到了支持。

我确实感谢有些人不允许将这种function添加到他们的系统中,但是大多数情况下添加上述工具会使人们的生活更轻松,因为它会减少使用Office 2007和使用旧版本的人之间的摩擦。版本。

您可以尝试使用Microsoft.Office.Interop.Excel。 您需要在尝试进行转换的计算机上安装Excel。 您可以从COM选项卡添加引用并使用Microsoft Excel 12.0对象库组件。

基本上,您将使用Workbook.Open()打开现有工作簿,创建新工作表并复制现有数据。 然后,您可以使用Workbook.SaveAs()方法,这将允许您在第二个参数中设置文件格式。

试试这段代码:

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(); 

这是我在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; }