欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用jxl操作Excel(三)

程序员文章站 2024-02-24 13:27:28
...
导出数据

try{
//设置导出文件为Excel格式,文件名为UserInfo+yyyyMMddHHmmss
HttpServletResponse response = ServletActionContext.getResponse();
Date dt = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String sFileName = "UserInfo" + df.format(dt).toString();
OutputStream ops = response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition", "attachment; filename="+ sFileName +".xls");
response.setContentType("application/msexcel");
//设置写入的数据表为Sheet1
WritableWorkbook wwb = Workbook.createWorkbook(ops);
WritableSheet ws = wwb.createSheet("Sheet1", 0);
//设置单元格格式
WritableFont wf = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(jxl.format.Alignment.CENTRE);
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
//设置列宽
ws.setColumnView(0, 10);
ws.setColumnView(1, 20);
//写入第一行(标题行)
ws.addCell(new Label(0, 0, "UserName", wcf));
ws.addCell(new Label(1, 0, "UserPassword", wcf));
//从数据库中读取内容并依次从第二行写入Excel
List<UserInfo> userInfoList = userInfoDao.findAll();
if (userInfoList.size() > 0){
for (int i=0; i<userInfoList.size(); i++){
ws.addCell(new Label(0, i+1, userInfoList.get(i).getUserName(), wcf));
ws.addCell(new Label(1, i+1, userInfoList.get(i).getUserPassword(), wcf));
}
}else{
result = "FAILURE";
return result;
}
//正式写入
wwb.write();
//关闭连接
wwb.close();
ops.close();
result = "SUCCESS";
}catch(Exception e){
result = "FAILURE";
}
相关标签: Excel WCF

上一篇: power bi 之星座分析

下一篇: power BI