public static void main(String[] args) throws IOException {
List<ProProduct> list = readXls(); for (ProProduct product : list){ ProProductClient.updateByProduct(product); }}
private static List<ProProduct> readXls() throws IOException {
InputStream is = new FileInputStream("/Users/wuzixin/work/meiliwan/trunk/product.xls"); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); ProProduct xlsDto = null; List<ProProduct> list = new ArrayList<ProProduct>(); // 循环工作表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 循环行Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } xlsDto = new ProProduct(); // 循环列Cell // 0学号 1姓名 2学院 3课程名 4 成绩 HSSFCell proId = hssfRow.getCell(0); if (proId == null) { continue; } xlsDto.setProId(Integer.valueOf(String.format("%.0f",Double.parseDouble(getValue(proId)))));HSSFCell status = hssfRow.getCell(3);
if (status == null) { continue; }else { if (getValue(status).equals("下架")){ xlsDto.setState((short)1); } } HSSFCell mlwPrice = hssfRow.getCell(8); if (mlwPrice == null) { continue; } xlsDto.setMlwPrice(new BigDecimal(getValue(mlwPrice))); list.add(xlsDto); } } return list; }@SuppressWarnings("static-access")
private static String getValue(HSSFCell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { // 返回布尔类型的值 return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { // 返回数值类型的值 return String.valueOf(String.format("%.2f",hssfCell.getNumericCellValue())); } else { // 返回字符串类型的值 return String.valueOf(hssfCell.getStringCellValue()); } }