您的位置:首页 > 编程语言 > Java开发

java导出excel,自定义列与行

2014-04-28 09:40 239 查看
 

private void createNewFolder(String folderPath) {

  try {

   File myFilePath = new File(folderPath);

   if (!myFilePath.exists()) {

    myFilePath.mkdir();

   }

  }

  catch (Exception e) {

   e.printStackTrace();

  }

 }

 

public String downloadMore(List<String[]> tjList, String name) {

  try {

   //name=new String(name.getBytes("ISO-8859-1"),"gbk");

   System.out.println("--name"+name);

   String subname=name;

   String[] strs=name.split(";");

   if(strs!=null&&strs.length>2)

   {

    subname=strs[0];//0:名字,1:第一个列民,2:第二个列民

 

   

   Date date=new Date();

   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

   String time=sdf.format(date);

   

   int a=(int)(Math.random()*100000);

   // 通过参数拼接出文件名称

   String fileName =time +"-"+a+ ".xls";

   // 查看本地是否存在 存在直接下载

   String enc = "UTF-8";

   String realPath = ServletActionContext.getServletContext().getRealPath("");

   createNewFolder(realPath + "\\download\\");

   File downloadFile = new File(realPath + "\\download\\" + fileName);

   if (!downloadFile.exists()) {

    FileOutputStream output = new FileOutputStream(downloadFile);

    // 通过service获取到数据集合 ,然后生产 xls保存到本地

    

    // 将结果集生成 xls

    HSSFWorkbook workbook = new HSSFWorkbook();

    HSSFSheet sheet = workbook.createSheet(subname + "_" + time);

    //HSSFSheet hws=workbook.getSheet(name);

    HSSFRow rowTitle = sheet.createRow(0);

    HSSFCell titleCell = rowTitle.createCell(0);

    titleCell.setCellValue("编号");

    for(int i=1;i<strs.length;i++)

    {

      titleCell = rowTitle.createCell(i);

      titleCell.setCellValue(strs[i]);

    }

   

    for (int rowIndex = 0; rowIndex < tjList.size(); rowIndex++) {

     String[] tj=tjList.get(rowIndex);

     HSSFRow row = sheet.createRow(rowIndex + 1);

     HSSFCell cell = row.createCell(0);

     cell.setCellValue(new HSSFRichTextString(rowIndex+""));

     for(int k=0;k<tj.length;k++)

     {

      cell = row.createCell(k+1);

         cell.setCellValue(new HSSFRichTextString(tj[k]));

     }

     

    }

    sheet.setColumnWidth(0, 0);

    workbook.write(output);

   }

   HttpServletResponse response = ServletActionContext.getResponse();

   response.setContentType("application/octet-stream");

   long length = downloadFile.length();

   response.setContentLength(Long.valueOf(length).intValue());

   fileName = URLEncoder.encode(downloadFile.getName(), enc);

   response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

   ServletOutputStream servletOutputStream = response.getOutputStream();

   FileInputStream fileInputStream = new FileInputStream(downloadFile);

   BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

   int size = 0;

   byte[] b = new byte[4096];

   while ((size = bufferedInputStream.read(b)) != -1) {

    servletOutputStream.write(b, 0, size);

   }

   servletOutputStream.flush();

   servletOutputStream.close();

   bufferedInputStream.close();

  }

  }

  catch (IOException e) {

   e.printStackTrace();

  }

  return null;

 }

 

参数说明:

List<String[]>tjList   ,String name  :name是由;隔开的一串字符串,第一个为标题,第二个为行名(所有行名一样的情况),从第三个开始为列名,可以自定义个数,但要和tjList中的个数对应

此方法其实还可以继续改造的,标题,行名可以传单独的参数过来,列名可以用一个数组传过来,这样更方便明了,容易看懂

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息