poi

excel insert line

private void test8() throws IOException {
    InputStream inputStream = this
            .getClass()
            .getClassLoader()
            .getResourceAsStream(
                    "template/AdvancedStudyStudentAttendance.xls");
    HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = workbook.getSheetAt(0);
    copyRow(workbook, sheet, 7, 8);//index從1開始的第8行
    int row = 7;
    for (int cellIndex = 0; cellIndex < 14; cellIndex++) {
        Cell cell = sheet.getRow(row).getCell(cellIndex);
        cell.setCellValue(cellIndex + 1);
    }
    for (int cellIndex = 0; cellIndex < 14; cellIndex++) {
        Cell cell = sheet.getRow(8).getCell(cellIndex);
        cell.setCellValue(cellIndex + 1);
    }
    FileOutputStream output_file = new FileOutputStream(new File(
            "C:\\simple.xls"));
    workbook.write(output_file);
    output_file.close();
}

private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet,
        int sourceRowNum, int destinationRowNum) {
    HSSFRow newRow = worksheet.getRow(destinationRowNum);
    HSSFRow sourceRow = worksheet.getRow(sourceRowNum);
    if (newRow != null) {
        worksheet
                .shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {
        newRow = worksheet.createRow(destinationRowNum);
    }

    System.out.println("sourceRow.getLastCellNum():"
            + sourceRow.getLastCellNum());

    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        HSSFCell oldCell = sourceRow.getCell(i);
        HSSFCell newCell = newRow.createCell(i);
        if (oldCell == null) {
            newCell = null;
            continue;
        }
        HSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        newCell.setCellStyle(newCellStyle);
        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }
        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }
        newCell.setCellType(oldCell.getCellType());
        switch (oldCell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
        int firstRow = cellRangeAddress.getFirstRow();
        int lastRow = cellRangeAddress.getLastRow();
        int firstCol = cellRangeAddress.getFirstColumn();
        int lastCol = cellRangeAddress.getLastColumn();
        String result = String.format(
                "firstRow:%d, lastRow:%d, firstCol:%d, lastCol:%d",
                firstRow, lastRow, firstCol, lastCol);
        System.out.println(result);

        if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(
                    newRow.getRowNum(),
                    (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress
                            .getFirstRow())), cellRangeAddress
                            .getFirstColumn(), cellRangeAddress
                            .getLastColumn());
            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }
    int sheetRows = worksheet.getPhysicalNumberOfRows();
    for (int i = sheetRows-1; i > sourceRowNum; i--) {
        worksheet.getRow(i).setHeight(worksheet.getRow(i-1).getHeight());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值