首先下载NPOI.dll,下载完成后解压添加引用;
调用示例:
页面按钮
<asp:Button ID="btnExecl" runat="server" Width="90px" Height="39px" Text="导出全部名单" BackColor="#DD99FF" OnClick="btnExecl_Click" />
页面要添加这两个引用
调用方法示例:
//点击导出按钮 protected void btnExecl_Click(object sender, EventArgs e) { //绑定数据源 DataSet ds = bll.GetList("Result=25"); if (ds.Tables[0].Rows.Count > 0) { DataTable dt = new DataTable(); dt.Columns.Add("RTime", typeof(System.String)); dt.Columns.Add("Result", typeof(System.String)); dt.Columns.Add("OName", typeof(System.String)); dt.Columns.Add("OSex", typeof(System.String)); dt.Columns.Add("OPhone", typeof(System.String)); dt.Columns.Add("OXueLi", typeof(System.String)); dt.Columns.Add("OAge", typeof(System.String)); dt.Columns.Add("OAddress", typeof(System.String)); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DataRow dr = dt.NewRow(); dr["RTime"] = ds.Tables[0].Rows[i]["RTime"].ToString(); dr["Result"] = ds.Tables[0].Rows[i]["Result"].ToString(); dr["OName"] = ds.Tables[0].Rows[i]["OName"].ToString(); dr["OSex"] = ds.Tables[0].Rows[i]["OSex"].ToString(); dr["OPhone"] = ds.Tables[0].Rows[i]["OPhone"].ToString(); dr["OXueLi"] = ds.Tables[0].Rows[i]["OXueLi"].ToString(); dr["OAge"] = ds.Tables[0].Rows[i]["OAge"].ToString(); dr["OAddress"] = ds.Tables[0].Rows[i]["OAddress"].ToString(); dt.Rows.Add(dr); } //调用导出 ExportExcel(dt, "导出数据:全对答题名单", "全对答题名单"); } else { ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", "yz('没有数据,无法导出',1);", true); return; } } #region 导出数据 /// <summary> /// DataTable导出Excel /// </summary> /// <param name="dt">datatable数据源</param> /// <param name="strFileName">文件名</param> /// <param name="strSheetName">工作簿名</param> public void ExportExcel(DataTable dt, string strFileName, string strSheetName) { HSSFWorkbook book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet(strSheetName); IRow headerrow = sheet.CreateRow(0); ICellStyle style = book.CreateCellStyle(); style.Alignment = HorizontalAlignment.Center; style.VerticalAlignment = VerticalAlignment.Center; HSSFRow dataRow = (HSSFRow)sheet.CreateRow(0); string strColumns = "答题时间,答对,姓名,性别,手机,职业,年龄,区域"; string[] strArry = strColumns.Split(','); for (int i = 0; i < strArry.Length; i++) { dataRow.CreateCell(i).SetCellValue(strArry[i]); dataRow.GetCell(i).CellStyle = style; } for (int i = 0; i < dt.Rows.Count; i++) { dataRow = (HSSFRow)sheet.CreateRow(i + 1); for (int j = 0; j < dt.Columns.Count; j++) { string ValueType = ""; string Value = ""; if (dt.Rows[i][j].ToString() != null) { ValueType = dt.Rows[i][j].GetType().ToString(); Value = dt.Rows[i][j].ToString(); } switch (ValueType) { case "System.String"://字符串类型 dataRow.CreateCell(j).SetCellValue(Value); break; case "System.DateTime"://日期类型 System.DateTime dateV; System.DateTime.TryParse(Value, out dateV); dataRow.CreateCell(j).SetCellValue(dateV); break; case "System.Boolean"://布尔型 bool boolV = false; bool.TryParse(Value, out boolV); dataRow.CreateCell(j).SetCellValue(boolV); break; case "System.Int16"://整型 case "System.Int32": case "System.Int64": case "System.Byte": int intV = 0; int.TryParse(Value, out intV); dataRow.CreateCell(j).SetCellValue(intV); break; case "System.Decimal"://浮点型 case "System.Double": double doubV = 0; double.TryParse(Value, out doubV); dataRow.CreateCell(j).SetCellValue(doubV); break; case "System.DBNull"://空值处理 dataRow.CreateCell(j).SetCellValue(""); break; default: dataRow.CreateCell(j).SetCellValue(""); break; } dataRow.GetCell(j).CellStyle = style; //设置宽度 sheet.SetColumnWidth(j, (Value.Length + 10) * 256); } } MemoryStream ms = new MemoryStream(); book.Write(ms); Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))); Response.BinaryWrite(ms.ToArray()); Response.End(); book = null; ms.Close(); ms.Dispose(); } #endregion