欢迎大家来吐口水、、
今天查了一天NPOI的资料、发现网上的都太高大上了、自己总结了一版
不多说、直接上代码
public void ExportToExcelTwo(System.Data.DataTable dt, string colTitle, string filePath) { HSSFWorkbook workBook = new HSSFWorkbook(); HSSFSheet sheet = (HSSFSheet)workBook.CreateSheet("sheet1"); HSSFRow row1 = (HSSFRow)sheet.CreateRow(0); string[] titles = colTitle.Split(','); for (int i = 0; i < titles.Length; i++) { HSSFCell cell = (HSSFCell)row1.CreateCell(i, CellType.STRING); cell.SetCellValue(titles[i]); } for (int j = 0; j < dt.Rows.Count; j++) { HSSFRow rows = (HSSFRow)sheet.CreateRow(j + 1); for (int k = 0; k < dt.Columns.Count; k++) { HSSFCell cells = (HSSFCell)rows.CreateCell(k); cells.SetCellValue(dt.Rows[j][k].ToString()); } } using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { workBook.Write(fs); } GC.Collect(); }