C#将Excel 2007(xlsx)文件转换为Excel 2003(xls)文件

时间:2023-01-15 10:27:28

I am working on a console application which will convert xlsx file into xls file. I don't want to rename it from xlsx to xls because it will get opened in excel 2007 but it will be shown as corrupted file in excel 2003. Looking for a way which will load the document and then it will be saved as xls format.

我正在开发一个控制台应用程序,它将xlsx文件转换为xls文件。我不想将它从xlsx重命名为xls,因为它将在excel 2007中打开,但它将在excel 2003中显示为损坏的文件。寻找一种方法来加载文档然后它将保存为xls格式。

My current code Just renames the xlsx to xls

我当前的代码只需将xlsx重命名为xls

string fileName = @"C:\Users\L-3\Desktop\my.xlsx";
string svfileName = @"C:\Users\L-3\Desktop\ssc\my1.xls";
object oMissing = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
wb.SaveAs(svfileName, XlFileFormat.xlOpenXMLTemplate, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();

4 个解决方案

#1


1  

Try with to save with XlFileFormat.xlExcel9795. You can also use XlFileFormat.xlWorkbookNormal

尝试使用XlFileFormat.xlExcel9795保存。您还可以使用XlFileFormat.xlWorkbookNormal

#2


4  

Your enum is wrong, instead of XlFileFormat.xlOpenXMLTemplate you want XlFileFormat.xlExcel8, so your code would be like so:

您的枚举是错误的,而不是XlFileFormat.xlOpenXMLTemplate您希望XlFileFormat.xlExcel8,所以您的代码将是这样的:

string fileName = @"C:\Users\L-3\Desktop\my.xlsx";
string svfileName = @"C:\Users\L-3\Desktop\ssc\my1.xls";
object oMissing = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
wb.SaveAs(svfileName, XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();

More info here.

更多信息在这里。

#3


0  

I've found the following snippet from here:

我从这里找到了以下代码段:

// Create new XLSX file.
var xlsxFile = new ExcelFile();

// Load data from XLSX file.
xlsxFile.LoadXlsx(fileName + ".xls", XlsxOptions.PreserveMakeCopy);

// Save XLSX file to XLS file.
xlsxFile.SaveXls(fileName + ".xls");

It uses this component.

它使用这个组件。

#4


0  

I think this has the answer you're looking for:

我认为这有你想要的答案:

What is the correct `XlFileFormat` enumeration for Excel 97-2003

Excel 97-2003的正确`XlFileFormat`枚举是什么

You're using file format xlOpenXMLTemplate. Try using xlExcel8 instead? That should be the file format for 97-2003 Workbooks.

您正在使用文件格式xlOpenXMLTemplate。尝试使用xlExcel8代替?这应该是97-2003工作簿的文件格式。

#1


1  

Try with to save with XlFileFormat.xlExcel9795. You can also use XlFileFormat.xlWorkbookNormal

尝试使用XlFileFormat.xlExcel9795保存。您还可以使用XlFileFormat.xlWorkbookNormal

#2


4  

Your enum is wrong, instead of XlFileFormat.xlOpenXMLTemplate you want XlFileFormat.xlExcel8, so your code would be like so:

您的枚举是错误的,而不是XlFileFormat.xlOpenXMLTemplate您希望XlFileFormat.xlExcel8,所以您的代码将是这样的:

string fileName = @"C:\Users\L-3\Desktop\my.xlsx";
string svfileName = @"C:\Users\L-3\Desktop\ssc\my1.xls";
object oMissing = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
wb.SaveAs(svfileName, XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();

More info here.

更多信息在这里。

#3


0  

I've found the following snippet from here:

我从这里找到了以下代码段:

// Create new XLSX file.
var xlsxFile = new ExcelFile();

// Load data from XLSX file.
xlsxFile.LoadXlsx(fileName + ".xls", XlsxOptions.PreserveMakeCopy);

// Save XLSX file to XLS file.
xlsxFile.SaveXls(fileName + ".xls");

It uses this component.

它使用这个组件。

#4


0  

I think this has the answer you're looking for:

我认为这有你想要的答案:

What is the correct `XlFileFormat` enumeration for Excel 97-2003

Excel 97-2003的正确`XlFileFormat`枚举是什么

You're using file format xlOpenXMLTemplate. Try using xlExcel8 instead? That should be the file format for 97-2003 Workbooks.

您正在使用文件格式xlOpenXMLTemplate。尝试使用xlExcel8代替?这应该是97-2003工作簿的文件格式。