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

发布于 2024-07-18 05:29:02 字数 374 浏览 9 评论 0原文

我发现 ExcelPackage,这是一个比 Excel Interop API 更好的库,可以以编程方式创建和维护 Excel 工作表,但是它们以 .xlsx 格式生成。 大多数看到这些文件的人只安装了 Office 2003,因此我需要在我的 C# 代码中将最终结果转换为 .xls 文件。

您知道在 C# 代码中有什么方法可以做到这一点吗?

** 更新我正在尝试使用 SaveAs 方法,但它不起作用,它只是不执行任何操作,或者返回错误 0x800A03EC 。

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.

Do you know any way to do it in C# code?

** UPDATE I'm trying to use SaveAs method, but it doesn't work, it just doesn't do anything, or return the error 0x800A03EC .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

氛圍 2024-07-25 05:29:02

我怀疑这不会是一个受欢迎的答案,但我不认为将文件从 .xlsx 转换为 .xls 是可取的(我本来打算建议这是没有必要的,但不幸的是,这是一个概括太远)。

“Microsoft Office 兼容包”可以免费下载,并为 Office XP 和 Office 2003 添加了对新格式的支持 - 因此,至少在一般情况下,说服用户使他们的系统符合规范比陷入困境要好得多你自己必须处理办公室互操作(这基本上会给你,很可能你的用户带来很多痛苦)。 同样,我相信 Open Office 3 中支持新格式。

我确实理解,在某些情况下,人们不会被允许将这种功能添加到他们的系统中,但在大多数情况下,添加上述工具将使人们的生活更轻松因为它将减少使用 Office 2007 的用户与使用旧版本的用户之间的摩擦。

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).

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.

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.

黎歌 2024-07-25 05:29:02

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

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

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.

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.

何其悲哀 2024-07-25 05:29:02

试试这个代码:

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

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();
糖粟与秋泊 2024-07-25 05:29:02

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

Here is a piece of code from my project with IBM iSeries. It will convert ANY Excel file to 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;    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文