Generate Excel document from Java

1

Good, I'm using the Apache POI library to generate excel files from Java. This works by loading the records in a JTable and pressing a button generates the excel document. The problem is that I need to generate the formatted document, that is, the numbers in thousands format and centered on the right, for example. This I already do with few records, but when there are many (over 50,000) it takes a lot to generate them. Obs. The limit of records is around 900,000 approximately , which are the annual reports that I need to obtain. I'm looking for some other bookstore to test it and compare the results, if you know any other I would appreciate your help.

    
asked by Carlos 08.03.2017 в 12:43
source

3 answers

0

I think the library you are using (POI) is the best for your purposes. Sometimes the problem may be in a non-optimized code or in using an outdated version of the library, the latest version is September 2016 .

Anyway:

Excel as Database

In Java you can read Excel sheets as if you were accessing a Database.

For this you need to add the JDBC ODBC drivers to your project.

I leave you an example code, published in Java2s . It is assumed in the example that you have a sheet called Sheet1 and in that sheet you have columns called FirstName , LastName . Queries as if it were a database, you get a rs and you operate with the returned values.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

  public static Connection getConnection() throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:excelDB";
    String username = "yourName";
    String password = "yourPass";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
  }

  public static void main(String args[]) throws Exception {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    conn = getConnection();
    stmt = conn.createStatement();
    String excelQuery = "select * from [Sheet1$]";
    rs = stmt.executeQuery(excelQuery);

    while (rs.next()) {
      System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName"));
    }

    rs.close();
    stmt.close();
    conn.close();
  }
}

Other bookstores

There are also these libraries:

  • JXLS
  • JExcelApi (looks a little old).
  • answered by 08.03.2017 / 13:51
    source
    0

    Have you thought about using jasperreports ? It allows you to create templates with numbers in the format you want, colors, cell style ...

        
    answered by 08.03.2017 в 13:40
    0

    Another useful library for generating and manipulating Excel documents is docx4j: link

        
    answered by 08.03.2017 в 13:51