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

    Export to Excel(Convert XLS to XLSX) getting error

    Hi,

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

    Response.Clear
    Response.ContentType = "application/vnd.ms-excel"
    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
    hi
    chitra

    Try this code and refer link which i have mention below

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

    http://www.tagwith.com/question_827430_how-to-transmit-a-xlsx-file-as-a-aspx-response-output

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

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

  • #760479
    Hello Chitra,

    Response.Clear();
    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.

    Regards,
    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.Clear
    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
    book.LoadFromFile("test.xls");

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

    Full tutorials:

    http://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Excel-Conversion/NET-Excel-Conversion-between-Excel-97-2003.xls-and-Excel-2007.xlsx.html


  • Sign In to post your comments