参考:

http://blog.csdn.net/zhouqinghe24/article/details/8649346 参考下载
http://www.cnblogs.com/dyllove98/archive/2013/08/06/3241515.html 参考多个sheet
http://www.cnblogs.com/jicheng/p/5961257.html 参考列表写入

1、nuget搜索安装Npoi

2、代码

  public class UserInfo
{
public string Name { get; set; }
public string Id { get; set; }
public string Phone { get; set; }
} /// <summary>
/// 生成excel文件到内存中
/// </summary>
/// <returns></returns>
public void CreateExcel()
{
MemoryStream ms = new MemoryStream(); List<UserInfo> listUser = new List<UserInfo>()
{
new UserInfo { Name="1", Id="1", Phone="1r" },
new UserInfo { Name="2", Id="2", Phone="2r" },
new UserInfo { Name="3", Id="3", Phone="3r" },
new UserInfo { Name="4", Id="4", Phone="4r" },
new UserInfo { Name="5", Id="5", Phone="5r" },
}; //创建工作簿对象
var workbook = new HSSFWorkbook(); #region DataTable数据
//创建工作表
ISheet sheet = workbook.CreateSheet("一个sheet");
IRow row0 = sheet.CreateRow(0);
row0.CreateCell(0).SetCellValue("用户Id");
row0.CreateCell(1).SetCellValue("用户名称");
row0.CreateCell(2).SetCellValue("用户备注信息"); var dtSource = GetDataTable();
for (int i = 0; i < dtSource.Rows.Count; i++)
{
row0 = sheet.CreateRow(i + 1);
for (int j = 0; j < dtSource.Columns.Count; j++)
{
row0.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
}
} #endregion #region list数据
ISheet sheet2 = workbook.CreateSheet("另一个sheet");
IRow row2 = sheet2.CreateRow(0);
row2.CreateCell(0).SetCellValue("用户Id2");
row2.CreateCell(1).SetCellValue("用户名称2");
row2.CreateCell(2).SetCellValue("用户备注信息2"); for (int r = 1; r < listUser.Count; r++)
{
//创建行row
IRow row = sheet2.CreateRow(r);
row.CreateCell(0).SetCellValue(listUser[r].Id);
row.CreateCell(1).SetCellValue(listUser[r].Name);
row.CreateCell(2).SetCellValue(listUser[r].Phone);
} #endregion workbook.Write(ms); ms.Flush();
ms.Position = 0; var fileName = "测试Excel" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";//xls
DownloadExcel(ms, fileName); } /// <summary>
/// 生成http流下载文件
/// </summary>
/// <param name="ms"></param>
/// <param name="fileName"></param>
private static void DownloadExcel(MemoryStream ms, string fileName)
{
#region 处理IE、火狐等浏览器文件名乱码
if (System.Web.HttpContext.Current.Request.ServerVariables["http_user_agent"].IndexOf("Firefox", StringComparison.Ordinal) != -1)
{
fileName = "=?UTF-8?B?" + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileName)) + "?=";
}
else
{
fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
fileName = fileName.Replace("+", "%20");
}
#endregion
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", ms.Length.ToString());
System.Web.HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream;charset=utf-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
} /// <summary>
/// 模拟DataTable
/// </summary>
/// <returns></returns>
public DataTable GetDataTable()
{
DataTable tblDatas = new DataTable("Datas");
DataColumn dc = null; dc = tblDatas.Columns.Add("Name", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Id", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Phone", Type.GetType("System.String")); DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Name"] = "大话西游";
newRow["Id"] = "2.0";
newRow["Phone"] = "我很喜欢";
tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow();
newRow["Name"] = "梦幻西游";
newRow["Id"] = "3.0";
newRow["Phone"] = "比大话更幼稚";
tblDatas.Rows.Add(newRow); return tblDatas;
}

