我已經通過StackOverflow搜索,但沒有找到明確的答案給我的問題。我有一個簡單的2列和24行xlsx(excel)文件(稍後在這個文件將有更多的列和行,也最終會有不同的工作表)。從excel文件讀取數據
第一行是標題行:year = x和population = y,並且對於每個標題我想讀取下面的行。後來我想能夠使用這些信息來創建一個動態圖(x,y),所以我認爲我需要將x和y信息保存在變量中。以下是我的僞代碼。在主功能
public class Point {
int x;
int y;
}
創建
List<Point> allPoints = new List<Point>();
然後從Excel讀取點添加到這個列表
中做到這一點:
//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
}
究竟是什麼問題了嗎?你是否得到例外? – gaurav5430
@ gaurav5430。我不知道應該在哪裏保存excel文件中的數據,以便在我想繪製動態圖時使用它作爲變量。另外,如何獲得每列的單元格值?標題年的所有年份以及標題人口中每年的人口數是多少? – user2481390
如何使用2d數組或Point類的數組(一個用int x annd int y作爲成員的自定義類)? – gaurav5430