problem001
3/31/2016 - 9:12 AM

使用npoi导出到excel。设置单元格背景色、字体大小、边框样式等 npoi 2.0.6.0

使用npoi导出到excel。设置单元格背景色、字体大小、边框样式等

npoi 2.0.6.0

using Lottery;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
using System.Windows.Forms;
using System;
using System.IO;

public class ExportSample
{
    /// <summary>
    /// 导出到Excel
    /// </summary>
    /// <returns>导出的文件名</returns>
    public string Export() {
        //模板
        string templateFilePath = "template\\file.xlsx";
        PoiUtils Poi = new PoiUtils(templateFilePath);
        Poi.SetActiveSheetAt(1);//第一页

        #region 单元格样式

        ICellStyle styleBig = Poi.CreateCellStyle();
        ICellStyle styleNone = Poi.CreateCellStyle();

        
        //用NPOI给Excel单元格设置样式也可以用CSS啦
        //http://www.yimingzhi.net/2014/01/set-cell-style-by-npoi-css
        
        //字体
        var fontWhite = Poi.CreateFont();
        fontWhite.Color = HSSFColor.White.Index;//文字颜色
        fontWhite.Boldweight = (short)FontBoldWeight.Bold;//粗体
        fontWhite.FontHeightInPoints = 14;//文字大小

        //设定单元格背景色
        styleBig.FillPattern = FillPattern.SolidForeground;
        //http://stackoverflow.com/questions/2803841/setting-foreground-color-for-hssfcellstyle-is-always-coming-out-black
        //用FillBackgroundColor输出的的单元格是黑底的。用FillForegroundColor正确
        //styleBig.FillBackgroundColor = 53;
        styleBig.SetFont(fontWhite);
        //对齐方式
        styleBig.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
        //边框
        styleBig.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        styleBig.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        styleBig.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        styleBig.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

        styleNone.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        styleNone.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        styleNone.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        styleNone.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

        #endregion

        //循环输出
        for (var jj = 0; jj < 6; jj++)
        {
            //通常模板里第一行是标题,第二行是数据行(模板)。
            //所以输出的第一条数据不用CreateRow(),第二条以后要CreateRow(),并复制第一行数据模板行的样式
            if (jj > 0)
            {
                Poi.CreateRow();
                PoiUtils.CopyRow(Poi, 2, 2 + jj);//复制第一行数据模板行的样式
            }
            int row = 2 + jj;
            Poi.SetCellText(row, 1, "test" + jj.ToString());
            if (jj % 2 == 0)
            {
                Poi.SetCellStyle(row, 1, styleBig);
            }
            else
            {
                Poi.SetCellStyle(row, 1, styleNone);
            }
        }
        //显示文件保存对话框
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "Excel|*.xlsx";
        saveFileDialog.FilterIndex = 2;
        saveFileDialog.RestoreDirectory = true;
        saveFileDialog.FileName = string.Format("文件-{0}", DateTime.Today.ToString("yyyyMMdd"));
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            string fName = saveFileDialog.FileName;
            //生成文件
            using (FileStream newFile = new FileStream(fName, FileMode.Create))
            {
                Poi.Workbook.Write(newFile);
                newFile.Close();
                return fName;
            }
        }

        return string.Empty;
    }
}