/** * @param srcSheet the sheet to copy. * @param destSheet the sheet to create. * @param srcRow the row to copy. * @param destRow the row to create. * @param styleMap - */ public static void copyRow( XSSFSheet srcSheet, XSSFSheet destSheet, XSSFRow srcRow, XSSFRow destRow, Map<Integer, XSSFCellStyle> styleMap) { Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>(); destRow.setHeight(srcRow.getHeight()); for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) { XSSFCell oldCell = srcRow.getCell(j); XSSFCell newCell = destRow.getCell(j); if (oldCell != null) { if (newCell == null) { newCell = destRow.createCell(j); } copyCell(oldCell, newCell, styleMap); CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(), (short) oldCell.getColumnIndex()); if (mergedRegion != null) { CellRangeAddress newMergedRegion = new CellRangeAddress( mergedRegion.getFirstRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastRow(), mergedRegion.getLastColumn()); CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion); if (isNewMergedRegion(wrapper, mergedRegions)) { mergedRegions.add(wrapper); destSheet.addMergedRegion(wrapper.range); } } } } }
public static void addErrorRow(XSSFRow srcRow) { try { XSSFRow destRow = sheetWrite.createRow(rowCount); // XSSFRow destRow =null; for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) { XSSFCell oldCell = srcRow.getCell(j); // ancienne cell XSSFCell newCell = destRow.getCell(j); // new cell if (oldCell != null) { if (newCell == null) { newCell = destRow.createCell(j); } copyCell(oldCell, newCell); } } } catch (Exception ex) { System.out.println("Error in writing Row in reject excel file :: " + ex.getMessage()); ex.printStackTrace(); } }
/** * 读取2007-2013格式 * * @param filePath 文件路径 * @return * @throws java.io.IOException */ @SuppressWarnings("rawtypes") public static List<Map> readExcel2007(String filePath) throws IOException { List<Map> valueList = new ArrayList<Map>(); FileInputStream fis = null; try { fis = new FileInputStream(filePath); XSSFWorkbook xwb = new XSSFWorkbook(fis); // 构造 XSSFWorkbook 对象,strPath 传入文件路径 XSSFSheet sheet = xwb.getSheetAt(0); // 读取第一章表格内容 // 定义 row、cell XSSFRow row; // 循环输出表格中的第一行内容 表头 Map<Integer, String> keys = new HashMap<Integer, String>(); row = sheet.getRow(0); if (row != null) { // System.out.println("j = row.getFirstCellNum()::"+row.getFirstCellNum()); // System.out.println("row.getPhysicalNumberOfCells()::"+row.getPhysicalNumberOfCells()); for (int j = row.getFirstCellNum(); j <= row.getPhysicalNumberOfCells(); j++) { // 通过 row.getCell(j).toString() 获取单元格内容, if (row.getCell(j) != null) { if (!row.getCell(j).toString().isEmpty()) { keys.put(j, row.getCell(j).toString()); } } else { keys.put(j, "K-R1C" + j + "E"); } } } // 循环输出表格中的从第二行开始内容 for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getPhysicalNumberOfRows(); i++) { row = sheet.getRow(i); if (row != null) { boolean isValidRow = false; Map<String, Object> val = new HashMap<String, Object>(); for (int j = row.getFirstCellNum(); j <= row.getPhysicalNumberOfCells(); j++) { XSSFCell cell = row.getCell(j); if (cell != null) { String cellValue = null; if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { if (DateUtil.isCellDateFormatted(cell)) { cellValue = new DataFormatter() .formatRawCellContents( cell.getNumericCellValue(), 0, "yyyy-MM-dd HH:mm:ss"); } else { cellValue = String.valueOf(cell.getNumericCellValue()); } } else { cellValue = cell.toString(); } if (cellValue != null && cellValue.trim().length() <= 0) { cellValue = null; } val.put(keys.get(j), cellValue); if (!isValidRow && cellValue != null && cellValue.trim().length() > 0) { isValidRow = true; } } } // 第I行所有的列数据读取完毕,放入valuelist if (isValidRow) { valueList.add(val); } } } } catch (IOException e) { e.printStackTrace(); } finally { fis.close(); } return valueList; }