用JAVA写一个excel导入
时间: 2023-05-12 09:03:29 浏览: 77
JAVA实现Excel表数据导入ORACLE数据库.txt
5星 · 资源好评率100%
非常感谢您的提问,我可以为您提供以下JAVA代码来实现excel导入功能:
```
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
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 ExcelImporter {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("example.xlsx"));
// Create Workbook instance holding reference to .xlsx file
Workbook workbook = new XSSFWorkbook(file);
// Get first/desired sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
// Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
System.out.println("");
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请注意,这只是一个简单的示例代码,您需要根据您的具体需求进行修改和优化。
阅读全文