2

I'm using ExcelLibrary to generate dynamic excel sheet. Which works perfectly.

Code

    //create new xls file
    string file = Server.MapPath("Discussion_Board_Report.xls");
    Workbook workbook = new Workbook();
    Worksheet worksheet = new Worksheet("Report");
    worksheet.Cells[0, 0] = new Cell("COMM 226 Case Discussions Report");

    worksheet.Cells[4, 0] = new Cell("Student ID");
    worksheet.Cells[4, 1] = new Cell("User Name");
    worksheet.Cells[4, 2] = new Cell("Case 1");
    worksheet.Cells[4, 3] = new Cell("Case 2");
    worksheet.Cells[4, 4] = new Cell("Case 3");
    worksheet.Cells[4, 5] = new Cell("Topics");
    worksheet.Cells[4, 6] = new Cell("Replies");

    workbook.Worksheets.Add(worksheet);
    workbook.Save(file);

Question

Problem with this code is, it's going to save the file in the server. I cannot save file in C://, because windows want allow me to save without user's permission! I want user to choose their own file path. How do I prompt a Save As dialog box?

HardCode
  • 2,025
  • 4
  • 33
  • 55

4 Answers4

2

It looks like you are using EPPlus, it has a Stream property on the ExcelPackage object that I believe you can use to write to the response stream. I've included some code that is similar to what I've used in the past to download a file.

I then put this logic into the page load of a page that I link to.

ExcelPackage package = new ExcelPackage();
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Name Here");
///worksheet logic


var msArray = package.Stream;
var response = HttpContext.Current.Response;
response.Clear();
response.ClearHeaders();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment; filename={0}.xls".FormatWith(reportName));
response.AddHeader("Content-Length", msArray.Length.ToString());
response.OutputStream.Write(msArray, 0, msArray.Length);
response.Flush();
response.End();
TalonMcShay
  • 316
  • 3
  • 9
1

What you need to do is stream the file to the user. I am sure the workbook class offers to save to a Stream, on this case the Response Stream

Something like this:

Response.ContentType = "application/vnd.ms-excel";
//Force the browser to offer to download the file
Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
workbook.SaveToStream(Response.OutputStream);

Update

Apparently, the author of the Excel library did not have support for this until .NET 4 and now claims to support it by doing this:

    MemoryStream m = new MemoryStream(); // Temp Stream
    workbook.Save(m);
    m.CopyTo(Response.OutputStream);

Update 2

Actually, the author is not the one that claims that calling Workbook's Save method on a MemoryStream will work but rather someone else. The author actually recommends this silly piece of code.

DataSet ds = GetData();
MemoryStream m = new MemoryStream();
ExcelLibrary.DataSetHelper.CreateWorkbook(m , ds);
m.WriteTo(Response.OutputStream);

If that doesn't work, then I am afraid you are better off if you stop using that library completely and use EPPlus instead.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • @HardCode You didn't say which library are you using so I didn't expect my code to work. My intention was to give you an idea of what you need to do. If you are using EPPlus, then look at Talon's answer; otherwise, explain which library you are using to be able to help better. – Icarus Aug 30 '12 at 16:22
  • Well, I'm using ExcelLibrary. – HardCode Aug 30 '12 at 16:41
  • Did you try calling the `SaveToStream` method instead of just save `Save`? – Icarus Aug 30 '12 at 16:51
  • @HardCode Posted and update. If that doesn't do it, stop using that library, it looks pretty bad to me. – Icarus Aug 30 '12 at 17:02
0

You can't do this in a pure application. Your code is running on the server - not on the user's machine.

You could generate Excel files on the server and then provide a link for your users to download them, but there's no way of getting the code on the server to save directly to your user's machine.

You could try doing something similar with a client-side plugin like Silverlight and you'll have limited access to the file system exposed by Silverlight.

Maybe you could let us know a bit more about what you're trying to do and we could provide better guidance.

dariom
  • 4,413
  • 28
  • 42
0

You can try the FileUpload server Control.

In the yourfile.aspx use this:

<asp:FileUpload ID="FileUpload1" runat="server" />

In the yourfile.aspx.cs use this for keep the path that the use choose:

Server.MapPath(FileUpload1.FileName);
phemt.latd
  • 1,775
  • 21
  • 33