Click here to Skip to main content
15,667,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am having some problem which is right now I had transfer my data into excel. But, since my computer no longer support xls, i need to convert to xlsx. Here is my coding

'Declaration for XLS
Dim excelApp As New excel.Application
Dim wbReport As Workbook
Dim wsReport As Worksheet
Dim rejRs As ADODB.Recordset

excelApp.Visible = False
excelApp.ScreenUpdating = False
excelApp.Interactive = False
excelApp.IgnoreRemoteRequests = True

Set wbReport = excelApp.Workbooks.Add
Set wsReport = wbReport.ActiveSheet

With wsReport
'Here will be my data that will transfer to excel
.Cells(1, 1).Value = "Student REPORT : " & stud

'This is anywhere code state I will save my excel
wbReport.SaveAs "C:\studReport\" & stud & "_WIP_" & Format(xserverdate, "YYYYMMDD") & ".xls"

What I have tried:

Right now, I try to change the code at SaveAs from xls become xlsx

wbReport.SaveAs "C:\studReport\" & stud & "_WIP_" & Format(xserverdate, "YYYYMMDD") & ".xlsx"

And it said the format is not valid. How can I convert to xlsx?
Please help

Update:: Since my excel is 2003, i cannot convert to xlsx. Thanks for the help
Updated 21-Aug-22 21:42pm

1 solution

You need to specify the file format explicitely. This works for me:
private void buttonExcel97_Click(object sender, EventArgs e)
         string safeExcelFilePath = @"c:\temp\cp.excel";

         string excel97File = Path.Combine(safeExcelFilePath, "Excel97.xls");
         string excelOpenXMLFile = Path.Combine(safeExcelFilePath, "ExcelOpenXML.xlsx");

         // Excel Application
         Microsoft.Office.Interop.Excel.Application _excel = new _Excel.Application();

         // Open workbook
         Workbook workbook1 = _excel.Workbooks.Open(excel97File);

         // Save as xlsx
         workbook1.SaveAs(excelOpenXMLFile, XlFileFormat.xlOpenXMLWorkbook);

         // Note: In case there are Macros involved
         //     workbook1.SaveAs("FileName.xlsm", XlFileFormat.xlOpenXMLWorkbookMacroEnabled);

For detail information have a look here: XlFileFormat enumeration (Excel) | Microsoft Docs[^]
Share this answer
Maciej Los 22-Aug-22 14:52pm    
0x01AA 22-Aug-22 15:33pm    
Thank you very much Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900