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 设置的更多相关文章

  1. NPOI 格式设置2—时间,千分位,繁体,小数位

    在Excel中我们经常要设置格式,比如说日期格式(yyyymmdd).小数点格式(1.20).货币格式($2000).百分比格式(99.99%)等等,这些东西在过去我们恐怕只能在服务器端生成好,不但增 ...

  2. NPOI格式设置1

    using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; //创建Execl IWorkbook hssfworkbook =new HSSFWorkbo ...

  3. NPOI格式设置

    using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; //创建Execl IWorkbook hssfworkbook =new HSSFWorkbo ...

  4. NPOI打印设置

    打印设置主要包括方向设置.缩放.纸张设置.页边距等.NPOI 1.2支持大部分打印属性,能够让你轻松满足客户的打印需要. 方向设置首先是方向设置,Excel支持两种页面方向,即纵向和横向. 在NPOI ...

  5. NPOI Excel设置样式

    在表格导出时,会碰到样式修改的问题,作如下简单归纳: //创建行样式ICellStyle style = workbook.CreateCellStyle();//前景色                ...

  6. NPOI 自定义单元格背景颜色-Excel

    NPOI针对office2003使用HSSFWorkbook,对于offce2007及以上使用XSSFWorkbook:今天我以HSSFWorkbook自定义颜色为例说明,Office2007的未研究 ...

  7. NPOI 教程 - 2.1单元格合并

    来源:http://liyingchun343333.blog.163.com/blog/static/3579731620091018212990/ 合并单元格在制作表格时很有用,比如说表格的标题就 ...

  8. NPOI使用手册[转]

    NPOI使用手册 目录 1.认识NPOI 2. 使用NPOI生成xls文件 2.1 创建基本内容 2.1.1创建Workbook和Sheet 2.1.2创建DocumentSummaryInforma ...

  9. [转]NPOI 单元格级别应用

    原文地址:http://hi.baidu.com/linrao/item/fadf96dce8770753d63aaef2 HSSFWorkbook hssfworkbook = new HSSFWo ...

随机推荐

  1. Make 命令教程(转载)

    代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编译的安排),叫做构建(build). Make是最常用的构建工具,诞生于1977年,主要用于C语言的项目.但是实际上 , ...

  2. sql 添加索引强大

    以前没有亲自添加过索引,今天添加了一下,果真强大.几百倍的速度提升. SELECT * FROM tbl_sys_menu m WHERE m.SID in (SELECT mr.MENU_SID F ...

  3. react native android 真机调试

    http://localhost:8081/index.android.bundle?platform=android 晕死,设备掉线了 C:\Users\ZHONGZHENHUA\.android\ ...

  4. 高性能Web服务器Nginx的配置与部署研究(9)核心模块之HTTP模块基本常用指令

    一.HTTP模块的作用是什么? Nginx的HTTP模块用于控制Nginx的HTTP进程. 二.指令 1. alias 含义:指定location使用的路径,与root类似,但不改变文件的跟路径,仅适 ...

  5. Appium+python自动化-Remote远程控制

    前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...

  6. Python——字典与字典方法

    字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字.字符串.元组,这种结构类型也称之为映射.字典类型是Python中唯一內建的映射类型,基本的操作包括如下: (1)len():返回字典中键— ...

  7. MAXOS 进程管理

    ps -ef|grep +程序名 注意进程名区分大小写 linux上进程有5种状态:1. 运行(正在运行或在运行队列中等待)2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)3. 不可中 ...

  8. 用C语言进行最基本的socket编程

    什么是socket 你经常听到人们谈论着 “socket”,或许你还不知道它的确切含义.现在让我告诉你:它是使用 标准Unix 文件描述符 (file descriptor) 和其它程序通讯的方式.什 ...

  9. ATM取款机的数据库模拟开发和实战总结

    一.ATM实战开发的简介. 学习了几天的Oracle,开始着手用数据库PL/SQL语言做一个简单的ATM取款机业务,主要是为了巩固数据库的知识,并非真正的去实现高端的业务.有兴趣的可以看看,希望对同胞 ...

  10. php使用curl 实现GET和POST请求(抓取网页,上传文件),支持跨项目和跨服务器

    一:curl 函数和参数详解 函数库:1:curl_init 初始化一个curl会话2:curl_close 关闭一个curl会话3:curl_setopt 为一个curl设置会话参数4:curl_e ...