0

我正在春季开发一个应用程序,并使用 Eclipse 作为 IDE 进行休眠。

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

我已经提到了以下链接。

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

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

4

2 回答 2

2

我使用 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();
    }

希望它有效!

于 2014-01-16T06:47:06.670 回答
0

这就是您读取 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<Row> rowIterator = sheet.iterator();
                rowIterator.next();
                while(rowIterator.hasNext())
                {
                    Row row = rowIterator.next();
                    //For each row, iterate through each columns
                    Iterator<Cell> 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();
            }
        }

    }

在此之后,您可以使用休眠并存储在您的域类中

于 2013-10-13T15:11:30.950 回答