jxl.jar 下载地址:jxl.jar
List authlist = queryList();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// 2.生成工作簿
WritableWorkbook wb = Workbook.createWorkbook(baos);
// 3.生成工作表
WritableSheet sheet = wb.createSheet("交易结果记录查询", 0);
//统计
Label label = new Label(0, 0, "交易验证总笔数");
sheet.addCell(label);
label = new Label(1, 0, String.valueOf(authlist.size()));
sheet.addCell(label);
label = new Label(2, 0, "成功笔数");
sheet.addCell(label);
label = new Label(3, 0, String.valueOf(request.getAttribute("success")));
sheet.addCell(label);
label = new Label(4, 0, "失败笔数");
sheet.addCell(label);
label = new Label(5, 0,String.valueOf( request.getAttribute("failure")));
sheet.addCell(label);
//标题
label = new Label(0, 1, "交易时间");
sheet.addCell(label);
label = new Label(1, 1, "交易种类");
sheet.addCell(label);
label = new Label(2, 1, "交易号");
sheet.addCell(label);
label = new Label(3, 1, "商户代码");
sheet.addCell(label);
label = new Label(4, 1, "VCN虚拟卡号");
sheet.addCell(label);
label = new Label(5, 1, "交易结果");
sheet.addCell(label);
List totalList = new ArrayList();
// 封装数据
for (int i = 0; i < authlist.size(); i++) {// 行
AuthTransactionMessageData auth = (AuthTransactionMessageData) authlist
.get(i);
int tmpresult = auth.getResult();
String result = "";
if(tmpresult==4){
result="成功";
}
else{
result="失败";
}
//每列的集合
List list = new ArrayList();
list.add(auth.getAuthTime().toString());
list.add("支付验证");
list.add(auth.getTradeRecordID());
list.add(auth.getMerID());
list.add(auth.getPan());
list.add(result);
totalList.add(list);
}
for (int i = 0; i < totalList.size(); i++) {
List list = (List) totalList.get(i);
for (int j = 0; j < list.size(); j++) {
label = new Label(j, i + 2, list.get(j).toString());
sheet.addCell(label);
}
}
wb.write();
wb.close();
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
String filename = "历史交易记录查询结果"+(new SimpleDateFormat("yyyy-MM-dd")).format(new Date()) + ".xls";
//解决下载文件的名称的中文乱码问题
response.setHeader("Content-disposition", "attachment; filename="
+ new String(filename.getBytes("gb2312"), "iso8859-1"));
int b;
while ((b = bais.read()) != -1) {
response.getOutputStream().write(b);
}
response.getOutputStream().flush();
if (bais != null)
bais.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}