简体   繁体   中英

Read and write data from particular column in excel file dynamically using java apache poi

I have done reading particular column from excel file but i don't know how to write that data into new sheet in excel file. Here is my code

Workbook wb = WorkbookFactory.create(new FileInputStream("E:\\Dharshan/test.xlsx"));
    Sheet sheet = wb.getSheetAt(0);
    int column_index_1 = 0;
    int column_index_2 = 0;
    Row row = sheet.getRow(0);
    for (Cell cell : row) {
        // Column header names.
        switch (cell.getStringCellValue()) {
            case "Issue Type":
                column_index_1 = cell.getColumnIndex();
                break;
            case "Issue key":
                column_index_2 = cell.getColumnIndex();
                break;
        }
    }
    XSSFSheet sheet1 = (XSSFSheet) wb.createSheet("student Details1");
    for (Row r : sheet) {
        if (r.getRowNum()==0) continue;//hearders
        Cell c_1 = r.getCell(column_index_1);
        Cell c_2 = r.getCell(column_index_2);
            System.out.print("  "+c_1 + "   " + c_2+"\n");

Until this code working fine it is reading data from excel and printing data in console

Map<String, Object[]> data = new TreeMap<String, Object[]>(); 
            data.put("1", new Object[]{ "S.no", "Issue Type", "Issue key"}); 
            data.put("2", new Object[]{ 1, c_1.getStringCellValue(), c_2.getStringCellValue()});
            data.put("3", new Object[]{ 2, c_1.getStringCellValue(), c_2.getStringCellValue()});

            // Iterate over data and write to sheet 
            Set<String> keyset = data.keySet(); 
            int rownum = 0; 
            for (String key : keyset) { 
                // this creates a new row in the sheet 
                Row row1 = sheet1.createRow(rownum++); 
                Object[] objArr = data.get(key); 
                int cellnum = 0; 
                for (Object obj : objArr) { 
                    // this line creates a cell in the next column of that row 
                    Cell cell = row1.createCell(cellnum++); 
                    if (obj instanceof String) 
                        cell.setCellValue((String)obj); 
                    else if (obj instanceof Integer) 
                        cell.setCellValue((Integer)obj); 
                } 
            } 
            try { 
                // this Writes the workbook gfgcontribute 
                FileOutputStream out = new FileOutputStream(new File("E:\\Dharshan/test.xlsx")); 
                wb.write(out); 
                out.close(); 
                System.out.println("test.xlsx written successfully on disk."); 
            } 
            catch (Exception e) { 
                e.printStackTrace(); 
            } 
    }

Finally i found solution for this issue. Here is my updated code

Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();  
  int i = 1;
    for(Row r1 : sheet) {
      //This is for empty cell value
       String c3;
       if(r1.getCell(column_index_3) == null)
         c3 = " ";
       else
         c3 = r1.getCell(column_index_3).getStringCellValue();
       data.put(i, new Object[]{ r1.getCell(column_index_1).getStringCellValue(), r1.getCell(column_index_2).getStringCellValue(), c3});
        i++;
}
Set<Integer> keyset = data.keySet(); 
            int rownum = 0; 
            for (Integer key : keyset) { 
                // this creates a new row in the sheet 
                Row row1 = sheet1.createRow(rownum++); 
                Object[] objArr = data.get(key); 
                int cellnum = 0; 
                for (Object obj : objArr) { 
                    // this line creates a cell in the next column of that row 
                    Cell cell = row1.createCell(cellnum++); 
                    if (obj instanceof String) 
                        cell.setCellValue((String)obj); 
                    else if (obj instanceof Integer) 
                        cell.setCellValue((Integer)obj);

                } 
            } 
try { 
            // this Writes the workbook gfgcontribute 
            FileOutputStream out = new FileOutputStream(new File("E:\\Dharshan/test.xlsx")); 
            wb.write(out); 
            out.close(); 
            System.out.println("test.xlsx written successfully on disk."); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM