DataTable 导出到Excel

简介: 代码如下:   #region DataTable 导出到Excel/// Author: jy Wang/// Date: 2008-7-28/// /// DataTable 导出到Excel/// /// 列名数组/// 列宽数组/// 报表标题/// 导出文件名/// 图标的位置...

代码如下:

 
#region DataTable 导出到Excel
/// Author: jy Wang
/// Date: 2008-7-28
/// <summary>
/// DataTable 导出到Excel
/// </summary>
/// <param name="colName">列名数组</param>
/// <param name="colWidth">列宽数组</param>
/// <param name="reprotTitle">报表标题</param>
/// <param name="exprortFile">导出文件名</param>
/// <param name="logoPosition">图标的位置</param>
/// <param name="table">要导出的datatable</param>
/// <param name="response">HttpResponse</param>
public void Export(string[] colName, int[] colWidth, string reprotTitle, string exprortFile, string logoPosition, System.Data.DataTable table, HttpResponse response, HttpRequest request)
{
string fileName = "" + exprortFile + ".xls";//客户端保存的文件名 
string filePath = request.PhysicalApplicationPath +"Exported\\" + exprortFile + ".xls";//路径 

if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}

GC.Collect();
Excel.Application excel = new Excel.Application();
Excel.Workbook book = excel.Workbooks.Add(Missing.Value);
Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;
// 
//取得标题 
// 
for (int col = 0; col < table.Columns.Count; col++)
{

sheet.Cells[5, col + 1] = colName[col];
sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; //设置标题格式为居中对齐 
sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).Interior.ColorIndex = 15; //设置为浅灰色,共计有56种 
sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).ColumnWidth = colWidth[col];
sheet.get_Range(excel.Cells[5, col + 1], excel.Cells[5, col + 1]).RowHeight = 25;
}

// 
//取得数据 
// 

for (int row = 0; row < table.Rows.Count; row++)
{
for (int col = 0; col < table.Columns.Count; col++)
{
sheet.Cells[row + 7, col + 1] = table.Rows[row][col].ToString();
}
}

// 
//取得整个报表的标题 
// 
excel.Cells[2, 5] = reprotTitle;
// 
//设置整个报表的标题格式 
// 
sheet.get_Range(excel.Cells[2, 5], excel.Cells[2, 5]).Font.Bold = true;
sheet.get_Range(excel.Cells[2, 5], excel.Cells[2, 5]).Font.Size = 22;


//设置内容字体
sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count + 1]).Font.Size = 9;
sheet.get_Range(excel.Cells[5, 1], excel.Cells[5, table.Columns.Count + 1]).Font.Bold = true;

//插入图标
try
{
sheet.Shapes.AddPicture(logoPosition, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 5, 60, 60);
}
catch (Exception ee)
{
throw new Exception("Summary Report: 插入图标失败 logoPosition::" + logoPosition +"-"+ ee.Message);

}
// 
//自动换行 
// 
sheet.get_Range(excel.Cells[6, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Select();
sheet.get_Range(excel.Cells[6, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).WrapText = true;

// 
//绘制边框 
// 
sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders.LineStyle = 1;
sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置左边线加粗 
sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置上边线加粗 
sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置右边线加粗 
sheet.get_Range(excel.Cells[5, 1], excel.Cells[table.Rows.Count + 6, table.Columns.Count]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置下边线加粗 


//文件保存地点
book.Close(true, filePath, Missing.Value);
excel.Quit();
GC.Collect();

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
response.AddHeader("Content-Length", fileInfo.Length.ToString());
response.AddHeader("Content-Transfer-Encoding", "binary");
response.ContentType = "application/octet-stream";
response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
response.WriteFile(fileInfo.FullName);
response.Flush();
System.IO.File.Delete(filePath);
response.End();
}
#endregion
 
 
代码结束。
相关文章
Excel中如何批量重命名工作表与将每个工作表导出到单独Excel文件
本文介绍了如何在Excel中使用VBA批量重命名工作表、根据单元格内容修改颜色,以及将工作表导出为独立文件的方法。同时提供了Python实现导出工作表的代码示例,适用于自动化处理Excel文档。
|
数据格式 UED
记录一次NPOI库导出Excel遇到的小问题解决方案
【11月更文挑战第16天】本文记录了使用 NPOI 库导出 Excel 过程中遇到的三个主要问题及其解决方案:单元格数据格式错误、日期格式不正确以及合并单元格边框缺失。通过自定义单元格样式、设置数据格式和手动添加边框,有效解决了这些问题,提升了导出文件的质量和用户体验。
1196 3
|
前端开发
实现Excel文件和其他文件导出为压缩包,并导入
实现Excel文件和其他文件导出为压缩包,并导入
421 1
|
JavaScript 前端开发 数据处理
Vue导出el-table表格为Excel文件的两种方式
Vue导出el-table表格为Excel文件的两种方式
1241 6
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
1190 4
|
Java API Apache
|
前端开发 JavaScript Java
导出excel的两个方式:前端vue+XLSX 导出excel,vue+后端POI 导出excel,并进行分析、比较
这篇文章介绍了使用前端Vue框架结合XLSX库和后端结合Apache POI库导出Excel文件的两种方法,并对比分析了它们的优缺点。
3116 0
如何根据Excel某列数据为依据分成一个新的工作表
在处理Excel数据时,我们常需要根据列值将数据分到不同的工作表或文件中。本文通过Python和VBA两种方法实现该操作:使用Python的`pandas`库按年级拆分为多个文件,再通过VBA宏按班级生成新的工作表,帮助高效整理复杂数据。
|
12月前
|
数据采集 数据可视化 数据挖掘
用 Excel+Power Query 做电商数据分析:从 “每天加班整理数据” 到 “一键生成报表” 的配置教程
在电商运营中,数据是增长的关键驱动力。然而,传统的手工数据处理方式效率低下,耗费大量时间且易出错。本文介绍如何利用 Excel 中的 Power Query 工具,自动化完成电商数据的采集、清洗与分析,大幅提升数据处理效率。通过某美妆电商的实战案例,详细拆解从多平台数据整合到可视化报表生成的全流程,帮助电商从业者摆脱繁琐操作,聚焦业务增长,实现数据驱动的高效运营。
|
存储 安全 大数据
网安工程师必看!AiPy解决fscan扫描数据整理难题—多种信息快速分拣+Excel结构化存储方案
作为一名安全测试工程师,分析fscan扫描结果曾是繁琐的手动活:从海量日志中提取开放端口、漏洞信息和主机数据,耗时又易错。但现在,借助AiPy开发的GUI解析工具,只需喝杯奶茶的时间,即可将[PORT]、[SERVICE]、[VULN]、[HOST]等关键信息智能分类,并生成三份清晰的Excel报表。告别手动整理,大幅提升效率!在安全行业,工具党正碾压手动党。掌握AiPy,把时间留给真正的攻防实战!官网链接:https://www.aipyaipy.com,解锁更多用法!