Npoi List DataTable导出一个Excel多个sheet 下载的更多相关文章

  1. NPOI使用Datatable导出到Excel

    首先要引用dll 下载地址:http://pan.baidu.com/s/1dFr2m 引入命名空间: using NPOI.HSSF.UserModel;using NPOI.SS.UserMode ...

  2. [转].net 使用NPOI或MyXls把DataTable导出到Excel

    本文转自:http://www.cnblogs.com/yongfa365/archive/2010/05/10/NPOI-MyXls-DataTable-To-Excel-From-Excel.ht ...

  3. NPOI通过DataTable导出和读取Excel

    Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你得 ...

  4. DataTable导出到Excel

    简单的导出到Excel中: 代码如下: using System; using System.Collections.Generic; using System.Data; using System. ...

  5. DataTable 导出到 Excel 类

    底层类: #region DataTable 导出到 Excel /// <summary> /// DataTable 导出到 Excel /// </summary> // ...

  6. c# DataTable导出为excel

    /// <summary> /// 将DataTable导出为Excel文件(.xls) /// </summary> /// <param name="dt& ...

  7. C# datatable 导出到Excel

    datatable导出到Excel /// <summary> /// 将DataTable导出为Excel文件(.xls) /// </summary> /// <pa ...

  8. csv/json/list/datatable导出为excel的通用模块设计

    导出excel的场景我一般都是一个List直接导出成一张sheet,用Npoi.Mapper库很方便,最近我经常是需要将接口返回的jsonarray转成一张excel表,比如从elasticsearc ...

  9. DataTable导出到Excel(.NET 4.0)

    最近在论坛里又看到很多关于DataTable(DataSet)导入Excel的帖子,我也温故知新一下,用VS2010重新整理了一个Sample.这个问题简化一下就是内存数据到文件,也就是遍历赋值,只不 ...

随机推荐

  1. c# 设置自动隐藏任务栏、获取状态

    from: http://stackoverflow.com/questions/1381821/how-to-toggle-switch-windows-taskbar-from-show-to-a ...

  2. iis 更改asp.net 版本设置

    参考来源: https://github.com/neo2018/ZYFC/blob/2e20009097c1e837a6e667a3dffd4224e28f4411/MderFc/Classes/I ...

  3. Winfrom中如何使用Iconfont字体图标补充

    1.参考技术文章:http://www.cnblogs.com/isaboy/p/csharp_Font_Awesome_window_form_icon.html 2.参考后主要纠结在图标的unic ...

  4. jsp的session完成登陆功能

    login.jsp: <%@ page language="java" import="java.util.*" contentType="te ...

  5. aix操作系统的版本中TL SP 含义

    AIX 分为四个主要的操作系统级别:版本.发行版.技术级 (TL) 和服务包 (SP).版本和发行版通常指的是 AIX 的名称,例如AIX 7.1.TL 是包含重大更新的操作系统的发行版,而 SP 包 ...

  6. webkit内核的浏览器为什么removeAttribute('style')会失效?

    做了一些研究,应该算是理清了问题. 首先,我们在这里常说的「属性」(attributes)其实分为两种:内容属性(content attributes)以及 IDL 属性(IDL attributes ...

  7. ZOJ-3230-Solving the Problems

    /*ZOJ Problem Set - 3230Solving the Problems ------------------------------------------------------- ...

  8. [译]作为一个web开发人员,哪些技术细节是在发布站点前你需要考虑到的

    前日在cnblogs上看到一遍文章<每个程序员都必读的12篇文章>,其中大多数是E文的. 先译其中一篇web相关的”每个程序员必知之WEB开发”. 原文: http://programme ...

  9. Nginx代理配置文件

    #user nginx; worker_processes 5; #error_log /var/log/nginx/error.log warn; #pid /var/run/nginx.pid; ...

  10. CSS控制文字只显示一行 超出部分显示省略号

         <p style="width: 120px; height: 50px; border: 1px solid blue; white-space: nowrap; over ...