using
NPOI.HSSF.UserModel;
using
NPOI.SS.UserModel;
try
{
//匯出EXCEL
int i = 0;
HSSFWorkbook book = new
HSSFWorkbook();
ISheet sheet =
book.CreateSheet("匯出資料");
IRow row;
IFont boldfont =
book.CreateFont();
boldfont.Boldweight =
(short)FontBoldWeight.Bold; //字型
ICellStyle cellstyle =
book.CreateCellStyle();
cellstyle.SetFont(boldfont);
int row_index, col_index;
FileStream file = new
FileStream(@"C:\Users\**\Desktop\excel1.xls", FileMode.Create); //產生檔案(指定產生路徑)
MemoryStream out_file = new
MemoryStream();
/////sheet1
sheet.CreateRow(0).CreateCell(0).SetCellValue(" 轉檔時間: " + DateTime.Now);
sheet.GetRow(0).GetCell(0).CellStyle = cellstyle;
//Header:匯出題目欄位
row = sheet.CreateRow(1);
row.CreateCell(0).SetCellValue("號碼");
row.CreateCell(1).SetCellValue("日期");
row.CreateCell(2).SetCellValue("狀態");
row.CreateCell(3).SetCellValue("天數");
row_index = 2;
row.RowStyle = cellstyle;
txtSQL = "SELECT * FROM A LEFT JOIN B ON A.ID=B.ID
WHERE *****";
DataTable dt = db.ExecuteFillToDataTable(txtSQL);
foreach
(DataRow row_dt in dt.Rows)
{
//匯出EXCEL
row =
sheet.CreateRow(row_index++);
col_index = 0;
row.CreateCell(col_index).SetCellValue(row_acp["Number"].ToString());
row.CreateCell(++col_index).SetCellValue(row_acp["Date"].ToString());
row.CreateCell(++col_index).SetCellValue(row_acp["State"].ToString());
row.CreateCell(++col_index).SetCellValue(spanDays - 1);
i = i++;
} //foreach
row_dt
//匯出EXCEL
book.Write(file);
file.Close();
}
Comments
Post a Comment