C#实现Excel导出

Excel导出广泛应用于管理、统计、金融等众多领域。

C#实现Excel导出需要引用Aspose.Cells。

Aspose.Cells下载链接
提取码:2n1u

Excel导出方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aspose.Cells;
using System.Data;/// <summary>
/// Excel 文件操作
/// </summary>
public class ExcelFile
{/// <summary>/// 获取工作本/// </summary>/// <returns>Workbook</returns>public static Workbook GetWorkBook(){Workbook workbook = new Workbook(); //工作簿 return workbook;}/// <summary>/// DataTable转Excel/// </summary>/// <param name="workbook">工作薄</param>/// <param name="dataTable">datatable 数据源</param>/// <param name="tableName">表格名称</param>/// <param name="exStyle">excel 整体样式</param>/// <returns>MemoryStream</returns>public static System.IO.MemoryStream OutStream(Workbook workbook, DataTable dataTable, string tableName, ExcelStyle exStyle){Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cells = sheet.Cells;//单元格 int Colnum = dataTable.Columns.Count;//表格列数 int Rownum = dataTable.Rows.Count;//表格行数 int index = 0;if (tableName != null){//生成行1 标题行    cells.Merge(0, 0, 1, Colnum);//合并单元格 cells[index, 0].PutValue(tableName);//填写内容 if (exStyle.TableNameStyle != null){cells[index, 0].SetStyle(exStyle.TableNameStyle);}index++;cells.SetRowHeight(0, 38);}//生成行2 列名行 for (int i = 0; i < Colnum; i++){cells[index, i].PutValue(dataTable.Columns[i].ColumnName);if (exStyle.TitleStyle != null){cells[index, i].SetStyle(exStyle.TitleStyle);}cells.SetRowHeight(index, 25);}index++;//生成数据行 for (int i = 0; i < Rownum; i++){for (int k = 0; k < Colnum; k++){cells[index + i, k].PutValue(dataTable.Rows[i][k].ToString());if (exStyle.ContentStyle != null){cells[index + i, k].SetStyle(exStyle.ContentStyle);}}cells.SetRowHeight(index + i, 24);}int columnCount = cells.MaxColumn;  //获取表页的最大列数int rowCount = cells.MaxRow;        //获取表页的最大行数for (int col = 0; col < columnCount; col++){sheet.AutoFitColumn(col, 0, rowCount);}//for (int col = 0; col < columnCount; col++)//{//    cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30);//}System.IO.MemoryStream ms = workbook.SaveToStream();return ms;}/// <summary>/// DataTable转Excel/// </summary>/// <param name="workbook">工作薄</param>/// <param name="dataTable">datatable 数据源</param>/// <param name="tableName">表格名称</param>/// <param name="reportInfo">报表信息</param>/// <param name="exStyle">excel 整体样式</param>/// <returns>MemoryStream</returns>public static System.IO.MemoryStream OutStream(Workbook workbook, DataTable dataTable, string tableName, string reportInfo, ExcelStyle exStyle){Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cells = sheet.Cells;//单元格 int Colnum = dataTable.Columns.Count;//表格列数 int Rownum = dataTable.Rows.Count;//表格行数 int index = 0;if (tableName != null){//生成行1 标题行    cells.Merge(0, 0, 1, Colnum);//合并单元格 cells[index, 0].PutValue(tableName);//填写内容 if (exStyle.TableNameStyle != null){cells[index, 0].SetStyle(exStyle.TableNameStyle);}index++;cells.SetRowHeight(0, 38);}if (reportInfo != null){//生成行2 标题行    cells.Merge(1, 0, 1, Colnum);//合并单元格 cells[index, 0].PutValue(reportInfo);//填写内容 if (exStyle.TableNameStyle != null){cells[index, 0].SetStyle(exStyle.ReprotInfoStyle);}index++;cells.SetRowHeight(0, 38);}//生成行1/2/3 列名行 for (int i = 0; i < Colnum; i++){cells[index, i].PutValue(dataTable.Columns[i].ColumnName);if (exStyle.TitleStyle != null){cells[index, i].SetStyle(exStyle.TitleStyle);}cells.SetRowHeight(index, 25);}index++;//生成数据行 for (int i = 0; i < Rownum; i++){for (int k = 0; k < Colnum; k++){cells[index + i, k].PutValue(dataTable.Rows[i][k].ToString());if (exStyle.ContentStyle != null){cells[index + i, k].SetStyle(exStyle.ContentStyle);}}cells.SetRowHeight(index + i, 24);}int columnCount = cells.MaxColumn;  //获取表页的最大列数int rowCount = cells.MaxRow;        //获取表页的最大行数for (int col = 0; col < columnCount; col++){sheet.AutoFitColumn(col, 0, rowCount);}for (int col = 0; col < columnCount; col++){cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30);}System.IO.MemoryStream ms = workbook.SaveToStream();return ms;}
}/// <summary>
/// Excle样式定义类
/// </summary>
public class ExcelStyle
{private Style tableNameStyle;/// <summary>/// 表名称样式/// </summary>public Style TableNameStyle{get { return tableNameStyle; }set { tableNameStyle = value; }}private Style reprotInfoStyle;/// <summary>/// 统计报表信息样式/// </summary>public Style ReprotInfoStyle{get { return reprotInfoStyle; }set { reprotInfoStyle = value; }}private Style titleStyle;/// <summary>/// 表头样式/// </summary>public Style TitleStyle{get { return titleStyle; }set { titleStyle = value; }}private Style contentStyle;/// <summary>/// 表格类容样式/// </summary>public Style ContentStyle{get { return contentStyle; }set { contentStyle = value; }}
}

