栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用HSSF(Apache POI)在现有Excel中的两行之间插入一行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用HSSF(Apache POI)在现有Excel中的两行之间插入一行

辅助功能从这里无耻地复制行

import org.apache.poi.hssf.usermodel.*;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.util.CellRangeAddress;import java.io.FileInputStream;import java.io.FileOutputStream;public class RowCopy {    public static void main(String[] args) throws Exception{        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("c:/input.xls"));        HSSFSheet sheet = workbook.getSheet("Sheet1");        copyRow(workbook, sheet, 0, 1);        FileOutputStream out = new FileOutputStream("c:/output.xls");        workbook.write(out);        out.close();    }    private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {        // Get the source / new row        HSSFRow newRow = worksheet.getRow(destinationRowNum);        HSSFRow sourceRow = worksheet.getRow(sourceRowNum);        // If the row exist in destination, push down all rows by 1 else create a new row        if (newRow != null) { worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);        } else { newRow = worksheet.createRow(destinationRowNum);        }        // Loop through source columns to add to new row        for (int i = 0; i < sourceRow.getLastCellNum(); i++) { // Grab a copy of the old/new cell HSSFCell oldCell = sourceRow.getCell(i); HSSFCell newCell = newRow.createCell(i); // If the old cell is null jump to next cell if (oldCell == null) {     newCell = null;     continue; } // Copy style from old cell and apply to new cell HSSFCellStyle newCellStyle = workbook.createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); ; newCell.setCellStyle(newCellStyle); // If there is a cell comment, copy if (oldCell.getCellComment() != null) {     newCell.setCellComment(oldCell.getCellComment()); } // If there is a cell hyperlink, copy if (oldCell.getHyperlink() != null) {     newCell.setHyperlink(oldCell.getHyperlink()); } // Set the cell data type newCell.setCellType(oldCell.getCellType()); // Set the cell data value 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; }        }        // If there are are any merged regions in the source row, copy to new row        for (int i = 0; i < worksheet.getNumMergedRegions(); i++) { CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {     CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),  (newRow.getRowNum() +          (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()       )),  cellRangeAddress.getFirstColumn(),  cellRangeAddress.getLastColumn());     worksheet.addMergedRegion(newCellRangeAddress); }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419001.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号