首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

每两列读取Excel文件,该怎么处理

2012-03-31 
每两列读取Excel文件如题:现在有个需求,按照每两列读取,将读取到的数据转成属性文件?Excel数据格式:Java c

每两列读取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) {                }            }        } 


[解决办法]

Java code
两种方式:     /**     * 读取xls文件     *      * @throws Exception     */    public static void readXlsfileMap(File filePath) {                try {            InputStream is = new FileInputStream(filePath);            Workbook rwb = Workbook.getWorkbook(is);            Sheet[] sheets = rwb.getSheets();// 获得当前Excel表共有几个sheet            int p = sheets.length;            for (int w = 0; w < p; w++) {                // 将每个sheet中的内容全部读取出来                Sheet rs = rwb.getSheet(w);                int rows = rs.getRows();// 行循环,Excel的行列是从(0,0)开始的,下列for循环从第二行第一列开始。。。                for (int d = 1; d < rows; d++) {                    Cell b00 = rs.getCell(0, d);                    String strb = b00.getContents();  //每行第一列数据                }            }        } catch (Exception e) {            System.out.println(e.getMessage());        }    }        /**     * 读取xls文件     *      * @throws Exception     */    public static void readXlsfile2(InputStream is){        POIFSFileSystem fs = null;         HSSFWorkbook wb = null;         HSSFSheet sheet = null;         HSSFRow row = null;        try{            fs = new POIFSFileSystem(is);            wb = new HSSFWorkbook(fs);        }catch (Exception e) {            System.out.println(e.getMessage());        }        for (int w = 0; w < 1; w++) {            sheet = wb.getSheetAt(0);            int rowNum = sheet.getLastRowNum();            row = sheet.getRow(0);            for (int i = 1; i <= rowNum; i++) {                row = sheet.getRow(i);                String col0 = row.getCell((short)0).toString());  // 如:col0为每行第1列数据。。            }        }    } 

热点排行