當前位置:首頁 » 數據倉庫 » javaexcel表格導入資料庫中
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

javaexcel表格導入資料庫中

發布時間: 2022-06-29 19:23:29

㈠ 如何用java實現把excel表中的數據導入到mysql資料庫已有的表中

package com.cn.gao;

import java.util.List;

public class FromExcelToDb {
public static void main(String[] args) {
//得到表格中所有的數據
List<Stu> listExcel=StuService.getAllByExcel("d://book.xls");
/*//得到資料庫表中所有的數據
List<Stu> listDb=StuService.getAllByDb();*/

DBhelper db=new DBhelper();

for (Stu stuEntity : listExcel) {
int id=stuEntity.getId();
if (!StuService.isExist(id)) {
//不存在就添加
String sql="insert into student (name,sex,num) values(?,?,?)";
String[] str=new String[]{stuEntity.getName(),stuEntity.getSex(),stuEntity.getNum()+""};
db.AddU(sql, str);
}else {
//存在就更新
String sql="update student set name=?,sex=?,num=? where id=?";
String[] str=new String[]{stuEntity.getName(),stuEntity.getSex(),stuEntity.getNum()+"",id+""};
db.AddU(sql, str);
}
}
System.out.println("數據更新成功!");
}
}

㈡ java 怎麼把 excel文件導入到資料庫

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import jxl.*;
public class SimUpdate {
private String fileName;
public ZfzSimUpdate(String fileName){
this.fileName = fileName;
}
static Map tNames;
static{
tNames = new HashMap();
}
/**
* 用於產生 資料庫的 ID 值,組成 [年月日時分秒(100-999)] 總共 17 位數.
* 根據不同的表名,可保證同一秒內產生的 ID 號不重復
*/
private static String getDtime() {
String rid;
Date nd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
rid = sdf.format(nd);
return rid;
}

public String getSeqNumber(String tableName) {
if(tableName == null || "".equals(tableName))
tableName = "GENERY";
Integer it;
// noinspection SynchronizeOnNonFinalField
synchronized(tNames){
it = (Integer)tNames.get(tableName);
if(it == null){
it = new Integer(100);
tNames.put(tableName, it);
}else{
if(it.intValue() > 998)
it = new Integer(100);
else
it = new Integer(1 + it.intValue());
tNames.put(tableName, it);
}
}
return getDtime() + String.valueOf(it);
}

private void updateDb(){
try{
Connection conn = DbPool.connectDB();
if(conn != null){
Statement stmt = conn.createStatement();
/**********************************************/
jxl.Workbook rwb = null;
try{
//構建Workbook對象 只讀Workbook對象
//直接從本地文件創建Workbook
//從輸入流創建Workbook
InputStream is = new FileInputStream(fileName);
rwb = Workbook.getWorkbook(is);
//Sheet(術語:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下標是從0開始的
//獲取第一張Sheet表
Sheet rs = rwb.getSheet(0);
//獲取Sheet表中所包含的總列數
int rsColumns = rs.getColumns();
//獲取Sheet表中所包含的總行數
int rsRows = rs.getRows();
//獲取指這下單元格的對象引用

String simNumber = "",termSeqId = "";
//指定SIM卡號及序列號
for(int i=0;i<rsRows;i++){
for(int j=0;j<rsColumns;j++){
Cell cell = rs.getCell(j,i);
if(j==0){
simNumber = cell.getContents();
}
termSeqId = "633"+simNumber;
}
String sql = "查詢SQL";
int isOk = stmt.executeUpdate(sql);
if(isOk == 0 && !simNumber.equals("")){
String termId = getSeqNumber("termInf");
String insertSql = "自定義INSERT";
int isAdd = stmt.executeUpdate(insertSql);
if(isAdd > 0){
System.out.println("成功插入第"+i+"條數據");
}

}
//System.out.println("SIM卡號:"+simNumber+",序列號:"+termSeqId);
}

//以下代碼為寫入新的EXCEL,這里不使用,所以注釋
/*

//利用已經創建的Excel工作薄創建新的可寫入的Excel工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("D://Book2.xls"),rwb);
//讀取第一張工作表
jxl.write.WritableSheet ws = wwb.getSheet(0);

//獲取第一個單元格對象
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
//決斷單元格的類型,做出相應的轉化
if (wc.getType() == CellType.LABEL) {
Label l = (Label) wc;
l.setString("The value has been modified.");
}
//寫入Excel對象
wwb.write();
wwb.close();
*/
}catch(Exception e){
e.printStackTrace();
}
finally{
//操作完成時,關閉對象,翻譯佔用的內存空間
rwb.close();

}
/*********************************************/

}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
DbPool dbPool = new DbPool("dbConn.cfg");//連接資料庫
SimUpdate simUpdate = new SimUpdate("zfz_sim.xls");
simUpdate.updateDb();

}

}

我只用了讀取XLS,寫入沒試,應該沒問題吧,你把注釋了的拿 來試一下吧

㈢ Java 怎麼把 excel文件導入到資料庫

package com.ddns.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.hssf.util.Region;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2010-6-28
* Time: 10:56:48
* To change this template use File | Settings | File Templates.
*/
public class ExcelFile {
/**
* 新建一個Excel文件,裡面添加5行5列的內容,再添加兩個高度為2的大單元格。
*
* @param fileName
*/
public void writeExcel(String fileName) {

//目標文件
File file = new File(fileName);
FileOutputStream fOut = null;
try {
// 創建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();

// 在Excel工作簿中建一工作表,其名為預設值。
// 也可以指定工作表的名字。
HSSFSheet sheet = workbook.createSheet("Test_Table");

// 創建字體,紅色、粗體
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

// 創建單元格的格式,如居中、左對齊等
HSSFCellStyle cellStyle = workbook.createCellStyle();
// 水平方向上居中對齊
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 垂直方向上居中對齊
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 設置字體
cellStyle.setFont(font);

//下面將建立一個4行3列的表。第一行為表頭。
int rowNum = 0;//行標
int colNum = 0;//列標
//建立表頭信息
// 在索引0的位置創建行(最頂端的行)
HSSFRow row = sheet.createRow((short) rowNum);
// 單元格
HSSFCell cell = null;
for (colNum = 0; colNum < 5; colNum++) {
// 在當前行的colNum列上創建單元格
cell = row.createCell((short) colNum);

// 定義單元格為字元類型,也可以指定為日期類型、數字類型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 定義編碼方式,為了支持中文,這里使用了ENCODING_UTF_16
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
// 為單元格設置格式
cell.setCellStyle(cellStyle);

// 添加內容至單元格
cell.setCellValue("表頭名-" + colNum);
}
rowNum++;
for (; rowNum < 5; rowNum++) {
//新建第rowNum行
row = sheet.createRow((short) rowNum);
for (colNum = 0; colNum < 5; colNum++) {
//在當前行的colNum位置創建單元格
cell = row.createCell((short) colNum);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue("值-" + rowNum + "-" + colNum);
}
}

//合並單元格
//先創建2行5列的單元格,然後將這些單元格合並為2個大單元格
rowNum = 5;
for (; rowNum < 7; rowNum++) {
row = sheet.createRow((short) rowNum);
for (colNum = 0; colNum < 5; colNum++) {
//在當前行的colNum位置創建單元格
cell = row.createCell((short) colNum);
}
}
//建立第一個大單元格,高度為2,寬度為2
rowNum = 5;
colNum = 0;
Region region = new Region(rowNum, (short) colNum, (rowNum + 1),(short) (colNum + 1));
sheet.addMergedRegion(region);
//獲得第一個大單元格
cell = sheet.getRow(rowNum).getCell((short) colNum);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue("第一個大單元格");

//建立第二個大單元格,高度為2,寬度為3
colNum = 2;
region = new Region(rowNum, (short) colNum, (rowNum + 1),(short) (colNum + 2));
sheet.addMergedRegion(region);
//獲得第二個大單元格
cell = sheet.getRow(rowNum).getCell((short) colNum);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue("第二個大單元格");

//工作薄建立完成,下面將工作薄存入文件
//新建一輸出文件流
fOut = new FileOutputStream(file);
//把相應的Excel 工作簿存檔
workbook.write(fOut);
fOut.flush();
//操作結束,關閉文件
fOut.close();

System.out
.println("Excel文件生成成功!Excel文件名:" + file.getAbsolutePath());
} catch (Exception e) {
System.out.println("Excel文件" + file.getAbsolutePath() + "生成失敗:" + e);
} finally {
if (fOut != null){
try {
fOut.close();
} catch (IOException e1) {
}
}
}
}

/**
* 讀Excel文件內容
* @param fileName
*/
public void readExcel(String fileName) {
File file = new File(fileName);
FileInputStream in = null;
try {
//創建對Excel工作簿文件的引用
in = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(in);

//創建對工作表的引用。
//這里使用按名引用
HSSFSheet sheet = workbook.getSheet("Test_Table");
//也可用getSheetAt(int index)按索引引用,
//在Excel文檔中,第一張工作表的預設索引是0,其語句為:
//HSSFSheet sheet = workbook.getSheetAt(0);

//下面讀取Excel的前5行的數據
System.out.println("下面是Excel文件" + file.getAbsolutePath() + "的內容:");
HSSFRow row = null;
HSSFCell cell = null;
int rowNum = 0;//行標
int colNum = 0;//列標
for (; rowNum < 5; rowNum++) {
//獲取第rowNum行
row = sheet.getRow((short) rowNum);
for (colNum = 0; colNum < 5; colNum++) {
// 獲取當前行的colNum位置的單元格
cell = row.getCell((short) colNum);
System.out.print(cell.getStringCellValue() + "\t");
}
//換行
System.out.println();
}

in.close();
} catch (Exception e) {
System.out.println("讀取Excel文件" + file.getAbsolutePath() + "失敗:" + e);
} finally {
if (in != null){
try {
in.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) throws Exception {
ExcelFile excel = new ExcelFile();
String fileName = "D:\\記錄明細.xls";
excel.writeExcel(fileName);
excel.readExcel(fileName);
}
}

㈣ 用java怎麼將excel表格數據導入到mysql資料庫中

現有poi讀取到java里,然後通過java操作資料庫把讀取的內容插入到資料庫里。

㈤ java中怎麼把excel表格中的數據導入資料庫

然後粘貼復制 非常簡單

㈥ 如何用java實現將execl表格導入到資料庫中

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import jxl.*;
public class SimUpdate {
private String fileName;
public ZfzSimUpdate(String fileName){
this.fileName = fileName;
}
static Map tNames;
static{
tNames = new HashMap();
}
/**
* 用於產生 資料庫的 ID 值,組成 [年月日時分秒(100-999)] 總共 17 位數.
* 根據不同的表名,可保證同一秒內產生的 ID 號不重復
*/
private static String getDtime() {
String rid;
Date nd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
rid = sdf.format(nd);
return rid;
}

public String getSeqNumber(String tableName) {
if(tableName == null || "".equals(tableName))
tableName = "GENERY";
Integer it;
// noinspection SynchronizeOnNonFinalField
synchronized(tNames){
it = (Integer)tNames.get(tableName);
if(it == null){
it = new Integer(100);
tNames.put(tableName, it);
}else{
if(it.intValue() > 998)
it = new Integer(100);
else
it = new Integer(1 + it.intValue());
tNames.put(tableName, it);
}
}
return getDtime() + String.valueOf(it);
}

private void updateDb(){
try{
Connection conn = DbPool.connectDB();
if(conn != null){
Statement stmt = conn.createStatement();
jxl.Workbook rwb = null;

try{
//構建Workbook對象 只讀Workbook對象
//直接從本地文件創建Workbook
//從輸入流創建Workbook
InputStream is = new FileInputStream(fileName);
rwb = Workbook.getWorkbook(is);
//Sheet(術語:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下標是從0開始的
//獲取第一張Sheet表
Sheet rs = rwb.getSheet(0);
//獲取Sheet表中所包含的總列數
int rsColumns = rs.getColumns();
//獲取Sheet表中所包含的總行數
int rsRows = rs.getRows();
//獲取指這下單元格的對象引用

String simNumber = "",termSeqId = "";
//指定SIM卡號及序列號
for(int i=0;i<rsRows;i++){
for(int j=0;j<rsColumns;j++){
Cell cell = rs.getCell(j,i);
if(j==0){
simNumber = cell.getContents();
}
termSeqId = "633"+simNumber;
}
String sql = "查詢SQL";
int isOk = stmt.executeUpdate(sql);
if(isOk == 0 && !simNumber.equals("")){
String termId = getSeqNumber("termInf");
String insertSql = "自定義INSERT";
int isAdd = stmt.executeUpdate(insertSql);
if(isAdd > 0){
System.out.println("成功插入第"+i+"條數據");
}

}
//System.out.println("SIM卡號:"+simNumber+",序列號:"+termSeqId);
}

//以下代碼為寫入新的EXCEL,這里不使用,所以注釋
/*

//利用已經創建的Excel工作薄創建新的可寫入的Excel工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("D://Book2.xls"),rwb);
//讀取第一張工作表
jxl.write.WritableSheet ws = wwb.getSheet(0);

//獲取第一個單元格對象
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
//決斷單元格的類型,做出相應的轉化
if (wc.getType() == CellType.LABEL) {
Label l = (Label) wc;
l.setString("The value has been modified.");
}
//寫入Excel對象
wwb.write();
wwb.close();
*/
}catch(Exception e){
e.printStackTrace();
}
finally{
//操作完成時,關閉對象,翻譯佔用的內存空間
rwb.close();

}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
DbPool dbPool = new DbPool("dbConn.cfg");//連接資料庫
SimUpdate simUpdate = new SimUpdate("zfz_sim.xls");
simUpdate.updateDb();

}

}