excel配合java和mysql_java-读excel,写入mysql

读excel

package xsl2mysql;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

public class xlsread {

public List readExcel(String xlspath) {

File file = new File(xlspath);

try {

// 创建输入流,读取Excel

InputStream is = new FileInputStream(file.getAbsolutePath());

// jxl提供的Workbook类

Workbook wb = Workbook.getWorkbook(is);

// Excel的页签数量

int sheet_size = wb.getNumberOfSheets();

for (int index = 0; index < sheet_size; index++) {

List outerList=new ArrayList();

// 每个页签创建一个Sheet对象

Sheet sheet = wb.getSheet(index);

// sheet.getRows()返回该页的总行数

for (int i = 0; i < sheet.getRows(); i++) {

List innerList=new ArrayList();

// sheet.getColumns()返回该页的总列数

for (int j = 0; j < sheet.getColumns(); j++) {

String cellinfo = sheet.getCell(j, i).getContents();

if(cellinfo.isEmpty()){

continue;

}

innerList.add(cellinfo);

//System.out.print(cellinfo);

}

outerList.add(i, innerList);

//System.out.println();

}

return outerList;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (BiffException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

}

首先mysql需要建表

package xsl2mysql;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.*;

public class write2mysql {

// JDBC 驱动名及数据库 URL

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static final String DB_URL = "jdbc:mysql://192.168.124.145:30963/RUNOOB";

// 数据库的用户名与密码,需要根据自己的设置

static final String USER = "root";

static final String PASS = "root";

public static void main(String[] args) {

Connection conn = null;

Statement stmt = null;

try{

// 注册 JDBC 驱动

Class.forName("com.mysql.cj.jdbc.Driver");

// 打开链接

System.out.println("连接数据库...");

conn = DriverManager.getConnection(DB_URL,USER,PASS);

// 执行查询

System.out.println(" 实例化Statement对象...");

stmt = conn.createStatement();

String sql;

sql = "CREATE TABLE bugall (id int not null, 问题类型 varchar(20) not null, 标识 char(20) not null, 主题 varchar(100) not null, 经办人 varchar(20) not null, 报告人 char(20) not null, 优先级 varchar(10) not null, 状态 varchar(10) not null,解决结果 varchar(10) not null,创建 Datetime not null,更新 Datetime not null, primary key (id));";

stmt.executeUpdate(sql);

stmt.close();

conn.close();

}catch(SQLException se){

// 处理 JDBC 错误

se.printStackTrace();

}catch(Exception e){

// 处理 Class.forName 错误

e.printStackTrace();

}finally{

// 关闭资源

try{

if(stmt!=null) stmt.close();

}catch(SQLException se2){

}// 什么都不做

try{

if(conn!=null) conn.close();

}catch(SQLException se){

se.printStackTrace();

}

}

System.out.println("Goodbye!");

}

}

读取到的List传入mysql

1.使用perparestatument

2.数据格式里面有个字符串转date,然后util.date转sql.date

package xsl2mysql;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.write.Label;

import java.io.File;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

public class test {

public void testsql(List newlist) throws Exception {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (Exception e) {

e.printStackTrace();

}

Connection c = DriverManager.getConnection("jdbc:mysql://192.168.124.145:30963/RUNOOB", "root", "root");

String sql = "insert into bugall values(?,?,?,?,?,?,?,?,?,?,?)";

PreparedStatement ps = c.prepareStatement(sql);

//SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 ");

SimpleDateFormat sDateFormat=new SimpleDateFormat("MM/dd/yy HH:mm");

for (int i = 0; i < newlist.size()-1; i++) {

List innerList = new ArrayList();

List list = (List) newlist.get(i);

ps.setInt(1, i + 1);

ps.setString(2, list.get(0).toString());

ps.setString(3, list.get(1).toString());

ps.setString(4, list.get(2).toString());

ps.setString(5, list.get(3).toString());

ps.setString(6, list.get(4).toString());

ps.setString(7, list.get(5).toString());

ps.setString(8, list.get(6).toString());

ps.setString(9, list.get(7).toString());

try{

Date date1 = sDateFormat.parse(list.get(8).toString());

java.sql.Date datesql1 = new java.sql.Date(date1.getTime());

ps.setDate(10, datesql1);

Date date2 = sDateFormat.parse(list.get(9).toString());

java.sql.Date datesql2 = new java.sql.Date(date2.getTime());

ps.setDate(11, datesql2);

} catch (ParseException px){

px.printStackTrace();

}

ps.execute();

}

}

}

要将本地Excel文件写入MySQL,需要以下步骤: 1. 首先,需要连接到MySQL数据库。可以使用Java中的JDBC API来实现连接。这需要在Java程序中导入JDBC驱动程序。 2. 接下来,需要使用Java中的Apache POI库来Excel文件。这需要在Java程序中导入Apache POI库。 3. 从Excel文件中取数据并将其存储在Java对象中。 4. 使用JDBC API将Java对象中的数据插入到MySQL数据库中。 以下是一些示例代码,可用作参考: 1. 连接到MySQL数据库 ``` String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "root"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); ``` 2. Excel文件 ``` FileInputStream inputStream = new FileInputStream(new File("path/to/excel/file.xlsx")); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); System.out.print(cell.getStringCellValue() + "\t"); } System.out.println(""); } workbook.close(); inputStream.close(); ``` 3. 将数据插入到MySQL数据库中 ``` PreparedStatement stmt = conn.prepareStatement("INSERT INTO mytable (column1, column2, column3) VALUES (?, ?, ?)"); stmt.setString(1, value1); stmt.setString(2, value2); stmt.setString(3, value3); stmt.executeUpdate(); stmt.close(); ``` 在这个示例中,`value1`、`value2`和`value3`是从Excel文件中取的数据。需要根据具体情况修改SQL语句和参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值