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 6562745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:44:45+00:00 2026-05-25T13:44:45+00:00

I am using the apache poi to handle the .xlsx file. I have two

  • 0

I am using the apache poi to handle the .xlsx file.

I have two .xlsx files,
part.xlsx,and full.xlsx,

they own the same structure.

Each record(The Row object in the poi) have three colmn:name,age,location.

There are almost 5000 rows in the part.xlsx,and 40000 rows in the full.xlsx.

Now I want to extra the rows from the full.xlsx who have the same value with that in the part.xlsx.

for example:

The part.xlsx:

Name age location
kk   23  USA
bb   24  England
......

The full.xlsx

Name age location
kk   23  USA
bb   24  England
xx   25  USA
......

Now I want to extra the ‘kk’ and ‘bb’ rows and save them to a new file.

This is the code code:

List<User> usersInpart=new ArrayList<User>();
List<Row> rows_to_be_saved=new ArrayList<Row>();

//read the part.xlsx and save them.
FileInputStream fis_part=new FileInputStream("part.xlsx");
WorkBook wb_part=WorkbookFactory.create(fis_part);
Sheet st_part=wb_part.getSheetAt(0);
for(Row row : st_part){
    if(row.getRowNum()==0) continue; //skip the first row(the title)
    User u=new User();
    u.setName(row.getCell(0).getRichStringValue().getString().trim());
    u.setAge(row.getCell(1).getNumericCellValue());
    u.setLocation(row.getCell(2).getRichStringValue().getString().trim());
    usersInpart.add(u);
}
fis_part.close();


//read the full.xlsx

FileInputStream fis_full=new FileInputStream("full.xlsx");
WorkBook wb_full=WorkbookFactory.create(fis_full);
Sheet st_full=wb_full.getSheetAt(0);
for(Row row : st_full){
    if(row.getRowNum()==0) continue; //skip the first row(the title)

    String name=row.getCell(0).getRichStringValue().getString().trim();
    double age=row.getCell(1).getNumericCellValue();
    String location=row.getCell(2).getRichStringValue().getString().trim();

    for(User u : usersInpart){
        if(u.getName.equals(name) && u.getAge==age && u.getLocation().equals(location))
            rows_to_be_saved.add(row);
    }
}
fis_full.close();

//write the selected rows to file

WorkBook wb_res=WorkbookFactory.create(fis_full);
Sheet st_res=wb_res.createSheet(0);

    int i=0;
    for (Row row : rows_to_be_saved) {
        Row rw=st_res.createRow(i);

        int k=0;
        for (Cell cell : row) {
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    rw.createCell(k).setCellValue(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        rw.createCell(k).setCellValue(cell.getDateCellValue());
                    } else {
                        rw.createCell(k).setCellValue(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    rw.createCell(k).setCellValue(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    rw.createCell(k).setCellValue(cell.getCellFormula());
                    break;
                default:
            }
            k++;
        }
        i++;
    }
//save the wb_res
wb_res.write(new FileOutputStrem("xx.xlsx"));

now I wonder any good ideas to save the file?

Since I have saved the selected rows in the “rows_to_be_saved”.

And I create the new sheet “st_res”,any way I can save these rows to the “st_res” directly? Since now,I have create each row according the rows in the “rows_to_be_saved”.

So there will be two List of rows. I think this is a waste of memory.

Any suggestion?

  • 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-25T13:44:45+00:00Added an answer on May 25, 2026 at 1:44 pm

    If memory usage is a concern, you could save much more by using the XSSF Event Model to read the full.xlsx file. You’re presently loading a 40,000-row file into memory, whereas the event model would only keep a single row in memory at a time.

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

Sidebar

Related Questions

I have been using Apache POI to manipulate Microsoft Word .docx files — ie
I am using Apache POI-HSSF for working with Excel files. I have a cell
I have opened a large excel file as a Apache POI workbook (using VM
I have just Extracted the cells from the excel sheet using Apache POI, everything
I'm using Apache POI 3.6, I want to read an excel file which has
I have an requirement of generating two excel files at once. We are using
I am using Apache Poi and Java for Excel manipulation. I have modified some
Using Apache POI HSSF, we can create xls file like this private void write(HSSFWorkbook
I have been using Apache POI for quite sometime and it works great but
I've been using Apache POI for some time to read existing Excel 2003 files

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.