0

I want to write column by column to an Excel file but I don't know how to do this. Is it possible to make it? I looked in the docs and see no methods to write column by column. Here is my code:

private void writeColumnByColumn() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    String[] strings = {"a", "b", "c"};
    Row row = sheet.createRow(0);
    for (int i = 0; i < 3; i++) {
        // here i want to write "a" in the first column, "b" in the second and "c" in the third
    }
    
    FileOutputStream outputStream = new FileOutputStream("ok.xlsx");
    try (outputStream) {
        workbook.write(outputStream);
    }
}
2

1 Answer 1

1

This should do the work.

  private void writeColumnByColumn() throws IOException {
   XSSFWorkbook workbook = new XSSFWorkbook();
   XSSFSheet sheet = workbook.createSheet();
   String[] strings = {"a", "b", "c"};
   Row row = sheet.createRow(0);
   for (int i = 0; i < 3; i++) {
          Cell cell = row.createCell(i);
          cell.setCellValue(strings[i]);
   } 
 
   FileOutputStream outputStream = new FileOutputStream("ok.xlsx");
   try (outputStream) {
       workbook.write(outputStream);
   }
  }
Sign up to request clarification or add additional context in comments.

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.