使用ClosedXML导出excel(且设置其带有时间戳的导出名字)
2017-09-08 17:47
357 查看
前段时间项目里有个关于报表变更,将导出的报表名要带有yyyyMMdd这种的时间戳。整理了一个简单关于ClosedXML的小demo:
1. 首先要在项目中引入两个library:ClosedXML.dll 和DocumentFormat.OpenXml.dll
2. 在项目代码中加入引用:usingClosedXML.Excel;
3. demo方法(在代码的最后部分使用了Response来设置export出excel的名字,当时修改这段代码时候出现了上下文不存在Response的错误,解决办法请参照:解决办法:The
name 'Response' does not exist in the current context 本文代码已经解决该异常):
注:本文原创由`bluetata`发布于blog.csdn.net、转载请务必注明出处。
1. 首先要在项目中引入两个library:ClosedXML.dll 和DocumentFormat.OpenXml.dll
2. 在项目代码中加入引用:usingClosedXML.Excel;
3. demo方法(在代码的最后部分使用了Response来设置export出excel的名字,当时修改这段代码时候出现了上下文不存在Response的错误,解决办法请参照:解决办法:The
name 'Response' does not exist in the current context 本文代码已经解决该异常):
/// <summary> /// Export an Excel using ClosedXML /// http://blog.csdn.net/dietime1943 /// </summary> /// <returns>Returns a byte array object.</returns> public byte[] FunctionDownload() { string[] ReportColumn = { "A","B" }; // A,B column string[] ReportColumn_values = { "NAME", "ADDRESS" }; // VALUE string nowTimeStamp = GetTimeStamp(); string basePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; string path = basePath + Common.Constants.Template.LocalPath; string sourceFile = path + "Export2ExcelForm.xlsx"; string destinationFile = path + "ReportForm" + nowTimeStamp + ".xlsx"; File.Copy(sourceFile, destinationFile, false); using (XLWorkbook wb = new XLWorkbook(destinationFile)) { var ws = wb.Worksheet(1); int row = 1; // + result.IndexOf(resultone); for (int m = 0; m < ReportColumn.Count(); m++) { ws.Cell(row, ReportColumn[m]).Value = ReportColumn_values[m]; // A,F column } //using (var ms = new MemoryStream()) //{ // wb.SaveAs(ms); // File.Delete(destinationFile); // return ms.ToArray(); //} // 设置report保存的时候名称为: ReportName_yyyyMMdd.xlsx using (var ms = new MemoryStream()) { wb.SaveAs(ms); //return ms.ToArray(); string printdate = Calendar.GetCurrentDate().ToString("yyyyMMdd"); string myName = "ReportForm" + "_" + printdate + ".xlsx"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + myName); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; // HttpContext.Current.Response.BinaryWrite(ms.ToArray()); HttpContext.Current.Response.End(); File.Delete(destinationFile); return ms.ToArray(); } } }
注:本文原创由`bluetata`发布于blog.csdn.net、转载请务必注明出处。
相关文章推荐
- 导出Excel之Epplus使用教程4(其他设置)
- 在使用 ADO.NET 导出 Excel 文件时,设置 IMEX=1 仍不能导出所有数据的解决办法
- 使用ClosedXML导出Excel
- 使用jxl导出Excel文件,并且设置Excel样式
- java使用poi导出excel设置颜色问题
- 导出Excel之Epplus使用教程2(样式设置)
- 使用POI导出EXCEL 并设置格式
- AX2009使用NPOI导出EXCEL颜色设置
- 使用NPOI导出Excel 并设置Excel样式(合并单元格、行高、宽度、字体、边框、位置)
- 在使用 ADO.NET 导出 Excel 文件时,设置 IMEX=1 仍不能导出所有数据的解决办法
- 带有时间的DataFrame绘图及excel的单元格设置显示标准时间
- 使用sqlplus工具导出数据到csv文件,要求文件带有时间戳
- java 使用poi导出Excel,设置单元格保护不可编辑,设置下拉框
- AX2009使用NPOI导出EXCEL常用格式设置
- angular时间json导出为excel,使用xsls
- 使用POI设置导出的EXCEL锁定指定的单元格
- js 使用前端导出excel,设置标题和页脚,实现特殊表格需求
- 在使用 ADO.NET 导出 Excel 文件时,设置 IMEX=1 仍不能导出所有数据的解决办法
- 导出Excel之Epplus使用教程2(样式设置)
- 导出Excel之Epplus使用教程2(样式设置)