Java 将整行从Sheet1(源)复制到Sheet2(目标)的下一可用行
如何将整行从工作表1复制到下一个可用空行上相同电子表格的另一个工作表(2)?例如,第1行和第2行不是空的,如何使用Java将复制的行从工作表1粘贴到工作表2的第3行和后续行 以下是我正在编写的代码: 我发现了一个代码,它将行向下推1,但我需要填充下一个可用行的代码Java 将整行从Sheet1(源)复制到Sheet2(目标)的下一可用行,java,selenium,Java,Selenium,如何将整行从工作表1复制到下一个可用空行上相同电子表格的另一个工作表(2)?例如,第1行和第2行不是空的,如何使用Java将复制的行从工作表1粘贴到工作表2的第3行和后续行 以下是我正在编写的代码: 我发现了一个代码,它将行向下推1,但我需要填充下一个可用行的代码 public class CopyFromExcel { public static void copyFromExcel(data) throws Exception { String rowNumber
public class CopyFromExcel {
public static void copyFromExcel(data) throws Exception {
String rowNumber = data.gr.getRowNumber();
int rowNum = Integer.parseInt(rowNumber);
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("file path + filename.xlsx"));
XSSFSheet sourcesheet = workbook.getSheet("sourcesheetname");
XSSFSheet resultsheet = workbook.getSheet("destinationsheetname");
copyRow(workbook, sourcesheet, resultsheet, rowNum, 1);
FileOutputStream fos = new FileOutputStream(file path + filename.xlsx");
workbook.write(fos);
fos.close();
}
private static void copyRow(XSSFWorkbook workbook, XSSFSheet sourcesheet, XSSFSheet resultsheet, int sourceRowNum, int destinationRowNum) {
XSSFRow newRow = resultsheet.getRow(destinationRowNum);
XSSFRow sourceRow = sourcesheet.getRow(sourceRowNum);
// If the row exist in destination, push down all rows by 1 else create a new row
if (newRow != null) {
resultsheet.shiftRows(destinationRowNum, resultsheet.getLastRowNum(), 1);
}else{
newRow = resultsheet.createRow(destinationRowNum);
}
// Loop through source columns to add to new row
for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
XSSFCell oldCell = sourceRow.getCell(i);
XSSFCell newCell = newRow.createCell(i);
// If the old cell is null jump to next cell
if (oldCell == null){
newCell = null;
continue;
}
// 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;
}
}
}
}
公共类CopyFromExcel{
公共静态void copyFromExcel(数据)引发异常{
字符串rowNumber=data.gr.getRowNumber();
int rowNum=Integer.parseInt(rowNumber);
XSSF工作簿=新XSSF工作簿(新文件输入流(“文件路径+文件名.xlsx”);
XSSFSheet sourcesheet=workbook.getSheet(“sourcesheetname”);
XSSFSheet resultsheet=workbook.getSheet(“destinationsheetname”);
copyRow(工作簿、源表、结果表、rowNum、1);
FileOutputStream fos=新的FileOutputStream(文件路径+文件名.xlsx”);
练习册。写作(fos);
fos.close();
}
私有静态void copyRow(XSSF工作簿工作簿、XSSFSheet源表、XSSFSheet结果表、int sourceRowNum、int destinationRowNum){
XSSFRow newRow=resultsheet.getRow(destinationRowNum);
XSSFRow sourceRow=sourcesheet.getRow(sourceRowNum);
//如果目标中存在该行,请将所有行向下按1,否则创建新行
if(newRow!=null){
resultsheet.shiftRows(destinationRowNum,resultsheet.getLastRowNum(),1);
}否则{
newRow=resultsheet.createRow(destinationRowNum);
}
//循环源列以添加到新行
对于(int i=0;i
您可以使用getLastRowNum()
在工作表上,并以相同的增量获得空行。您可以检查Sheet2中有多少行,并将其用作下一个空闲行的索引。您好,谢谢Doshi,实际上工作表2是空的,它只包含标题,我在stackoverflow中发现一个代码,如果不为空,它将向下移动整行,我需要的是使用值和p跳过该行aste从工作表1中复制的行belowHi kumar,感谢您的评论。我确实使用了getlastRowNum(),但它只是向下移动该行,而不是实际跳转到下一个可用行//如果目标中存在该行,请按1向下移动所有行,否则如果(newRow!=null){resultsheet.shiftRows,请创建一个新行(destinationRowNum,resultsheet.getLastRowNum(),1);}其他{newRow=resultsheet.createRow(destinationRowNum);}