[SOLVED] Problem with Application.Download and Application.DownloadAndOpen

Answered Closed
0
0

I have a problem trying to use the Application.Download (stream, “file.xls”) method;
and    Application.DownloadAndOpen (“_ selft”, stream, “test.xls”).

I attach the image when we try to open the excel file it seems that it is corrupt or empty.

In my company we use this library
http://www.smartxls.com
 Example code below

using (var workbook = new XLWorkbook())
{
using (MemoryStream stream = new MemoryStream())
{
WorkBook workBook = new WorkBook();
//set data
workBook.setText(0, 1, “Jan”);
workBook.setText(0, 2, “Feb”);
workBook.setText(0, 3, “Mar”);
workBook.setText(0, 4, “Apr”);
workBook.setText(0, 5, “Jun”);

workBook.setText(1, 0, “Comfrey”);
workBook.setText(2, 0, “Bananas”);
workBook.setText(3, 0, “Papaya”);
workBook.setText(4, 0, “Mango”);
workBook.setText(5, 0, “Lilikoi”);
for (int col = 1; col <= 5; col++)
for (int row = 1; row <= 5; row++)
workBook.setFormula(row, col, “RAND()”);
workBook.setText(6, 0, “Total”);
workBook.setFormula(6, 1, “SUM(B2:B6)”);
workBook.setSelection(“B7:F7”);
//auto fill the range with the first cell’s formula or data
workBook.editCopyRight();

//select range A1:F7
workBook.setSelection(0, 0, 6, 5);
//Creating an AutoFilter
workBook.autoFilter();

//Counting the auto filtered value in the cell “E11”
workBook.setFormula(10, 4, “SUBTOTAL(2,B1:B7)”);

workbook.SaveAs(stream);
Application.Download(stream, “test.xls”);
Application.DownloadAndOpen(“_selft”,stream, “test2.xls”);
}
}

 

  • You must to post comments
Best Answer
0
0

Hi  Jose

Great!

I tested your code and before to generate a file from stream I reset the position zero. and works perfect

Here the test
//auto fill the range with the first cell’s formula or data

workBook.editCopyRight();

//select range A1:F7
workBook.setSelection(0, 0, 6, 5);
//Creating an AutoFilter
workBook.autoFilter();

//Counting the auto filtered value in the cell “E11”
workBook.setFormula(10, 4, “SUBTOTAL(2, B1: B7)”);

workBook.write(stream);

//*** resetting the position of the stream to the beginning
stream.Seek(0, SeekOrigin.Begin);

Application.Download(stream, “test.xls”);

//*** resetting the position of the stream to the beginning
stream.Seek(0, SeekOrigin.Begin);

Application.DownloadAndOpen(“_selft”, stream, “test2.xls”);

Regards

Happy coding

 

  • Jose Roberto Taveras
    Thanks, Mil Gracias me funciono.
  • Paul (ITG)
    For help you I’ll Appreciate if could mark how “Accept this answer” Regards, happy coding
  • You must to post comments
0
0

What you indicated me reading the file works well with wisej,
but it seems that you have some kind of problem assigning the contenttypes,
I made a normal asp page in the project and put the following code:
using (MemoryStream stream = new MemoryStream ())
{
workBook.writeXLSX (stream);
var response = HttpContext.Current.Response;
stream.Position = 0;
response.Clear ();
stream.Seek (0, SeekOrigin.Begin);

stream.WriteTo (response.OutputStream);
response.ContentType = “application / vnd.openxmlformats-officedocument.spreadsheetml.sheet”;
response.AddHeader (“Content-Disposition”, “attachment; filename = text”);
response.StatusCode = 200;
response.Flush ();
response.End ();

}
And it worked correctly, I also use the library with visualwebgui and it works well for me.

  • You must to post comments
0
0

Hi Jose

¿Did you test downloading a excel file, with the same structure that show in your code and opened as a stream before download?
I know that is not you need, its for discart some issue in the library that you use

Sample code

using (var stream = new FileStream(Path.Combine(Application.StartupPath, “Excel.xls”), FileMode.Open, FileAccess.Read))
{
Application.Download(stream, “Excel.xls”);
}

Regards

  • Jose Roberto Taveras
    Below I share a comment with what I did with a normal asp page and it works well for me, that the problem is not with the library
  • You must to post comments
Showing 3 results