[英]How to read data from XLS (Excel) file [Java, Android]
我已經搜索過stackoverflow,但沒有找到明確的答案。 如何將數據從 XLS 文件的特定行和列讀取到我的 Android 應用程序? 如何讀取XLS文件? 我不想將其轉換為 CSV,因為我在嘗試轉換時遇到錯誤。
也許我可以使用這個http://www.andykhan.com/jexcelapi/tutorial.html#reading但我什至不知道如何將它導入到我的項目中。 請幫忙。
嗨,您只需要包含一個外部 jxl jar,您就可以完成相同的教程,這將幫助您了解讀取 excel 文件的過程.. 作為您的參考,我正在粘貼一些參考。 讀取第一張excel表格並創建結果集的代碼。
public List<String> read(String key) throws IOException {
List<String> resultSet = new ArrayList<String>();
File inputWorkbook = new File(inputFile);
if(inputWorkbook.exists()){
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over column and lines
for (int j = 0; j < sheet.getRows(); j++) {
Cell cell = sheet.getCell(0, j);
if(cell.getContents().equalsIgnoreCase(key)){
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cel = sheet.getCell(i, j);
resultSet.add(cel.getContents());
}
}
continue;
}
} catch (BiffException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
resultSet.add("File not found..!");
}
if(resultSet.size()==0){
resultSet.add("Data not found..!");
}
return resultSet;
}
private void printlnToUser(Cell str) {
final Cell string = str;
if (output.length() > 8000) {
CharSequence fullOutput = output.getText();
fullOutput = fullOutput.subSequence(5000, fullOutput.length());
output.setText(fullOutput);
}
output.append(string + "\n");
}
public void ReadXLSX(File path) {
try {
FileInputStream fis = new FileInputStream(path);
XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator<Row> rowIterator = mySheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
break;
case Cell.CELL_TYPE_NUMERIC:
break;
case Cell.CELL_TYPE_BOOLEAN:
break;
default:
}
printlnToUser(cell);
}
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.