java - 使用 Apache POI 将 XLS 文件读取到 Java 中的二维数组中

标签 java arrays apache-poi xls

我的任务是读取一个如下所示的 Excel 文件 http://postimg.org/image/jxgc6ng51/ 然后将匹配的产品 ID 的单位相加,并以类似的列/行格式打印出来。抱歉,如果这是一种草率的做法,我是 java 新手,目前我对 java API 的了解非常有限......这是我的代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Read {

            public static void readXLSFile() throws IOException{
        InputStream ExcelFile = new FileInputStream("C:/Sales Data.xls");
        HSSFWorkbook wb = new HSSFWorkbook(ExcelFile);
                HSSFSheet sheet=wb.getSheetAt(0);
                int numRows=sheet.getPhysicalNumberOfRows();
                int[][]idSale=new int[numRows][2];

                for(int i=1;i<numRows;i++){
                    HSSFCell proId = sheet.getRow(i).getCell(1);
                    HSSFCell sales = sheet.getRow(i).getCell(2);
                    idSale[i][0]=(int)proId.getNumericCellValue();
                    idSale[i][1]=(int)sales.getNumericCellValue();
                    //the data from the excel sheet is copied into the 2D array at this point
                }

                /*for loop to attempt to compare id number of array to the rest of the array
                problem is there are duplicate ID's and it is re-comparing and adding
                the sales total into the duplicate as well. 
                How to make it so it ignores all duplicate ID's or make it so it 
                only prints out the element the first time the ID pops up and doesn't print the other elements with the same ID?*/
                for(int j=1;j<numRows;j++){
                    for(int jj=j+1;jj<numRows;jj++)
                    if(idSale[j][0]==idSale[jj][0]){
                        idSale[j][1]+=idSale[jj][1];
                    }
                }
            }



            public static void main(String[] args) throws IOException {
        readXLSFile();
            }
}

最佳答案

您可以尝试使用 Map,这样您只需要一个 for 循环,如下所示:-

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
     for(int i=1;i<numRows;i++){
         HSSFCell proId = sheet.getRow(i).getCell(1);
         HSSFCell sales = sheet.getRow(i).getCell(2);
         int prodId =(int)proId.getNumericCellValue();
         int currentSales =(int)sales.getNumericCellValue();
         //the data from the excel sheet is copied into the 2D array at this point
        if (map.containsKey(i)) {
            int prevSales = map.get(i);
            map.put(i, prevSales+currentSales);
        } else {
            map.put(i, currentSales);
        }
    }

关于java - 使用 Apache POI 将 XLS 文件读取到 Java 中的二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32328307/

相关文章:

java - 计算 Word 文档中的页数

java - 如何从静态类获取 SharedPreferences 值

java - 应用程序不是为平板电脑设计的

java - 从文件中读取二维数组-Java

c - 为什么这不会打印?

ios - NSMutablearray 删除对象也删除其他 NSMutablearray 中的对象

java - 在 Apache POI 中过滤

java - 如何在 Java 上更新 JSONArray 值

java - 运行时异常无法启动 MainActivity

java - 使用Java从Excel中读取日期字段