java读取excel并导入数据库_java利用poi读取excel中数据并导入数据库

package comglj.utils;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.text.DecimalFormat;

import java.text.SimpleDateFormat;

import java.util.LinkedList;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**

* 读取excel文件

* @author GLJ

*Poi解析2003时使用的是HSSFCell,而2007的则是

XSSFCell,是完全不同的两套API

必须先要判断excel的类型,不过 HSSFWorkbook 和 XSSFWorkbook 实现的接口都是一样的Workbook

直接在实例化接口的时候有点区别其他时候没有任何差异。

*/

public class ReadExcelUtil {

public static List> readExcel(File file) throws IOException {

String fileName = file.getName();

String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName

.substring(fileName.lastIndexOf(".") + 1);

if ("xls".equals(extension)) {

return read2003Excel(file);

} else if ("xlsx".equals(extension)) {

return read2007Excel(file);

} else {

throw new IOException("不支持的文件类型");

}

}

/**

* 读取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Java 代码示例,用于读取 Excel 文件并将其内容导入数据库: ```java import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; 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.ss.usermodel.WorkbookFactory; public class ExcelToDatabase { public static void main(String[] args) { String fileName = "path/to/excel/file.xlsx"; String jdbcUrl = "jdbc:mysql://localhost:3306/mydatabase"; String dbUser = "root"; String dbPassword = "password"; try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword)) { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(fis); Sheet sheet = workbook.getSheetAt(0); String sql = "INSERT INTO mytable (col1, col2, col3) VALUES (?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); int rowNumber = 0; for (Row row : sheet) { if (rowNumber == 0) { // Skip header row rowNumber++; continue; } Cell cell1 = row.getCell(0); Cell cell2 = row.getCell(1); Cell cell3 = row.getCell(2); pstmt.setString(1, cell1.getStringCellValue()); pstmt.setString(2, cell2.getStringCellValue()); pstmt.setString(3, cell3.getStringCellValue()); pstmt.executeUpdate(); rowNumber++; } pstmt.close(); fis.close(); workbook.close(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,此示例代码需要导入 Apache POI 和 MySQL JDBC 驱动程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值