.Net web 实现数据导出到Excel中

数据库相关

  数据库名称:EHotelDb    表名称:Booking

  

后台代码相关:(节选)

  自己封装的Function类,用于数据库操作

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Data.SqlClient;
 5 using System.Linq;
 6 using System.Web;
 7 
 8 namespace EHotelMS.Models
 9 {
10     public class Functions
11     {
12         private SqlConnection Con;
13         private SqlCommand Cmd;
14         private DataTable dt;
15         private string ConStr;
16         private SqlDataAdapter sda;
17 
18         public int setData(string Query)
19         {
20             int Cnt;
21             if(Con.State == ConnectionState.Closed)
22             {
23                 Con.Open();
24             }
25             Cmd.CommandText = Query;
26             Cnt = Cmd.ExecuteNonQuery();
27             Con.Close();
28             return Cnt;
29         }
30 
31         public DataTable GetData(string Query)
32         {
33             dt = new DataTable();
34             sda = new SqlDataAdapter(Query, ConStr);
35             sda.Fill(dt);
36             return dt;
37         }
38 
39         public Functions()//连接数据库
40         {
41             ConStr = @"Data Source=.;Initial Catalog=EHotelDb;Integrated Security=True";
42             Con = new SqlConnection(ConStr);
43             Cmd = new SqlCommand();
44             Cmd.Connection = Con;
45         }
46     }
47 }

  后台页面XXX.aspx.cs中调用Function类,实现数据库的连接等操作

 1 using System.Web;
 2 using System.Web.UI;
 3 using System.Web.UI.WebControls;
 4 using System.Globalization;
 5 using System.Data;
 6 using NPOI.HSSF.UserModel;
 7 using NPOI.SS.UserModel;
 8 using System.IO;
 9 
10 namespace EHotelMS.View.Users
11 {
12     public partial class Booking : System.Web.UI.Page
13     {
14         Models.Functions Con;
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             Con = new Models.Functions();
18         }
19 20

  下面的代码也都是写在上面说的XXX.aspx.cs中

  在控件(导出按钮)的监听事件中,将查询出数据的DataTable和自定义的文件名传给NpoiExcel函数

1 protected void BookingGV_SelectedIndexChanged(object sender, EventArgs e)
2         {
3             string Query = "select * from BookingTb1 ";
4             NpoiExcel(Con.GetData(Query), "文件名");
5         }

  NpoiExcel函数

 1 public void NpoiExcel(DataTable dt, string title)
 2         {
 3             HSSFWorkbook book = new HSSFWorkbook();
 4             ISheet sheet = book.CreateSheet("工作簿1");
 5 
 6             IRow headerrow = sheet.CreateRow(0);
 7             ICellStyle style = book.CreateCellStyle();
 8             style.Alignment = HorizontalAlignment.Center;
 9             style.VerticalAlignment = VerticalAlignment.Center;
10             string[] headName = {"订单号","当前时间","房间序号","用户ID","入住时间","退房时间","金额" };
11             //设置表头:查询有几列,然后给每列赋值上字段名称
12             for (int i = 0; i < dt.Columns.Count; i++)
13             {
14                 ICell cell = headerrow.CreateCell(i);
15                 cell.CellStyle = style;
16                 //cell.SetCellValue(dt.Columns[i].ColumnName);//将表头设置为数据库的字段名
17                 cell.SetCellValue(headName[i]);//自定义表头,从自定义的数组中取值
18 
19             }
20             //设置表中数据
21             for(int i = 0; i < dt.Rows.Count; i++)//控制行
22             {
23                 IRow LineRow = sheet.CreateRow(i+1);
24                 for(int j = 0; j < dt.Columns.Count; j++)//控制列
25                 {
26                     
27                     ICell lineCell = LineRow.CreateCell(j);
28                     lineCell.CellStyle = style;
29                     lineCell.SetCellValue(dt.Rows[i][j].ToString());
30                     if (j == 1 || j == 4 || j == 5)//这些列和时间相关,需要转换时间格式
31                     {
32                         lineCell.SetCellValue(Convert.ToDateTime(dt.Rows[i][j]).ToString("yyyy-MM-dd"));
33                     }
34                 }
35             }
36             
37             MemoryStream ms = new MemoryStream();
38             book.Write(ms);
39             Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode(title + "_" + DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8)));
40             Response.BinaryWrite(ms.ToArray());
41             Response.End();
42             book = null;
43             ms.Close();
44             ms.Dispose();
45         }

  

  

  

posted @ 2023-03-30 10:01  胡烙懿  阅读(77)  评论(0编辑  收藏  举报