[.NET开发] NPOI导出

本文介绍了一种将专家管理系统的数据批量导出至Excel的方法,包括使用NPOI库操作Excel文件的具体实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  //导出全部

  expertPara = GetExpetPara();

  expertPara.BeginIndex = pager.CurrentPageIndex;

  expertPara.EndIndex = int.MaxValue;

  DataSet ds = ExpertBLL.GetPriMngExpertDate(expertPara);

  DataTable dt = ds.Tables[0];

  //判断加载哪个模板

  string tempath = "~/Templates/专家录入管理表.xls";

  //1、获取数据。

  using (FileStream file = new FileStream(HttpContext.Current.Server.MapPath(tempath), FileMode.Open, FileAccess.Read))

  {

  string ReportFileName = Server.MapPath("out.xls");

  HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);

  HSSFSheet sheet1 = hssfworkbook.GetSheetAt(0) as HSSFSheet;

  sheet1.ForceFormulaRecalculation = true;

  sheet1.IsPrintGridlines = true;

  sheet1.DisplayGridlines = true;

  HSSFCellStyle _style = sheet1.Workbook.CreateCellStyle() as HSSFCellStyle;

  _style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;

  _style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;

  int rowIndex = 0;

  {

  rowIndex = 2;

  int i = 1;

  foreach (DataRow row in ds.Tables[0].Select())

  {

  int cellIndex = 0;

  HSSFRow fr = GetRow(sheet1, rowIndex++);

  GetCell(fr, cellIndex++, _style).SetCellValue(i.ToString());

  GetCell(fr, cellIndex++, _style).SetCellValue(row["C_Name"].ToString());

  GetCell(fr, cellIndex++, _style).SetCellValue(DelHTML(row["C_Sex"].ToString()));

  GetCell(fr, cellIndex++, _style).SetCellValue(DelHTML(row["C_WorkUnit"].ToString()));

  GetCell(fr, cellIndex++, _style).SetCellValue(row["C_expert_type"].ToString());

  GetCell(fr, cellIndex++, _style).SetCellValue(row["C_position"].ToString());

  GetCell(fr, cellIndex++, _style).SetCellValue(row["C_title"].ToString());

  GetCell(fr, cellIndex++, _style).SetCellValue(row["C_Tel"].ToString());

  GetCell(fr, cellIndex++, _style).SetCellValue(row["C_BusinessOutlets"].ToString());

  GetCell(fr, cellIndex++, _style).SetCellValue(row["C_BankAccount"].ToString());

  i++;

  }

  sheet1.ForceFormulaRecalculation = true;

  using (FileStream filess = File.OpenWrite(ReportFileName))

  {

  hssfworkbook.Write(filess);

  }

  System.IO.FileInfo filet = new System.IO.FileInfo(ReportFileName);

  Response.Clear();

  Response.Charset = "GB2312";

  Response.ContentEncoding = System.Text.Encoding.UTF8;

  // 添加头信息,为"文件下载/另存为"对话框指定默认文件名

  string name = DateTime.Now + ".xls";

  Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));

  // 添加头信息,指定文件大小,让浏览器能够显示下载进度

  Response.AddHeader("Content-Length", filet.Length.ToString());

  // 指定返回的是一个不能被客户端读取的流,必须被下载

  Response.ContentType = "application/ms-excel";

  // 把文件流发送到客户端

  Response.WriteFile(filet.FullName);

  // 停止页面的执行

  Response.End();

  }

  }

  复制代码

  (编辑:雷林鹏 来源:网络)

转载于:https://www.cnblogs.com/pengpeng1208/p/9212150.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值