public static void NPOIExportReport( string [] titlearry, string fileurl,DataTable dt) { FileStream fileStream = null; HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("sheet1"); try { sheet = workbook.GetSheetAt(0); IRow row1 = sheet.CreateRow(0); //表头 for (int m= 0; m< dt.Columns.Count; m++) { ICell cell = row1.CreateCell(m); cell.SetCellValue(titlearry[m]); } for (int i=0;i<dt.Rows.Count;i++) { IRow row = sheet.CreateRow(i + 1); for(int t = 0; t < dt.Columns.Count; t++) { ICell cell = row.CreateCell(t); cell.SetCellValue(dt.Rows[i][t].ToString()); } } fileStream = System.IO.File.Create(fileurl); workbook.Write(fileStream); fileStream.Close(); downloadfile(fileurl); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (workbook != null) workbook.Close(); }
/// <summary> /// http页面包头 /// </summary> public static void downloadfile(string s_path) { System.IO.FileInfo file = new System.IO.FileInfo(s_path); HttpContext.Current.Response.ContentType = "application/ms-download"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream"); HttpContext.Current.Response.Charset = "utf-8"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8)); HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); HttpContext.Current.Response.WriteFile(file.FullName); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.End(); }