更新.xls或.xlsx文件中的数据

时间:2016-03-21 15:58:38

标签: java xls

我有一个基于键值的列表记录哪个是巨大的,我使用' CellReference' ,我只是想知道任何其他方式可能有效的方式,因为我的事情' CellReference' 有更多时间进行更新。

数据有这样的方式: -

      Column/value

       B9     35
       B8     63
       B7     11
       B6     36
       B5     87
       B14    3066
       B13    5025

我的每对迭代代码是: -

        Map<String,Integer> result= getMap();//getting Column and value
        Iterator it = result.entrySet().iterator();
        CellReference reference=null;
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                reference = new CellReference(pair.getKey().toString());
                Row row = spreadsheet.getRow(reference.getRow());
                Cell cell = null;
                if (row != null) {
                    cell = row.getCell(ref.getCol());
                }
            if (cell != null)
                cell.setCellValue(Double.parseDouble(pair.getValue().toString()));
        }

1 个答案:

答案 0 :(得分:0)

“效率”一词在这里很不清楚,但您也可以尝试

          List<String> dataList = new ArrayList<String>();
    List<Integer> rowNo=new ArrayList<Integer>();
    List<Integer> colNo=new ArrayList<Integer>();
    int countRow=1;
    int countCol=1;
    try {

        FileInputStream fis = new FileInputStream(file);
    //  System.out.println("loadinggg !!!");
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            rowNo.add(countRow);
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                cell = cellIterator.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING: {
                    dataList.add(cell.getStringCellValue());
                    cell.setCellValue("iterate through your own list here");
                }
                    break;
                }
            }
        }