[英]How to convert Excel sheet into a Vector using Jxl
因此,我有一個變量PV1,該變量從Excel文件存儲第一行。 但是我想要一個將整個行存儲在向量中的變量。
我使用System.out.println()
只是看它是否占用了良好的列。
String PV1;
for(int col = 0;col < columns;col++)
{
for(int row = 1;row < rows;row++)
{
PV1 = sheet.getCell(1, row).getContents();
System.out.println(Double.parseDouble(PV1));
}
}
我正在使用jxl訪問Excel文件。
任何幫助將非常感激!
編輯:這是表 ,我需要在PV1中存儲所有行。
這對你有幫助
String PV1;
Object[] data = Object[columns]
for(int col = 0;col < columns;col++)
{
for(int row = 1;row < rows;row++)
{
PV1 = sheet.getCell(1, row).getContents();
data[col] = PV1;
System.out.println(Double.parseDouble(PV1));
}
}
如果我理解正確,則需要一個向量,該向量應包含第一行中的所有列。
在這種情況下,您可以執行以下操作:
Vector<Double> firstRow = new Vector<>();
if(rows > 0){
for(int col = 0;col < columns;col++){
String pv = sheet.getCell(1, col).getContents();
firstRow.add(Double.parseDouble(pv));
}
}
您可能應該考慮使用List
而不是Vector
。
從您的評論看來,您想要從所有行中檢索特定的列。 您可以這樣做:
int pv1ColumnIndex = 1;
List<Double> pv1Columns = new ArrayList<>();
for(int row = 1;row < rows;row++){// row = 1 to skip the header
String pv1 = sheet.getCell(pv1ColumnIndex, row).getContents();
pv1Columns.add(Double.parseDouble(pv1));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.