NPOI的C# Helper代码 1 public static void WriteExcel(DataTable dt, string filePath) 2 { 3 if (!string.IsNullOrEmpty(filePath) & ...
NPOI的C# Helper代码
1 public static void WriteExcel(DataTable dt, get='_blank'>string filePath) 2 { 3 if (!string.IsNullOrEmpty(filePath) && dt.Rows.Count > 0) 4 { 5 HSSFWorkbook wk = new HSSFWorkbook(); 6 ISheet sheet = wk.CreateSheet(dt.TableName); 7 8 //列头 9 IRow headerRow = sheet.CreateRow(0);10 for (int i = 0; i < dt.Columns.Count; i++)11 {12 headerRow.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);13 }14 15 //填充内容16 for (int i = 0; i < dt.Rows.Count; i++) //注意条件dt.Rows.Count17 {18 IRow row = sheet.CreateRow(i+1);19 for (int j = 0; j < dt.Columns.Count; j++)//注意条件dt.Columns.Count20 {21 row.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j])); //注意这里写法22 }23 }24 //写入到客户端25 using (MemoryStream ms = new MemoryStream())26 {27 wk.Write(ms);28 using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))29 {30 byte[] data = ms.ToArray();31 file.Write(data,0,data.Length);32 file.Flush();33 }34 wk = null;35 }36 37 }38 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:用NPOI从DataBase到Excel
关键词:excel
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。