Create Microsoft Excel file from CSV file

1143015 pts.
Tags:
CSV file
Microsoft Excel
Within Java, I have to create a Microsoft Excel file from a CSV file. What would be the best API to do this? Would Apache POI framework be good? Thank you for all the help.
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

Yes, Apache POI is probably your best bet.

Take a look at the developer’s guide for documentation and examples:
Busy Developers’ Guide to HSSF and XSSF Features

————————–

Here is some sample code:

-----

package net.codejava.excel;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
/**
 * A very simple program that writes some data to an Excel file
 * using the Apache POI library.
 * @author www.codejava.net
 *
 */
public class SimpleExcelWriterExample {
 
    public static void main(String[] args) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Java Books");
         
        Object[][] bookData = {
                {"Head First Java", "Kathy Serria", 79},
                {"Effective Java", "Joshua Bloch", 36},
                {"Clean Code", "Robert martin", 42},
                {"Thinking in Java", "Bruce Eckel", 35},
        };
 
        int rowCount = 0;
         
        for (Object[] aBook : bookData) {
            Row row = sheet.createRow(++rowCount);
             
            int columnCount = 0;
             
            for (Object field : aBook) {
                Cell cell = row.createCell(++columnCount);
                if (field instanceof String) {
                    cell.setCellValue((String) field);
                } else if (field instanceof Integer) {
                    cell.setCellValue((Integer) field);
                }
            }
             
        }
         
         
        try (FileOutputStream outputStream = new FileOutputStream("JavaBooks.xlsx")) {
            workbook.write(outputStream);
        }
    }
 
}

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • ToddN2000
    I'd follow carlosdl method. What I find strange is the problem itself. Excel can open CSV files already so why the need to convert it?
    132,840 pointsBadges:
    report
  • carlosdl
    I agree. There should not be a need to convert from CSV to Excel.

    The only reason I can think of would be to be able to add additional characteristics, such as colors or formulas, but if that was the case, then a simple conversion from CSV would not suffice anyway.
    84,745 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: