java导出excel,自定义列与行

 

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中的个数对应

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

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值