0

i need to append excel. I write code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

    public class AppendExcel {



public static void main(String[] args) throws Exception {
    String excelFilePath = "append.xlsx";

    try {
        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
        Workbook workbook = WorkbookFactory.create(inputStream);

        Sheet sheet = workbook.getSheetAt(0);

        DbManagerSQL test = new DbManagerSQL();
        String sql = "select distinct Code,Item,Description from MyTable";

        Statement stmt = test.dbConnect().createStatement();
        ResultSet rs = stmt.executeQuery(sql);


        int rowCount = sheet.getLastRowNum();
        while (rs.next()) {


            Row row = sheet.createRow(++rowCount);

            int columnCount = 0;

            Cell cell = row.createCell(columnCount);
            cell.setCellValue(rowCount);

            cell = row.createCell(++columnCount);
            cell.setCellValue((String) rs.getString(1));
            cell.setCellValue((String) rs.getString(2));
            cell.setCellValue((String) rs.getString(3));

        }

        inputStream.close();

        FileOutputStream outputStream = new FileOutputStream("append.xlsx");
        workbook.write(outputStream);
        // workbook.close();
        outputStream.close();

    } catch (IOException | EncryptedDocumentException | InvalidFormatException ex) {
        ex.printStackTrace();
    }
}

}

My Excel has the column

enter image description here

but when I append Excel I got next

enter image description here

It looks like remember only the last column. I think the prblem is in this line of code

cell = row.createCell(++columnCount);

but i don't now how to add data to get excel like this

enter image description here

Any idea???

2 Answers 2

1

Create a cell for every entry:

int columnCount = 0;

Cell cell = row.createCell(columnCount);
cell.setCellValue(rowCount);
columnCount++;

cell = row.createCell(columnCount);
cell.setCellValue((String) rs.getString(1));
columnCount++;

cell = row.createCell(columnCount);
cell.setCellValue((String) rs.getString(2));
columnCount++;

cell = row.createCell(columnCount);
cell.setCellValue((String) rs.getString(3));
Sign up to request clarification or add additional context in comments.

Comments

0

You increment your columnCount in the while:

int columnCount = 0;
cell = row.createCell(++columnCount);

So you always write in the same cell. You must put the initialization out of the wile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.