论坛首页 Java企业应用论坛

导出excel封装类

浏览 1928 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-14  
action中调用方法

private void exportList(List numberList,String numType, HttpServletResponse response) throws Exception {

List excellist = new ArrayList();//数据列表
String docName = "exp189CDMA.xls";
String sheetname ="";
ExcelUtil excelutil = new ExcelUtil();
try {
List headerlist = new ArrayList();
//表头
headFont(headerlist);
headerList(headerlist,numType);
excellist.add(headerlist);//加入表头
if (numberList.size() > 0) {
for (int i = 0; i < numberList.size(); i++) {
Map map =new HashMap();
if(numberList.get(i) instanceof Map){
map = (Map)numberList.get(i);
List lineDataList = new ArrayList();
lineDataList.add(map.get("province"));
lineDataList.add(map.get("localname"));
lineDataList.add(map.get("telCode"));
for(int k=0;k<=9;k++){
lineDataList.add(map.get("hcode"+k));
}
excellist.add(lineDataList);
}
}
}

} catch (Exception e) {
throw e;
}

response.reset();
response.setCharacterEncoding("UTF-8");
docName = URLEncoder.encode(docName, "UTF-8");
response.setContentType("application/x-msdownload;charset=UTF-8");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(docName.getBytes("UTF-8"), "UTF-8"));

OutputStream outStream = response.getOutputStream();
excelutil.getWorkbook(excellist, sheetname).write(outStream);
}

/**
* 导出EXCEL
*
* @param mapping
* @param form
* @param request
* @param response
* @throws Exception
* @throws Exception
*/
private void exportList(List[] numberList,String[] reportSel, HttpServletResponse response) throws Exception {

int length = reportSel.length;
String docName = "exp189CDMA.xls";
List[] excellist = new ArrayList[length];//数据列表
String[] sheetnames = new String[length];
for(int i=0;i<length;i++){
sheetnames[i] =reportSel[i];
}
if(StringUtils.isEmpty("")){}
ExcelUtil excelutil = new ExcelUtil();
for(int n = 0;n<reportSel.length;n++){
String selValue = reportSel[n];
if("189H码新增表".equals(selValue)){
try {
//新增表

excellist[n] = new ArrayList();
List headerlist = new ArrayList();
headFont(headerlist);
//头部号码
headerList(headerlist,"189");
excellist[n].add(headerlist);//加入表头
List list1 = numberList[n];
if (list1.size() > 0) {
for (int i = 0; i < list1.size(); i++) {
Num189CDMAExpVO vo = (Num189CDMAExpVO)list1.get(i);
List lineDataList = new ArrayList();
lineDataList.add(vo.getProvince());
excellist[n].add(lineDataList);
}
}

} catch (Exception e) {
throw e;
}
}

response.reset();
response.setCharacterEncoding("UTF-8");
docName = URLEncoder.encode(docName, "UTF-8");
response.setContentType("application/x-msdownload;charset=UTF-8");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(docName.getBytes("UTF-8"), "UTF-8"));

OutputStream outStream = response.getOutputStream();
excelutil.getWorkbook(excellist, sheetnames).write(outStream);
}
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics