NPOI CellStyle 设置
public class ExcelNPOIUnit
{
public static void SetCell(IWorkbook workbook, ISheet sheet,
IRow row, int createCellIndex, object cellContent, CellType cellType, HorizontalAlignment alignment)
{
IDataFormat celldataformat = workbook.CreateDataFormat();
IFont font = workbook.CreateFont();
font.FontName = "Calibri"; ICell cell = row.FirstOrDefault(n => n.ColumnIndex == createCellIndex);
if (cell == null)
cell = row.CreateCell(createCellIndex); cell.CellStyle.SetFont(font);
cell.CellStyle.BorderLeft = BorderStyle.Thin;
cell.CellStyle.BorderRight = BorderStyle.Thin;
cell.CellStyle.BorderTop = BorderStyle.Thin;
cell.CellStyle.BorderBottom = BorderStyle.Thin; //在这里设置会影响全部单元格
//cell.CellStyle.Alignment = alignment; double tmp = -;
if (cellType == CellType.Numeric && double.TryParse(cellContent.ToString(), out tmp))
{
//必须在这里这样设置,才能对当前单元格有效
ICellStyle cellstyle = workbook.CreateCellStyle();
cellstyle.CloneStyleFrom(cell.CellStyle);
cellstyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0");
cellstyle.Alignment = alignment;
cell.CellStyle = cellstyle; cell.SetCellValue(tmp);
}
else
{
//必须在这里这样设置,才能对当前单元格有效
ICellStyle cellstyle = workbook.CreateCellStyle();
cellstyle.CloneStyleFrom(cell.CellStyle);
cellstyle.Alignment = alignment;
cell.CellStyle = cellstyle;
cell.SetCellValue(cellContent.ToString());
}
} public static void SaveSheet(string fullname, ISheet sheet)
{
using (FileStream writefile = new FileStream(fullname, FileMode.Create, FileAccess.Write))
{
sheet.Workbook.Write(writefile);
}
}
public static IWorkbook GetWorkbook(string fullname)
{
IWorkbook workbook = null; if (fullname.ToLower().EndsWith(".xls"))
{
using (FileStream fs = new FileStream(fullname, FileMode.Open, FileAccess.Read))
{
workbook = new HSSFWorkbook(fs);
}
}
else
{
using (FileStream fs = new FileStream(fullname, FileMode.Open, FileAccess.Read))
{
workbook = new XSSFWorkbook(fs);
}
} return workbook;
} }
var wk = ExcelNPOIUnit.GetWorkbook(logname);
var sheet = wk.GetSheet("Sheet1");
int rowindex = 2;
int cellindex = 0;
foreach (var item in list)
{
IRow row = sheet.CreateRow(rowindex);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.CreateDate, CellType.String, HorizontalAlignment.Center);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.TotalFilesReceived, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.TotalPagesReceived, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.ShippedFiles, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.ShippedPages, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.PendingFiles, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.PendingPages, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NofFiles_Priority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofPgs_Priority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofFiles_NoPriority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofPgs_NoPriority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofFiles_Today, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofPgs_Today, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.Remarks, CellType.Numeric, HorizontalAlignment.Left);
rowindex++;
cellindex = 0;
}
ExcelNPOIUnit.SaveSheet(logname, sheet);
NPOI CellStyle 设置的更多相关文章
- NPOI 格式设置2—时间,千分位,繁体,小数位
在Excel中我们经常要设置格式,比如说日期格式(yyyymmdd).小数点格式(1.20).货币格式($2000).百分比格式(99.99%)等等,这些东西在过去我们恐怕只能在服务器端生成好,不但增 ...
- NPOI格式设置1
using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; //创建Execl IWorkbook hssfworkbook =new HSSFWorkbo ...
- NPOI格式设置
using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; //创建Execl IWorkbook hssfworkbook =new HSSFWorkbo ...
- NPOI打印设置
打印设置主要包括方向设置.缩放.纸张设置.页边距等.NPOI 1.2支持大部分打印属性,能够让你轻松满足客户的打印需要. 方向设置首先是方向设置,Excel支持两种页面方向,即纵向和横向. 在NPOI ...
- NPOI Excel设置样式
在表格导出时,会碰到样式修改的问题,作如下简单归纳: //创建行样式ICellStyle style = workbook.CreateCellStyle();//前景色 ...
- NPOI 自定义单元格背景颜色-Excel
NPOI针对office2003使用HSSFWorkbook,对于offce2007及以上使用XSSFWorkbook:今天我以HSSFWorkbook自定义颜色为例说明,Office2007的未研究 ...
- NPOI 教程 - 2.1单元格合并
来源:http://liyingchun343333.blog.163.com/blog/static/3579731620091018212990/ 合并单元格在制作表格时很有用,比如说表格的标题就 ...
- NPOI使用手册[转]
NPOI使用手册 目录 1.认识NPOI 2. 使用NPOI生成xls文件 2.1 创建基本内容 2.1.1创建Workbook和Sheet 2.1.2创建DocumentSummaryInforma ...
- [转]NPOI 单元格级别应用
原文地址:http://hi.baidu.com/linrao/item/fadf96dce8770753d63aaef2 HSSFWorkbook hssfworkbook = new HSSFWo ...
随机推荐
- Python 多人聊天工具 ( 多线程 )
程序实现: 1.单或多客户端使用 telnet 登陆服务端 ( 可远程 ) 进行会话 2.服务端实现登陆.注册.退出功能 3.客户端发送的消息会被广播到已经登陆的其他用户界面 4.连接到服务端后,可以 ...
- js生成元素的事件不执行问题
要求:双击span标签,可以修改.<div class="commands_ticketmiddl_ine_new" > <span>NO:</spa ...
- nginx 真实ip
server { listen 80; server_name localhost; location /{ root html; index index.html index.h ...
- Linux下Mysql数据库互为主从的配置过程
配置准备: 两台机器:A(193.168.10.101) B(193.168.10.102) mysql大版本需要一致,小版本可忽略 配置过程: A(193.168.10.101) 机器配置: 执行 ...
- 编写DLL
想想还是把这个记录下吧,虽然不难,但由于平时写得不多,老是搞忘了. 1.我们来编写一个简单的DLL程序. 首先,我们来看下入口函数DllMain().DllMain()有3个参数: (1)hModul ...
- 微信OAuth2.0网页授权php示例
1.配置授权回调页面域名,如 www.aaa.com 2.模拟公众号的第三方网页,fn_system.php <?php if(empty($_SESSION['user'])){ header ...
- oracle 创建一个用户,只能访问指定的对象
1>创建一个ORACLE 的用户 create user username identified by pws; 2>给用户授权 grant connect,resource to us ...
- Django常见出错解决方案汇总-乾颐堂
一.模板类型错误: 错误原因:在models中漏掉了return: class UserProfile(models.Model): """ 功能说明: 扩 ...
- Python使用日常
#Python中文件夹和文件的判断import os My_Path = "/home/lpworkstudy/Gooddir/" #现在我们判断这个文件夹是否存在 #如果不存在, ...
- nginx在windows平台下的使用笔记
nginx主要提供反向代理及负载均衡的能力,重定向报文代理及报文数据替换也是常用功能.(参考https://www.cnblogs.com/fanzhidongyzby/p/5194895.html) ...