1.支持导出多个sheet文件

  /// <summary>
/// 导出到Excel并下载(html)
/// </summary>
/// <param name="tablels">需要导出的Excel表集合</param>
public static void ToExcel(List<DataTable> tablels, string fileName = "")
{ if (fileName == "")
{
fileName = "数据导出汇总-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称
}
string urlPath = "UpFiles/ExcelFiles/" + fileName; // 文件下载的URL地址,供给前台下载 string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路径 string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
IWorkbook workBook = new HSSFWorkbook(); foreach (DataTable table in tablels)
{ string sheetName = table.TableName;
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
sheetName = string.IsNullOrEmpty(sheetName) ? "sheet1" : sheetName;
ISheet sheet = workBook.CreateSheet(sheetName); //处理表格标题
IRow row = sheet.CreateRow();
row.CreateCell().SetCellValue(sheetName);
sheet.AddMergedRegion(new CellRangeAddress(, , , table.Columns.Count - ));
row.Height = ; ICellStyle cellStyle = workBook.CreateCellStyle();
IFont font = workBook.CreateFont();
font.FontName = "微软雅黑";
font.FontHeightInPoints = ;
cellStyle.SetFont(font);
cellStyle.VerticalAlignment = VerticalAlignment.Center;
cellStyle.Alignment = HorizontalAlignment.Center;
row.Cells[].CellStyle = cellStyle; //处理表格列头
row = sheet.CreateRow();
for (int i = ; i < table.Columns.Count; i++)
{
row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
row.Height = ;
sheet.AutoSizeColumn(i);
} //处理数据内容
for (int i = ; i < table.Rows.Count; i++)
{
row = sheet.CreateRow( + i);
row.Height = ;
for (int j = ; j < table.Columns.Count; j++)
{
row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
sheet.SetColumnWidth(j, * );
}
}
//写入数据流
workBook.Write(fs);
fs.Flush();
fs.Close();
}
// 3.进行Excel转换操作,并返回转换的文件下载链接
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Write(js.Serialize(urlPath)); // 返回Json格式的内容
}

2.只支持一个sheet文件

  /// <summary>
/// 导出到Excel并下载(html)
/// </summary>
/// <param name="table">数据源</param>
/// <param name="title">标题</param>
/// <param name="sheetName">sheetName名称</param>
/// <param name="fileName">路径</param>
public static void ToExcel(DataTable table, string title, string sheetName, string fileName = "")
{
if (fileName == "")
{
fileName = sheetName + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称
}
string urlPath = "UpFiles/ExcelFiles/" + fileName; // 文件下载的URL地址,供给前台下载 string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路径 string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
} FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
IWorkbook workBook = new HSSFWorkbook();
sheetName = string.IsNullOrEmpty(sheetName) ? "sheet1" : sheetName;
ISheet sheet = workBook.CreateSheet(sheetName); //处理表格标题
IRow row = sheet.CreateRow();
row.CreateCell().SetCellValue(title);
sheet.AddMergedRegion(new CellRangeAddress(, , , table.Columns.Count - ));
row.Height = ; ICellStyle cellStyle = workBook.CreateCellStyle();
IFont font = workBook.CreateFont();
font.FontName = "微软雅黑";
font.FontHeightInPoints = ;
cellStyle.SetFont(font);
cellStyle.VerticalAlignment = VerticalAlignment.Center;
cellStyle.Alignment = HorizontalAlignment.Center;
row.Cells[].CellStyle = cellStyle; //处理表格列头
row = sheet.CreateRow();
for (int i = ; i < table.Columns.Count; i++)
{
row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
row.Height = ;
sheet.AutoSizeColumn(i);
} //处理数据内容
for (int i = ; i < table.Rows.Count; i++)
{
row = sheet.CreateRow( + i);
row.Height = ;
for (int j = ; j < table.Columns.Count; j++)
{
row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
sheet.SetColumnWidth(j, * );
}
} //写入数据流
workBook.Write(fs);
fs.Flush();
fs.Close(); // 3.进行Excel转换操作,并返回转换的文件下载链接
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Write(js.Serialize(urlPath)); // 返回Json格式的内容
}

关于NPOIExcel导出excel的更多相关文章

  1. Asp.Net 使用Npoi导出Excel

    引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...

  2. Web C# 导出Excel 方法总结

    方法1:微软推荐服务器需安装Excel型 依赖: 软件:Office Excel 2007-2013 引用:Microsoft Office 14.0 Object Library 1.1 数据准备 ...

  3. asp.net mvc4 easyui datagrid 增删改查分页 导出 先上传后导入 NPOI批量导入 导出EXCEL

    效果图 数据库代码 create database CardManage use CardManage create table CardManage ( ID ,) primary key, use ...

  4. NPOI导出excel(带图片)

    近期项目中用到Excel导出功能,之前都是用普通的office组件导出的方法,今天尝试用下NPOI,故作此文以备日后查阅. 1.NPOI官网http://npoi.codeplex.com/,下载最新 ...

  5. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

  6. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  7. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

  8. 利用poi导出Excel

    import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...

  9. [django]数据导出excel升级强化版(很强大!)

    不多说了,原理采用xlwt导出excel文件,所谓的强化版指的是实现在网页上选择一定条件导出对应的数据 之前我的博文出过这类文章,但只是实现导出数据,这次左思右想,再加上网上的搜索,终于找出方法实现条 ...

随机推荐

  1. SQLServer中的事物与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

  2. 【bzoj1025】[SCOI2009]游戏

    1025: [SCOI2009]游戏 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1987  Solved: 1289[Submit][Status] ...

  3. 【总结整理】webGIS学习thinkGIS(四)WebGIS中通过行列号来换算出多种瓦片的URL 之离线地

    http://www.thinkgis.cn/topic/541a5319da8db186fd06e097 1.前言 在前面我花了两个篇幅来讲解行列号的获取,也解释了为什么要获取行列号.在这一章,我将 ...

  4. ubuntu14.04 使用笔记

    这是第二次安装使用ubuntu了,虽然上一次因为不习惯和不会使用一两天就放弃了,这次坚持的时间稍微长一点,目前ubuntu的基本使用也熟悉了.但是由于ubuntu上的应用太少,常用软件,比如QQ,Ph ...

  5. SpringBoot:阿里数据源配置、JPA显示sql语句、格式化JPA查询的sql语句

    1 数据源和JPA配置 1.1 显示sql配置和格式化sql配置 者两个配置都是属于hibernate的配置,但是springdatajpa给我们简化了:所有hibernate的配置都在jpa下面的p ...

  6. Python程序调试-TabError: inconsistent use of tabs and spaces in indentation

    报错信息:TabError: inconsistent use of tabs and spaces in indentation 说明:代码缩进统一使用Tab键或空格键,不能混用. 解决办法: 1. ...

  7. 正确理解WPF中的TemplatedParent (转贴)

    http://blog.csdn.net/idebian/article/details/8761388 (注:Logical Tree中文称为逻辑树,Visual Tree中文称为可视化树或者视觉树 ...

  8. Codeforces 12D Ball(线段树)

    N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty ...

  9. Android下拉选择框之PopupWindow

    1.效果图 2.思路分析 1.点击弹出对话框 popupwindow 2.对popupwindow进行相关设置,popupwindow中设置view为listview 3.listview中item设 ...

  10. 在普通的"类库"项目中添加 WPF 的 Window 对象

    最近开发一个 WPF 项目, 在此项目中有个类库工程, 在开发的过程中发现在类库工程中竟然添加不了 WPF 窗口对象和一些其他的 WPF 对象,在新建窗口中选 WPF 类型,只有一个 “用户控件(WP ...