导出excel的action方法,我这里是直接调用接口过来的JSON数据需用自己解析
/**
* 设备导出excel
*
* @return
*/
public String exportDevDataExcel() {
OutputStream os;
try {
String fileName="设备管理";
String outFileName = new String(fileName.getBytes("GBK"), "ISO8859_1");
os = response.getOutputStream();
response.reset();
response.setContentType("application/vnd.ms-excel");// 设置生成的文件类型
response.setHeader("Content-disposition", "attachment; filename="+ outFileName + ".xls");
WritableWorkbook workbook = Workbook.createWorkbook(os);// 建立excel文件
WritableSheet wsheet = workbook.createSheet("设备", 0); // // 工作表名称
// 设置Excel字体
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 15,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat titleFormat = new WritableCellFormat(wfont);
titleFormat.setAlignment(Alignment.CENTRE);
// 水平居中显示
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直居中显示
wsheet.mergeCells(0, 0, 6, 2);// 合并单元格,从0开始
titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
wsheet.addCell(new Label(0, 0, fileName, titleFormat));// 填写工作表标题
String[] title = { "序号", "设备名称", "设置Mac地址", "库存状态", "在线状态", "在线用户",
"在线时长" };
// 设置Excel表头
for (int i = 0; i < title.length; i++) {
WritableFont font = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);// 定义字体
WritableCellFormat titleWritableFormat = new WritableCellFormat(font);// 定义格式化对象
titleWritableFormat.setAlignment(Alignment.CENTRE);// 水平居中显示
titleWritableFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
titleWritableFormat.setWrap(true);// 自动换行
// wsheet.mergeCells(i, 3, i, 4);//合并单元格
wsheet.setColumnView(i, 13);// 设置列宽
// wsheet.setRowView(3,12);// 设置行高
titleWritableFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
Label excelTitle = new Label(i, 3, title[i],titleWritableFormat);
// System.out.println("Excel title = " + title[i]);
wsheet.addCell(excelTitle);
}
//获取JSON数据(---这里也换成java操作数据查询的LIST等等)
json = this.getDevListMethod(agencyName, keyword, devName, devSn, modelId, modelName, versionName, stock, online, shopName, maintainerName);
JSONObject dataJson = JSONObject.fromObject(json);
JSONArray dataArray =JSONArray.fromObject(dataJson.get("data"));
WritableCellFormat rowFormat = new WritableCellFormat();// 定义格式化对象
rowFormat.setAlignment(Alignment.CENTRE);// 水平居中显示
// rowFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//把垂直对齐方式指定为居中
// rowFormat.setWrap(true);//设置自动换行
rowFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
int c = 3; // 用于循环时Excel的行号
for (int i = 1; i < dataArray.size(); i++) {
c++;
Label content0 = new Label(0, c, String.valueOf(i ), rowFormat);
wsheet.addCell(content0);
Label content2 = new Label(1, c, dataArray.getJSONObject(i).getString("name"), rowFormat);
wsheet.addCell(content2);
Label content3 = new Label(2, c, dataArray.getJSONObject(i).getString("sn"), rowFormat);
wsheet.addCell(content3);
String stock = "未绑定";
if(dataArray.getJSONObject(i).getInt("stock")==1){
stock = "未绑定";
}else if (dataArray.getJSONObject(i).getInt("stock")==2) {
stock = "绑定";
}else if(dataArray.getJSONObject(i).getInt("stock")==3) {
stock = "调拨";
}else {
stock = "其他";
};
Label content4 = new Label(3, c,stock, rowFormat);
wsheet.addCell(content4);
String online = "离线";
if(dataArray.getJSONObject(i).getInt("online")==1){
online = "在线";
}else {
online = "离线";
};
Label content5= new Label(4, c,online, rowFormat);
wsheet.addCell(content5);
Integer users=dataArray.getJSONObject(i).getInt("users");
Label content6= new Label(5, c,users.toString() ,rowFormat);
wsheet.addCell(content6);
Integer duration=dataArray.getJSONObject(i).getInt("duration");
Label content7= new Label(6, c,duration.toString(),rowFormat);
wsheet.addCell(content7);
}
workbook.write(); // 写入文件
workbook.close();
os.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}