0

У меня при записи в файл excel данных получается лесенка

1  | 2 | ...
   |111
   |111
000|
000|
000|

а нужно чтобы было

1  | 2 | ...
000|111
000|111
000|

мой код

public void write2(String PATH) {
    try {
        InputStream inp = new FileInputStream(PATH + "\\workbook.xlsx");
        Workbook wb = null;
        try {
            wb = WorkbookFactory.create(inp);
        } catch (EncryptedDocumentException | InvalidFormatException e) {
            e.printStackTrace();
        }
        Sheet sheet = wb.getSheetAt(2);
        int rowCounter = 0;
        for (int i = 0; i < size; i++) {
            Row row = sheet.createRow(rowCounter);
            Cell cell1 = row.createCell(0);
            Cell s = wb.getSheetAt(0).getRow(i).getCell(0);// получаем "000"
            String str = s.toString();
            cell1.setCellType(Cell.CELL_TYPE_STRING);
            cell1.setCellValue(str);
            rowCounter++;
        }
        Sheet sheet2 = wb.getSheetAt(2);
        // rowCounter = 0; - если эту переменную занулить, то получается только лист с данными из нижнего цикла.
        for (int i = 0; i < size; i++) {
            Row row = sheet2.createRow(rowCounter);
            Cell cell2 = row.createCell(1);
            Cell s2 = wb.getSheetAt(1).getRow(i).getCell(1);// а тут "111"
            String str2 = s2.toString();
            cell2.setCellType(Cell.CELL_TYPE_STRING);
            cell2.setCellValue(str2);
            rowCounter++;
        }
        FileOutputStream fileOut = new FileOutputStream(PATH + "\\workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
4
  • У вас данные на разных страницах одного файла? 19 сен 2017 в 6:43
  • да, на первом листе 1 столбец, на втором столбце второй столбец. 19 сен 2017 в 6:48
  • решил! нужно было оставить один цикл, дописал решение к основному вопросу. 19 сен 2017 в 7:07
  • 1
    Вы можете переместить ваш ответ именно в секцию ответ 19 сен 2017 в 7:15

1 ответ 1

0

Решение:

public void write2(String PATH) {
    try {
        InputStream inp = new FileInputStream(PATH + "\\workbook.xlsx");
        Workbook wb = null;
        try {
            wb = WorkbookFactory.create(inp);
        } catch (EncryptedDocumentException | InvalidFormatException e) {
            e.printStackTrace();
        }
        Sheet sheet = wb.getSheetAt(2);
        int rowCounter = 0;
        for (int i = 0; i < size; i++) {
            Row row = sheet.createRow(rowCounter);
            Cell s1 = wb.getSheetAt(0).getRow(i).getCell(0);
            String str1 = s1.toString();
            Cell s2 = wb.getSheetAt(1).getRow(i).getCell(1);
            String str2 = s2.toString();
            row.createCell(0).setCellValue(str1);
            row.createCell(1).setCellValue(str2);
            rowCounter++;
        }
        FileOutputStream fileOut = new FileOutputStream(PATH + "\\workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.