After Download the Excel 2007 file i.e .XLSX I am not able to open the file

Hello Dear Aspose team,

Currently We have one problem to be solved.

Currently We are using Aspose Version 4.8.1.3 in my dotnet code where in uploads the excel file and download the excel file whether it is Excel 2003 or Excel 2007 i.e (.Xls,.Xlsx).Currently we are trying to download the records more than 100k from grid ,for the same I have modified the code in my application and i have added filename with extension .Xlsx (which is support to unlimited records ,for my current requirement) and I ran my code as well ,and I able to download the Excel 2007 format i.e .XLSX(After add the extension .Xlsx in my code).Earlier it was .Xls extension with application name.

After download the Excel file I am not able to open the Excel file(Which I have downloaded from grid).When I am trying to open the file it's give me the error saying that

"Excel cannot open the file "ParameterList.xlsx" because the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

I am pasting the code below,Which is generating the excel file.

if (dtSRCLV != null)

{

MemoryStream strmExcelFile = (MemoryStream)objConverter.ConvertSRCLVToExcel(dtSRCLV);

//export to excel

Response.Clear();

Response.Buffer = true;

Response.ContentType = "application/vnd.ms-excel";Response.Charset =

"";

this.EnableViewState = false;Response.AddHeader(

"Content-Disposition", "attachment; filename=\"ParameterList.xlsx\"");

//Supporting Excel2007 for download

////Response.AddHeader("Content-Length", strmExcelFile.Length.ToString());

//Response.ContentType = "Application/msexcel";

Response.OutputStream.Write(strmExcelFile.ToArray(), 0, (int)strmExcelFile.Length - 1);

Response.Flush();

Please look into on this ASAP.Pleas let me know if you need any clarifications regarding the same.

Regards

Sreenivas


This message was posted using Aspose.Live 2 Forum

Hi,

Please make sure that the memory stream should contain your xlsx file.

Could you confirm if the issue is with Aspose.Cells for .NET or
not. You can manually create sample xlsx file into MS Excel,
then use your own code streaming the file without involving
Aspose.Cells APIs and response to the client browser to check if it
works fine or you got the similar problem.

e.g.

FileStream fs1 = new FileStream(“d:\ParameterList.xlsx”, FileMode.Open, FileAccess.Read);

byte[] data1 = new byte[fs1.Length];

fs1.Read(data1, 0, data1.Length);

this.Response.ContentType = “application/xlsx”;

Response.AddHeader( “content-disposition”,“attachment; filename=outParameterList.xlsx”);

Response.BinaryWrite(data1);

Response.End();




Thank you.

Hello Amzad,

Thanks for update:)

I have tried accordingly as you suggested,I have replaced the code which was sent by you,after replace the code I am able to open the excel file(Which I am trying to open the excel file,after download from the grid) but it's not returning the header column names,and also I had Live chat with Aspose team, and finally they told that "Need a sample application to look into in detail".

What would be the solution for this??Could you please suggest me what should be done in next?

Awaiting for the same.

Regards

Sreenivas

Hi,

Yes, please create a simple sample application/project with latest Aspose.Cells for .NET v4.8.2, zip it and post it here to reproduce the issue on our end. We will surely look into your issue to figure it out soon.

Thank you.

Hi Aspose Team,

I am re-stating my problem again. Please go through the same.

I am currently using the Aspose Version 4.8.1.0. and .Net 2.0 version. Currently I can export successfully to Excel 2003 with the below code. Below is the code for the same.

MemoryStream strmExcelFile = (MemoryStream)objConverter.ConvertSRCLVToExcel(dtSRCLV);
//export to excel
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.xls";
Response.Charset = "";
this.EnableViewState = false;
Response.AddHeader("Content-Disposition", "attachment; filename=\"ParametersList.xls\"");
Response.OutputStream.Write(strmExcelFile.ToArray(), 0, (int)strmExcelFile.Length - 1);
Response.Flush();


But when I change the code in the below 2 methods, to enable it to work with Excel 2007, it fails.

Response.ContentType()
Response.AddHeader()

I tried the following values for Response.ContentType and AddHeader method.

1) Response.ContentType = "application/vnd.xlsx"

or

2) Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

or

3) Response.ContentType = "application/xlsx"

The only change that was made to the AddHeader method was to introduce the extension .xlsx as shown the code below.

Response.AddHeader("Content-Disposition", "attachment; filename=\"ParametersList.xlsx\"");

My requirement is currently that I need to use the MemorySteam object and not the FileStream example that was suggested by you earlier. Currently I am not able to create a sample application which helps to me export to Excel 2007. Hence I am asking help from your side. Could you please create a sample application which uses MemorySteam object (as shown in the above code ) to support export to Excel 2007.

Please write back to me if this is a limitation of Aspose.. Please note that this is urgent issue and is running since a long time.. Kindly let me the solution for the same.

Thanks,

Sreenivas

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.

