protected void daochu_Click(object sender, EventArgs e)
{
string hql = "select * from Car";
List<Vehicle> list = GetVehicle(hql.ToString()); /*获得数据源*/
string filename = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString() + "\\Template" + "\\Vehicle.xls"); /*获得文件路劲*/
/*不过我的导出Excel文件路劲是写死的,这是不好的地方*/
HSSFWorkbook work = new HSSFWorkbook(); /*创建工作薄*/
ISheet sheet = work.CreateSheet("第一页"); /*创建表*/
IRow row = sheet.CreateRow(0); /*创建行*/
row.CreateCell(0, CellType.String).SetCellValue("管辖机构"); /*创建列*/
row.CreateCell(1, CellType.String).SetCellValue("号牌种类");
row.CreateCell(2, CellType.String).SetCellValue("号牌号码");
row.CreateCell(3, CellType.String).SetCellValue("车辆类型");
row.CreateCell(4, CellType.String).SetCellValue("使用性质");
row.CreateCell(5, CellType.String).SetCellValue("所有人");
row.CreateCell(6, CellType.String).SetCellValue("初次登记日期");
row.CreateCell(7, CellType.String).SetCellValue("强制报废期止");
row.CreateCell(8, CellType.String).SetCellValue("有效期止");
row.CreateCell(9, CellType.String).SetCellValue("车辆状态");
for (int i = 0; i < list.Count; i++)
{
sheet.AutoSizeColumn(i); /*列的自适应*/
IRow rows = sheet.CreateRow(i + 1); /*给列赋值*/
rows.CreateCell(0, CellType.String).SetCellValue(list[i].GLBM);
rows.CreateCell(1, CellType.String).SetCellValue(list[i].HPZL);
rows.CreateCell(2, CellType.String).SetCellValue(list[i].HPHM);
rows.CreateCell(3, CellType.String).SetCellValue(list[i].CLLX);
rows.CreateCell(4, CellType.String).SetCellValue(list[i].SYXZ);
rows.CreateCell(5, CellType.String).SetCellValue(list[i].SYR);
rows.CreateCell(6, CellType.String).SetCellValue("'" + Convert.ToDateTime(list[i].CCDJRQ).ToString("yyyy-MM-dd"));
rows.CreateCell(7, CellType.String).SetCellValue("'" + Convert.ToDateTime(list[i].QZBFQZ).ToString("yyyy-MM-dd"));
rows.CreateCell(8, CellType.String).SetCellValue("'" + Convert.ToDateTime(list[i].YXQZ).ToString("yyyy-MM-dd"));
rows.CreateCell(9, CellType.String).SetCellValue(list[i].ZT);
}
using (FileStream stream = new FileStream(filename, FileMode.Open))
{
work.Write(stream); /*输出*/
}
}