调用方法

public void Excel(){DataTable dataTable = dataTable ();//执行SQL语句返回DataTableAspose.Cells.Workbook workbook = ExcelFile.GetWorkBook();//为标题设置样式Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中styleTitle.Font.Name = "宋体";//文字字体styleTitle.Font.Size = 18;//文字大小styleTitle.Font.IsBold = true;//粗体//样式2Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中style2.Font.Name = "宋体";//文字字体style2.Font.Size = 14;//文字大小style2.Font.IsBold = true;//粗体style2.IsTextWrapped = false;//单元格内容自动换行style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//样式3Aspose.Cells.Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中style3.Font.Name = "宋体";//文字字体style3.Font.Size = 12;//文字大小style3.IsTextWrapped = false;style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;ExcelStyle exStyle = new ExcelStyle();exStyle.ContentStyle = style3;exStyle.TitleStyle = style2;exStyle.TableNameStyle = styleTitle;System.IO.MemoryStream ms = ExcelFile.OutStream(workbook, dataTable, "Excel文件名", exStyle);HttpResponse response = HttpContext.Current.Response;response.Clear();response.Buffer = true;response.Charset = "utf-8";response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode("Excel文件名.xls"));response.ContentEncoding = System.Text.Encoding.UTF8;response.ContentType = "application/ms-excel; charset=UTF-8 ";response.BinaryWrite(ms.ToArray());response.End();
}

例:

执行SQL语句如下:

导出至Excel文件信息如下:

