本文目錄一覽:
- 1、java如何把excel內容導入到mysql資料庫,資料庫的列名就是excel的列名
- 2、如何用java實現把excel表中的數據導入到mysql資料庫已有的表中
- 3、用java怎麼將excel表格數據導入到mysql資料庫中
- 4、java中如何講Excel中的數據導入到Mysql
java如何把excel內容導入到mysql資料庫,資料庫的列名就是excel的列名
1、添加POI jar包到項目的lib目錄下-
2、Excel文件目錄:d://excel.xls-
3、資料庫欄位為:num1 num2 num3 num4 num5 num6-
4、資料庫名:blog-
5、表名:test-
6、編寫類:連接mysql的字元串方法、插入的方法、實體類–
import java.io.FileInputStream;-
import java.io.FileNotFoundException;-
import java.io.IOException;-
import org.apache.commons.logging.Log;-
import org.apache.commons.logging.LogFactory;-
import org.apache.poi.hssf.usermodel.HSSFCell;-
import org.apache.poi.hssf.usermodel.HSSFRow;-
import org.apache.poi.hssf.usermodel.HSSFSheet;-
import org.apache.poi.hssf.usermodel.HSSFWorkbook;-
public class TestExcel {-
//記錄類的輸出信息-
static Log log = LogFactory.getLog(TestExcel.class); –
//獲取Excel文檔的路徑-
public static String filePath = “D://excel.xls”;-
public static void main(String[] args) {-
try {-
// 創建對Excel工作簿文件的引用-
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));-
// 在Excel文檔中,第一張工作表的預設索引是0,-
// 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);-
HSSFSheet sheet = wookbook.getSheet(“Sheet1”);-
//獲取到Excel文件中的所有行數-
int rows = sheet.getPhysicalNumberOfRows();-
//遍歷行-
for (int i = 0; i rows; i++) {-
// 讀取左上端單元格-
HSSFRow row = sheet.getRow(i);-
// 行不為空-
if (row != null) {-
//獲取到Excel文件中的所有的列-
int cells = row.getPhysicalNumberOfCells();-
String value = “”; –
//遍歷列-
for (int j = 0; j cells; j++) {-
//獲取到列的值-
HSSFCell cell = row.getCell(j);-
if (cell != null) {-
switch (cell.getCellType()) {-
case HSSFCell.CELL_TYPE_FORMULA:-
break;-
case HSSFCell.CELL_TYPE_NUMERIC:-
value += cell.getNumericCellValue() + “,”; –
break; –
case HSSFCell.CELL_TYPE_STRING:-
value += cell.getStringCellValue() + “,”;-
break;-
default:-
value += “0”;-
break;-
}-
} –
}-
// 將數據插入到mysql資料庫中-
String[] val = value.split(“,”);-
TestEntity entity = new TestEntity();-
entity.setNum1(val[0]);-
entity.setNum2(val[1]);-
entity.setNum3(val[2]);-
entity.setNum4(val[3]);-
entity.setNum5(val[4]);-
entity.setNum6(val[5]);-
TestMethod method = new TestMethod();-
method.Add(entity);-
}-
}-
} catch (FileNotFoundException e) {-
e.printStackTrace();-
} catch (IOException e) {-
e.printStackTrace();-
}-
}-
}-
如何用java實現把excel表中的數據導入到mysql資料庫已有的表中
package com.cn.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
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;
import org.springframework.web.multipart.MultipartFile;
/**
* excel讀寫工具類
* @author sun.kai
*/
public class POIUtil {
private static Logger logger = Logger.getLogger(POIUtil.class);
private final static String xls = “xls”;
private final static String xlsx = “xlsx”;
/**
* 讀入excel文件,解析後返回
* @param file
* @throws IOException
*/
public static ListString[] readExcel(MultipartFile file) throws IOException{
//檢查文件
checkFile(file);
//獲得Workbook工作薄對象
Workbook workbook = getWorkBook(file);
//創建返回對象,把每行中的值作為一個數組,所有行作為一個集合返回
ListString[] list = new ArrayListString[]();
if(workbook != null){
for(int sheetNum = 0;sheetNum workbook.getNumberOfSheets();sheetNum++){
//獲得當前sheet工作表
Sheet sheet = workbook.getSheetAt(sheetNum);
if(sheet == null){
continue;
}
//獲得當前sheet的開始行
int firstRowNum = sheet.getFirstRowNum();
//獲得當前sheet的結束行
int lastRowNum = sheet.getLastRowNum();
//循環除了第一行的所有行
for(int rowNum = firstRowNum+1;rowNum = lastRowNum;rowNum++){
//獲得當前行
Row row = sheet.getRow(rowNum);
if(row == null){
continue;
}
//獲得當前行的開始列
int firstCellNum = row.getFirstCellNum();
//獲得當前行的列數
int lastCellNum = row.getPhysicalNumberOfCells();
String[] cells = new String[row.getPhysicalNumberOfCells()];
//循環當前行
for(int cellNum = firstCellNum; cellNum lastCellNum;cellNum++){
Cell cell = row.getCell(cellNum);
cells[cellNum] = getCellValue(cell);
}
list.add(cells);
}
}
workbook.close();
}
return list;
}
public static void checkFile(MultipartFile file) throws IOException{
//判斷文件是否存在
if(null == file){
logger.error(“文件不存在!”);
throw new FileNotFoundException(“文件不存在!”);
}
//獲得文件名
String fileName = file.getOriginalFilename();
//判斷文件是否是excel文件
if(!fileName.endsWith(xls) !fileName.endsWith(xlsx)){
logger.error(fileName + “不是excel文件”);
throw new IOException(fileName + “不是excel文件”);
}
}
public static Workbook getWorkBook(MultipartFile file) {
//獲得文件名
String fileName = file.getOriginalFilename();
//創建Workbook工作薄對象,表示整個excel
Workbook workbook = null;
try {
//獲取excel文件的io流
InputStream is = file.getInputStream();
//根據文件後綴名不同(xls和xlsx)獲得不同的Workbook實現類對象
if(fileName.endsWith(xls)){
//2003
workbook = new HSSFWorkbook(is);
}else if(fileName.endsWith(xlsx)){
//2007
workbook = new XSSFWorkbook(is);
}
} catch (IOException e) {
logger.info(e.getMessage());
}
return workbook;
}
public static String getCellValue(Cell cell){
String cellValue = “”;
if(cell == null){
return cellValue;
}
//把數字當成String來讀,避免出現1讀成1.0的情況
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
//判斷數據的類型
switch (cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC: //數字
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING: //字元串
cellValue = String.valueOf(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: //Boolean
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: //空值
cellValue = “”;
break;
case Cell.CELL_TYPE_ERROR: //故障
cellValue = “非法字元”;
break;
default:
cellValue = “未知類型”;
break;
}
return cellValue;
}
}
用java怎麼將excel表格數據導入到mysql資料庫中
參考下面方法:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcel {
//記錄類的輸出信息
static Log log = LogFactory.getLog(TestExcel.class);
//獲取Excel文檔的路徑
public static String filePath = “D://excel.xls”;
public static void main(String[] args) {
try {
// 創建對Excel工作簿文件的引用
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));
// 在Excel文檔中,第一張工作表的預設索引是0
// 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);
HSSFSheet sheet = wookbook.getSheet(“Sheet1”);
//獲取到Excel文件中的所有行數
int rows = sheet.getPhysicalNumberOfRows();
//遍歷行
for (int i = 0; i rows; i++) {
// 讀取左上端單元格
HSSFRow row = sheet.getRow(i);
// 行不為空
if (row != null) {
//獲取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells();
String value = “”;
//遍歷列
for (int j = 0; j cells; j++) {
//獲取到列的值
HSSFCell cell = row.getCell(j);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value += cell.getNumericCellValue() + “,”;
break;
case HSSFCell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + “,”;
break;
default:
value += “0”;
break;
}
}
}
// 將數據插入到mysql資料庫中
String[] val = value.split(“,”);
TestEntity entity = new TestEntity();
entity.setNum1(val[0]);
entity.setNum2(val[1]);
entity.setNum3(val[2]);
entity.setNum4(val[3]);
entity.setNum5(val[4]);
entity.setNum6(val[5]);
TestMethod method = new TestMethod();
method.Add(entity);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java中如何講Excel中的數據導入到Mysql
你可以用JXL做。讀到資料庫,可以通過jxl來實現,可以把EXCEL文檔上傳到系統的上傳目錄下後,然後再取得這個文件,或者直接取得這個文件,對這個文件進行操作。
例子:
public void addCustomerAssign(File file,SysExpo expo,SysUser user1)//添加客服中心數據
{
jxl.Workbook rwb = null;
try{
//構建Workbook對象, 只讀Workbook對象
//直接從本地文件創建Workbook
//從輸入流創建Workbook
InputStream is = new FileInputStream(file);
rwb = Workbook.getWorkbook(is);
String createTime = DateUtil.getDateTime( “yyyy-MM-dd HH:mm “,new Date()).toString();
//Sheet(術語:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下標是從0開始
//獲取第一張Sheet表
Sheet rs = rwb.getSheet(0);
//獲取Sheet表中所包含的總列數
// int rsColumns = rs.getColumns();
//獲取Sheet表中所包含的總行數
int rsRows = rs.getRows();
//獲取指定單元格的對象引用
// rs.getCell(列,行);
for(int i=1;i rsRows;i++){//如第一行為屬性項則從第二行開始取數據(int i=0 ;i rsRows;i++)
//for(int j=0;j rsColumns;j++){
//Cell cell = rs.getCell(j,i);
// System.out.print(cell.getContents()+ ” “);
// }
//Cell cell = rs.getCell(0,i).getContents()+ ” “;
String cell1= rs.getCell(0,i).getContents()+ ” “;//序號
String cell7 = rs.getCell(6,i).getContents()+ ” “;//公司名稱
if(cell1!=null!cell1.equals( ” “)cell7!=null!cell7.equals( ” “))//判斷當前行是否為有效行 是插入否找下行
{
Company company = new Company();
company.setName(rs.getCell(0,i).getContents()+ ” “);//1名稱
company.setManager(rs.getCell(1,i).getContents()+ ” “);//2法人
}
}
}catch(Exception e){
e.printStackTrace();
}
finally{
//操作完成時,關閉對象,釋放佔用的內存空間
rwb.close();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/188976.html