0
If FileUpload.HasFile Then

        Dim FileNameXlsx = Path.ChangeExtension(Path.GetFileName(FileUpload.PostedFile.FileName), ".xlsx")

        Dim pathOfUploadedCCP As String = Server.MapPath("~/Uploaded/Documents/TemporaryUploadedFileForCCP/") + FileNameXlsx

        FileUpload.SaveAs(pathOfUploadedCCP)

This code only changes the extension but I am getting the error that the file is corrupted. Please help me: How do I change the format of files to excel workbook (.xlsx)?

  • Can you just save the file as .xlsx, upload it, then delete it perhaps? (Sorry, had a typo saying .Xlsb) – BruceWayne Dec 20 '16 at 06:17
  • before uploading i have to validate the file, for that i am using SpreadsheetLight, so i want all the files in (.xlsx) extension only – shabnam ansari Dec 20 '16 at 06:19
  • Just changing the extension will not work, ever. Use save as and specify openxmlworkbook format for xlsx. https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 – cyboashu Dec 20 '16 at 07:05
  • similar question [Programmatically convert Excel 2003 files to 2007+](http://stackoverflow.com/questions/18904561/programmatically-convert-excel-2003-files-to-2007) – Baby Dec 20 '16 at 07:24

3 Answers3

0

Refer below link, this may help you.

https://www.gemboxsoftware.com/spreadsheet/articles/convert-xls-xlsx-ods-csv-html-net

As I can see in example, you need to include their library in your project.they have given example you can check it by using below link.

https://www.gemboxsoftware.com/spreadsheet/examples/c-sharp-convert-excel-to-pdf/404

0

The XLS and XLSX file formats are different file formats. You cannot just change the extension. The following example is an extreme analogy, but let's say that you want to change a text file (TXT extension) to image file (PNG extension).

When you will try to open the file, MS Excel recognize the invalid format and this is the error that you get.

You must convert your xls file to xlsx file format.

You can use an Excel library like EasyXLS to achieve this goal:

Dim workbook As New ExcelDocument
'Load the xls file
workbook.easy_LoadXLSFile(FileNameXls)
'Convert the xls file to xlsx file
workbook.easy_WriteXLSXFile(FileNameXlsx)
alex.pulver
  • 2,107
  • 2
  • 31
  • 31
-2

Can you do this?

Dim myFiles As String()
myFiles = IO.Directory.GetFiles("<Folder Location of thge File>", "*.xls")
Dim newFilePath As String
For Each filepath As String In myFiles
newFilePath = filepath.Replace("<Folder Location of thge File>", ".xlsx")
System.IO.File.Move(filepath, newFilePath)
Next
End Sub

It will replace the file extension of a file in a folder if the program find an extension of .xls

If the code above wont work try this.

Dim Path as String
Path = "C:\Programs\And_So_On\"
Dim Final As String
Final = Path & "YourFile.xlsx"

YourWorkBook.SaveAs(Final)
Shadow Fiend
  • 351
  • 6
  • 18
  • I dont know the reason why it is required to downvote an answer if it is not working by the way take a look at the code updated above – Shadow Fiend Dec 20 '16 at 07:29
  • @OP already has code to change the file extension, that's not the issue they are facing. They need to also convert the actual File Format as just changing the file extension will not do this in `VB`. – Jordan Dec 20 '16 at 09:42
  • @ShadowFiend your code will only change the extension of file, but i have to change the format also, just changing the extension is giving me error that file is corrupted – shabnam ansari Dec 22 '16 at 10:12