这就是我尝试的achieve...the输出可以打印在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();
}我之所以想到这个,是因为我必须根据它们的符号来计算值,并使之为零。
发布于 2017-05-30 04:02:46
您将需要使用某种循环遍历行。这应该对你有帮助。
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();
}优秀的医生。

发布于 2017-06-02 04:14:04
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
https://stackoverflow.com/questions/44237000
复制相似问题