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. Leetcode:Substring with Concatenation of All Words分析和实现

    题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...

  2. c++多线程编程(一)

    C++本身并没有提供任何多线程机制,但是在windows下,我们可以调用SDK win32 api来编写多线程的程序,下面就此简单的讲一下: 创建线程的函数 HANDLE CreateThread( ...

  3. 717. 1-bit and 2-bit Characters最后一位数是否为0

    [抄题]: We have two special characters. The first character can be represented by one bit 0. The secon ...

  4. Luogu 3261 [JLOI2015]城池攻占

    BZOJ 4003 需要实现一个可并堆. 每个点维护一个小根堆,然后一开始把所有骑士加入到它所在的点的小根堆当中,实际上空间是$O(m)$的,然后我们从上到下不断合并这个小根堆,合并完之后如果遇到堆顶 ...

  5. Python基础 之列表、字典、元组、集合

    基础数据类型汇总 一.列表(list) 例如:删除索引为奇数的元素 lis=[11,22,33,44,55] #第一种: for i in range(len(lis)): if i%2==1: de ...

  6. Python字符编码详解,str,bytes

    什么是明文 “明文”是可以是文本,音乐,可以编码成mp3文件.明文可以是图像的,可以编码为gif.png或jpg文件.明文是电影的,可以编码成wmv文件.不一而足. 什么是编码?把明文变成计算机语言 ...

  7. XE StringGrid应用(G1属性触发G2)

    unit UnitMain; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System. ...

  8. 新编html网页设计从入门到精通 (龙马工作室) pdf扫描版​

    新编html网页设计从入门到精通共分为21章,全面系统地讲解了html的发展历史及4.0版的新特性.基本概念.设计原则.文件结构.文件属性标记.用格式标记进行页面排版.使用图像装饰页面.超链接的使用. ...

  9. android IntentService和ResultReceiver的异步处理

    IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...

  10. 001.linux的基础优化(期中架构方面的优化)

    1. linux内核优化 第一步 cat >>/etc/sysctl.conf<<EOF net.ipv4.tcp_fin_timeout = 2 net.ipv4.tcp_t ...