• 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();
            }
            
            
        }
    }
  • 相关阅读:
    t
    bert4keras
    embeding应用-airbnb推荐
    The Neural Autoregressive Distribution Estimator
    3.redis desktop manager--redis 可视化工具安装及使用
    Day06作业(postman接口测试)
    DRF学习day01(web应用模式,api接口,RESTful API规范,序列化,Django Rest_Framework)
    restFul接口设计规范
    Vue学习之荏苒资讯项目(一)
    微信小程序开发四:Promise的使用,解决回调地狱
  • 原文地址:https://www.cnblogs.com/rsdqc/p/7840107.html
Copyright © 2020-2023  润新知