I have created a sample web application for you to try in your environment and see if it works fine for you. I have created the application using the latest version of Aspose.Cells V4.8.2 and it works fine for Excel 2007 XLSX files. There is no such limitation as per Aspose.Cells as many users are using our component to export the Excel 2007 files in web application.

Thank you & Best Regards,

Will the attched code support File Format Type of Excel 2010?

thanks, Jane

Hi,

I think yes, because in the attached code you will see these lines.


// Send as file to browser…

Response.ClearContent();

Response.ClearHeaders();

Response.AppendHeader(“Content-Disposition”, "inline;filename = " + “ParametersList.xlsx”);

Response.ContentType = “application/XLSX”;

Response.AddHeader(“content-length”, Convert.ToString(aBytes.Length));

Response.BinaryWrite(aBytes);

Response.Flush();

Response.Close();

The output is in xlsx format i.e ParametersList.xlsx which is actually Ms-Excel 2007 and 2010 format.

Below is my code.I am able to open it in Xls format but it is giving error when i am opening it in .xlsx format."Excel cannot open the file filename.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."

Can you please help me.

HttpResponse response = HttpContext.Current.Response;

// first let's clean up the response.object
response.Clear();
response.Charset = "";
response.ClearContent();
response.ClearHeaders();
string strfileName = "AduitListReport_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".xlsx";
response.ContentType = "application/XLSX";
response.AddHeader("Content-Disposition", "attachment;filename=" + strfileName);
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{

// instantiate a datagrid
GridView dg = new GridView();
clsReports vr = new clsReports();
DataSet ds = new DataSet();
ds = // Reteriveing data here
ds.Tables[0].DefaultView.RowFilter = FilterExp();
dg.DataSource = ds.Tables[0].DefaultView;
dg.HeaderStyle.BackColor = System.Drawing.Color.Green;
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
//}
}
}

Can you please let me guide where is the problem ?

Hi,


Using Aspose.Cells for .NET APIs, I have no problem at all. I create a new Workbook, input some value into the first worksheet’s cell, now save the file through response object streaming the XLSX file. The file does open into MS Excel 2007 fine on the fly.

Sample code:

//Creating an Workbook object
Workbook workbook = new Workbook(FileFormatType.Xlsx);
//Get the first default sheet: Sheet1
Worksheet sheet = workbook.Worksheets[0];
//Put a value to a a cell: A1
sheet.Cells[“A1”].PutValue(“Hello World!”);

//… Your extra code goes here if you have

//Save in xlsx format and send the file to user so that he may open the file in
//some application or save it to some location
workbook.Save(this.Response, “Report.xlsx”, ContentDisposition.Attachment, new OoxmlSaveOptions());
Response.End();

Thank you.

Hi,

sheet.Cells["A1"].PutValue("Hello World!");

If there is huge data then it will take lot of time.

Below is the code where it is saveing the xlsx format but the problem which i am faceing is it take a lot time and instead of saving the file directly I want the dialog box which ask for open and save ?

protected void ExportToSpreadsheet(DataTable table)

{

Excel.Application xlApp;

Excel.Workbook xlWorkBook;

Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();

xlWorkBook = xlApp.Workbooks.Add(misValue);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

string strfileName = "AduitListReport_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".xlsx";

System.Data.DataColumn dc = new DataColumn();

int colIndex = 0;

int rowIndex = 0;

foreach (DataColumn dcol in table.Columns)

{

colIndex = colIndex + 1;

xlWorkSheet.Cells[1, colIndex] = dcol.ColumnName;

}

foreach (DataRow drow in table.Rows)

{

rowIndex = rowIndex + 1;

colIndex = 0;

foreach (DataColumn dcol in table.Columns)

{

colIndex = colIndex + 1;

xlWorkSheet.Cells[rowIndex + 1, colIndex] = drow[dcol.ColumnName];

}

}

xlWorkSheet.Columns.AutoFit();

Excel.Range range = xlWorkSheet.get_Range("A1", "BK1");

range.Font.Bold = true;

range.Interior.Color = 128;

xlWorkBook.SaveAs(strfileName);

xlWorkBook.Close(true, misValue, misValue);

xlApp.Quit();

releaseObject(xlWorkSheet);

releaseObject(xlWorkBook);

releaseObject(xlApp);

}

private void releaseObject(object obj)

{

try

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

obj = null;

}

catch (Exception ex)

{

obj = null;

//MessageBox.Show("Exception Occured while releasing object " + ex.ToString());

}

finally

{

GC.Collect();

}

}

Hi,

Please download and try the latest fix:
Aspose.Cells
for .NET v7.1.1.5


Please note, Aspose.Cells is not a UI component, so it will not prompt you while saving the worbook.

But you can use .NET framework UI components for this purpose.

e.g

System.Windows.Forms.MessageBox
System.Windows.Forms.SaveFileDialog
System.Windows.Forms.OpenFileDialog

For web application, use this code.

workbook.Save(this.Response, “Output.xlsx”, ContentDisposition.Attachment, new OoxmlSaveOptions());
Response.End();