zoukankan      html  css  js  c++  java
  • Excel 读取写入数据库

    // Excel 读取写入数据库 // 3.8版本的poi  4.0 可以不用写  parseCell  这个方法,可以直接赋值 STRING 类型 

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    
    import org.apache.poi.ss.usermodel.DateUtil;
    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 com.e6soft.base.service.JService;
    import com.e6soft.base.annotations.JCall;
    import com.e6soft.base.util.WebUtil;
    
    
    @JActionService(uri="ledger") 
    public class Ledger extends JService {
    
    private String parseCell(Cell cell){
            String cellStr = "";
            switch(cell.getCellType()){
            
                case HSSFCell.CELL_TYPE_NUMERIC : // 数学
                    if(HSSFDateUtil.isCellDateFormatted(cell)){ // 日期,时间
                        SimpleDateFormat sdf = null;
                        if(cell.getCellStyle().getDataFormat() == 
                                HSSFDataFormat.getBuiltinFormat("h:mm")){ // 时间 HH:mm
                            sdf = new SimpleDateFormat("HH:mm");
                        } else {
                            sdf = new SimpleDateFormat("yyyy-MM-dd");
                        }
                        cellStr = sdf.format(cell.getDateCellValue());
                    }else if(cell.getCellStyle().getDataFormat() == 58){
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        double temp = cell.getNumericCellValue();
                        Date date = DateUtil.getJavaDate(temp);
                        cellStr = sdf.format(date);
                    }else{
                        double temp = cell.getNumericCellValue();
                        String style = cell.getCellStyle().getDataFormatString();
                        DecimalFormat format = new DecimalFormat();
                        if(style.equals("General")){
                            format.applyPattern("#");
                        }
                        cellStr = format.format(temp);
                    }
                    break;
                case HSSFCell.CELL_TYPE_STRING : // 字符串
                    cellStr = cell.getRichStringCellValue().toString();
                    break;
                case HSSFCell.CELL_TYPE_BLANK : // 布尔
                    cellStr = "";
                    break;
                default :
                    cellStr = "";
            }
            
            return cellStr;
        }
        
        @JCall
        public void  ImportTable() throws Exception{ // 读取上传文件的xls 存入数据库
            /*实现过程:用户先上传文件至服务器的临时位置,然后写一个读取excel的方法,程序从服务器的临时位置读取excel文件;
             * 然后循环工作表和行,将单元格的内容存入集合;再上传至临时表(每次先清空),再根据sku(主键)更新正式数据库即可完成导入文件更新字段信息。*/
            
            String Id=WebUtil.getParam("id");
            String fileSaverId=WebUtil.getParam("fileSaverId");
            String fwqdz=WebUtil.getParam("dz");
            String dqid = WebUtil.getParam("dqid"); // 文件id
            String gs=WebUtil.getParam("gs");
            
            String sql = " SELECT I.FILE_INFO_ID,I.FILE_INDEX,I.FILE_FILENAME,I.FILE_URL,I.FILE_SAVER "
                        +" FROM FSXC3.T_P_FILE_INFO I "
                        +" WHERE I.FILE_SAVER = '"+fileSaverId+"' AND I.FILE_INDEX = '"+Id+"' and I.FILE_INFO_ID = '"+dqid+"'";// not_save_402881f66abf8173016abfae26d00011"
            List<Map<String, Object>> sj = dBSelect(sql);
            String wj = "";
            String savePath=System.getProperty("catalina.home")+"/webapps/webdav/";
            for(Map<String, Object> dz:sj){
                wj = dz.get("file_info_id").toString();
            }
            String widz = savePath+wj+".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行不读
                    for (int r = 2; 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.print(cellValue + "    ");  
                        }  */
                        
                        // 定义每一行的变量
                        
                        //String ID = parseCell(row.getCell(0));  
                        String XMMC = parseCell(row.getCell(0)); 
                        String ZTZ = parseCell(row.getCell(1)); 
                        String ZJLY = parseCell(row.getCell(2)); 
                        String GCJSTZ = parseCell(row.getCell(3)); 
                        String JSGM = parseCell(row.getCell(4)); 
                        String JHGQ = parseCell(row.getCell(5)); 
                        String C = parseCell(row.getCell(6)); 
                        String CC = parseCell(row.getCell(7));
                        
                        String CCC = parseCell(row.getCell(8));  
                        String CCCC = parseCell(row.getCell(9)); 
                        String XJ = parseCell(row.getCell(10)); 
                        String QQFY = parseCell(row.getCell(11)); 
                        String GCFY = parseCell(row.getCell(12)); 
                        String ZDCQFY = parseCell(row.getCell(13)); 
                        String TZXJ = parseCell(row.getCell(14)); 
                        String JDJH = parseCell(row.getCell(15)); 
                        String ZJJH = parseCell(row.getCell(16));
                        
                        String LX = parseCell(row.getCell(17)); 
                        String WJJY = parseCell(row.getCell(18)); 
                        String ZCSS = parseCell(row.getCell(19)); 
                        String XMXZ = parseCell(row.getCell(20)); 
                        String CZWT = parseCell(row.getCell(21)); 
                        String YDJHCZ = parseCell(row.getCell(22)); 
                        String NF = parseCell(row.getCell(23));
                        
                        //按照字符串类型读取单元格内数据
                        //String a = A.getStringCellValue();
    
                        Map<String,Object> dataMap = new HashMap<String,Object>();
                        // 顺序一致!!
                        //dataMap.put("ID", ID);//ID    ID
                        dataMap.put("XMMC", XMMC); // XMMC 
                        dataMap.put("ZTZ", ZTZ); // ZTZ        
                        dataMap.put("ZJLY", ZJLY); // ZJLY   
                        dataMap.put("GCJSTZ", GCJSTZ); // GCJSTZ    
                        dataMap.put("JSGM", JSGM); // JSGM    
                        dataMap.put("JHGQ", JHGQ); // JHGQ   
                        dataMap.put("C", C); // C        
                        dataMap.put("CC", CC);// CC       
                        dataMap.put("CCC", CCC); // CCC      
                        dataMap.put("CCCC", CCCC); // CCCC  
                        dataMap.put("XJ", XJ); // XJ        
                        dataMap.put("QQFY", QQFY); // QQFY   
                        dataMap.put("GCFY", GCFY); // GCFY   
                        dataMap.put("ZDCQFY", ZDCQFY); // ZDCQFY    
                        dataMap.put("TZXJ", TZXJ); // TZXJ    
                        dataMap.put("JDJH", JDJH); // JDJH    
                        dataMap.put("ZJJH", ZJJH); // ZJJH    
                        dataMap.put("LX", LX); //LX        
                        dataMap.put("WJJY", WJJY); //WJJY    
                        dataMap.put("ZCSS", ZCSS); //ZCSS    
                        dataMap.put("XMXZ", XMXZ); //XMXZ   
                        dataMap.put("CZWT", CZWT); //CZWT
                        dataMap.put("YDJHCZ", YDJHCZ); //YDJHCZ
                        dataMap.put("NF", NF); //NF        年份,用于区分年度填报
                        //dataMap.put("DLRID", I); //DLRID    登录人id
                        
                         dataMap.put("GS", gs); // gs  
                        
                        // System.out.println(dataMap); 
                        // 加入 
                        //qbsj.add(dataMap);
                        
                        
                        // 写入数据库
                    
                        this.insert(dataMap,"t_fsxc_tzjsjh",null);
                    }  
               // }  
          
            }  
            catch (Exception e) {  
                e.printStackTrace();  
            }
    
            
        
          }  
            
        }
  • 相关阅读:
    mysql复合索引的优点和注意事项
    linux服务器时间更新
    mysql慢日志
    Proftpd linux服务器FTP安装配置
    js 截取字符串
    在工信部注销网站备案
    史玉柱传奇
    css white-space属性
    获取微信授权
    禁用ipv6
  • 原文地址:https://www.cnblogs.com/mysterious-killer/p/10912962.html
Copyright © 2011-2022 走看看