JFinal,MySQL数据Excel文件导出
作者:互联网
JFinal,MySQL数据Excel文件导出
页面
使用a标签浏览器显示下载,不能直接写后台接口需要添加
javascript:window.location.href=
<a href="javascript:window.location.href='/nsUser/daochu'">一键导出</a>
Controller
/**
* 导出方法
*/
public void daochu() {
List<Record> list = nsUserService.getSelectList();//调用后台
// 创建
HSSFWorkbook wk = new HSSFWorkbook();
// 创建一张工作表
HSSFSheet sheet = wk.createSheet();
//在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
sheet.setColumnWidth(0, 5000);
//
HSSFRow row = sheet.createRow(0);
// 创建第一行的第一个单元格
// 向单元格写值
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("ID");
cell = row.createCell((short)1);
cell.setCellValue("姓名");
cell = row.createCell((short)2);
cell.setCellValue("性别");
cell = row.createCell((short)3);
cell.setCellValue("联系电话");
cell = row.createCell((short)4);
cell.setCellValue("身份证");
cell = row.createCell((short)5);
cell.setCellValue("录入时间");
cell = row.createCell((short)6);
cell.setCellValue("地址");
cell = row.createCell((short)7);
cell.setCellValue("录入人");
cell = row.createCell((short)8);
cell.setCellValue("所属区域");
cell = row.createCell((short)9);
cell.setCellValue("所属支行");
// 创建第一行
for (int i=0;i<list.size();i++)
{
row = sheet.createRow(i+1);
row.createCell(0).setCellValue(list.get(i).getStr("uid"));
row.createCell(1).setCellValue(list.get(i).getStr("uname"));
row.createCell(2).setCellValue(list.get(i).getStr("usex"));
row.createCell(3).setCellValue(list.get(i).getStr("uphone"));
row.createCell(4).setCellValue(list.get(i).getStr("usfid"));
row.createCell(5).setCellValue(list.get(i).getStr("uaddtime"));
row.createCell(6).setCellValue(list.get(i).getStr("uaddress"));
row.createCell(7).setCellValue(list.get(i).getStr("ujid"));
row.createCell(8).setCellValue(list.get(i).getStr("uqid"));
row.createCell(9).setCellValue(list.get(i).getStr("uzid"));
}
try {
/**
* 弹出下载选择路径框
*/
HttpServletResponse response = getResponse(); //创建response回应
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename = UserInFo.xls");//默认Excel名称
response.flushBuffer();
wk.write(response.getOutputStream());
wk.write(new FileOutputStream(new File("D://daochu/a.xls")));
wk.close();
} catch (IOException e) {
e.printStackTrace();
}
}
sercive
/**
* 查询导出
* @return
* @throws IOException
*/
public List<Record> getSelectList(){
String sql = "select * from ns_user";
return Db.find(sql);
}
标签:short,sheet,Excel,cell,createCell,MySQL,JFinal,setCellValue,row 来源: https://www.cnblogs.com/yu-si/p/12702000.html