zoukankan      html  css  js  c++  java
  • 导出excel按照指定格式

    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 = "	";
        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)+"
    ");
                            bw.write(dataList.get(k)+"
    ");
    
                        }
                    }
                }
    
                if(footerList != null && !footerList.isEmpty()){
                    for(String data : footerList){
                        bw.write(data+"
    ");
                    }
                }
                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;
        }
    
    
    
    }
  • 相关阅读:
    VSTS知识整理 荣
    扯淡 荣
    我安装了SQL Server2005后,为什么在IIS的默认站点下面并没有发现Reports? 荣
    ERROR 32000 错误 荣
    vs2012程序打包部署下载InstallShield2015LimitedEdition的下载及安装打包整套教程
    微信小程序之保持登录状态即session不改变
    微信小程序如何调用API实现数据请求wx.request()
    改版kingsmotor.cn用到的参考网站
    第一个css+div网页(太弱智了)
    超级搞笑的笑话
  • 原文地址:https://www.cnblogs.com/thinkingandworkinghard/p/11881553.html
Copyright © 2011-2022 走看看