[英]Access data from excel in single column using Apache POI
This is what I'm trying to achieve...the output can be printed on console of the IDE instead in the excel itself 这就是我要实现的目标...输出可以在IDE的控制台上打印,而不是在Excel本身中打印
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();
}
I thought of this because I have to calculate the values on the basis their signs and make it zero. 我想到这一点是因为我必须根据其符号计算值并将其设为零。
You will need to iterate though the rows using a loop of some kind. 您将需要使用某种循环来遍历行。 This should help you. 这应该对您有帮助。
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();
}
Excel doc. Excel文件
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();
}
}
} }
This is what I was trying to achieve. 这就是我想要实现的目标。 It will make the rows zero where the values can be operated on. 它将使行可以在其中操作值的位置为零。 @Piyush @Piyush
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.