0

I was following This tutorial on EPPlus, but I am left scratching my head on how to set the downloads location to the logged in users "Downloads" folder? I know for me the location would be this, but is their a way to assign it w/o knowing the logged on persons account info?

C:\Users\laptop\Downloads

And here is my syntax:

 string location = "C:\\";
 string filename = "EPPlusTest.xlsx";
 using (ExcelPackage objExcelPackage = new ExcelPackage())
 {
    ExcelWorksheet objWorksheet = objExcelPackage.Workbook.Worksheets.Add("Sheet 1");
    objWorksheet.Cells["A1"].LoadFromDataTable(dataTable, true);
    using (ExcelRange objRange = objWorksheet.Cells["A1:XFD1"])
    {
      objRange.Style.Font.Bold = true;
      objRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
      objRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;                
    }
    using (ExcelRange dataRange = objWorksheet.Cells["A2:XFD20"])
    {
      dataRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
      dataRange.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom;
    }
  FileStream objFileStrm = File.Create(filename);
  objFileStrm.Close();
  File.WriteAllBytes(location + filename, objExcelPackage.GetAsByteArray());
}
Bob Goblin
  • 1,251
  • 3
  • 16
  • 33
  • 1
    What does your question have to do with EPPlus or the code you provided? – Cory Nelson Jun 29 '15 at 19:04
  • 1
    You can't set the users download location - they have to do this in their own browser settings - or offer them a dialog to save as.. and then they can choose. This is a security thing with browsers. – Darren Wainwright Jun 29 '15 at 19:05
  • @Darren if you use HttpContext.Current.Response. it will "download" the file to your "Downloads" folder (Exactly what I am wanting just with EPPlus syntax not that syntax) In my current syntax it will save the file to the logged in users C: drive. So you can hardcode a download location. – Bob Goblin Jun 29 '15 at 19:17
  • @CoryNelson - The syntax is using EPPlus - I was asking if their is any syntax that can be added to set the save location of the users Downloads folder. In my current code, it saves to the logged in users C:\\ drive. – Bob Goblin Jun 29 '15 at 19:18
  • EPPlus doesn't download anything though. Post how you're downloading the file (ASP.NET?), not your EPPlus code. – Cory Nelson Jun 29 '15 at 19:31
  • @CoryNelson to my understanding it is the File.WriteAllBytes... line in the above syntax that is actually downloading the file. I am giving that line a location & filename and the objExcelPackage.GetAsByteArray() is writing to Excel. – Bob Goblin Jun 29 '15 at 19:37
  • @user2676140 - It will download it to where you have set your downloads folder, in your browser. You cannot control this server-side. – Darren Wainwright Jun 29 '15 at 20:39
  • also - there is more to downloading a file than you have put. If you use content-disposition you can offer the user a save-as dialog; much cleaner. – Darren Wainwright Jun 29 '15 at 20:39
  • @Darren - this is intriguing to me as the file is being saved to wherever I set in the value of my variable location in the code above. Where I have my downloads folder set through the browswer makes no difference. – Bob Goblin Jun 30 '15 at 01:51
  • That's because you're doing it locally. Deploy your site and it won't do what you think. – Darren Wainwright Jun 30 '15 at 12:06

1 Answers1

1

You are mixing apples and oranges. File.WriteAllBytes is part of System.IO and it intended to manipulate files "locally", e.g. the app writes to the local hard drive.

Asp.net is web so you need to do something like this:

using (ExcelPackage pck = new ExcelPackage())
{
    var ws = pck.Workbook.Worksheets.Add("Demo");
    ws.Cells[1, 2].Value = "Excel Test";

    var fileBytes = pck.GetAsByteArray();
    Response.Clear();

    Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
    Response.AppendHeader("Content-Disposition",
        String.Format("attachment; filename=\"{0}\"; size={1}; creation-date={2}; modification-date={2}; read-date={2}"
            , "temp.xlsx"
            , fileBytes.Length
            , DateTime.Now.ToString("R"))
        );
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    Response.BinaryWrite(fileBytes);
    Response.End();
}

(Take from my post here: Open ExcelPackage Object with Excel application without saving it on local file path)

This will fire the download through the browser which will use what default settings the use has and get you what your looking for.

Community
  • 1
  • 1
Ernie S
  • 13,902
  • 4
  • 52
  • 79
  • Is their a way to set horizontal & vertical alignment using this methodology? – Bob Goblin Jun 29 '15 at 20:53
  • Sure (kind of a separate question so if you get stuck post another on SO). Check this out: http://stackoverflow.com/questions/31042901/export-datatable-to-excel-using-epplus-with-additional-data which shows horizontal alignment. Vertical work much the same way. – Ernie S Jun 29 '15 at 21:30