• POI 4.0 读取Excel


    ...

    package POIXLS;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellType; 
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    
    import java.sql.*;
    
    //import com.e6soft.base.service.JService;
    //import com.e6soft.base.annotations.JCall;
    //import com.e6soft.base.util.WebUtil;
     
    public class CS  {
        
        public static List<Map<String, Object>> E() throws Exception{
            /*用户先上传文件至服务器的临时位置,然后写一个读取excel的方法,程序从服务器的临时位置读取excel文件;
             * 然后循环工作表和行,将单元格的内容存入集合;再上传至临时表(每次先清空),再根据sku(主键)更新正式数据库即可完成导入文件更新字段信息。*/
              
            String widz =  "C:\Users\killer\Desktop\参考\工作簿1.xls"; // 文件在服务器的地址
            // 获得文件所在地
            File file = new File(widz);
            
            List<Map<String, Object>> qbsj = new ArrayList<Map<String,Object>>();
            // 读取文件
            try {  
                //同时支持Excel 2003、2007  
                //File excelFile = new File("F:\MyEclipse 2017 CI\SY\src\word\aa.xls"); //创建文件对象  
                FileInputStream is = new FileInputStream(file); //文件流  
                Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的  
                int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量  
                
                //遍历每个Sheet  
                //for (int s = 0; s < sheetCount; s++) {  
                    Sheet sheet = workbook.getSheetAt(0);  
                    int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数  
                    //遍历每一行  // 前3行不读
                    int jl = 0; // 记录上一行多少
                    for (int r = 4; r < rowCount; r++) {  
                        Row row = sheet.getRow(r);  
                        int cellCount = row.getPhysicalNumberOfCells(); //获取总列数  
                        
                        //遍历每一个单元格  
                        for (int c = 0; c < cellCount; c++) {  
                            Cell cell = row.getCell(c);  // 序号
                            CellType cellType = cell.getCellType();   // STRING 
                            String cellValue = null;
                            
                            //在读取单元格内容前,设置所有单元格中内容都是字符串类型
                            // cell.setCellType(CellType.STRING); // //设置单元格类型
                            cell.setCellType(CellType.STRING); // //设置单元格类型
                            
                            //按照字符串类型读取单元格内数据
                            cellValue = cell.getStringCellValue();
                            
                            //在这里可以对每个单元格中的值进行二次操作转化
                            
                            System.out.println(">>>"+cellValue);  
                        }  
                        //
                        CellType BHcellType = row.getCell(0).getCellType();   // STRING 
                        row.getCell(0).setCellType(BHcellType.STRING); // //设置单元格类型
                        String bh =row.getCell(0).getStringCellValue();
                         
                        CellType XMMCcellType = row.getCell(1).getCellType();   // STRING 
                        row.getCell(1).setCellType(XMMCcellType.STRING); // //设置单元格类型
                        String xmmc =row.getCell(1).getStringCellValue();
                          
                        CellType HTMCcellType = row.getCell(2).getCellType();   // STRING 
                        row.getCell(2).setCellType(HTMCcellType.STRING); // //设置单元格类型
                        String htmc =row.getCell(2).getStringCellValue();
                         
                        CellType HTDSRcellType = row.getCell(3).getCellType();   // STRING 
                        row.getCell(3).setCellType(HTDSRcellType.STRING); // //设置单元格类型
                        String htdsr =row.getCell(3).getStringCellValue();
                         
                        CellType HTNRcellType = row.getCell(4).getCellType();   // STRING 
                        row.getCell(4).setCellType(HTNRcellType.STRING); // //设置单元格类型
                        String htnr =row.getCell(4).getStringCellValue();
                          
                        CellType WTFScellType = row.getCell(5).getCellType();   // STRING 
                        row.getCell(5).setCellType(WTFScellType.STRING); // //设置单元格类型
                        String wtfs =row.getCell(5).getStringCellValue();
                          
                        CellType HTJKcellType = row.getCell(6).getCellType();   // STRING 
                        row.getCell(6).setCellType(HTJKcellType.STRING); // //设置单元格类型
                        String htjk =row.getCell(6).getStringCellValue();
                          
                        CellType HTGQcellType = row.getCell(7).getCellType();   // STRING 
                        row.getCell(7).setCellType(HTGQcellType.STRING); // //设置单元格类型
                        String htgq =row.getCell(7).getStringCellValue();
                          
                        CellType QDSJcellType = row.getCell(8).getCellType();   // STRING 
                        row.getCell(8).setCellType(QDSJcellType.STRING); // //设置单元格类型
                        String qdsj =row.getCell(8).getStringCellValue();
                          
                        CellType XMZBBMcellType = row.getCell(9).getCellType();   // STRING 
                        row.getCell(9).setCellType(XMZBBMcellType.STRING); // //设置单元格类型
                        String xmzbbm =row.getCell(9).getStringCellValue();
                          
                        CellType XBRcellType = row.getCell(10).getCellType();   // STRING 
                        row.getCell(10).setCellType(XBRcellType.STRING); // //设置单元格类型
                        String xbr =row.getCell(10).getStringCellValue();
                          
                        CellType YJGDSJcellType = row.getCell(11).getCellType();   // STRING 
                        row.getCell(11).setCellType(YJGDSJcellType.STRING); // //设置单元格类型
                        String yjgdsj =row.getCell(11).getStringCellValue();
                          
                        CellType HTJXQKcellType = row.getCell(12).getCellType();   // STRING 
                        row.getCell(12).setCellType(HTJXQKcellType.STRING); // //设置单元格类型
                        String htjxqk =row.getCell(12).getStringCellValue();
                          
                        CellType YSRQcellType = row.getCell(13).getCellType();   // STRING 
                        row.getCell(13).setCellType(YSRQcellType.STRING); // //设置单元格类型
                        String ysrq =row.getCell(13).getStringCellValue();
                         /*
                        CellType NFcellType = row.getCell(14).getCellType();   // STRING 
                        row.getCell(14).setCellType(NFcellType.STRING); // //设置单元格类型
                        String nf =row.getCell(14).getStringCellValue();  */
                         
                        row.getCell(14).setCellType(row.getCell(14).getCellType().STRING); // //设置单元格类型
                        String nf =row.getCell(14).getStringCellValue();  
                        
                        Map<String,Object> dataMap = new HashMap<String,Object>();
                        
                        if(bh.equals("") && xmmc.equals("") && htmc.equals("")&& !htdsr.equals("")){ // 编号&项目&名称为空,    人不为空的情况
                            if(qbsj.get(jl-1).get("htdsr_y").equals("")){
                                qbsj.get(jl-1).put("htdsr_y",qbsj.get(jl-1).get("htdsr_y") + row.getCell(3).getStringCellValue());
                            }else{
                                qbsj.get(jl-1).put("htdsr_y",qbsj.get(jl-1).get("htdsr_y") +","+ row.getCell(3).getStringCellValue()); // 两个甲方,两个乙方 ,丙方,丁方,的情况
                            }
                        }else if(!bh.equals("") && xmmc.equals("") && htmc.equals("")){ // 合同编号不为空,项目名称    与合同名称为空的情况
                            // 
                             
                            dataMap.put("bh", bh); // gs
                            dataMap.put("xmmc", qbsj.get(jl-1).get("xmmc"));
                            dataMap.put("htmc", qbsj.get(jl-1).get("htmc"));
                            if(!htdsr.equals("")){
                                if(htdsr.substring(0,1).equals("乙")){// 编号不为空,项目与名称为空的情况       其甲方没有的情况 也是甲方在上层的情况
                                    dataMap.put("htdsr_j", qbsj.get(jl-1).get("htdsr_j"));
                                    dataMap.put("htdsr_y", htdsr);
                                    
                                }else{
                                    dataMap.put("htdsr_j", htdsr);
                                    dataMap.put("htdsr_y", "");
                                }
                            }
                            // 是否为空的情况
                            if(htnr.equals("")){
                                dataMap.put("htnr", qbsj.get(jl-1).get("htnr"));
                            }else{
                                dataMap.put("htnr", htnr);
                            }
                            if(wtfs.equals("")){
                                dataMap.put("wtfs", qbsj.get(jl-1).get("wtfs"));
                            }else{
                                dataMap.put("wtfs", wtfs);
                            }
                            if(htjk.equals("")){
                                dataMap.put("htjk", qbsj.get(jl-1).get("htjk"));
                            }else{
                                dataMap.put("htjk", htjk);
                            }
                            if(htgq.equals("")){
                                dataMap.put("htgq", qbsj.get(jl-1).get("htgq"));
                            }else{
                                dataMap.put("htgq", htgq);
                            }
                            if(qdsj.equals("")){
                                dataMap.put("qdsj", qbsj.get(jl-1).get("qdsj"));
                            }else{
                                dataMap.put("qdsj", qdsj);
                            }
                            if(xmzbbm.equals("")){
                                dataMap.put("xmzbbm", qbsj.get(jl-1).get("xmzbbm"));//
                            }else{
                                dataMap.put("xmzbbm", xmzbbm);//
                            }
                            if(xbr.equals("")){
                                dataMap.put("xbr", qbsj.get(jl-1).get("xbr"));
                            }else{
                                dataMap.put("xbr", xbr);
                            }
                            if(yjgdsj.equals("")){
                                dataMap.put("yjgdsj", qbsj.get(jl-1).get("yjgdsj"));
                            }else{
                                dataMap.put("yjgdsj", yjgdsj);
                            }
                            if(htjxqk.equals("")){
                                dataMap.put("htjxqk", qbsj.get(jl-1).get("htjxqk"));
                            }else{
                                dataMap.put("htjxqk", htjxqk);
                            }
                            if(ysrq.equals("")){
                                dataMap.put("ysrq", qbsj.get(jl-1).get("ysrq"));
                            }else{
                                dataMap.put("ysrq", ysrq);
                            }
                            dataMap.put("nf", nf);  
                            qbsj.add(dataMap);
                            jl++;
                         // }else if(){ // 
                            
                        }else if(!bh.equals("") && xmmc.equals("") && !htmc.equals("")){ // 编号不为空,    项目为空,    名称不为空的情况
                             
                            dataMap.put("bh", bh); // gs
                            dataMap.put("xmmc", qbsj.get(jl-1).get("xmmc"));
                            dataMap.put("htmc", htmc);
                            dataMap.put("htdsr_j", htdsr);
                            dataMap.put("htdsr_y", "");
                            dataMap.put("htnr", htnr);
                            dataMap.put("wtfs", wtfs);
                            dataMap.put("htjk", htjk);
                            dataMap.put("htgq", htgq);
                            dataMap.put("qdsj", qdsj);
                            dataMap.put("xmzbbm", xmzbbm);//
                            dataMap.put("xbr", xbr);
                            dataMap.put("yjgdsj", yjgdsj);
                            dataMap.put("htjxqk", htjxqk);
                            dataMap.put("ysrq", ysrq);
                            dataMap.put("nf", nf); 
                            
                            qbsj.add(dataMap);jl++;
                            
                        }else if(bh.equals("") && !xmmc.equals("") && !htmc.equals("")){ //编号为空,项目与名称不为空的情况
                              
                            dataMap.put("bh", qbsj.get(jl-1).get("bh")); // gs
                            dataMap.put("xmmc", xmmc);
                            dataMap.put("htmc", htmc);
                            dataMap.put("htdsr_j", htdsr);
                            dataMap.put("htdsr_y", "");
                            dataMap.put("htnr", htnr);
                            dataMap.put("wtfs", wtfs);
                            dataMap.put("htjk", htjk);
                            dataMap.put("htgq", htgq);
                            dataMap.put("qdsj", qdsj);
                            dataMap.put("xmzbbm", xmzbbm);//
                            dataMap.put("xbr", xbr);
                            dataMap.put("yjgdsj", yjgdsj);
                            dataMap.put("htjxqk", htjxqk);
                            dataMap.put("ysrq", ysrq);
                            dataMap.put("nf", nf); 
                            
                            qbsj.add(dataMap);jl++;
    
                        }else if(!bh.equals("") && !xmmc.equals("") && !htmc.equals("")){ // 正常 
                             
                            
                            dataMap.put("bh", bh); // gs
                            dataMap.put("xmmc", xmmc);
                            dataMap.put("htmc", htmc);
                            dataMap.put("htdsr_j", htdsr);
                            dataMap.put("htdsr_y", "");
                            dataMap.put("htnr", htnr);
                            dataMap.put("wtfs", wtfs);
                            dataMap.put("htjk", htjk);
                            dataMap.put("htgq", htgq);
                            dataMap.put("qdsj", qdsj);
                            dataMap.put("xmzbbm", xmzbbm);//
                            dataMap.put("xbr", xbr);
                            dataMap.put("yjgdsj", yjgdsj);
                            dataMap.put("htjxqk", htjxqk);
                            dataMap.put("ysrq", ysrq);
                            dataMap.put("nf", nf); 
                            
                            qbsj.add(dataMap);jl++;
                            
                        }else if(bh.equals("") && xmmc.equals("") && htmc.equals("")&& htdsr.equals("")){ // 都为空的情况 // 有年份的情况
                            System.out.println(">>>>>>>>>>>>>>>>>>>>>>> 都为空的情况 <<<<<<<<<<<<<<<<<<<<<<");
                        }else{
                            System.out.println(">>>>>>>>>>>>>>>>>>>>>>> 未发现的情况 <<<<<<<<<<<<<<<<<<<<<<");
                        }
                        /*
                        // 定义每一行的变量
                        
                        //String ID = parseCell(row.getCell(0));   
    
                        Map<String,Object> dataMap = new HashMap<String,Object>();
                        // 顺序一致!! 
                        //dataMap.put("DLRID", I); //DLRID    登录人id
                        
                         dataMap.put("GS", ""); // gs  
                        
                        // System.out.println(dataMap); 
                        // 加入 
                        //qbsj.add(dataMap);*/
                         
                    }
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    System.out.println(">>>"+qbsj);
                    return qbsj;
          
            }  
            catch (Exception e) {  
                e.printStackTrace();  
                return qbsj;
            }
        } 
        
        
        public static void main(String[] args)  throws Exception{ 
            
            List<Map<String, Object>> sj = E(); 
            
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            //System.out.println(">>>"+fh); 
          }   
            
        }

    1.合同编号&项目名称&合同名称为空, 合同当事人不为空的情况

    2.合同编号不为空,项目名称 与合同名称为空的情况

    2.1合同编号不为空,项目名称与合同名称为空的情况       其甲方没有的情况 也是甲方在上层的情况

    3.合同编号不为空, 项目名称为空, 合同名称不为空的情况

    4.合同编号为空,项目名称与合同名称不为空的情况

    5.正常

    6.// 都为空的情况 // 有年份的情况

  • 相关阅读:
    java基础-数组
    泛型 --集合
    单例模式(新)
    static 关键字
    单例模式
    迭代器模式(java版)
    Object类
    JavaWeb学习总结(十七)——JSP中的九个内置对象
    javaweb学习总结(十六)——JSP指令
    javaweb学习总结(十五)——JSP基础语法
  • 原文地址:https://www.cnblogs.com/mysterious-killer/p/11223828.html
Copyright © 2020-2023  润新知