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.// 都為空的情況 // 有年份的情況


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM