Gy30n_W00
Gy30n_W00

Reputation: 5

Problems Read / Write Excel File In Java Using Apache POI

Hello guys i am a newbie on java poi library and i was trying all my luck to learn this library but still no luck

I would like to have this output

Excel1.xls has this data

ZIP CODE | PLACE | DATE
211

and I want to copy the all the first row data.

ZIP CODE | PLACE | DATE

and place it to another sheet

This is the code that i have made

public static void main(String[] args) throws IOException{

    try {
        FileInputStream file = new FileInputStream(new File("d:\\input.xls"));

        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");


        for(Row row : sheet){
            int i=0;

            for(Cell cell : row){

                cell.setCellType(Cell.CELL_TYPE_STRING);
                System.out.print(cell.getStringCellValue() + "\t");
                String a = cell.getStringCellValue();

                cell = zip1.createRow(i).createCell(i);

                i++;
                cell.setCellValue(a);
             }
             break;

         }

        file.close();
        FileOutputStream outFile =new FileOutputStream(new File("d:\\output.xls"));
        workbook.write(outFile);
        outFile.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 3468

Answers (2)

Ran Adler
Ran Adler

Reputation: 3711

Try to use Xcelite
https://github.com/eBay/xcelite#writing

Write:

public class User { 

  @Column (name="Firstname")
  private String firstName;

  @Column (name="Lastname")
  private String lastName;

  @Column
  private long id; 

  @Column
  private Date birthDate; 
}

Xcelite xcelite = new Xcelite();    
XceliteSheet sheet = xcelite.createSheet("users");
SheetWriter<User> writer = sheet.getBeanWriter(User.class);
List<User> users = new ArrayList<User>();
// ...fill up users
writer.write(users); 
xcelite.write(new File("users_doc.xlsx"));

Upvotes: 0

Sully
Sully

Reputation: 14943

Issues found

  1. For .xlsx files -> HSSFWorkbook should be XSSFWorkbook

  2. Loop. You do not want to loop every row you only want the first one. Just loop the columns

  3. Do not create a row everytime you want to write to a cell. Create a new row only once.

Working Example:

try {
    FileInputStream file = new FileInputStream(new File(
            "C:\\path\\Book1.xlsx"));

    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheetAt(0);
    XSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");

    Row readFirstRow = sheet.getRow(0);
    Row writeFirstRow = zip1.createRow(0);

    for (Cell cell : readFirstRow) {

        cell.setCellType(Cell.CELL_TYPE_STRING);
        String a = cell.getStringCellValue();

        cell = writeFirstRow.createCell(cell.getColumnIndex());
        cell.setCellValue(a);
    }

    file.close();
    FileOutputStream outFile = new FileOutputStream(new File(
            "C:\\path\\BookOut.xlsx"));
    workbook.write(outFile);
    outFile.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 3

Related Questions