zoukankan      html  css  js  c++  java
  • java解析excel:案例一

    需求:

    转成

    代码如下:

      

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.lang.StringUtils;
    
    import com.common.util.ExcelReader;
    import com.common.util.dto.ExcelDto;
    import com.common.util.dto.ExcelSheetDto;
    import com.dr.util.PublicUtil;
    
    public class ExcelReaderTest {
        public static void main(String[] args) throws FileNotFoundException {
            //引入excel解析
            ExcelReader excelReader = new ExcelReader();
            
    //        List<Map> list = new ArrayList<Map>();
            Map<String, List<String>> map = new HashMap<String, List<String>>();
            File file = new File("E:\testExcel\sxd.xlsx");
            
            try {
                ExcelDto excelDto = excelReader.readExcel(new FileInputStream(file), file.getName());
                List<ExcelSheetDto> excelSheet= excelDto.getExcelSheetList();
                
                //获取最后一个不为空的sheet
                ExcelSheetDto sheetDto=PublicUtil.getLastNotNullSheet(excelSheet);
                
                String[] colNames = sheetDto.getColNames();
                
                String[][] contents = sheetDto.getRowsContent();
                // 设置时间
                for (int i = 1; i < contents.length; i++) {
                    if (contents[i] != null && StringUtils.isEmpty(contents[i][0])
                            && i > 0) {
                        contents[i][0] = contents[i - 1][0];
                    }
                }
                
                if(colNames!=null){
                    for (int i = 0; i < colNames.length; i++) {
                        int count = 1;
                        String colName = colNames[i];
                        List<String> list = new ArrayList<String>();
                        for (int j = 0; j < contents.length; j++) {
                            list.add(contents[j][i]);
                        }
                        map.put(colName, list);
                    }
                    for (int i = 0; i < map.get("日期").size()/2; i++) {
                        for (int j = 1; j < 5; j++) {
                            System.out.println(map.get("日期").get(i*2));
                            String apptype = "XJ_JLM_JLM0"+j;
                            System.out.println(apptype);
                            System.out.println(map.get(apptype).get(i*2));
                            System.out.println(map.get(apptype).get(i*2+1));
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            
        }
    }
  • 相关阅读:
    Mysql系列(十二)—— 索引下推优化
    Mysql系列(十一)—— 性能分析其他常用监控
    套路篇(一)之软件配置
    Mysql中的变量
    Mysql系列(十一)—— 性能分析慢查询日志
    Mysql系列(十)—— 性能分析工具profiling
    Mysql系列(九)—— 性能分析explain执行计划
    secure-file-priv特性
    Levenberg-Marquardt优化和zipf分布
    关于ADM和高维空间下距离度量的问题
  • 原文地址:https://www.cnblogs.com/rsdqc/p/7840107.html
Copyright © 2011-2022 走看看