新聞中心
隨著Web項(xiàng)目的不斷增多,無(wú)論是企業(yè)內(nèi)部的數(shù)據(jù)維護(hù),還是對(duì)外發(fā)布的數(shù)據(jù)交流,都需要使用到數(shù)據(jù)庫(kù)。而將Web項(xiàng)目中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中,是一個(gè)常見(jiàn)的需求。這時(shí)我們就需要使用一些工具來(lái)完成這一任務(wù)。其中,POI工具是一個(gè)非常常用的工具,它可以幫助我們將從Web項(xiàng)目中提取出來(lái)的數(shù)據(jù)進(jìn)行處理,并將其導(dǎo)入到數(shù)據(jù)庫(kù)中。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括商都網(wǎng)站建設(shè)、商都網(wǎng)站制作、商都網(wǎng)頁(yè)制作以及商都網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,商都網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到商都省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
POI工具是一個(gè)用于對(duì)Microsoft Office格式文件進(jìn)行操作的Java庫(kù)。它可以打開(kāi)、編輯、保存Microsoft Excel、Word和PowerPoint格式的文件。在時(shí),我們需要使用POI的Excel讀寫(xiě)功能來(lái)處理Excel格式的數(shù)據(jù)。
我們需要準(zhǔn)備好Excel數(shù)據(jù)表格。這個(gè)表格應(yīng)該包含所有要導(dǎo)入到數(shù)據(jù)庫(kù)中的數(shù)據(jù),每個(gè)工作表應(yīng)該對(duì)應(yīng)一個(gè)表格。然后,我們需要使用POI的API來(lái)讀取Excel數(shù)據(jù),如下所示:
“`
FileInputStream inputStream = new FileInputStream(new File(“data.xlsx”));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0); //獲取之一個(gè)工作表
Iterator iterator = sheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
//處理當(dāng)前行數(shù)據(jù),并將其導(dǎo)入到數(shù)據(jù)庫(kù)中
}
“`
上面的代碼片段中,我們首先使用FileInputStream類來(lái)讀取Excel文件,然后使用XSSFWorkbook類來(lái)代表工作簿。接著,我們使用getSheetAt()方法獲取工作表,從而可以遍歷該表格的所有行。對(duì)于每一行,我們可以使用POI的API來(lái)獲取該行的所有單元格,并對(duì)其進(jìn)行處理。在處理完成后,我們可以將數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中。
接下來(lái),我們需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)連接,以便將數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中。使用JDBC來(lái)連接數(shù)據(jù)庫(kù),并創(chuàng)建SQL語(yǔ)句來(lái)插入數(shù)據(jù)。示例代碼如下所示:
“`
//創(chuàng)建連接
String jdbcUrl = “jdbc:mysql://localhost/testdb”;
String username = “root”;
String password = “password”;
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
//創(chuàng)建插入SQL
String insertSql = “INSERT INTO employees (id, name, age) VALUES (?, ?, ?)”;
//預(yù)編譯SQL,提高效率
PreparedStatement statement = connection.prepareStatement(insertSql);
//為SQL語(yǔ)句中的每個(gè)參數(shù)設(shè)置值
statement.setInt(1, id);
statement.setString(2, name);
statement.setInt(3, age);
//執(zhí)行SQL語(yǔ)句,將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中
statement.executeUpdate();
“`
上面的代碼片段中,我們首先使用JDBC連接到MySQL數(shù)據(jù)庫(kù),然后創(chuàng)建SQL語(yǔ)句來(lái)插入數(shù)據(jù)。為了提高效率,我們可以使用PreparedStatement類進(jìn)行預(yù)編譯,然后為SQL語(yǔ)句中的每個(gè)參數(shù)設(shè)置值,并使用executeUpdate()方法將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。
我們使用POI的API來(lái)將數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中。完整代碼如下所示:
“`
FileInputStream inputStream = new FileInputStream(new File(“data.xlsx”));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0); //獲取之一個(gè)工作表
String jdbcUrl = “jdbc:mysql://localhost/testdb”;
String username = “root”;
String password = “password”;
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
String insertSql = “INSERT INTO employees (id, name, age) VALUES (?, ?, ?)”;
PreparedStatement statement = connection.prepareStatement(insertSql);
Iterator iterator = sheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
int id = (int) currentRow.getCell(0).getNumericCellValue();
String name = currentRow.getCell(1).getStringCellValue();
int age = (int) currentRow.getCell(2).getNumericCellValue();
statement.setInt(1, id);
statement.setString(2, name);
statement.setInt(3, age);
statement.executeUpdate();
}
statement.close();
connection.close();
workbook.close();
inputStream.close();
“`
上述代碼會(huì)按順序執(zhí)行以下操作:
1. 讀取Excel數(shù)據(jù);
2. 連接到MySQL數(shù)據(jù)庫(kù);
3. 創(chuàng)建插入SQL語(yǔ)句;
4. 遍歷Excel表格的每一行;
5. 將每行數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中;
6. 關(guān)閉PreparedStatement、Connection、Workbook和FileInputStream。
相關(guān)問(wèn)題拓展閱讀:
- java web 的工程中 導(dǎo)入excel到數(shù)據(jù)庫(kù)時(shí),怎么把任意的文件導(dǎo)入到數(shù)據(jù)庫(kù)? 就是任意的文件的那段代碼是什
- poi導(dǎo)入excel
java web 的工程中 導(dǎo)入excel到數(shù)據(jù)庫(kù)時(shí),怎么把任意的文件導(dǎo)入到數(shù)據(jù)庫(kù)? 就是任意的文件的那段代碼是什
錯(cuò)了,你是要批量導(dǎo)入嗎?
需要下載poi包
首先要先把文件轉(zhuǎn)變冊(cè)謹(jǐn)成數(shù)據(jù)流InputStream,然后
try
{
HSSFWorkbook workbook = new HSSFWorkbook(is);
HSSFSheet sheet=workbook.getSheetAt(0);
}
catch(Exception e)
{
e.printStackTrace();
}
if(sheet==null)
{
close(streamIn);
return null;
}
int rows = sheet.getPhysicalNumberOfRows();
if(rows 0) {
if (!importFile.getFileName().endsWith(“.xls”)) {
return mapping.findForward(“Success”滲局運(yùn)升);
}
try {
HSSFWorkbook workbook = new HSSFWorkbook(importFile
.getInputStream());
HSSFSheet sheet = workbook.getSheetAt(0);
poi導(dǎo)入excel
方法/步驟
一, ExcelUtils 工具類(也就是解析EXCEL文件,判斷EXCEL的類型以及數(shù)據(jù)的類型)
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 ExcelUtils {
private final static String excel2023L = “.xls” ; // 2023-版本的excel
private final static String excel2023U = “.xlsx” ; // 2023 +版本的excel
/ **
*描述:獲取IO流中的數(shù)據(jù),組裝成List 對(duì)象
* @param in,fileName
* @返回
* @throws IOException
* /
public List getBankListByExcel(InputStream in,String fileName) throws Exception {
列表 list = null ;
//創(chuàng)建的Excel工作薄
Workbook work = this .getWorkbook(in,fileName);
if (null == work){
拋出新的 異常(“創(chuàng)建Excel工作薄為空!” );
}
Sheet sheet = null ; //頁(yè)數(shù)
行row = null ; //行數(shù)
Cell cell = null ; //列數(shù)
list = new ArrayList >();
//遍歷的Excel中所有的片
for (int i = 0 ; i li = new ArrayList ();
for (int y = row.getFirstCellNum(); y import org.apache.poi.xssf.usermodel.XSSFCellStyle;
公共類 ExcelBean 實(shí)現(xiàn) java.io.Serializable {
private String headTextName; //列頭(標(biāo)題)名
private String propertyName; //對(duì)應(yīng)字段名
私有 整數(shù)列; //合并單元格數(shù)
私人 XSSFCellStyle cellStyle;
public ExcelBean(){
}
public ExcelBean(String headTextName,String propertyName){
這個(gè).headTextName = headTextName;
這個(gè).propertyName = propertyName;
}
public ExcelBean(String headTextName,String propertyName,Integer cols){
super ();
這個(gè).headTextName = headTextName;
這個(gè).propertyName = propertyName;
這個(gè).cols = cols;
}
public String getHeadTextName(){
return headTextName;
}
public void setHeadTextName(String headTextName){
這個(gè).headTextName = headTextName;
}
public String getPropertyName(){
return propertyName;
}
public void setPropertyName(String propertyName){
這個(gè).propertyName = propertyName;
}
public Integer getCols(){
返回列 ;
}
公共無(wú)效 setCols(Integer cols){
這個(gè).cols = cols;
}
上市 XSSFCellStyle getCellStyle(){
返回 cellStyle;
}
公共無(wú)效 setCellStyle(XSSFCellStyle cellStyle){
這個(gè) .cellStyle = cellStyle;
}
}
people.java
import java.util.Date;
公共課 人
私有 整數(shù)id
private String userName;
私人 字符串密碼;
私人 整數(shù)年齡;
私人 日期;
public Integer getId(){
返回 id
}
public void setId(Integer id){
這個(gè).id = id;
}
public String getUserName(){
return userName;
}
public void setUserName(String userName){
這個(gè).userName = userName == null ? null :userName.trim();
}
public String getPassword(){
返回 密碼
}
public void setPassword(String password){
這個(gè).password = password == null ? null :password.trim();
}
public Integer getAge(){
回歸 年齡
}
public void setAge(Integer age){
這個(gè).age = age
}
public Date getDate(){
退貨 日期
}
public void setDate(Date date){
這個(gè).date = date
}
}
web項(xiàng)目poi導(dǎo)入數(shù)據(jù)庫(kù)的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于web項(xiàng)目poi導(dǎo)入數(shù)據(jù)庫(kù),使用POI工具將Web項(xiàng)目數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù),java web 的工程中 導(dǎo)入excel到數(shù)據(jù)庫(kù)時(shí),怎么把任意的文件導(dǎo)入到數(shù)據(jù)庫(kù)? 就是任意的文件的那段代碼是什,poi導(dǎo)入excel的信息別忘了在本站進(jìn)行查找喔。
創(chuàng)新互聯(lián)服務(wù)器托管擁有成都T3+級(jí)標(biāo)準(zhǔn)機(jī)房資源,具備完善的安防設(shè)施、三線及BGP網(wǎng)絡(luò)接入帶寬達(dá)10T,機(jī)柜接入千兆交換機(jī),能夠有效保證服務(wù)器托管業(yè)務(wù)安全、可靠、穩(wěn)定、高效運(yùn)行;創(chuàng)新互聯(lián)專注于成都服務(wù)器托管租用十余年,得到成都等地區(qū)行業(yè)客戶的一致認(rèn)可。
分享名稱:使用POI工具將Web項(xiàng)目數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)(web項(xiàng)目poi導(dǎo)入數(shù)據(jù)庫(kù))
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/dpisihd.html


咨詢
建站咨詢
