NPOI操作、导出Excel
//使用NPOI操作Excel
private void ExcelNPOI(System.Data.DataTable dt, HttpContext context)
{
IWorkbook workbook = null;//工作薄
IRow row = null;//行
ICell cell = null;//单元格
ISheet sheet = null;//工作表
try
{
//如果表中查询的有数据
if (dt != null && dt.Rows.Count > )
{
//创建工作薄
//workbook = new HSSFWorkbook(); //导出后缀为xls
workbook = new XSSFWorkbook();//导出后缀为xlsx
sheet = workbook.CreateSheet("Sheet1");//创建一个名称为Sheet1的表
int rowCount = dt.Rows.Count;//行数
int columnCount = dt.Columns.Count;//列数 //npoi设置Excel样式
ICellStyle cellStyle = workbook.CreateCellStyle();
//设置单元格为数字格式
cellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("0.00");
//居中对齐
cellStyle.Alignment = HorizontalAlignment.Center;
cellStyle.VerticalAlignment = VerticalAlignment.Center;
//边框
cellStyle.BorderTop = BorderStyle.Thin;
cellStyle.BorderBottom = BorderStyle.Thin;
cellStyle.BorderLeft = BorderStyle.Thin;
cellStyle.BorderRight = BorderStyle.Thin;
//创建一个字体样式对象
NPOI.SS.UserModel.IFont FontRow = workbook.CreateFont();
//设置字体样式
FontRow.FontName = "宋体";
//设置字体加粗样式
FontRow.Boldweight = (short)FontBoldWeight.Bold;
//设置字体大小
FontRow.FontHeightInPoints = ;
//是否加粗
//FontRow.IsBold = false;
//字体样式添加进去
cellStyle.SetFont(FontRow); //合并单元格 起始行号,终止行号, 起始列号,终止列号 execl的行列都是从0开始,而不是从1开始
sheet.AddMergedRegion(new CellRangeAddress(, , , )); //添加第一行 并赋值
row = sheet.CreateRow();
row.CreateCell().SetCellValue("值");
cell = row.GetCell();
cell.CellStyle = cellStyle;
//添加第二行 定义表头
row = sheet.CreateRow();
//单元格赋值
row.CreateCell().SetCellValue("值");
row.CreateCell().SetCellValue("值");
row.CreateCell().SetCellValue("值");
row.CreateCell().SetCellValue("值");
row.CreateCell().SetCellValue("值");
row.CreateCell().SetCellValue("值"); //设置列宽
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * );
sheet.SetColumnWidth(, * ); //设置行高 第一行
row = sheet.GetRow();
row.Height = short.Parse((22.5 * ).ToString());
//使用SetFont方法将字体样式添加到单元格样式中
cellStyle.SetFont(FontRow); //设置行高 第二行
row = sheet.GetRow();
row.Height = short.Parse((18.5 * ).ToString());
//获得第二行的单元格
List<ICell> cells = row.Cells;
for (int i = ; i < cells.Count; i++)
{
//获得当前行
cell = row.GetCell(i);
//设置样式
cell.CellStyle = cellStyle;
} //写入数据
for (int i = ; i < rowCount; i++)
{
//创建新行
row = sheet.CreateRow(i + );
//定义新行行高
row.Height = short.Parse((13.5 * ).ToString());
for (int j = ; j < columnCount; j++)
{
if (j - >= )
{
//创建新的单元格
cell = row.CreateCell(j - );
//赋值
cell.SetCellValue(dt.Rows[i][j].ToString());
cell.CellStyle = cellStyle;
}
}
}
string Excelfile = context.Server.MapPath("路径");
string path = context.Server.MapPath("excel再上一级的路径");
DirectoryInfo folder = new DirectoryInfo(path);
//文件夹是否存在当前Excel
foreach (FileInfo file in folder.GetFiles("*.xlsx"))
{
if (file.FullName == Excelfile)
{
try
{
File.Delete(Excelfile);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
}
using (FileStream file = new FileStream(Excelfile, FileMode.Create))
{
workbook.Write(file); //写入数据 创建文件。
file.Close();
}
}
}
catch (Exception ex)
{
}
}
NPOI操作、导出Excel的更多相关文章
- NPOI导入导出Excel
.net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交 代码: 第一步. 在页面里面加入2个隐藏的iframe, 如下 ...
- .Net core NPOI导入导出Excel
最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- Excel操作--使用NPOI导入导出Excel为DataTable
1.ExcelHelper封装 namespace NPOI操作Excel { public class ExcelHelper { /// <summary> /// DataTable ...
- <转>Npoi导入导出Excel操作<载>
//Datatable导出Excel private static void GridToExcelByNPOI(DataTable dt, string strExcelFileName) { tr ...
- .net mvc利用NPOI导入导出excel
1.导出Excel :首先引用NPOI包(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> ...
- ASP.Net MVC利用NPOI导入导出Excel
因近期项目遇到所以记录一下: 首先导出Excel: 首先引用NPOI包 http://pan.baidu.com/s/1i3Fosux (Action一定要用FileResult) /// <s ...
- net mvc 利用NPOI导入导出excel
1.导出Excel : 首先引用NPOI包(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> ...
- NPOI操作之一EXCEL数据导入数据库
一.概要 前面讲到NPOI操作EXCEL导出功能,下面讲下从EXCEL里获取数据添加进数据库. 二.代码 HSSFWorkbook hssfworkbook; public void ExcelDat ...
随机推荐
- 原生JS实现彩票36选7不重复(优化)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- mysql基本笔记之二
1.查看当前编码 show variables like '%char%' 2.修改user表中id=1的name 为 A where后面是条件,就是定位 3.符号 > //大于符号 < ...
- sql删除重复的记录保留一条
delete from A_TO_NOW where yuan_name in (select yuan_name from A_TO_NOW group by yuan_name hav ...
- Jquery 页面打印
<script src="~/Scripts/js/dist/jquery.jqprint-0.3.js"></script> <script typ ...
- fft模板 HDU 1402
// fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...
- SSM-5zookeeper在LINUX上自启
把zookeeper做成服务 1.进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本 [root@zookeeper ~]# cd /etc/rc.d/init.d/ [ro ...
- 异步 I/O 和事件驱动
异步IO(asynchronous I/O) 首先来理解几个容易混淆的概念,阻塞IO(blocking I/O)和非阻塞IO(non-blocking I/O),同步IO(synchronous I/ ...
- jquery 点击图片弹出遮罩层查看大图
<div class="photobox"> <ul> <li data-date="'+data[i].id+'"> &l ...
- XML解析器之JAXP与DOM4J
XML是一种数据格式,那么需要对XML文件进行操作就需要用到XML解析器---------针对dom方式和sax方式提供了不同的解析技术-----需要不同的XML解析器 dom方式:会把文档中所有元素 ...
- Linux预习
目录 linux系统和unix系统的简介 linux系统和unix系统的简介 unix是什么:和widows一样 特点:多用户,多任务 同一时刻,多用户同时执行多项程序,互不干扰 GNU项目 就是一个 ...