我正在尝试编写一个方法,它接受两个HSSFSheets(来自两个不同的HSSFWorkbooks,通过FileChooser导入),并确保Sheet1的每一行的名称(如Sheet1的每一行的第一个单元格中)必须是与Sheet2的每一行相同的名称 . 如果不是这种情况,则该方法必须重新组织Sheet2,因此Sheet2的每一行的名称应与Sheet1的名称相匹配 .

最后,该方法应创建并打开一个新的excel文件,该文件应包含重组的Sheet2 . 这就是问题所在

public void organizeSheets(HSSFSheet sheet1, HSSFSheet sheet2) throws FileNotFoundException, IOException {

    HSSFWorkbook permutationWb = new HSSFWorkbook();
    HSSFSheet permutationSheet = permutationWb.createSheet();
    short noOfColumns;
    HSSFRow testrow = sheet1.getRow(0);
    short testcell = testrow.getLastCellNum();
    noOfColumns = testcell;
    int noOfRows = sheet1.getPhysicalNumberOfRows();
    short noOfColumns2;
    HSSFRow testrow2 = sheet2.getRow(0);
    short testcell2 = testrow2.getLastCellNum();
    noOfColumns2 = testcell2;

    for (int i = 0; i < noOfRows; i++) {
        if (!getValue(sheet1, 0, i).toString().equals(getValue(sheet2, 0, i).toString())) {

            System.out.println(getValue(sheet1, 0, i));
            System.out.println(getValue(sheet2, 0, i));
            int j = 0;

            while (!getValue(sheet1, 0, i).toString().equals(getValue(sheet2, 0, j).toString())) {
                j = j + 1;
            }

            HSSFRow transitionRow1 = permutationSheet.createRow(0);
            HSSFRow transitionRow2 = permutationSheet.createRow(1);

            for (int l=0; l < noOfColumns2; l++) {
                transitionRow1.createCell(l);
                transitionRow1.getCell(l).setCellValue(getValue(sheet2, l, j));
                //System.out.println(transitionRow1.getCell(l));
            }

            for (int p=0; p < noOfColumns2; p++) {
                transitionRow2.createCell(p);
                transitionRow2.getCell(p).setCellValue(getValue(sheet2, p, i));
            }


            for (int k=0; k < noOfColumns2; k++) {
                sheet2.getRow(j).getCell(k).setCellValue(getValue(permutationSheet, k, 1).toString());
                sheet2.getRow(i).getCell(k).setCellValue(getValue(permutationSheet, k, 0).toString());

            }
        }
    }
    sheet2.getWorkbook().write(new FileOutputStream("C:\\Users\\PC HP\\Desktop\\organized.xls"));
    sheet2.getWorkbook().close();
    Desktop.getDesktop().open(new File("C:\\Users\\PC HP\\Desktop\\organized.xls"));

}

getValue(sheet,row,column)允许我获取坐标位于方法参数中的单元格的值 .

这是我得到的错误消息

Exception in thread "main" java.lang.IndexOutOfBoundsException: Block 33 not found
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.getBlockAt(NPOIFSFileSystem.java:486)
at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:169)
at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:142)
at org.apache.poi.poifs.filesystem.NDocumentInputStream.readFully(NDocumentInputStream.java:248)
at org.apache.poi.poifs.filesystem.NDocumentInputStream.read(NDocumentInputStream.java:150)
at org.apache.poi.poifs.filesystem.DocumentInputStream.read(DocumentInputStream.java:125)
at org.apache.poi.hpsf.PropertySet.isPropertySetStream(PropertySet.java:336)
at org.apache.poi.hpsf.PropertySet.<init>(PropertySet.java:241)
at org.apache.poi.hpsf.PropertySetFactory.create(PropertySetFactory.java:92)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:211)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:168)
at org.apache.poi.POIDocument.readProperties(POIDocument.java:141)
at org.apache.poi.POIDocument.getSummaryInformation(POIDocument.java:108)
at org.apache.poi.POIDocument.writeProperties(POIDocument.java:260)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1377)
at model.main.organizeSheets(main.java:377)
at model.main.main(main.java:42)
Caused by: java.lang.IndexOutOfBoundsException: Unable to read 512 bytes from 17408 in stream of length -1
at     org.apache.poi.poifs.nio.ByteArrayBackedDataSource.read(ByteArrayBackedDataSource.java:42)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.getBlockAt(NPOIFSFileSystem.java:484)
... 16 more

我相信Sheet2行之间的实际切换没有问题(我已经用打印测试了,如果我测试不够,我可能会错)并且我不知道如何处理这种情况 .

为了实例化Sheet2,代码要求用户从他的计算机中选择一个工作簿,然后在工作簿中选择一个工作表,如下所示

excelworkbook1 = o.chooseFile1();
    Scanner scanner1 = new Scanner(System.in);
    int sheetnumber1 = Integer.parseInt(scanner1.nextLine());
    excelsheet1 = o.chooseSheet(excelworkbook1, sheetnumber1);

public HSSFWorkbook chooseFile1() throws IOException {
    JFrame frame = null;
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(frame);
    String filepath = new String();

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        filepath = fc.getSelectedFile().getCanonicalPath();
        setFile1Adress(filepath);
    } else {
        System.exit(1);
    }

    FileInputStream file1InputStream;
    file1InputStream = new FileInputStream(filepath);

    NPOIFSFileSystem file;

    file = new NPOIFSFileSystem(file1InputStream);

    HSSFWorkbook workbook = new HSSFWorkbook(file.getRoot(), true);
    file.close();

    return workbook;
}

 public HSSFSheet chooseSheet(HSSFWorkbook excelfile, int entry) {
    return excelfile.getSheetAt(entry - 1);
}