Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7086693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:34:54+00:00 2026-05-28T07:34:54+00:00

Here is the code that generates .xls file using JXL: public void generateXls(String fileName,

  • 0

Here is the code that generates .xls file using JXL:

public void generateXls(String fileName, int sheets, int cols, int rows) {

    if (cols > 256) {
        throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
    }

    if (rows > 65536) {
        throw new IllegalArgumentException("Error: number of rows per sheet  must be < 65536");
    }

    String fullName = fileName + ".xls";
    WritableWorkbook workbook = null;
    try {
        workbook = Workbook.createWorkbook(new File(fullName));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Random random = new Random();
    for (int s = 0; s < sheets; s++ ) {
        WritableSheet sheet = workbook.createSheet("Sheet" + s, 0);
        for (int i = 0; i < cols ; i++) {
            for (int j = 0;  j < rows; j++) {
                Number number = new Number(i, j, random.nextDouble()*1000);
                try {
                    sheet.addCell(number);
                } catch (RowsExceededException e) {
                    throw new RuntimeException("Error: too many rows in a sheet");
                } catch (WriteException e) {
                    throw new RuntimeException("Error occured while adding cell to sheet", e);
                }
            }
        }
    }

    try {
        workbook.write(); 
        workbook.close();
    } catch (WriteException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Here is the code that generates .xls and .xlsx files using POI:

public void generateXlsx(String fileName, int sheets, int cols, int rows) {

    if (cols > 16383) {
        throw new IllegalArgumentException("Error: number of cols per sheet must be < 16383");
    }
    XSSFWorkbook workbook = new XSSFWorkbook();

    Random random = new Random();
    for (int s = 0; s < sheets; s++ ) {
        XSSFSheet sheet = workbook.createSheet();
        for (int i = 0; i < rows ; i++) {
            XSSFRow row =  sheet.createRow(i);
            for (int j = 0;  j < cols; j++) {
                XSSFCell cell = row.createCell(j);
                cell.setCellValue(random.nextDouble()*1000);
            }
        }
    }

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(fileName);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        workbook.write(fileOut);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        fileOut.flush();
        fileOut.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

public void generateXls(String fileName, int sheets, int cols, int rows) {

    if (cols > 256) {
        throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
    }

    HSSFWorkbook workbook = new HSSFWorkbook();

    Random random = new Random();
    for (int s = 0; s < sheets; s++ ) {
        HSSFSheet sheet = workbook.createSheet();
        for (int i = 0; i < rows ; i++) {
            HSSFRow row =  sheet.createRow(i);
            for (int j = 0;  j < cols; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellValue(random.nextDouble()*1000);
            }
        }
    }

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(fileName);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        workbook.write(fileOut);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        fileOut.flush();
        fileOut.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

I wrote a performance test:

public static void main(String[] args) {

    int sheets =1;
    int cols = 255;
    int rows = 20000;
    long a1 = System.currentTimeMillis();
    ExcelFileGenerator generator  = new ExcelFileGenerator();
    generator.generateXls("xlsJXL.xls", sheets, cols, rows);
    long xls = System.currentTimeMillis()-a1;
    System.out.println("xlsJXL: " + xls);
    ExcelFileGeneratorPOI generatorPOI = new ExcelFileGeneratorPOI();
    long a2 = System.currentTimeMillis();
    generatorPOI.generateXls("xlsPOI.xls", sheets, cols, rows);
    long xlsPoi = System.currentTimeMillis()-a2;
    System.out.println("xlsPOI: " + xlsPoi);
    long a3 = System.currentTimeMillis();
    generatorPOI.generateXlsx("xlsxPOI.xlsx", sheets, cols, rows);
    long xlsx = System.currentTimeMillis()-a3;
    System.out.println("xlsxPOI: " + xlsx);
}

The results are:
sheets =1
cols = 255
rows = 10
xlsJXL: 133
xlsPOI: 162
xlsxPOI: 645

sheets =1
cols = 10
rows = 255
xlsJXL: 130
xlsPOI: 140
xlsxPOI: 650

sheets =10
cols = 255
rows = 255
xlsJXL: 611
xlsPOI: 784
xlsxPOI: 16228

sheets =2
cols = 100
rows = 10000

xlsJXL: 2755
xlsPOI: 3270
xlsxPOI: Exception in thread “main” java.lang.OutOfMemoryError: Java heap space

any reason by creating .xlsx with POI is much slower than creating .xls?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:34:54+00:00Added an answer on May 28, 2026 at 7:34 am

    xls is a binary based format, xlsx is an XML based format and requires more work to read/write.

    xlsx might also require an in memory document model to parse/build the XML which could be more complex.

    Finally, xls could be optimised better because it has been supported longer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code here that generates XML for Engineer list using linq. My
Here is my code that fails: bool Table::win(const Card &card) { for (int i
I have a piece of code here that does not work despite me using
What does this error mean? Here is the line of code that generates it
i have existing code that generates a spreadsheet from excel using OpenXML. It works
Here is the code that generates my chart: System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart(); Chart2.Width
I have some working Javascript code that generates an RDF/XML document using variables picked
I have code that generates PDF for me.I am using FPDF to generate PDF.sometimes
I'm think something like Facebook apps here. User generated pieces of code that people
Here is the code that cause me problem since few hours: TabItem newTab =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.