惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
小众软件
小众软件
The Cloudflare Blog
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
美团技术团队
WordPress大学
WordPress大学
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI
月光博客
月光博客
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
Google Developers Blog
GbyAI
GbyAI
B
Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗

博客园 - Yu

Illegal characters in path 依赖注入的 Singleton、Scoped、Transient vs2015 编译报错:The project references NuGet package(s) that are missing on this computer... discuz 迁移 uc_server 报错 The timeout period elapsed prior to completion of the operation or the server is not responding. 微信小程序笔记 xmlns 与 targetNamespace 的解释 Ubuntu64 apache2+lvs+Keepalived mysql --initialize specified but the data directory has files in it select2 多选设置默认值 VS 2015 IDE 不支持 MS SQL 2000 生成 dbml Ajax 如何执行 Response.Redirect ng 发生 Error: ELOOP: too many symbolic links encountered... jquery call cross-domain webapi owin self-host HtmlAgilityPack 使用 微信 oauth2 两次回调 EF 热加载 Winform/Asp.net vs2015 使用 Eazfuscator.NET 3.3 The requested page cannot be accessed because the related configuration data for the page is invalid.
NPOI CellStyle 设置
Yu · 2018-01-31 · via 博客园 - Yu
    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 = -1;
            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);