I have searched through StackOverflow, but didn't find a clear answer to my question.
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).
First row is header row: year=x and population=y, and for each header I want to read the rows below. Later on I want to be able to use this information to create a dynamic plot (x,y),
So I think I need to save the x and y information in variables. 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
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.
also you might want to use the Point class available in java.awt directly. 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
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()
You can use a java.util.ArrayList
of java.awt.geom.Point2D.Double
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.