Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从xls工作表和csv文件检索数据时如何使用“修剪”?_Java_Apache Poi_Hssf - Fatal编程技术网

Java 从xls工作表和csv文件检索数据时如何使用“修剪”?

Java 从xls工作表和csv文件检索数据时如何使用“修剪”?,java,apache-poi,hssf,Java,Apache Poi,Hssf,为了从xls工作表和csv文件中检索代码,我做了以下工作。我尝试使用.trim,但没有获得所需的输出。请告诉我哪里出错,以便从xls单元格和csv文件中删除任何尾随空格 for (int rowNum = rowStart-1; rowNum < rowEnd+1; rowNum++) { List<String> cellTempList = new ArrayList<String>(); Row rowObj

为了从xls工作表和csv文件中检索代码,我做了以下工作。我尝试使用.trim,但没有获得所需的输出。请告诉我哪里出错,以便从xls单元格和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;

}

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

如果你得到字符串中的值,那么只需使用trim方法。你面临什么问题?显示当前输出和预期输出。如-cellTempList.addcell.toString.trim+;或大提琴圣殿师.addcell.toString.trim+@akash您不需要修剪cell.toString其已修剪。您需要修剪列表中的每个元素