asp教程.net HttpHandler从数据库教程表导出Excel文件
1.新建“导出Excel”asp.net教程 Web应用程序 文件。
2.拷贝NPOI库文件到新建的“lib”目录下,添加引用把7个.dll选上。
3.添加一般处理程序,文件名DownloadExcel1.ashx
4.建数据库UserDB.mdf 字段为:UserName,Password
5.在Default.aspx文件中加上:
6.在DownloadExcel1.ashx.cs文件中添加以下代码:
context.Response.ContentType = "application/x-excel";
string filename = HttpUtility.UrlEncode("用户数据.xls");
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet();
using (SqlConnection conn = new SqlConnection(@"Data Source=.SQLEXPRESS;AttachDBFilename=|DataDirectory|UserDB.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (IDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Users";
using (IDataReader reader = cmd.ExecuteReader())
{
int rownum = 0;
while (reader.Read())
{
string username = reader.GetString(reader.GetOrdinal("UserName"));
string password = reader.GetString(reader.GetOrdinal("Password"));
HSSFRow row = sheet.CreateRow(rownum);
row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(username);
row.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue(password);
rownum++;
}
}
}
}
workbook.Write(context.Response.OutputStream);
}
洋果子店rose最新版本
洋果子店最新版是一款十分容易让人垂涎的开店经营类手游。游戏玩
明日大亨最新版
明日大亨手游,一个非常经典有趣的模拟经营类型的商战手游,在游
早安我的少年日服最新版2024
早安我的少年日服是一款治愈恋爱手游,清新明亮的游戏画面,让你
我的世界基岩版1.16正式版
我的世界基岩版是一款模拟经营手游,游戏采用的是经典像素玩法,
早安我的少年最新版2024
早安我的少年是一款养成恋爱手游,在这款游戏中,玩家们将可以体