Workbook.Save method doesn't use title parm

On a page named Reports.aspx, I am trying to save a workbook.

string WorkbookTitle = medicalGroup + " Current Balances";

wb.Save(WorkbookTitle, FileFormatType.Excel2003, SaveType.OpenInBrowser, Response);

The workbook should be named "SMG Current Balance", but is instead named "reports.xls". Also, it is read-only. How do I name it and make it editable? Thanks.

Hi,

Thanks for considering Aspose.

If you use SaveType.OpenInBrowser option, it is the routine of IE that .aspx page name shows for the excel file and the file would be read-only. I think you may utilize SaveType.OpenInExcel option for your requirement.

i.e..,

wb.Save(WorkbookTitle, FileFormatType.Excel2003, SaveType.OpenInExcel, Response);

Thank you.

No, that's still not it.

If I specify SaveType.OpenInExcel and the user selects Open from the Excel dialog, I still get a read-only excel workbook named reports.aspx. Same behavior if I specify SaveType.OpenInBrowser.

If the user selects Save from the excel dialog, then the file gets saved with the correct file name, not read-only. But she has to go to windows explorer and find the file, then double-click on it to bring it into Excel. Is there any way to just have the Workbook.Save method name it the with the string specified and write it so it is editable?

Hi,

I think it might be Https / SSL problem. Could you try to replace the Workbook.Save() line of code by the following code to make sure if it work fine for your need.

[C#]

//Saves file to memory

MemoryStream stream = new MemoryStream();

excel.Save(stream, FileFormatType.Default);

response.ContentType = "application/vnd.ms-excel";

//This is same as OpenInExcel option

response.AddHeader( "content-disposition","attachment; filename=book1.xls");

//This is same as OpenInBrowser option

//response.AddHeader( "content-disposition","inline; filename=book1.xls");

response.BinaryWrite(stream.ToArray());

[Visual Basic]

'Saves file to memory

Dim stream As MemoryStream = New MemoryStream()

excel.Save(stream, FileFormatType.Default)

response.ContentType = "application/vnd.ms-excel"

'This is same as OpenInExcel option

response.AddHeader("content-disposition","attachment; filename=book1.xls")

'This is same as OpenInBrowser option

'response.AddHeader( "content-disposition","inline; filename=book1.xls")

response.BinaryWrite(stream.ToArray())

Thank you.

Nope, we're not using https/SSL and using this code still produces a read-only workbook if the user chooses "Open".

MemoryStream stream = new MemoryStream();

wb.Save(stream, FileFormatType.Default);

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

//This is same as OpenInExcel option

Response.AddHeader("content-disposition", "attachment; filename=" + WorkbookTitle);

//This is same as OpenInBrowser option

//Response.AddHeader( "content-disposition","inline; filename=" + WorkbookTitle);

Response.BinaryWrite(stream.ToArray());

What else you got?

Hi,

Do you enable http compression on your IIS? Could you try the following code without involving Aspose.Cells APIs if it work fine or you got the same problem. Could you give us details of IIS Settings, .NET framework, Browser type, OS etc.

[C#]

FileStream fs1 = new FileStream("d:\\invoice.xls", FileMode.Open, FileAccess.Read);

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

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

this.Response.ContentType = "application/xls";

Response.AddHeader( "content-disposition","attachment; filename=book1.xls");

Response.BinaryWrite(data1);

Response.End();

Thank you.

Once i created c:\invoice.xls, your sample code worked. It created a workbook that was not read-only named book1.xls. I fiddled about with the code and got this to work for saving my workbook:

MemoryStream stream = new MemoryStream();

wb.Save(stream, FileFormatType.Default);

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

Response.AddHeader("content-disposition", "attachment; filename=" + WorkbookTitle);

Response.BinaryWrite(stream.ToArray());

It throws an error if I include Response.End().

I would still like to know why I have to save the file to a stream first. The browser is IE6 on XPSP2. .NET version 2.0.50727.

Aspose.Cells also save a file to a stream first internally before sending. And we use similiar code as yours. The only difference is we call Reponse.Clear() before this piece of code. Maybe that causes your problem.

Anyway, you can use your code to handle HttpResponse header. That's more flexible.

Laurence:

...The only difference is we call Reponse.Clear() before this piece of code. Maybe that causes your problem.

Really? Where? Before the workbook save?

We call Response.Clear() method before we set http headers:

response.Clear();

//Set content type
switch(fileFormatType)
{
case FileFormatType.CSV:
response.ContentType = "application/csv";
break;
case FileFormatType.TabDelimited:
response.ContentType = "application/txt";
break;
case FileFormatType.Excel2007Xlsx:
case FileFormatType.Excel2007Xlsm:
response.ContentType = "application/xlsx";
break;
default:
response.ContentType = "application/vnd.ms-excel";
break;
}



//This is for http compression
if(this.m_HTTPCompression)
{
HttpCachePolicy cache = response.Cache;
cache.SetExpires(DateTime.Now - TimeSpan.FromMinutes(1));
cache.SetCacheability(HttpCacheability.Private);

response.AddHeader("pragma","no-cache");
}


//Add header
if(saveType == SaveType.OpenInExcel)
response.AddHeader( "content-disposition","attachment; filename=" + resultSpreadsheet);
else
response.AddHeader( "content-disposition","inline; filename=" + resultSpreadsheet);