//建立 Workbook
NPOI.HSSF.UserModel.HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook();
//建立 MemoryStream
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//建立一個叫做"Sheet1"的 Sheet
NPOI.HSSF.UserModel.HSSFSheet st = (NPOI.HSSF.UserModel.HSSFSheet)wb.CreateSheet("Sheet1");
//產生第一列 (第一列:0, 第二列:1, ...類推)
NPOI.HSSF.UserModel.HSSFRow r1 = (NPOI.HSSF.UserModel.HSSFRow)st.CreateRow(0);
//建立第一列的儲存格格式
NPOI.SS.UserModel.ICellStyle cs1 = wb.CreateCellStyle();
//設定背景色為黑色
cs1.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
cs1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLACK.index;
//字型為粗體白字
NPOI.SS.UserModel.IFont fWhiteBold = wb.CreateFont();
fWhiteBold.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
fWhiteBold.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;
cs1.SetFont(fWhiteBold);
//建立第一列的第一格 (A1, 第一格:0, 第二格:1, ...類推)
NPOI.HSSF.UserModel.HSSFCell c1 = (NPOI.HSSF.UserModel.HSSFCell)r1.CreateCell(0);
//設定A1儲存格格式及內容值
c1.CellStyle = cs1;
c1.SetCellValue("Title");
//產生第二列
NPOI.HSSF.UserModel.HSSFRow r2 = (NPOI.HSSF.UserModel.HSSFRow)st.CreateRow(1);
//建立第二列的儲存格格式
NPOI.SS.UserModel.ICellStyle cs2 = wb.CreateCellStyle();
//設定背景色為近似#99FF33 (依NPOI內建顏色為主)
System.Drawing.Color SysColor = System.Drawing.Color.FromArgb(153, 255, 51);
NPOI.HSSF.UserModel.HSSFPalette NPOIPalette = wb.GetCustomPalette();
NPOI.HSSF.Util.HSSFColor NPOIColor = NPOIPalette.FindSimilarColor(SysColor.R, SysColor.G, SysColor.B);
cs2.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
cs2.FillForegroundColor = NPOIColor.GetIndex();
//建立第二列的第一格 (A2)
NPOI.HSSF.UserModel.HSSFCell c2 = (NPOI.HSSF.UserModel.HSSFCell)r2.CreateCell(0);
//設定A2儲存格格式及內容值
c2.CellStyle = cs2;
c2.SetCellValue("abcdefgh");
//寫入 Memory
wb.Write(ms);
//設定預設檔名
Response.AddHeader("Content-Disposition", "Book1.xls");
//傳送到 Client
Response.BinaryWrite(ms.ToArray());
//關閉 Workbook
wb = null;
//關閉 Memory
ms.Close();
ms.Dispose();
NPOI.HSSF.UserModel.HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook();
//建立 MemoryStream
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//建立一個叫做"Sheet1"的 Sheet
NPOI.HSSF.UserModel.HSSFSheet st = (NPOI.HSSF.UserModel.HSSFSheet)wb.CreateSheet("Sheet1");
//產生第一列 (第一列:0, 第二列:1, ...類推)
NPOI.HSSF.UserModel.HSSFRow r1 = (NPOI.HSSF.UserModel.HSSFRow)st.CreateRow(0);
//建立第一列的儲存格格式
NPOI.SS.UserModel.ICellStyle cs1 = wb.CreateCellStyle();
//設定背景色為黑色
cs1.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
cs1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLACK.index;
//字型為粗體白字
NPOI.SS.UserModel.IFont fWhiteBold = wb.CreateFont();
fWhiteBold.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
fWhiteBold.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;
cs1.SetFont(fWhiteBold);
//建立第一列的第一格 (A1, 第一格:0, 第二格:1, ...類推)
NPOI.HSSF.UserModel.HSSFCell c1 = (NPOI.HSSF.UserModel.HSSFCell)r1.CreateCell(0);
//設定A1儲存格格式及內容值
c1.CellStyle = cs1;
c1.SetCellValue("Title");
//產生第二列
NPOI.HSSF.UserModel.HSSFRow r2 = (NPOI.HSSF.UserModel.HSSFRow)st.CreateRow(1);
//建立第二列的儲存格格式
NPOI.SS.UserModel.ICellStyle cs2 = wb.CreateCellStyle();
//設定背景色為近似#99FF33 (依NPOI內建顏色為主)
System.Drawing.Color SysColor = System.Drawing.Color.FromArgb(153, 255, 51);
NPOI.HSSF.UserModel.HSSFPalette NPOIPalette = wb.GetCustomPalette();
NPOI.HSSF.Util.HSSFColor NPOIColor = NPOIPalette.FindSimilarColor(SysColor.R, SysColor.G, SysColor.B);
cs2.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
cs2.FillForegroundColor = NPOIColor.GetIndex();
//建立第二列的第一格 (A2)
NPOI.HSSF.UserModel.HSSFCell c2 = (NPOI.HSSF.UserModel.HSSFCell)r2.CreateCell(0);
//設定A2儲存格格式及內容值
c2.CellStyle = cs2;
c2.SetCellValue("abcdefgh");
//寫入 Memory
wb.Write(ms);
//設定預設檔名
Response.AddHeader("Content-Disposition", "Book1.xls");
//傳送到 Client
Response.BinaryWrite(ms.ToArray());
//關閉 Workbook
wb = null;
//關閉 Memory
ms.Close();
ms.Dispose();
補充 : 在建立儲存格時, 可以定義儲存格的數值格式
using NPOI.HSSF.UserModel;
HSSFCell c3 = (HSSFCell)r2.CreateCell(1, NPOI.SS.UserModel.CellType.NUMERIC);
HSSFCell c3 = (HSSFCell)r2.CreateCell(1, NPOI.SS.UserModel.CellType.NUMERIC);
NPOI.SS.UserModel.CellType 有下列格式 :
BLANK / BOOLEAN / ERROR / FORMULA / NUMERIC / STRING / Unknown
感謝分享,非常實用。謝謝您。
回覆刪除