0

私はデータを単一の列に書き込むことができました。私の要件は、最初の列のデータをオーバーライドせずに、次の隣接する列にデータを書き込むことです。何か助けはありますか?

InputStream inp;
    try {
        inp = new FileInputStream("D:\\test.xls");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        Row row =  null;

        Cell c = null;

        int rowNum=0;
        Set<String> keySet = new HashSet<String>();
        keySet = tushMap.keySet();
        for (Entry<String, String> entry : tushMap.entrySet()) {
            row=sheet.createRow((short) (sheet.getLastRowNum() + 1));
            System.out.println((short) (sheet.getLastRowNum() + 1));
            c = row.createCell(0);
            c.setCellValue("fff");
            System.out.println("fff");
            System.out.println(entry.getKey() + "/" + entry.getValue());
            rowNum++;
        }


        FileOutputStream fileOut = new FileOutputStream("D:\\test.xls");
        wb.write(fileOut);
        fileOut.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

これは、1 番目の列に入力するために記述されたコードです。2番目の列にデータを入力すると、最初の1が上書きされます.. Plsはいくつかの解決策を提案します..よろしくお願いします。

4

1 に答える 1

1

データを並べて取得するには、ループの外側の最後の行を取得し、最初の行として保存します。ループ内で編集している行のカウンターを保持します。行が null の場合、行を作成します。

rowPos = sheet.getLastRowNum();
for (Entry<String, String> entry : tushMap.entrySet()) {
    rowPos++;
    Row currentRow = sheet.getRow(rowPos);
    if(null == currentRow)
        currentRow = createRow(rowPos);

編集:

これを使用して必要な出力を取得しています

int beginingRow = sheet.getLastRowNum();
            for( int col=0; col<2; col++){
                int currentRow = beginingRow;
                for (int i=0; i<10; i++) {
                    currentRow++;
                    row = sheet.getRow(currentRow);
                    if(null == row)
                        row=sheet.createRow(currentRow);
                    c = row.createCell(col);
                    c.setCellValue("fff");
                    System.out.println("fff");
                    //System.out.println(entry.getKey() + "/" + entry.getValue());
                    rowNum++;
                }
            }

出力

fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
fff fff
于 2012-04-07T08:08:34.670 に答える