日期:2014-05-20  浏览次数:20727 次

每两列读取Excel文件
如题:

现在有个需求,按照每两列读取,将读取到的数据转成属性文件?

Excel数据格式:
Java code

错误码          描述                            异常原因                        解决办法
490080016    对用户名和密码加密时出错        对用户名和密码加密时出错    加密用户名和密码时出错
490080017    域已经存在                    域已经被接入                    设备不同的域名或者接入不同的域
490080018    设备已经被接入                    这个设备已经被接入    
490080019    重复浮动IP、主机IP和备机IP重复    设置不同的浮动IP、主机IP和备机IP  



Properties数据格式:
Java code

490080016_desc   = 对用户名和密码加密时出错。
490080016_detail = 对用户名和密码加密时出错。
490080016_reason = 无法对用户名和密码进行加密。
490080016_advice = 加密用户名和密码时出错。



------解决方案--------------------
POI读取EXCEL..
parameterName就是错误码+四个后缀,parameterValue就是后面三列内容
//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
Print.print("ConfigInfoError","Visit "+filePath+" for updating "+parameterName+" value error");
}

------解决方案--------------------
Java code

FileInputStream fis = null;
        try {
            fis = new FileInputStream(filename) ;
            POIFSFileSystem fs = new POIFSFileSystem(fis);
            
            if(fs!=null){
                list = new ArrayList<CpVehicleOnline>() ;
                HSSFWorkbook wb = new HSSFWorkbook(fs);
                for (int k = 0; k < wb.getNumberOfSheets(); k++) {
    
                    HSSFSheet sheet = wb.getSheetAt(k);
    
                    int rows = sheet.getPhysicalNumberOfRows();
    
                    for (int r = 1; r < rows; r++) {
                        HSSFRow row = sheet.getRow(r);
                        if (row == null) {
                            continue;
                        }
                        
                        int cells = row.getPhysicalNumberOfCells();
                        
                        for (int c = 0; c < cells; c++) {
                            HSSFCell cell = row.getCell(c);
                            if (cell == null) {
                                continue;
                            }
    
                            switch (c) {
                                case 0:
                                    String x = "" ;
                                    switch (cell.getCellType()) {
                                        case HSSFCell.CELL_TYPE_STRING:
                                            x=cell.getStringCellValue().trim();
                                            break;
                                        default:
                                            break;
                                        }
                                    break;
                                case 1:
                                    
                                    break;
                                break;
                            }
                        }
                        
                    }
                }
            }
        
        } catch (Exception e) {
            log.error("", e);
        }finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (Exception e2) {
                }
            }
        }