简体   繁体   English

从Excel文件读取数据

[英]Read data from excel file

I have searched through StackOverflow, but didn't find a clear answer to my question. 我已经搜索了StackOverflow,但是没有找到明确的答案。

I have a simple 2 columns and 24 rows xlsx (excel) file (later on this file will have more columns and rows , and also eventually it will have different sheets). 我有一个简单的2列和24行xlsx(excel)文件(此文件以后将具有更多的列和行,并且最终它将具有不同的工作表)。

First row is header row: year=x and population=y, and for each header I want to read the rows below. 第一行是标题行:year = x和population = y,对于每个标题,我想阅读以下行。 Later on I want to be able to use this information to create a dynamic plot (x,y), 稍后,我希望能够使用此信息来创建动态图(x,y),

So I think I need to save the x and y information in variables. 所以我认为我需要将x和y信息保存在变量中。 Below is my pseudo code. 下面是我的伪代码。

//Sheet Pop

final String POP = "Pop";   
int startRowHeader = 1,      
    startRowNumeric = 2,     
    startColNumeric = 1, 
    nrOfCols = 0,
    nrOfRows  = 0;

int nrOfSheets = excelFile.getWorkbook().getNumberOfSheets(); 
org.apache.poi.ss.usermodel.Sheet sheet1 = excelFile.getWorkbook().getSheetAt(0);  
String[] sheets = {POP};
boolean sheetExists;
String activeSheet = "";   

nrOfCols = sheet1.getRow(0).getLastCellNum();
 nrOfRows = excelFile.getLastRowNum(POP);

try {

    for (String sheet : sheets) {
        sheetExists = false;
        for (int sheetIndex = 0; sheetIndex < nrOfSheets; sheetIndex++) {
        if (sheet.equalsIgnoreCase( excelFile.getWorkbook().getSheetName(sheetIndex))) { 
                SheetExists = true; 
            }
        }
        if (!sheetExists) {
            throw new Exception("Sheet " + sheet + " is missing!");
        }
    } 


    //Take off!

    // Sheet Pop
    activeSheet = sheets[0];

    for(int j=0; j < nrOfCols; j++) {
        for(int i=0; i<nrofRows; i++) {
            column[i] = (int)excelFile.getCellNumericValue(POP, startRowNumeric, startColNumeric);  
        }
    } 

}  catch (Exception e) {
    traceln(" ..... ");
    traceln("Error in sheet: " + activeSheet);
    traceln("Message: " + e.getMessage());  //Write out in console
} 

create a new class: 创建一个新类:

public class Point {
        int x;
        int y;

}

in your main function create a 在您的主要功能中创建一个

List<Point> allPoints = new List<Point>();

then add the points you read from excel to this List 然后将您从excel中读取的点添加到此列表中

do this in a loop 循环执行此操作

Point p = new Point();
p.x = xCoordinateFromExcel;
p.y = yCoordinateFromExcel;

allPoints.add(p);

also consider adding constructor and getter/setters to your Point class. 也可以考虑将构造函数和getter / setter添加到Point类。

also you might want to use the Point class available in java.awt directly. 您也可能想直接使用java.awt中可用的Point类。 http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html

EDIT: regarding fetching values from excel this is what i understand from your question you have 24 rows with 2 columns first column has x, second column has y value 编辑:关于从excel取值,这是我从您的问题中了解到的内容,您有24行2列,第一列具有x,第二列具有y值

so you can just do something like this in a loop 所以你可以循环做这样的事情

for(int i=0;i<24;i++){
    Row row = sheet.getRow(i);    
    Cell cellX = row.getCell(1);
    Cell cellY = row.getCell(2);

//assign to point class
...
}

dont forget to parse it to int, or use getNumericCellValue() http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#getNumericCellValue() 不要忘记将其解析为int,或使用getNumericCellValue() http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#getNumericCellValue()

You can use a java.util.ArrayList of java.awt.geom.Point2D.Double 您可以使用java.awt.geom.Point2D.Doublejava.util.ArrayList

Declare the list: 声明清单:

ArrayList<Point2D.Double> points = new ArrayList<>();

Then for each row: 然后对于每一行:

// Read x
// Read y
Point2D.Double p = new Point2D.Double(x, y);
/// And add to the list: 
points.add(p);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM