asp.net 导出excel--NPOI
1.使用OLEDB导出Excel ,这种方式有点慢,慎用
/// <summary>
/// 使用OLEDB导出Excel
/// </summary>
/// <param name="dt"></param>
/// <param name="filepath"></param>
/// <param name="tablename"></param> public static void Export(System.Data.DataTable dt, string filepath, string tablename)
{ //excel 2003格式 // string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;"; //Excel 2007格式 string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0 Xml;"; try
{ using (OleDbConnection con = new OleDbConnection(connString))
{
con.Open();
StringBuilder strSQL = new StringBuilder();
strSQL.Append("CREATE TABLE ").Append("[" + tablename + "]"); strSQL.Append("(");
for (int i = ; i < dt.Columns.Count; i++)
{
strSQL.Append("[" + dt.Columns[i].ColumnName + "] text,"); }
strSQL = strSQL.Remove(strSQL.Length - , );
strSQL.Append(")"); OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con);
cmd.ExecuteNonQuery();
for (int i = ; i < dt.Rows.Count; i++)
{
strSQL.Clear(); StringBuilder strfield = new StringBuilder(); StringBuilder strvalue = new StringBuilder(); for (int j = ; j < dt.Columns.Count; j++)
{ strfield.Append("[" + dt.Columns[j].ColumnName + "]"); strvalue.Append("'" + dt.Rows[i][j].ToString() + "'"); if (j != dt.Columns.Count - )
{ strfield.Append(","); strvalue.Append(","); } else
{ } } cmd.CommandText = strSQL.Append(" insert into [" + tablename + "]( ") .Append(strfield.ToString()) .Append(") values (").Append(strvalue).Append(")").ToString(); cmd.ExecuteNonQuery(); } con.Close(); } Console.WriteLine("OK"); } catch (Exception ex)
{ Console.WriteLine(ex.Message); } }
2.NPOI 导出excel
2007 版
public FileResult Export(SC_ServiceCardUsedRecordSearchParam param)
{ int cou = ; //创建Excel文件的对象
//NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();// NPOI.XSSF.UserModel.XSSFWorkbook book = new NPOI.XSSF.UserModel.XSSFWorkbook();
//NPOI.XSSF.UserModel.XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
//添加一个sheet
//NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1"); // NPOI.SS.UserModel.ISheet sheet1= book.CreateSheet("Sheet1"); // List<SC_ServiceCardUsedRecord> cards = new List<SC_ServiceCardUsedRecord>(); //获取list数据
cards = _AppContext.ServiceCardUsedRecordApp.SelectRecordList(param).ToList<SC_ServiceCardUsedRecord>(); //给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow();
row1.CreateCell().SetCellValue("编号");
row1.CreateCell().SetCellValue("卡券号码");
row1.CreateCell().SetCellValue("手机号");
row1.CreateCell().SetCellValue("姓名");
row1.CreateCell().SetCellValue("车架号");
row1.CreateCell().SetCellValue("经销商");
row1.CreateCell().SetCellValue("行驶里程");
row1.CreateCell().SetCellValue("创建时间");
row1.CreateCell().SetCellValue("卡劵类型");
row1.CreateCell().SetCellValue("车型");
row1.CreateCell().SetCellValue("来源");
row1.CreateCell().SetCellValue("经销商名称");
row1.CreateCell().SetCellValue("发卡时间");
row1.CreateCell().SetCellValue("购车时间");
row1.CreateCell().SetCellValue("会员等级"); //将数据逐步写入sheet1各个行
for (int i = ; i < cards.Count(); i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + ); rowtemp.CreateCell().SetCellValue(cards[i].Id.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CardNo.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].PhoneNumber.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CustName == null ? "" : cards[i].CustName.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].VIN == null ? "" : cards[i].VIN.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].DealerId == null ? "" : cards[i].DealerId.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].Mileage == null ? "" : cards[i].Mileage.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CreateTime.ToLongDateString());
rowtemp.CreateCell().SetCellValue(cards[i].CardTypeName == null ? "" : cards[i].CardTypeName.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CarCategory == null ? "" : cards[i].CarCategory.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].ActivityTag == null ? "" : cards[i].ActivityTag.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].DealerName == null ? "" : cards[i].DealerName.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].kqCreateTime == null ? "" : cards[i].kqCreateTime.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].BuyTime == null ? "" : cards[i].BuyTime.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].MLevel == null ? "" : cards[i].MLevel.ToString()); } // 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// MemoryStream stream = new MemoryStream();
book.Write(ms);
var buf = ms.ToArray();
//ms.Seek(0, SeekOrigin.Begin); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
//return File(ms, "application/vnd.ms-excel", "Card.xlsx");
return File(buf, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Card.xlsx");
}
2003 版
public ActionResult Export(DateTime? startTime, DateTime? endTime, string tableName)
{
IWorkbook book = new HSSFWorkbook(); //获取list数据
DataTable dt = _AppContext.ReportApp.GetReport(startTime ?? DateTime.Now.AddDays(-), endTime ?? DateTime.Now, tableName); if (dt.Rows.Count < EXCEL03_MaxRow)
DataWrite2Sheet(dt, , dt.Rows.Count - , book, "Sheet1");
else
{
int page = dt.Rows.Count / EXCEL03_MaxRow;
for (int i = ; i < page; i++)
{
int start = i * EXCEL03_MaxRow;
int end = (i * EXCEL03_MaxRow) + EXCEL03_MaxRow - ;
DataWrite2Sheet(dt, start, end, book, "Sheet" + i.ToString());
}
int lastPageItemCount = dt.Rows.Count % EXCEL03_MaxRow;
DataWrite2Sheet(dt, dt.Rows.Count - lastPageItemCount, dt.Rows.Count - , book, "Sheet" + page.ToString());
} // 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", tableName + ".xls");
}
注意:操作Excel2003与操作Excel2007使用的是不同的命名空间下的内容
使用NPOI.HSSF.UserModel空间下的HSSFWorkbook操作Excel2003
使用NPOI.XSSF.UserModel空间下的XSSFWorkbook操作Excel2007
asp.net 导出excel--NPOI的更多相关文章
- Asp.net导出Excel续章(自定义合并单元格,非Office组件)
结合上次写的导出Excel方法,这次上头要求我将列头进行一下合并 以前的效果: 改进后的效果: 在上篇文章中写到了Excel的导出方法,这次为了避免在生产环境中使用Office组件,服务器各种权限配置 ...
- asp.net导出excel示例代码
asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> ); ; ...
- [转] Asp.Net 导出 Excel 数据的9种方案
湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...
- ASP.NET导出EXCEL类
最新ASP.NET导出EXCEL类 说明:可以导出ASP.NET页面和DATAGRID(WebControl)数据,可以导出表单头 using System;using System.Data;usi ...
- asp.net导出excel并弹出保存提示框
asp.net导出excel并弹出保存提示框 2013-07-12 | 阅:1 转:78 | 分享 腾讯空间 人人网 开心网 新浪微博 腾讯微博 搜狐空间 推荐给朋友 举报 ...
- ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)
网上提供了很多Asp.net中操作Excel的方法,其中大部分是调用微软的Office组件,下面提供三个无须安装Office即可从Asp.net输出Excel的方法. 1 简单方法 //下面代码输出的 ...
- ASP.NET MVC导出excel npoi
使用npoi组件 前端代码: @Html.ActionLink("导出Excel", "ExportWarehouseInOutDetailTable", ne ...
- asp.net 导出Excel
分享一个asp.net 导出假Excel代码.优点,不用借助于任何插件比如(NPOI),复制代码,修改grid.DataSource直接导出. 先看导出后的效果图 System.Web.UI.WebC ...
- ASP.NETCore -----导出Excel文件并下载
本事例分为nopi(安装DotNetCore.NPOI)下载和EPPlus(EPPlus.Core.dll)下载,其中npoi下载演示的是根据执行的模板进行数据下载 npoi帮助类NpoiExcelU ...
- Asp.net导出Excel乱码的解决方法
通过跟踪Asp.net服务器代码,没有乱码,然而导出Excel到浏览器后,打开时出现乱码. 解决方法是添加编码格式的前缀字节码:Response.BinaryWrite(System.Text.Enc ...
随机推荐
- linux php --ini
$ php --ini
- 【nodejs】初识 NodeJS(三)
上节我们将 http 服务器(server.js)和请求路由模块(route.js)整合在一起了,当然这还不够,路由,顾名思义,是指我们要针对不同的 url 有不同的处理方式. 请求处理程序模块(re ...
- Logstash安装和使用
Logstash 是开源的服务器端数据处理管道,能够同时 从多个来源采集数据.转换数据,然后将数据发送到您最喜欢的 “存储库” 中.(我们的存储库当然是 Elasticsearch.) 作用:集中.转 ...
- MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report e
早上来到公司,线上的项目报错: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionExcepti ...
- css3 - 纯css实现一个轮播图
这是我上一次的面试题.一晃两个月过去了. 从前都是拿原理骗人,把怎么实现的思路说出来. 我今天又被人问到了,才想起来真正码出来.码出来效果说明一切: 以上gif,只用到了5张图片,一个html+css ...
- 【腾讯云的1001种玩法】几种在腾讯云建立WordPress的方法(Linux)(二)
版权声明:本文由张宁原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/126547001488207964 来源:腾云阁 ht ...
- thinphp5框架遇到 mkdir() Permission denied 解决办法
网站重装 直接复制本地程序文件 里面数据库链接信息要改成线上的 然后mysql apache 等都没有动 运行后出现错误 mkdir() Permission denied 这是由于runtime目录 ...
- 线程的简述Thread
为什么有进程? 原来操作系统只能处理一件任务,有了进程就可以让操作系统处理多个任务.因为进程与进程之间是完全隔离的,涉及到了内存空间.数据的切换,所以就有了进程的概念. 已经有了进程,为什么还要线程? ...
- oracle 28000错误解决方法
ORA-28000: the account is locked-的解决办法 ORA-28000: the account is locked 第一步:使用PL/SQL,登录名为system,数据库名 ...
- Java设计模式系列 — 构造器模式
想象下你有一个类,像下图所示有许多属性.假设你想让你的类不可变(顺便说一下,除非有一个好的理由不这样做,否则你应该坚持.但是我们会以另一种方式来达到要求.) public class User { p ...