使用Java Apache POI动态读取和写入Excel文件中特定列的数据

时间:2019-01-16 05:46:18

标签: java apache-poi

我已经从excel文件中读取了特定的列,但是我不知道如何将数据写入excel文件中的新表中。这是我的代码

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");

在此代码正常工作之前,它是从excel读取数据并在控制台中打印数据

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(); 
            } 
    }

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决此问题的方法。这是我更新的代码

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(); 
        } 
}