/** * 新增一行數據並拷貝上一行的樣式 * * @param sheet * @param startRow 開始新增行數 * @param cellNum 增加的列數 * @return */ private void insertRow(HSSFSheet sheet, int startRow, int cellNum) { sheet.shiftRows(startRow, sheet.getLastRowNum(), 1, true, false); //獲取當前行 HSSFRow rowSource = sheet.getRow(startRow - 1); //獲取當前行樣式 HSSFCellStyle rowStyle = rowSource.getRowStyle(); //新增行 HSSFRow rowInsert = sheet.createRow(startRow); if (rowStyle != null) { rowInsert.setRowStyle(rowStyle); rowInsert.setHeight(rowSource.getHeight()); } for (int col = 0; col < cellNum; col++) { HSSFCell cellSource = rowSource.getCell(col); HSSFCell cellInsert = rowInsert.createCell(col); HSSFCellStyle cellStyle = cellSource.getCellStyle(); //設置單元格樣式 if (cellStyle != null) { cellInsert.setCellStyle(cellStyle); } } }
