将Excel数据转换为MySql表

我正在使用Eclipse作为IDE在Spring中开发一个应用程序并进行hibernate。

我想将Excel文件数据转换为MySql表。

我已经提到以下链接。

http://www.coderanch.com/t/608700/JDBC/databases/import-data-excel-files-database

任何人都可以给我一个有用的链接或简单的Java代码吗?

这是您如何阅读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并存储在您的域类中

我用POI来读取excel文件。 以下代码可以帮助您使用hibernate将数据插入数据库:

 try{ FileInputStream input = new FileInputStream("D:\\employeedata.xls"); POIFSFileSystem fs = new POIFSFileSystem( input ); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row; for(int i=1; i<=sheet.getLastRowNum(); i++) { Employee employee=new Employee(); row = sheet.getRow(i); employee.setEmployeeName(String.valueOf(row.getCell(0).getRichStringCellValue())); employee.setDesignation(String.valueOf(row.getCell(1).getRichStringCellValue())); employee.setSalary((long)row.getCell(2).getNumericCellValue()); employeeService.insert(employee); // call to spring service layer } } catch (FileNotFoundException ec) { ec.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

希望它有效!