poi进行xlsx 复制一行_如何使用HSSF(Apache POI)在现有excel中的两行之间插入一行...

使用Apache POI库在现有Excel文件中插入新行时,遇到格式未正确转移的问题。隐藏行未随新行插入而移动,且非单元格对象(如文本框)的位置固定不变。提供了复制行的辅助函数代码,以解决格式丢失和对象定位问题。
摘要由CSDN通过智能技术生成

Somehow I manage to create new rows between two rows in an existing excel file. The problem is, some of the formatting were not include along the shifting of the rows.

One of this, is the row that are hide are not relatively go along during the shift. What I mean is(ex.), rows from 20 to 30 is hidden, but when a create new rows the formating still there. The hidden rows must also move during the insertion/creation of new rows, it should be 21 to 31.

Another thing is, the other object in the sheet that are not in the cell. Like the text box are not move along after the new row is created. Its like the position of these object are fixed. But I want it to move, the same thing as I insert a new row or paste row in excel. If there is a function of inserting a new row, please let me know.

This what I have right now, just a snippet from my code.

HSSFWorkbook wb = new HSSFWorkbook(template); //template is the source of file

HSSFSheet sheet = wb.getSheet("SAMPLE");

HSSFRow newRow;

HSSFCell cellData;

int createNewRowAt = 9; //Add the new row between row 9 and 10

sheet.shiftRows(createNewRowAt, sheet.getLastRowNum(), 1, true, false);

newRow = sheet.createRow(createNewRowAt);

newRow = sheet.getRow(createNewRowAt);

If copy and paste of rows is possible that would be big help. But I already ask it here and can't find a solution. So I decided to create a row as an interim solution. I'm done with it but having a problem like this.

Any help will be much appreciated. Thanks!

解决方案

Helper function to copy rows shamelessly adapted from here

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);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值