NPOI导出Excel(异步请求)

来源:互联网 发布:网站域名怎么续费go 编辑:程序博客网 时间:2024/04/29 23:05
1:前台JavaScript异步调用后台C#方法
<script type="javascript">  <span style="white-space:pre"></span>function ReportDepartmentExcel() {          <span style="white-space:pre"></span>Ywsz.Web.Windows.WindowsCheckInfo.CreateExcel(return_Excel);          }          function return_Excel(res) {              <span style="white-space:pre"></span>window.location = res.value;          }  </script>  

2:后台C#方法

[AjaxPro.AjaxMethod]  public string ExportExcelSample()  {      DataSet ds = Department_Bll.GetList("");      HSSFWorkbook hssfworkbook = new HSSFWorkbook();      HSSFSheet sheet = (HSSFSheet)hssfworkbook.CreateSheet();      for (int i = 0; i < ds.Tables[0].Rows.Count + 1; i++)      {          IRow row = sheet.CreateRow(i);          for (int j = 0; j < 3; j++)          {              row.CreateCell(j);          }      }      sheet.GetRow(0).GetCell(0).SetCellValue("部门编码");      sheet.GetRow(0).GetCell(1).SetCellValue("部门名称");      sheet.GetRow(0).GetCell(2).SetCellValue("办公号码");      for (int k = 0; k < ds.Tables[0].Rows.Count; k++)      {          DataRow dr = ds.Tables[0].Rows[k];          sheet.GetRow(k + 1).GetCell(0).SetCellValue(dr["DeptCode"].ToString());          sheet.GetRow(k + 1).GetCell(1).SetCellValue(dr["DeptName"].ToString());          sheet.GetRow(k + 1).GetCell(2).SetCellValue(dr["DeptTelephone"].ToString());      }      //自动列宽    for (int i = 0; i < 3; i++)      {          int leng = 4;          for (int j = 0; j < ds.Tables[0].Rows.Count + 1; j++)          {              ICell cell = sheet.GetRow(j).GetCell(i);              if (Encoding.Default.GetBytes(cell.ToString()).Length > leng)              {                  leng = Encoding.Default.GetBytes(cell.ToString()).Length;              }          }          sheet.SetColumnWidth(i, (leng) * 256);      }      string savapath = "../Windows" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";      FileStream fileout = new FileStream(Server.MapPath(savapath), FileMode.Create);      hssfworkbook.Write(fileout);      fileout.Close();      fileout.Dispose();        return savapath;    }  


0 0