程序猿的世界你不懂
/*** excel导出
* @param req
* @param resp
* void
* @throws IOException
*/
private void exportAccountData(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
String sql=" select>
+ "where user_id=?";
User user=(User) req.getSession().getAttribute("user");
List<Account> list=MyBaseDao.queryRows(sql, new Object[]{user.getId()}, Account.class);
if(null!=list&&list.size()>0){
String fileName="账户数据.xls";
resp.setHeader("Content-disposition","attachment;filename="
+new String(fileName.getBytes("gb2312"),"ISO8859-1")); //设置文件头编码格式
resp.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//设置类型
resp.setHeader("Cache-Control","no-cache");//设置头
resp.setDateHeader("Expires", 0);//设置日期头
HSSFWorkbook book=new HSSFWorkbook();
HSSFSheet sheet=book.createSheet();
CellStyle cellStyle=book.createCellStyle();
cellStyle.setDataFormat(book.createDataFormat().getFormat("yyyy-MM-dd"));
for(int i=0;i<list.size();i++){
Account account=list.get(i);
Row row=sheet.createRow(i);
Cell cell1=row.createCell(0);
cell1.setCellValue(account.getId());
Cell cell2=row.createCell(1);
cell2.setCellValue(account.getAname());
Cell cell3=row.createCell(2);
cell3.setCellValue(account.getMoney());
Cell cell4=row.createCell(3);
cell4.setCellValue(account.getType());
Cell cell5=row.createCell(4);
cell5.setCellValue(account.getRemark());
Cell cell6=row.createCell(5);
cell6.setCellValue(account.getCreateTime());
cell6.setCellStyle(cellStyle);
Cell cell7=row.createCell(6);
cell7.setCellStyle(cellStyle);
cell7.setCellValue(account.getUpdateTime());
}
book.write(resp.getOutputStream());
resp.getOutputStream().flush();
resp.getOutputStream().close();
}
}
页:
[1]