【问题标题】:Conversion of excel data to MySql tablesexcel数据到MySql表的转换
【发布时间】:2013-10-13 15:00:37
【问题描述】:

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

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

我参考了以下链接。

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

谁能给我一个有用的链接或简单的java代码?

【问题讨论】:

    标签: java hibernate spring-mvc


    【解决方案1】:

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

    希望它有效!

    【讨论】:

      【解决方案2】:

      这是您读取 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-06-16
        • 2017-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        • 1970-01-01
        • 2016-07-29
        • 2011-03-18
        相关资源
        最近更新 更多