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 ...
随机推荐
- Java内功修炼系列一拦截器
在动态代理中,我们知道在代理类中,执行真实对象的方法前后可以增加一些其他的逻辑,这些逻辑并不是真实对象能够实现的方法,比如一个租房的用户希望租一套公寓,但是中介所代理的这个房东并没有可以出租的公寓,那 ...
- html css javascript mysql php学习总结
一. html:超文本标记语言,运行在浏览器上,由浏览器解析 1.格式 <!doctype html> 声明文档类型,说明html版本号 <html> 说明代码格式 <h ...
- Spring.之.报错:Caused by: java.lang.IllegalArgumentException: No Spring Session store is configured: set the 'spring.session.store-type' property
Spring.之.报错 No Spring Session store is configured springboot在启动的时候报如下错误: Error starting ApplicationC ...
- VS中warning MSB8004和error MSB4018解决方案
问题如下: warning MSB8004: Output Directory does not end with a trailing slash. This build instance wil ...
- Java 后端彻底解决跨域问题(CORS)
接口调用出现跨域问题时,浏览器会报如下提示 XMLHttpRequest cannot load xxx. Request header field Authorization is not allo ...
- Linux 定时任务执行 php artisan
*/ * * * * php /www/wwwroot/project/artisan command:exec postNews 5分钟执行一次
- LUOGU P2441 角色属性树
题目描述 绪萌同人社是一个有趣的组织,该组织结构是一个树形结构.有一个社长,直接下属一些副社长.每个副社长又直接下属一些部长--. 每个成员都有一个萌点的属性,萌点属性是由一些质数的萌元素乘积构成(例 ...
- Amazon EBS的功能更新
Amazon EBS(Elastic Block Store.简称EBS) 为 Amazon EC2 实例提供块级存储服务.EBS 卷须要通过网络訪问,而且能独立于实例的生命周期而存在.也就是说假如E ...
- NOIP模拟题17.9.26
B 君的任务(task)[题目描述]与君初相识,犹如故人归.B 君看到了Z 君的第一题,觉得很难.于是自己出了一个简单题.你需要完成n 个任务,第i 任务有2 个属性ai; bi.其中ai 是完成这个 ...
- Git pull 强制覆盖本地文件 - CSDN博客
Git pull 强制覆盖本地文件 原创 2015年11月16日 22:07:56 标签: git git fetch --all git reset --hard origin/master git ...