Android Java读取excel内容代码
时间: 2023-05-31 09:07:16 浏览: 84
android 读取Excel表格内容
5星 · 资源好评率100%
好的,以下是Java读取Excel内容的示例代码:
```
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream(new File("sample.xlsx"));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
// 遍历行
for (Row row : sheet) {
// 遍历列
for (Cell cell : row) {
// 读取单元格内容并输出
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default:
System.out.print("\t");
}
}
System.out.println();
}
workbook.close();
inputStream.close();
}
}
```
注意,需要导入Apache POI的库来操作Excel文件。
阅读全文