c# NPOI Excel로 데이터 내보내기(Simple)

팁: NPOI가 Excel로 데이터를 내보내려면 NPOI 라이브러리를 참조해야 합니다. /// /// Excel /// /// dataTable /// Excel /// public byte[] CreateExcel(DataTable dataTable, string name) { HSSFWorkbook workbook = null; try { workbook = new HSSFWorkbook();// HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(name);// Sheet //CreateFreezePane // ; // ; sheet.CreateFreezePane(0, 1); HSSFRow row_title = sheet.CreateRow(0) as HSSFRow; row_title.HeightInPoints = 19.5F; // for (int i = 0; i < dataTable.Columns.Count; i++) { HSSFCell cell_row_title = row_title.CreateCell(i) as HSSFCell; sheet.AutoSizeColumn(i, true);// cell_row_title.SetCellValue(dataTable.Columns[i].ColumnName); } // for (int i = 0; i < dataTable.Rows.Count; i++) { HSSFRow row = sheet.CreateRow(i + 1) as HSSFRow; for (int j = 0; j < dataTable.Columns.Count; j++) { HSSFCell cell_row = row.CreateCell(j) as HSSFCell; //sheet.AutoSizeColumn(j, true);// cell_row.SetCellValue(dataTable.Rows[i][j].ToString()); } } using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); byte[] buff = ms.ToArray(); return buff; } } catch (Exception e) { } finally { if (workbook != null) { workbook.Close(); } } return null; }

좋은 웹페이지 즐겨찾기