1.项目有个需求,要按照特定格式 导出Excel表格。 正常的都是一行 ,下面是数据。这次有个变动,就是每隔 几列要换行,下面是数据。在下面是数据部分。花了一上午写了下需求,不难但是花时间
//实现特定的业务需求 每隔7行换行 String value = builder.toString().substring(0,builder.length()-1); String[] valueStr = value.split(","); String str = ""; for(int k = 0;k<valueStr.length;k++){ int zhengshu = k%7; while (zhengshu==0 && StringUtils.isNotEmpty(str)){ listStr.add(str.substring(0,str.length()-1)); str = ""; } str += valueStr[k]+","; }
package com.zhuanche.util.excel; import javax.servlet.http.HttpServletResponse; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.List; /** * 导出的excel 需要添加额外的输出 */ public class SupplierFeeCsvUtils { public static final Integer downPerSize = 10000; public static final String tab = "\t"; private OutputStreamWriter osw; private BufferedWriter bw = null; public OutputStreamWriter getOsw() { return osw; } public void setOsw(OutputStreamWriter osw) { this.osw = osw; } public BufferedWriter getBw() { return bw; } public void setBw(BufferedWriter bw) { this.bw = bw; } public boolean exportCsvV2(HttpServletResponse response, List<String> dataList, List<String> headdataList, String fileName,boolean isFirst,boolean islast,List<String> footerList,int length) throws IOException { boolean isSucess=false; OutputStreamWriter osw = this.getOsw(); BufferedWriter bw = this.getBw(); try { if(isFirst){ response.reset(); //设置response response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("content-disposition", "attachment; filename="+fileName); } if(osw == null){ osw = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF })); this.setOsw(osw); } if(bw == null){ bw = new BufferedWriter(osw); this.setBw(bw); } if(isFirst){ if(headdataList!=null && !headdataList.isEmpty()){ for(int k = 0;k<length;k++){ bw.write(headdataList.get(k)+"\r\n"); bw.write(dataList.get(k)+"\r\n"); } } } if(footerList != null && !footerList.isEmpty()){ for(String data : footerList){ bw.write(data+"\r\n"); } } isSucess=true; } catch (Exception e) { isSucess=false; }finally{ if(islast){ if(bw!=null){ try { bw.close(); bw=null; } catch (IOException e) { e.printStackTrace(); } } if(osw!=null){ try { osw.close(); osw=null; } catch (IOException e) { e.printStackTrace(); } } }else{ if(bw!=null){ try { bw.flush(); } catch (IOException e) { e.printStackTrace(); } } if(osw!=null){ try { osw.flush(); } catch (IOException e) { e.printStackTrace(); } } } } return isSucess; } }