每两列读取Excel文件
如题:
现在有个需求,按照每两列读取,将读取到的数据转成属性文件?
Excel数据格式:
错误码 描述 异常原因 解决办法490080016 对用户名和密码加密时出错 对用户名和密码加密时出错 加密用户名和密码时出错490080017 域已经存在 域已经被接入 设备不同的域名或者接入不同的域490080018 设备已经被接入 这个设备已经被接入 490080019 重复浮动IP、主机IP和备机IP重复 设置不同的浮动IP、主机IP和备机IP
490080016_desc = 对用户名和密码加密时出错。490080016_detail = 对用户名和密码加密时出错。490080016_reason = 无法对用户名和密码进行加密。490080016_advice = 加密用户名和密码时出错。
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) { } } }
[解决办法]
两种方式: /** * 读取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列数据。。 } } }