2016-11-13 147 views
0

我有一个报告模板: enter image description here数据导出到Excel模板C#

我应该如何将数据导出到Excel文件相同的图像。

+0

此图像是您想要导出还是存储在数据库某处的数据。阅读本文http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp?rq=1 – Adrian

+0

您的问题将受益于更多的细节。特别是关于如何生成用于下载的数据。 –

回答

0

您不应该在有时被视为解决方案的服务器上安装Excel。一种简单的方法是将下载创建为csv文件。这将在客户端计算机上的Excel中打开。下面的方法告诉您如何做到这一点:

public ActionResult DownloadSelectedReport(int ReportID) 
{ 
    string filename = String.Format("DownloadList{0:yyMMdd}.csv", DateTime.Today); 
    MemoryStream memStream = new MemoryStream(); 
    UnicodeEncoding uniEncoding = new UnicodeEncoding(); 
    byte[] reportString = uniEncoding.GetBytes(BuildReportAsCSV(ReportID)); 
    memStream.Write(reportString, 0, reportString.Length); 
    return File(memStream.ToArray(), "application/vnd.ms-excel", Server.UrlEncode(filename)); 
} 

使用BuildReportAsCSV(ReportID)生成您下载的报告。