首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Apache在单列中访问excel中的数据

使用Apache在单列中访问excel中的数据
EN

Stack Overflow用户
提问于 2017-05-29 07:38:02
回答 2查看 100关注 0票数 0

我的输入数据

  1. 我已经附加了一个带有几行的Excel列
  2. 445.04在本专栏中重复了三次。
  3. 我必须用java编写一个逻辑,这将帮助我使用200+行遍历本专栏,并添加这些类型的值将导致零。
  4. 数字可以是不同的,取任何值,当它们的可能性在操作时可能导致零。
  5. 这是另一张不同风景的照片
  6. 在上面,10和-3-7加起来可以是零,5和-5也可以是零。7.如何编写能够访问此类场景中的单个列的代码?如果需要更多的评论,请留下评论。

这就是我尝试的achieve...the输出可以打印在IDE的控制台上,而不是在excel本身中。

预期结果

代码语言:javascript
复制
    Iterator<Row> iterator = firstSheet.iterator();
    List<Double> posValue = new ArrayList<Double>();
    List<Double> negValue = new ArrayList<Double>();
    while(iterator.hasNext())//goes down the row, until there is nothing
    {    
        Row nexRow = iterator.next();
        if(nexRow.getRowNum()!=0){
            value = nexRow.getCell(3).getNumericCellValue();

            if(value>0){
                posValue.add(value);
            }
            else if(value <0){
                //System.out.println("ehre");
                negValue.add(value);
            }
        }
        Iterator<Double> pIterator = posValue.iterator();
        Iterator<Double> nIterator = negValue.iterator();

        for(int i = 0; i<posValue.size();i++){
            for(int j=0; j<negValue.size(); j++){
                //System.out.println(negValue.get(j));
                result= posValue.get(i)+negValue.get(j);
            }
        }
        if(result==0){
            System.out.println(result+"    "+nexRow.getRowNum());
        }
        workbook.close();
        inputStream.close();
    }

我之所以想到这个,是因为我必须根据它们的符号来计算值,并使之为零。

EN

回答 2

Stack Overflow用户

发布于 2017-05-30 04:02:46

您将需要使用某种循环遍历行。这应该对你有帮助。

代码语言:javascript
复制
public static void main(String[] args) throws IOException {
    String excelFilePath = "Book1.xlsx";
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));

    Workbook workbook = new XSSFWorkbook(inputStream);
    XSSFSheet firstSheet = (XSSFSheet) workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.iterator();
    iterator.next();// skip the header row
    Double result = 0.0;

    while (iterator.hasNext()) {
        Row nextRow = iterator.next();
        result += nextRow.getCell(0).getNumericCellValue();
    }
    System.out.println(result);

    workbook.close();
    inputStream.close();
}

优秀的医生。

票数 0
EN

Stack Overflow用户

发布于 2017-06-02 04:14:04

代码语言:javascript
复制
    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(0);
    double addedValue = 0;
    double value = 0;
    List<Double> value1 = new ArrayList<Double>();
    double result =0;
    Iterator<Row> iterator = firstSheet.iterator();
    List<Double> posValue = new ArrayList<Double>();
    double negValue = 0;
    while(iterator.hasNext())//goes down the row, until there is nothing
    {    
        Row nexRow =iterator.next();
        if(nexRow.getRowNum()!=0)
        {
            value = nexRow.getCell(3).getNumericCellValue();
            value1.add(value);
            for(int i=0; i<value1.size();i++)
            {
                //For storing all the values 
                addedValue=value1.get(i);


                /* Checking if the values are negative or not 
          If it is, then convert it to positive */
                if(addedValue<0)
                {                         
                    negValue=Math.abs(value1.get(i));
                }
            }
            /*checking for the numbers which can be operated to make it zero
               and then displaying the row numbers which have been cancelled out*/

            if(addedValue==negValue)
            {
                result = addedValue-negValue;
                nexRow.getCell(3).setCellValue(0.0);
                System.out.println(result+"  Matched rows--> "+nexRow.getRowNum());

            }
        }
        workbook.close();
        inputStream.close();
    }
}

}

这就是我想要达到的目标。它将使可以对值进行操作的行为零。@Piyush

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44237000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档