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

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


咨詢
建站咨詢
