使用ClosedXML下载文件

时间:2014-03-10 09:17:31

标签: c# asp.net closedxml

所有

如何下​​载文件以便用户看到它正在下载(如使用流?)

我目前正在使用ClosedXML,但如果我使用SaveAs方法,我必须提供一个硬编码的URL,如果我只提供文件名,它就不会自动下载到下载文件夹。

下面的方法效果很好,但我必须创建自己的excel文件,该文件基于HTML,并且文件增长得太大,当我使用ClosedXML文件只有50%或更少时,代码大小如下: 但是,下载行为是我希望的。

有没有办法可以转换下面的代码,以便我可以将我的'工作簿'作为对象,它只是下载这个工作簿?

HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";    
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState =false;   
System.IO.StringWriter  tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();

由于

7 个答案:

答案 0 :(得分:30)

SaveAs()方法支持流,因此要将ClosedXml工作簿作为我使用的流获取:

public Stream GetStream(XLWorkbook excelWorkbook)
{
    Stream fs = new MemoryStream();
    excelWorkbook.SaveAs(fs);
    fs.Position = 0;
    return fs;
}

然后下载文件:

string myName = Server.UrlEncode(ReportName + "_" + DateTime.Now.ToShortDateString() + ".xlsx");
MemoryStream stream = GetStream(ExcelWorkbook);

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + myName);
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(stream.ToArray());
Response.End();

答案 1 :(得分:13)

旧线程,但我无法让公认的解决方案正常工作。我想到了更多的搜索,这对我来说非常有用:

use Symfony\Component\Console\Output\Output;

class IndentedOutput extends Output
{
    const INDENT_AMOUNT = 4;

    private $output;
    private $indentLevel = 0;
    private $resetLine = false;

    public function setOutput(OutputInterface $output)
    {
        $this->output = $output;
    }

    public function increaseLevel()
    {
        $this->indentLevel += 1;
    }

    public function decreaseLevel()
    {
        $this->indentLevel -= 1;
    }

    public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
    {
        $prependBy = str_repeat(' ', self::INDENT_AMOUNT * $this->indentLevel);

        if ($newline) {
            $this->resetLine = true;
            $messages = $prependBy.$messages;
        }

        if ($this->resetLine && !$newline) {
            $messages = $prependBy.$messages;
            $this->resetLine = false;
        }

        $this->output->write($messages, $newline, $type);
    }

    public function doWrite($message, $newline)
    {
        $this->output->doWrite($message, $newline);
    }
}

答案 2 :(得分:6)

The download can be done somewhat simpler and shorter, so the complete action in your controller could look like this - the download part is just one line instead of seven to ten

public ActionResult XLSX()
{
    System.IO.Stream spreadsheetStream = new System.IO.MemoryStream();
    XLWorkbook workbook = new XLWorkbook();
    IXLWorksheet worksheet = workbook.Worksheets.Add("example");
    worksheet.Cell(1, 1).SetValue("example");
    workbook.SaveAs(spreadsheetStream);
    spreadsheetStream.Position = 0;

    return new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "example.xlsx" };
}

答案 3 :(得分:0)

public ActionResult SendFile()
{
    // Create the workbook
    XLWorkbook workbook = new XLWorkbook();
    workbook.Worksheets.Add("Sample").Cell(1, 1).SetValue("Hello World");

    // Send the file
    MemoryStream excelStream = new MemoryStream();
    workbook.SaveAs(excelStream);
    excelStream.Position = 0;
    return File(excelStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "MyFileName.xlsx");
}

答案 4 :(得分:0)

如果使用Asp.Net MVC,基本相同但略微整洁(我认为如此:)。

public ActionResult DownloadFile(XXXModel model)
{
    using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
    {
        // create worksheets etc..

        // return 
        using (var stream = new MemoryStream())
        {
            workbook.SaveAs(stream);
            stream.Flush();

            return new FileContentResult(stream.ToArray(),
                   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                   {
                       FileDownloadName = "XXXName.xlsx"
                   };
        }
    }

答案 5 :(得分:0)

如果您使用的是MVC,只需使用File()方法,如下所示:

using (XLWorkbook wb = new XLWorkbook())
 {
  //here you put your data in the WorkBook
  //then create a Memory Stream object
using (MemoryStream stream = new MemoryStream())
  {
   //save the workbook as a MemoryStream
     wb.SaveAs(stream);

   //use the File method to return a File
   return File(stream.ToArray(), "filetype","fileName.extension");
   }
 }

答案 6 :(得分:-1)

//Method for Export Excel using Closed Xml
public void ExportDataWithClosedXml_Method(DataTable table, string tabName, string fileType)
{
    var workbook = new XLWorkbook();
    var ws = workbook.Worksheets.Add(table, tabName);
    int row = 2 + table.Rows.Count;
    int col = table.Columns.Count;
    var redRow = ws.Row(1);
    //redRow.Style.Fill.BackgroundColor = XLColor.Red;
    redRow.InsertRowsAbove(1);
    ws.Cell(1, 1).Value = "Name of Report Type";
    ws.Cell(1, 1).Style.Font.Bold = true;
    ws.Table(0).ShowAutoFilter = false;
    //ws.Row(2).Style.Fill.BackgroundColor = XLColor.Red;
    ws.Range(2, 1, 2, col).Style.Fill.BackgroundColor = XLColor.Green;
    ws.Range(2, 1, 2, col).Style.Font.Bold = true;
    ws.Range(3, 1, row, col).Style.Font.Italic = true;

    HttpContext.Current.Response.Clear();
    using (MemoryStream memoryStream = new MemoryStream())
    {
        workbook.SaveAs(memoryStream);
        memoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
        memoryStream.Close();
    }
    if (fileType == "xlsx")
    {
        HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=\"Samplefile.xlsx\"");
    }
    else
    {
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=\"Samplefile.xls\"");
    }
    HttpContext.Current.Response.End();
}