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. Pycharm 激活码 2019年1月9日16:57:13

    K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...

  2. 一条跨库更新数据的sql

    UPDATE [db1].[dbo].[R_ResAndBook]     SET SectionID=TT2.newsecidFROM [SmartCampus].[dbo].[R_ResAndBo ...

  3. C#路径的相关操作

    1.判定一个给定的C#路径是否有效,合法 通过Path.GetInvalidPathChars或Path.GetInvalidFileNameChars方法获得非法的C#路径/文件名字符,可以根据它来 ...

  4. 用python给图片添加文字(水印)

    题目来源于:Python 练习册,每天一个小程序 第0000题 代码如下: #-*- coding:utf-8 -*- import PIL from PIL import Image from PI ...

  5. 【2018沈阳赛区网络预选赛J题】Ka Chang【分块+DFS序+线段树】

    题意 给出一个有根树(根是1),有n个结点.初始的时候每个结点的值都是0.下面有q个操作,操作有两种,操作1.将深度为L的点的值全部增加X.操作2.查询以x为根的子树的结点值得和. 其中N,Q< ...

  6. TTF字体基本知识及其在QT中的应用

    字体类型 以Windows为例,有4种字体技术: Raster:光栅型,就是用位图来绘制字形(glyph),每个字都以位图形式保存 Vector:矢量型,就是用一系列直线的结束点来表示字形 TrueT ...

  7. PCA 原理

      PCA的数学原理(转) 1 年前 PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取 ...

  8. [GO]等待时间的使用

    package main import ( "time" "fmt" ) func main() { <-time.After(*time.Second) ...

  9. kcp结构体字段含义

    conv 会话IDmtu 最大传输单元mss 最大分片大小state 连接状态(0xFFFFFFFF表示断开连接)snd_una 第一个未确认的包snd_nxt 下一个待分配的包的序号rcv_nxt ...

  10. .NET基础 (05)内存管理和垃圾回收

    内存管理和垃圾回收1 简述.NET中堆栈和堆的特点和差异2 执行string abc="aaa"+"bbb"+"ccc"共分配了多少内存3 ...