首页 > 学院 > 开发设计 > 正文

导出Excel(npoi)

2019-11-10 18:35:07
字体:
来源:转载
供稿:网友
public static void ExportExcel(DataTable dt, string fileName = "") { //生成Excel IWorkbook book = BuildWorkbook(dt); //web 下载 if (fileName == "") fileName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now); fileName = fileName.Trim(); string ext = Path.GetExtension(fileName); if (ext.ToLower() == ".xls" || ext.ToLower() == ".xlsx") fileName = fileName.Replace(ext, string.Empty); HttPResponse httpResponse = HttpContext.Current.Response; httpResponse.Clear(); httpResponse.Buffer = true; httpResponse.Charset = Encoding.UTF8.BodyName; httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); httpResponse.ContentEncoding = Encoding.UTF8; httpResponse.ContentType = "application/vnd.ms-excel; charset=UTF-8"; book.Write(httpResponse.OutputStream); httpResponse.End(); }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表