NPOI操作创建Excel
一、下载NPOI类库
使用Nuget在线搜索NPOI,下载安装
二、代码开撸
var workBook = new HSSFWorkbook(); #region 设置样式 IFont font = workBook.CreateFont();
font.FontHeightInPoints = ;
font.FontName = "宋体";
font.Boldweight = (short)FontBoldWeight.Bold;
font.Color = HSSFColor.White.Index; HSSFPalette palette = workBook.GetCustomPalette(); //调色板实例
palette.SetColorAtIndex((short), (byte), (byte), (byte));//设置表头背景色
palette.SetColorAtIndex((short), (byte), (byte), (byte));//设置内容背景色
palette.SetColorAtIndex((short), (byte), (byte), (byte));//设置下边框线颜色
palette.SetColorAtIndex((short), (byte), (byte), (byte));//设置下边框线颜色
palette.SetColorAtIndex((short), (byte), (byte), (byte));//设置黄色 var cellMidStyle = workBook.CreateCellStyle();
cellMidStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//设置水平居中
cellMidStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//设置垂直居中
cellMidStyle.FillPattern = FillPattern.SolidForeground;
cellMidStyle.FillForegroundColor = palette.FindColor((byte), (byte), (byte)).Indexed;
cellMidStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cellMidStyle.BottomBorderColor = palette.FindColor((byte), (byte), (byte)).Indexed;
cellMidStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cellMidStyle.LeftBorderColor = palette.FindColor((byte), (byte), (byte)).Indexed;
cellMidStyle.SetFont(font);
#endregion var sheet = workBook.CreateSheet("sheet1");//创建表格
// 第一行存放列名
var row = sheet.CreateRow();
for (int i = ; i < titles.Count; i++)
{
var cell = row.CreateCell(i);
cell.SetCellValue(titles[i]); int length = Encoding.UTF8.GetBytes(titles[i]).Length;
sheet.SetColumnWidth(i, length * );//设置表格列宽
cell.CellStyle = cellStyle;//设置单元格样式
}
int rowIndex = ;
foreach (var record in list)
{
row = sheet.CreateRow(rowIndex);
row.CreateCell().SetCellValue("单元格1");//给rowIndex行的第1列的单元格赋值
rowIndex++;
}
workBook.SetSheetOrder(sheet.SheetName, );//设置表格位置
workBook.SetActiveSheet();//设置默认表格
// 创建文件
using (FileStream fs = new FileStream(string.Format("{0}/{1}", path, DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"), FileMode.Create))
{
workBook.Write(fs);
}
NPOI操作创建Excel的更多相关文章
- NPOI操作之一EXCEL数据导入数据库
一.概要 前面讲到NPOI操作EXCEL导出功能,下面讲下从EXCEL里获取数据添加进数据库. 二.代码 HSSFWorkbook hssfworkbook; public void ExcelDat ...
- 使用NPOI随意创建Excel(含下拉列表)
//创建工作簿 HSSFWorkbook ssfworkbook = new HSSFWorkbook(); //创建工作表(页) HSSFSheet sheet1 = ssfworkbook.Cre ...
- NPOI操作EXCEL(四)——反射机制批量导出excel文件
前面我们已经实现了反射机制进行excel表格数据的解析,既然有上传就得有下载,我们再来写一个通用的导出方法,利用反射机制实现对系统所有数据列表的筛选结果导出excel功能. 我们来构想一下这样一个画面 ...
- NPOI操作EXCEL(二)——大量不同模板时设计方式
上一篇文章介绍了一些NPOI的基础接口,我们现在就来看看具体怎么用NPOI来解析一个EXCEL. 博主现在有这么一堆excel需要解析数据入库: 当然这只是员工的简要模板,还有很多其他的模板.我们可以 ...
- NPOI操作excel之写入数据到excel表
在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...
- C#开发中使用Npoi操作excel实例代码
C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...
- 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数
2.4.1 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数 NPOI教程:http://www.cnb ...
- Excel操作--使用NPOI导入导出Excel为DataTable
1.ExcelHelper封装 namespace NPOI操作Excel { public class ExcelHelper { /// <summary> /// DataTable ...
- 使用NPOI操作Excel
案例:用NPOI动态生成一个Excel表,然后弹出对话框让用户下载,文件名是"用户列表.xls" 先去相关网站下载 NPOI DLL文件,再引用 application/x-e ...
随机推荐
- 解决本地SqlServer无法连接远程服务器数据库,错误10060
本地SqlServer 连不上服务器的数据库环境,错误信息如下图,折腾来折腾去,最终还是解决了 第一步 查看服务器本地端口是否已经打开,查看方法:首先向C:\Windows\System32文件夹添加 ...
- 安装pandas报错(AttributeError: 'module' object has no attribute 'main')
在pycharm中安装pandas出现报错:AttributeError: 'module' object has no attribute 'main', 刚开始以为是pip的版本太旧了,于是乎将其 ...
- jQuery mouse and lunbo
自动轮播 和 鼠标事件var arr = ["images/d.jpg", "images/q.jpg", "images/c.jpg", ...
- 题解-COCI-2015Norma
Problem SPOJ-NORMA2 & bzoj3745 题意概要:给定一个正整数序列 \(\{a_i\}\),求 \[\sum_{i=1}^n\sum_{j=i}^n(j-i+1)\mi ...
- windows 环境下切换 python2 与 pythone3 以及常用命令
windows 环境下切换运行时的 python2 与 pythone3 当需要 python2 时执行:py -2 当需要 python3 时执行:py -3 windows下通过cmd切换pyth ...
- patch 28729262
打补丁最后出个error OPatch found the word "error" in the stderr of the make command.Please look a ...
- Mac环境下 elasticsearch-6.0.1 和 elasticsearch-head 完整安装过程
安装步骤: 安装java jdk 安装elasticsearch-6.0.1 及中文分词 anslysis-ik-6.0.1 安装elasticsearch-head 下载jdk https://w ...
- Image Pipeline
Image Pipeline Scrapy 提供了专门下载文件或者图片的Pipeline,下载图片与文件的原理同抓取网页的原理是一样的,所以他们的下载过程支持多线程与异步,十分的高效 Image Pi ...
- 小程序 movable-view 在页面中的可移动图标
项目中需要一个可拖动的小图标, 1.小程序组件movable-view 文档地址:https://developers.weixin.qq.com/miniprogram/dev/component/ ...
- 基于python的WGS84转百度坐标
from urllib.request import urlopen, quote import json def wgs84tobaidu(x,y): data=str(x)+','+str(y); ...