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; } }