C#实现Excel导出相关推荐

  1. Excel向数据库插入数据和数据库向Excel导出数据

    为了熟悉java里工作簿的相关知识点,所以找了"Excel向数据库插入数据和数据库向Excel导出数据"的功能来实现. 注意事项:1,mysql数据库: 2,需要导入的jar包有 ...

  2. C# 中Excel导出,可以自由设置导出的excel格式

    Excel导出,不管在java,C#等后台语言,或者是javascrit,jquery等脚本语言,有很多种方式都可以将查出的数据导成excel的格式.我这次是从公司的一个同事那里学来的一个方法.是有关 ...

  3. 数据库数据用Excel导出的3种方法

    数据库数据用Excel导出的3种方法 分类: .Net 2008-06-30 11:07 173人阅读 评论(2) 收藏 举报 将数据库数据用Excel导出主要有3种方法:用Excel.Applica ...

  4. php phppowerpoint 生成表格_php之EXCEL导出代码生成器的实现思路

    背景: 在实际工作中经常会遇到将数据导出到excel这样的需求,对于php语言来说导出excel也不是什么难事,因为借助phpoffice之phpspreadsheet开源库可以轻松实现.有过导出EX ...

  5. C#中用NPOI的excel导出

    //机构表导出 private static List<User2> amininf = new BLL.Bll().GetUser2s(); //定义数据源导出对象 #region 导出 ...

  6. php用Simple Excel导出xls

    因为前几天写了篇文章,用php-excel-reader类导入excel内容,顺便说些excel导出问题,我用的是simple excel,一个很简单的导出xls类,特好用! simple excel ...

  7. ASP excel导出/导入Access数据库(代码+实例下载)

    Excel导出函数 <% Sub   ExportToExcel Response.ContentType   =   "application/vnd.ms-Excel"  ...

  8. 记一次悲惨的 Excel 导出事件

    背景 话说这个背景挺惨的,京东某系统使用了poi-ooxml-3.5-final做excel导出功能.起初使用该版本的poi的HSSF配合多线程生成excel,没有任何问题,后来改成了XSSF生成后上 ...

  9. python数据导出excel_python 数据生成excel导出(xlwt,wlsxwrite)代码实例

    这篇文章主要介绍了python 数据生成excel导出(xlwt,wlsxwrite)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 话不多 ...

  10. Apache POI操作Excel导出JAVABEAN对象方法

    2019独角兽企业重金招聘Python工程师标准>>> Apache POI操作Excel导出方法说明 Apache的POI组件是Java操作Microsoft Office办公套件 ...

最新文章

  1. 相机标定:(1)相机模型
  2. U3D的飞船太空射击例子中,使用coroutine
  3. ReactJS入门之ReactJS简介
  4. Linux启动容器端口,docker安装 创建镜像 启动容器 的 使用方法
  5. Webpack打包工具的使用---打包引用JS和CSS文件
  6. 好用的 windows10 软件推荐
  7. 最全PR曲线、ROC曲线以及AUC计算公式详解
  8. 常用的八个思维导图工具
  9. linux物理接口数据结构,Linux Regulator Framework(1)_概述
  10. 嵌入式设备中OpenCV的移植
  11. Tablespace free space
  12. wlacm一笔画问题(图的遍历) 题解
  13. Linux内核中的Watchdog
  14. python如何设置rgb颜色_Python 转换RGB颜色值的示例代码
  15. 概率论与数理统计学习笔记——第13讲——依概率收敛的意义
  16. 什么是Oracle AOL
  17. EP-PEG2000-EP,具有两个环氧基团的线性双功能PEG,EP-PEG2000-Epoxide
  18. 基于Castle ActiveRecord开发Domain Model详解(一)对象关系到数据表的映射
  19. 怎么用stata打开dta文件_用shell命令打开任意格式的文件
  20. Nexus5X Android 安卓 6.0 刷 8.0.1 root

热门文章

  1. bootstrap table背景_bootstrap table给行怎么加背景色
  2. 【爬虫】获取新郑机场出租车实时数据
  3. 遗传算法原理及其matlab程序实现
  4. MySQL数据库优化的八种方式(总结)
  5. Ansible自动化运维
  6. java删除图片杂色_PS中级教程!手把手教你绘制霸气酷炫的暗黑3壁纸
  7. 微信开发者工具下载安装教程
  8. PUBG - 罗技鼠标宏 | 兴趣使然的项目,完虐收费宏!点个Star支持一下作者!
  9. 使用ajax实现文件上传功能
  10. Hadoop开发环境搭建