0

i can save a datagrid using the vb.net code:

                saveFileDialog.Filter = "Excel2007 (*.xlsx)|*.xlsx|All files (*.*)|*.*"

i wish to give the user the option to save in either Excel2003 or Excel2007 format. What changes do i need to make in the code and what about references and imports? thanks...

user2444712
  • 65
  • 1
  • 13
  • Well, Excel 2003 is ".xls"...... – Tim Aug 14 '13 at 08:19
  • i changed it to .xls . i can open the file but i get a message' the file you are trying to open, 'student.xls' is in a different format than specified by the file extension. this means its not getting saved in excel 2003 right??? – user2444712 Aug 14 '13 at 08:27
  • I would suspect so, yes. – Tim Aug 14 '13 at 08:36

1 Answers1

0

Additionally to the adequate extension, you have to provide the Excel version when calling SaveAs():

curBook.SaveAs("full path with adequate extension", Excel.XlFileFormat.xlWorkbookDefault)

You can find the list of values for XlFileFormat here. Although the names in this list are not too descriptive. Here you have a translation:

  • 2003 -> Excel.XlFileFormat.xlExcel8 (as explained here)
  • 2007 -> Excel.XlFileFormat.xlOpenXMLWorkbook (as explained here)

NOTE: bear in mind that the code above relies on Office Interop.

Community
  • 1
  • 1
varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • Thanks...the SaveAs() is something like this in my code: if saveFileDialog.ShowDialog() = DialogResult.OK Then xlWorkSheet.SaveAs(saveFileDialog.FileName) MsgBox(" Your file has been saved") End If – user2444712 Aug 14 '13 at 08:48
  • @user2444712 Sorry I realised about my error: you are not saving the WorkBook, but the WorkSheet. The options above work with the WorkBook (the whole file), the WorkSheet is just one of the tabs. Thus you are using Office Interop but you are not using it completely right (you cannot save a worksheet into a workbook of an old version). Please, correct your code such that you perform the right SaveAs: replace xlWorkSheet with the corresponding WorkBook name (xlWorkBook I guess...). – varocarbas Aug 14 '13 at 09:03