Java 如何使用ApachePOI将现有excel工作表中的行复制到新的excel工作表?
我想比较两个excel工作表,找出是否有行不同,如果有,我想将与该excel不同的单行复制到新excel中的新行。下面是我从网络上获取的一段代码片段,并尝试过,但问题在于如果我将现有excel的第10行(例如)复制到新excel的第一行,则第一行会正确复制到新excel中,但第一个excel中的所有剩余行也会复制到新excel中。我想问题出在我写新excel的方式上[工作簿.写(出)]请帮忙!!提前谢谢Java 如何使用ApachePOI将现有excel工作表中的行复制到新的excel工作表?,java,excel,apache-poi,Java,Excel,Apache Poi,我想比较两个excel工作表,找出是否有行不同,如果有,我想将与该excel不同的单行复制到新excel中的新行。下面是我从网络上获取的一段代码片段,并尝试过,但问题在于如果我将现有excel的第10行(例如)复制到新excel的第一行,则第一行会正确复制到新excel中,但第一个excel中的所有剩余行也会复制到新excel中。我想问题出在我写新excel的方式上[工作簿.写(出)]请帮忙!!提前谢谢 public class RowCopy { public static void
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;
}
}
}
公共类行复制{
公共静态void main(字符串[]args)引发异常{
HSSFWorkbook=新的HSSFWorkbook(新文件输入流(“c:/input.xls”);
HSSFSheet sheet=workbook.getSheet(“Sheet1”);
复制行(工作簿,工作表,0,1);
FileOutputStream out=新的FileOutputStream(“c:/output.xls”);
练习册。写(出);
out.close();
}
专用静态无效复制行(HSSF工作簿、HSSF工作表、int sourceRowNum、int destinationRowNum){
//获取源/新行
HSSFRow newRow=工作表.getRow(destinationRowNum);
HSSFRow sourceRow=工作表.getRow(sourceRowNum);
//如果目标中存在该行,请将所有行向下按1,否则创建新行
if(newRow!=null){
sheet.shiftRows(destinationRowNum,sheet.getLastRowNum(),1);
}否则{
newRow=工作表.createRow(destinationRowNum);
}
//循环源列以添加到新行
对于(int i=0;i
只需修改copyRow
方法,添加参数HSSFSheet resultSheet
,然后在方法中修改newRow
变量,以从resultSheet
中获得该变量,如下所示
private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, HSSFSheet resultSheet, int sourceRowNum, int destinationRowNum) {
// Get the source / new row
HSSFRow newRow = resultSheet.getRow(destinationRowNum);
HSSFRow sourceRow = worksheet.getRow(sourceRowNum);
从您的目标“output.xls”获取该结果表只需修改
copyRow
方法,添加参数HSSFSheet resultSheet
,然后在方法中修改newRow
变量,即可从resultSheet
获得该结果表,如下所示
private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, HSSFSheet resultSheet, int sourceRowNum, int destinationRowNum) {
// Get the source / new row
HSSFRow newRow = resultSheet.getRow(destinationRowNum);
HSSFRow sourceRow = worksheet.getRow(sourceRowNum);
从您的目标“output.xls”获取结果表从一张表复制到另一张表
public static void copyFromSourceToDestinationRow(XSSFWorkbook workbook, XSSFSheet sourceWorksheet, int sourceRowNum, XSSFSheet destinationWorksheet, int destinationRowNum) {
// Get the source / new row
XSSFRow sourceRow = sourceWorksheet.getRow(sourceRowNum);
XSSFRow newRow = destinationWorksheet.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
XSSFCell oldCell = sourceRow.getCell(i);
XSSFCell newCell = newRow.createCell(i);
// If the old cell is null jump to next cell
if (oldCell == null) {
continue;
}
// Copy style from old cell and apply to new cell
XSSFCellStyle 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.getCellTypeEnum());
// Set the cell data value
switch (oldCell.getCellTypeEnum()) {
case BLANK:// Cell.CELL_TYPE_BLANK:
newCell.setCellValue(oldCell.getStringCellValue());
break;
case BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
case NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case STRING:
newCell.setCellValue(oldCell.getRichStringCellValue());
break;
default:
break;
}
}
// If there are are any merged regions in the source row, copy to new row
for (int i = 0; i < sourceWorksheet.getNumMergedRegions(); i++) {
CellRangeAddress cellRangeAddress = sourceWorksheet.getMergedRegion(i);
if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
(newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())),
cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
destinationWorksheet.addMergedRegion(newCellRangeAddress);
}
}
}
public static void copyFromSourceToDestinationRow(XSSF工作簿、XSSFSheet sourceWorksheet、int-sourceRowNum、XSSFSheet destinationWorksheet、int-destinationRowNum){
//获取源/新行
XSSFRow sourceRow=sourcesheet.getRow(sourceRowNum);
XSSFRow newRow=destinationWorksheet.createRow(destinationRowNum);
//循环源列以添加到新行
对于(int i=0;i