You must Sign In to post a response.
  • Category: .NET

    Export to Excel(Convert XLS to XLSX) getting error


    In the classic ASP the code has written like the below for export to excel

    Response.ContentType = "application/"
    Response.AddHeader "Content-Disposition","attachment;filename=" & Session.SessionID & ".xls"

    But i need to convert as "XLSX". So i changed the last line to
    Response.AddHeader "Content-Disposition","attachment;filename=" & Session.SessionID & ".xlsx"

    But i am getting the error that" Excel cannot open the file 'XX.xlsx',because the file format or file extension is not valid.Verify that the file has not been corrupted and that the file extension matches the format of the file. "

    Please let me know how to resolve this.
  • #760477

    Try this code and refer link which i have mention below

    string path = Server.MapPath(@"\Images\Test.xlsx");
    Response.ContentType = "application/";
    Response.AppendHeader("Content-Disposition", "attachment; filename=myfile.xlsx");

    Name : Dotnet Developer-2015
    Email Id :

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #760479
    Hello Chitra,

    Response.Buffer = true;
    Response.AddHeader("Content-Disposition", "attachment; filename=Report.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    You can replace this code as per your need.

    Hope this will help you.

    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #760480
    Its not a dotnet application. Its classic ASP application.

    The previous code of opening as "XLS" was :
    Response.ContentType = "text/plain"
    Response.AddHeader "Content-Disposition","attachment;filename=" & Session.SessionID & ".xls"

    It is working properly. but for opening as "XLSX" i wrote the below line , getting error

    Response.AddHeader "Content-Disposition", "attachment;Filename=" & Session.SessionID & ".XLSX"
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

  • #760514
    You could use following method to convert XLS TO XLSX in C#:

    // create a new excel document
    Workbook book = new Workbook();

    //load a document from file

    //Save the file to the version you want
    book.SaveToFile("test.xlsx", ExcelVersion.Version2007);

    Full tutorials:

  • Sign In to post your comments