新聞中心
隨著互聯(lián)網(wǎng)的發(fā)展和大數(shù)據(jù)的興起,數(shù)據(jù)的處理和管理成為了企業(yè)和個人不可或缺的能力。其中,將Excel表格中的數(shù)據(jù)導入數(shù)據(jù)庫是一種常見的數(shù)據(jù)處理方式。本文將介紹如何利用Java語言將Excel中的數(shù)據(jù)導入到MySQL數(shù)據(jù)庫中。

為沽源等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及沽源網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、沽源網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
一、環(huán)境配置
在進行Excel數(shù)據(jù)導入之前,需要先準備好相應(yīng)的環(huán)境。我們需要從官網(wǎng)上下載JDBC驅(qū)動程序,并將其加入Java項目中;同時,我們還需要依賴POI庫來讀取Excel文件。
二、讀取Excel文件
Java中讀取Excel文件的方式有很多,本文將介紹一種基于Apache POI的讀取方式。
1.導入POI庫:
“`
org.apache.poi
poi
4.1.2
org.apache.poi
poi-ooxml
4.1.2
“`
2.代碼實現(xiàn):
“`
public static void readExcel(String filePath) throws IOException {
// 創(chuàng)建 Excel 文件的輸入流對象
FileInputStream excelFile = new FileInputStream(new File(filePath));
Workbook workbook = null;
// 根據(jù)文件后綴名不同(xls和xlsx)獲得不同的Workbook實現(xiàn)類對象
if (filePath.endsWith(“xls”)) {
workbook = new HSSFWorkbook(excelFile);
} else if (filePath.endsWith(“xlsx”)) {
workbook = new XSSFWorkbook(excelFile);
}
// 獲得之一個sheet的內(nèi)容
Sheet sheet = workbook.getSheetAt(0);
// 獲得sheet中的所有行
Iterator rows = sheet.iterator();
// 遍歷所有行
while (rows.hasNext()) {
Row row = rows.next();
// 獲得當前行的所有列
Iterator cells = row.iterator();
while (cells.hasNext()) {
Cell cell = cells.next();
// 根據(jù)單元格的類型讀取相應(yīng)的數(shù)據(jù)
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:
break;
}
}
System.out.println();
}
excelFile.close();
}
“`
通過上述代碼實現(xiàn),我們可以將Excel文件中的內(nèi)容讀取出來,并輸出到控制臺中,也可以將其保存到數(shù)據(jù)庫中。
三、連接數(shù)據(jù)庫
數(shù)據(jù)導入的前提是需要在Java程序中連接上MySQL數(shù)據(jù)庫。連接流程大致為:加載數(shù)據(jù)庫驅(qū)動程序,創(chuàng)建連接,關(guān)閉連接。其中,需要在Java項目中引入外部的MySQL數(shù)據(jù)庫驅(qū)動jar包,MySQL Connector/J是使用最廣泛的JDBC驅(qū)動。
1.導入MySQL Connector/J庫:
“`
mysql
mysql-connector-java
8.0.26
“`
2.代碼實現(xiàn):
“`
public static Connection getConnection() {
String driver = “com.mysql.cj.jdbc.Driver”;
String url = “jdbc:mysql://localhost:3306/test”;
String username = “root”;
String password = “123456”;
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
if(conn!=null){
System.out.println(“連接成功!”);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
“`
通過上述代碼實現(xiàn),我們可以連接上MySQL數(shù)據(jù)庫并輸出連接成功的信息。
四、數(shù)據(jù)導入
連接上數(shù)據(jù)庫之后,我們需要將Excel文件中的數(shù)據(jù)逐條插入到數(shù)據(jù)庫中。
1.編寫插入語句:
“`
String sql = “INSERT INTO student (id, name, age, sex) VALUES (?, ?, ?, ?)”;
“`
2.將Excel文件中的數(shù)據(jù)插入到數(shù)據(jù)庫中:
“`
public static void insertData() throws IOException, SQLException {
Connection conn = getConnection();
String filePath = “C:/Users/Administrator/Desktop/student.xlsx”;
FileInputStream excelFile = new FileInputStream(new File(filePath));
Workbook workbook = null;
if (filePath.endsWith(“xls”)) {
workbook = new HSSFWorkbook(excelFile);
} else if (filePath.endsWith(“xlsx”)) {
workbook = new XSSFWorkbook(excelFile);
}
Sheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.iterator();
while (rows.hasNext()) {
Row row = rows.next();
Iterator cells = row.iterator();
PreparedStatement pstmt = conn.prepareStatement(sql);
while (cells.hasNext()) {
Cell cell = cells.next();
switch (cell.getCellType()) {
case STRING:
pstmt.setString(1, cell.getStringCellValue());
pstmt.setString(2, cell.getStringCellValue());
pstmt.setString(4, cell.getStringCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
pstmt.setDate(3, new java.sql.Date(cell.getDateCellValue().getTime()));
} else {
pstmt.setInt(1, (int) cell.getNumericCellValue());
pstmt.setInt(3, (int) cell.getNumericCellValue());
}
break;
default:
break;
}
}
pstmt.executeUpdate();
System.out.println(“插入成功!”);
}
excelFile.close();
conn.close();
}
“`
通過上述代碼實現(xiàn),我們可以將Excel文件中的數(shù)據(jù)插入到MySQL數(shù)據(jù)庫中。
相關(guān)問題拓展閱讀:
- 如何用java導入Excel數(shù)據(jù)到數(shù)據(jù)庫
如何用java導入Excel數(shù)據(jù)到數(shù)據(jù)庫
你是怎么樣用JAVA從EXCEL中取得數(shù)據(jù)不了解?
導入數(shù)據(jù)庫,就是從EXCEL中取得數(shù)據(jù),
然后生成INSERT語句,向數(shù)據(jù)庫中插入數(shù)據(jù)。
參考下面代碼:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class WriteExcel {
WritableWorkbook book=null;
public void OutputExcel(ArrayList arlist,String Path){
try{
book = Workbook.createWorkbook(new File(Path));
//設(shè)置表名
WritableSheet sheet = book.createSheet(“考試單”,0);
//生成表格題頭
Label labe1 = new Label(0, 0, “考生姓名” );
Label labe2 = new Label(1, 0, “地區(qū)”);
Label labe3 = new Label(2, 0, “所屬院校”);
Label labe4 = new Label(3, 0, “班級”);
Label labe5 = new Label(4, 0, “考試號”);
Label labe6 = new Label(5, 0, “考試時間”);
Label labe7 = new Label(6, 0, “科目名稱”);
//將生成的單元格添加到工作表中
sheet.addCell(labe1);
sheet.addCell(labe2);
sheet.addCell(labe3);
sheet.addCell(labe4);
sheet.addCell(labe5);
sheet.addCell(labe6);
sheet.addCell(labe7);
Iterator it = arlist.iterator();
int i = 1;
while(it.hasNext()){
//通過迭代獲得arlist里的MarkesData對象
MarkesData temp = (MarkesData)it.next();
//取得數(shù)據(jù)生成單元格
Label label1=new Label(0,i,temp.getUser_name());
Label label2=new Label(1,i,temp.getArea_name());
Label label3=new Label(2,i,temp.getCollege_name());
Label label4=new Label(3,i,temp.getClass_name());
Label label5=new Label(4,i,temp.getTest_name());
Label label6=new Label(5,i,temp.getStarttime());
Label label7=new Label(6,i,temp.getSubject_name());
//將生成的單元格添加到工作表中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
sheet.addCell(label6);
sheet.addCell(label7);
i++;
}
book.write();
book.close();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(book!=null)book.close();
}catch(Exception e){
System.out.println(“exception when closing Connection in finally”);
System.out.println(e.getMessage().toString());
}
}
}
}
關(guān)于java excel 導入數(shù)據(jù)庫的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都服務(wù)器托管選創(chuàng)新互聯(lián),先上架開通再付費。
創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)-網(wǎng)站建設(shè),軟件開發(fā)老牌服務(wù)商!微信小程序開發(fā),APP開發(fā),網(wǎng)站制作,網(wǎng)站營銷推廣服務(wù)眾多企業(yè)。電話:028-86922220
新聞標題:使用Java將Excel數(shù)據(jù)導入數(shù)據(jù)庫 (java excel 導入數(shù)據(jù)庫)
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/dppijec.html


咨詢
建站咨詢
