0

春にアプリケーションを開発し、Eclipse を IDE として休止状態にします。

ExcelファイルのデータをMySqlテーブルに変換したい。

以下のリンクを参考にしました。

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

誰でも便利なリンクまたは同じための簡単なJavaコードを送ってもらえますか?

4

2 に答える 2

2

Excelファイルの読み取りにPOIを使用しました。次のコードは、休止状態を使用してデータベースにデータを挿入するのに役立ちます。

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 に答える