如何从xls sheet和csv文件中检索数据时使用“trim”?

时间:2014-02-13 06:22:17

标签: java apache-poi hssf

我已经完成了从xls sheet和csv文件中检索代码的操作。我尝试使用“.trim()”,但我没有得到所需的输出。请告诉我出错的地方,以便从xls cell和csv文件中删除任何尾随空格。

 for (int rowNum = rowStart-1; rowNum < rowEnd+1; rowNum++) {
            List<String> cellTempList = new ArrayList<String>();
            Row rowObj = hssfSheet.getRow(rowNum);


            for (int col = 0; col < columnEnd; col++) {
                Cell cell = rowObj.getCell(col, Row.CREATE_NULL_AS_BLANK);
                if(cell.getCellType()==cell.CELL_TYPE_NUMERIC)
                {

                    DecimalFormat df = new DecimalFormat("#.##");
                    String cellValue=df.format(cell.getNumericCellValue());
                    cellTempList.add(cellValue.toString());
                    logger.trace(TId + " Adding cell having string to "+cellTempList);
                } else {
                    cellTempList.add(cell.toString()+"");
                }
            }

            cellDataList.add(cellTempList);


 //following to retrieve data from a csv file 
  while ((line = stream.readLine()) != null) {
            if(iteration < iteration1-1 || StringUtils.isBlank(line)) {
                iteration++;  
                continue;
            }

            //String[] splitted = line.split(",");
            String[] splitted = StringUtils.splitPreserveAllTokens(line,',');
            List<String> dataLine = new ArrayList<String>(splitted.length);
            for (String data : splitted)
                dataLine.add(data.trim());
            csvData.add(dataLine);

        }
    }
    catch (Exception e) {
        logger.error(TId + " Other Exception : " + e.getStackTrace());
        throw new Exception("-8");
    }
    finally {
        if (stream != null)
            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
                logger.error(TId + " IOException : " + e.getStackTrace());
                throw new Exception("-8");
            }
    }
    logger.info(TId + " [Flow]:Exiting readcsvFile Method...." + csvData);
    return csvData;

}

请帮助我尝试修剪,但我没有得到所需的输出

0 个答案:

没有答案