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;
        }
    
    
    
    }
  • 相关阅读:
    Kafka架构概述
    Logstash使用进阶篇
    Logstash使用快速入门
    Ubuntu环境部署Logstash实战案例
    Nginx代理Kibana并实现登录认证实战案例
    Ubuntu环境部署Kibana实战案例
    Idea打包JAR包图解
    Wormhole部署实战案例
    编译Wormhole实战篇
    Wormhole的核心概念
  • 原文地址:https://www.cnblogs.com/thinkingandworkinghard/p/11881553.html
Copyright © 2011-2022 走看看