How to save created excel file to Client pc in ASP.NET

前端 未结 4 562
南旧
南旧 2021-01-25 06:26

I am creating a excel report and html report in asp.net when I clik the button and my application create it and save to the client desktop but, it is not working correctly becau

相关标签:
4条回答
  • 2021-01-25 06:39

    One important thing to note so that the selected best answer works for you:
    You have to use to use a different library than the Microsoft.Office.Interop.Excel implied in the question. You have to use EPPlus. This is how you set it up in your project:

    1. From Visual Studio's Package Manager Console, type: Install-Package EPPlus (this will install the library and the reference as needed)
    2. Add this using statement (using OfficeOpenXml;)

    And this is some sample code to create the excel package (pck) as referred to in the best answer:

        ExcelPackage pck = new ExcelPackage();
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("exported data");
        // Excel sheet headings
        ws.Cells[1, 1].Value = "Column 1 title";
        ws.Cells[1, 2].Value = "Column 2 title";
    


    Now you can use the code provided in the best answer.

    0 讨论(0)
  • 2021-01-25 06:43

    You need to send the file to client using Response object. To ignore the warning message like when client is opening the excel file -

    enter image description here

    To prevent this you need to mention the content type and length in the response use the sample code

    //Read the Excel file in a byte array. here pck is the Excelworkbook              
    Byte[] fileBytes = pck.GetAsByteArray();
    
    //Clear the response               
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Cookies.Clear();
    //Add the header & other information      
    Response.Cache.SetCacheability(HttpCacheability.Private);
    Response.CacheControl = "private";
    Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
    Response.AppendHeader("Pragma", "cache");
    Response.AppendHeader("Expires", "60");
    Response.AppendHeader("Content-Disposition",
    "attachment; " +
    "filename=\"ExcelReport.xlsx\"; " +
    "size=" + fileBytes.Length.ToString() + "; " +
    "creation-date=" + DateTime.Now.ToString("R") + "; " +
    "modification-date=" + DateTime.Now.ToString("R") + "; " +
    "read-date=" + DateTime.Now.ToString("R"));
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    //Write it back to the client    
    Response.BinaryWrite(fileBytes);
    Response.End();
    
    0 讨论(0)
  • 2021-01-25 06:44

    You can write excel file to response HttpResponse.WriteFile Method

    string CurrentDate;
    DateTime saveNow = DateTime.Now;
    CurrentDate = saveNow.Date.ToShortDateString();
    string reportContent = prepareHTM();
    
    string pathFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) + "\\As_Build_Report_ "+ CurrentDate + ".html";
    
    using (StreamWriter outfile = new StreamWriter(pathFile, true))
    {
        outfile.WriteLine(reportContent);
    }
    
    System.IO.FileInfo file = new System.IO.FileInfo(pathFile); 
    Response.Clear(); 
    Response.Charset="UTF-8"; 
    Response.ContentEncoding=System.Text.Encoding.UTF8; 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
    Response.AddHeader("Content-Length", file.Length.ToString());    
    Response.ContentType = "application/ms-excel";  
    Response.WriteFile(file.FullName); 
    Response.End(); 
    
    0 讨论(0)
  • 2021-01-25 06:49

    You can not directly save it to a specific clientside location. What you can do is returning the file with that request so that on the browser side a "save file" dialog pops up.

    0 讨论(0)
提交回复
热议问题