java导入excel到数据库
时间: 2023-09-11 08:09:45 浏览: 51
1. 首先需要使用Java中的JDBC连接数据库。可以使用JDBC API中的Connection类来建立与数据库的连接。
2. 使用Apache POI库读取Excel文件。Apache POI是Java编程语言的开源库,用于读写Microsoft Office文件格式的文件,包括Excel。
3. 读取Excel文件中的数据,并将其存储在Java对象中。
4. 使用JDBC API中的PreparedStatement类来构建插入数据的SQL语句,并将数据插入到数据库中。
下面是一个示例代码,演示如何将从Excel文件中读取的数据导入到MySQL数据库中:
```
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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 ExcelToDatabase {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
PreparedStatement statement = null;
FileInputStream file = new FileInputStream("path/to/excel/file.xlsx");
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
statement = connection.prepareStatement("INSERT INTO mytable (column1, column2, column3) VALUES (?, ?, ?)");
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex) {
case 0:
statement.setString(1, cell.getStringCellValue());
break;
case 1:
statement.setString(2, cell.getStringCellValue());
break;
case 2:
statement.setDouble(3, cell.getNumericCellValue());
break;
}
}
statement.executeUpdate();
}
connection.close();
file.close();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
上面的示例代码使用了XSSFWorkbook类来读取Excel文件,这个类支持.xlsx格式的文件。如果要读取.xls格式的文件,可以使用HSSFWorkbook类代替。在读取Excel文件时,使用了Iterator类来遍历文件中的行和单元格,并使用PreparedStatement类构建SQL语句,将数据插入到数据库中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![mp4](https://img-home.csdnimg.cn/images/20210720083504.png)