将excel转换为mysql_将Excel数据转换为MySql表

本文介绍了一种使用Java Apache POI库读取Excel文件,并通过Hibernate将数据存储到MySQL数据库的方法。首先,通过FileInputStream打开Excel文件,然后使用XSSFWorkbook和XSSFSheet获取工作簿和工作表。接着,遍历每一行和每一列,根据单元格类型(如布尔型、数值型、字符串型),打印或处理数据,以便用Hibernate进行数据库操作。
摘要由CSDN通过智能技术生成

小编典典

这是您阅读Excel文件并将其存储在集合对象中的方式

import java.io.*;

import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelFile {

public static void main(String[] args)

{

try {

FileInputStream file = new FileInputStream(new File("C:/Users/hussain.a/Desktop/newExcelFile.xlsx"));

XSSFWorkbook workbook = new XSSFWorkbook(file);

XSSFSheet sheet = workbook.getSheetAt(0);

Iterator rowIterator = sheet.iterator();

rowIterator.next();

while(rowIterator.hasNext())

{

Row row = rowIterator.next();

//For each row, iterate through each columns

Iterator cellIterator = row.cellIterator();

while(cellIterator.hasNext())

{

Cell cell = cellIterator.next();

switch(cell.getCellType())

{

case Cell.CELL_TYPE_BOOLEAN:

System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t");

// write hibernate lines here to store it in your domain

break;

case Cell.CELL_TYPE_NUMERIC:

System.out.println("numeric===>>>"+cell.getNumericCellValue() + "\t");

// write hibernate lines here to store it in your domain

break;

case Cell.CELL_TYPE_STRING:

System.out.println("String===>>>"+cell.getStringCellValue() + "\t");

// write hibernate lines here to store it in your domain

break;

}

}

System.out.println("");

}

file.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

之后,您可以使用hibernate模式并存储在您的域类中

2020-06-01

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值