How to convert xlsx files into 2003 xls files programatically in C#?

前端 未结 4 1693
遥遥无期
遥遥无期 2021-01-03 12:39

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 wi

相关标签:
4条回答
  • 2021-01-03 13:15

    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.

    0 讨论(0)
  • 2021-01-03 13:16

    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.

    0 讨论(0)
  • 2021-01-03 13:23

    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();
    
    0 讨论(0)
  • 2021-01-03 13:34

    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;    
    }
    
    0 讨论(0)
提交回复
热议问题