1. 程式人生 > 其它 >C# NPOI XSSFCellStyle設定Excel單元格樣式

C# NPOI XSSFCellStyle設定Excel單元格樣式

以下是先手動建立一個DataTable,利用NPOI匯出Excel,在向Sheet匯入資料後可以設定樣式
 static void Main()
        {
            DataTableToExcel(GetDt());
        }
        public static void DataTableToExcel(DataTable dt)
        {
            IWorkbook workbook = null;
            IRow row = null;
            ISheet sheet = null
; ICell cell = null; FileStream fs = null; if (dt != null && dt.Rows.Count > 0) { workbook = new XSSFWorkbook(); sheet = workbook.CreateSheet("Sheet0"); int rowCount = dt.Rows.Count;
int columnCount = dt.Columns.Count; //設定列頭 row = sheet.CreateRow(0); for (int c = 0; c < columnCount; c++) { cell = row.CreateCell(c); cell.SetCellValue(dt.Columns[c].ColumnName); }
//設定每行每列的單元格, for (int i = 0; i < rowCount; i++) { row = sheet.CreateRow(i + 1); for (int j = 0; j < columnCount; j++) { cell = row.CreateCell(j); cell.SetCellValue(dt.Rows[i][j].ToString()); } } //背景色 XSSFCellStyle cellStyle = (XSSFCellStyle)workbook.CreateCellStyle(); cellStyle.FillPattern = FillPattern.SolidForeground; cellStyle.FillBackgroundColor = IndexedColors.LightGreen.Index; cellStyle.FillForegroundColor = IndexedColors.LightGreen.Index; sheet.GetRow(1).GetCell(3).CellStyle = cellStyle; //字型顏色 IFont font = workbook.CreateFont();//建立一個字型並且設定顏色 font.Color = IndexedColors.Red.Index; ICellStyle style = workbook.CreateCellStyle(); style.SetFont(font); sheet.GetRow(2).GetCell(3).CellStyle = style; using (fs = File.OpenWrite(@"D:\a.xlsx")) { workbook.Write(fs);//向開啟的這個xls檔案中寫入資料 } System.Diagnostics.Process.Start(@"D:\a.xlsx"); } } static DataTable GetDt() { DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;//自動增加 dc.AutoIncrementSeed = 1;//起始為1 dc.AutoIncrementStep = 1;//步長為1 dc.AllowDBNull = false;// dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; newRow = tblDatas.NewRow(); newRow["Product"] = "VS"; newRow["Version"] = "2022"; newRow["Description"] = "又快又好用"; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "VS"; newRow["Version"] = "2019"; newRow["Description"] = "好用穩定"; tblDatas.Rows.Add(newRow); return tblDatas; }
View Code

主要程式碼:

//背景色
                XSSFCellStyle cellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
                cellStyle.FillPattern = FillPattern.SolidForeground;
                cellStyle.FillBackgroundColor = IndexedColors.LightGreen.Index;
                cellStyle.FillForegroundColor = IndexedColors.LightGreen.Index;
                sheet.GetRow(1).GetCell(3).CellStyle = cellStyle;

                //字型顏色
                IFont font = workbook.CreateFont();//建立一個字型並且設定顏色
                font.Color = IndexedColors.Red.Index;
                ICellStyle style = workbook.CreateCellStyle();
                style.SetFont(font);
                sheet.GetRow(2).GetCell(3).CellStyle = style;

結果: