jxl导出execl实例

注意:实例中list数据是map类型的,其他类型需修改
      实例中导出execl采用jxl方式,需要引入jxl架包
1.后台Action方法

/**
* 导出Excel
* @param title 标题
* @param headerArr 表头数组
* @param mapArr map字符串数组,mapArr与headerArr一致
* @param dataList list
* @throws IOException
* @throws WriteException
*/
@SuppressWarnings("unchecked")
public void exportExcelByListMap(String title, String[] headerArr,String[] mapArr, List dataList) throws IOException, WriteException{
//导出excel
OutputStream out = this.getResponse().getOutputStream();
response.setContentType("application/x-download");
//需添加日期格式化
response.addHeader("Content-Disposition", "attachment;filename="+toUtf8String(title+"_") + (DateUtil.formatDate(new Date(), "yyyyMMDDHHmmss")) + ".xls");

WritableWorkbook workbook = Workbook.createWorkbook(out);
WritableSheet sheet = null;
int sheetNum = 0;
if(dataList!=null && dataList.size()>0){
for(int i = 0,len = dataList.size(); i < len ; i++){
int column = 0;
int row = 0;
if(i%65536 == 0){
sheetNum++;
//设置title样式
WritableFont wf = new WritableFont(WritableFont.TIMES, 12,WritableFont.BOLD, false);
WritableCellFormat wcf = new WritableCellFormat(NumberFormats.TEXT);
wcf.setFont(wf);
wcf.setBackground(Colour.PALE_BLUE);
wcf.setAlignment(Alignment.CENTRE);
sheet = workbook.createSheet(title + sheetNum, 0);
for(String head : headerArr){
addLabel(sheet, column++, 0, head,wcf);
}
}
column = 0;
row = (i%65535) + 1;
//设置单元格显示样式
WritableFont wf = new WritableFont(WritableFont.TIMES, 10,WritableFont.NO_BOLD, false);
WritableCellFormat wcf = new WritableCellFormat(NumberFormats.TEXT);
wcf.setFont(wf);

HashMap map = (HashMap) dataList.get(i);// 循环获取每一个map对象
for(String field : mapArr){
addLabel(sheet, column++, row, (String)map.get(field),wcf);
}
}
//设置列宽
for(int i=0,len=headerArr.length;i= 0 && c <= 255){
sb.append(c);
}else{
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception e) {
e.printStackTrace();
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0){
k += 256;
}
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();

}



2.前端jsp页面
 jsp添加内容:
 
 
 js添加内容:
 jConfirm('确定要导出excel文件?',  '消息', function(rs) {
        if (rs == true) {
          var exportXlsButton = document.getElementById("exportXlsButton");
          exportXlsButton.href = url; //url地址
          exportXlsButton.click();              
   }
 });






你可能感兴趣的:(JSP,导入导出excel)