[英]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.