简体   繁体   English

如何在java中动态地使用excel中第一行的内容在mysql中创建表

[英]how to create table in mysql with the contents of first row in excel in java dynamicallly

How to create table in MySQL with the contents of first row in excel in java dynamically without hard-coding and creating and I have a excel in which columns are added or deleted frequently... so I need to update my table frequently with new columns.. anyone please help.如何在没有硬编码和创建的情况下动态地使用java中excel中第一行的内容在MySQL中创建表,并且我有一个经常添加或删除列的excel......所以我需要经常用新列更新我的表..请任何人帮忙。 I am using spring-jdbc to connect to database and creating maven project... and how to read a cell value with date in it with format MM/DD/YYYY?我正在使用 spring-jdbc 连接到数据库并创建 maven 项目......以及如何读取格式为 MM/DD/YYYY 的带有日期的单元格值? for reading excel data I am using Apache-poi..为了读取 excel 数据,我正在使用 Apache-poi ..

I want table in mysql like我想要 mysql 中的表,例如

ID  Employee Manager   Onsite  4/15/2015    4/16/2015  4/17/2015

How do I create?我如何创建? Please refer the below excel..the major thing is after two day again another date column will be added to my excel..that also I need to save in table previously that I have created..I am not getting idea how it can be done... I can't create the table as I have created in my code...请参考下面的 excel.. 主要是两天后另一个日期列将添加到我的 excel.. 那我也需要保存在我之前创建的表中..我不知道它是怎么回事完成...我无法像在我的代码中创建的那样创建表...

My excel is like this.. and having more than 100 rows我的excel是这样的..有100多行

ID        Employee    Manager    Onsite        4/15/2015   4/16/2015  4/17/2015
1           raju        ram      offshore        8            8          8

My code我的代码

package controller;


import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;

import javax.sql.DataSource;

import model.PMOEmployee;





import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.jdbc.core.JdbcTemplate;






    public class ExcelDBJDBCDAO implements ExcelDBDAO {

        private DataSource dataSource;

        private  JdbcTemplate jdbcTemplate;

        public DataSource getDataSource() {
            return dataSource;
        }

        public void setdataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }

        public JdbcTemplate getjdbcTemplate() {
            return jdbcTemplate;
        }

        public void setjdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }

        public void importdata() {
             TODO Auto-generated method stub
             table creation
            String sql="create table PMO(Id int ,Employee varchar(50),Manager varchar(50),Onsite varchar(50))";
            getjdbcTemplate().update(sql);        
    System.out.println("table created Successfully :)");


            ArrayList<PMOEmployee> list = new ArrayList<PMOEmployee>();

            try
            {
                FileInputStream file = new FileInputStream(new File("/home/svenkatakishore/Untitled 1.xls"));

                //Create Workbook instance holding reference to .xlsx file
                HSSFWorkbook workbook = new HSSFWorkbook(file);

                //Get first/desired sheet from the workbook
                HSSFSheet sheet = workbook.getSheetAt(0);

                //Iterate through each rows one by one
                Iterator<Row> rowIterator = sheet.iterator();

                while (rowIterator.hasNext())
                {
                    Row row = rowIterator.next();
                    PMOEmployee employee = new PMOEmployee();
                    //For each row, iterate through all the columns

                    Iterator<Cell> cellIterator = row.cellIterator();                 
                    while (cellIterator.hasNext())
                    {

                        Cell cell = cellIterator.next();
                        //Check the cell type and format accordingly
                        switch (cell.getCellType())
                        {
                        case Cell.CELL_TYPE_NUMERIC:
                            if (cell.getColumnIndex() == 0) {
                                employee.setId((int)cell.getNumericCellValue());
                            } 

                            break;
                            case Cell.CELL_TYPE_STRING:
                                if (cell.getColumnIndex() == 1) {
                                    employee.setEmployeeName(cell.getStringCellValue());
                                }
                                else if (cell.getColumnIndex() == 2) {
                                    employee.setManager(cell.getStringCellValue());
                                }
                                else if (cell.getColumnIndex() == 3) {
                                    employee.setOnsite(cell.getStringCellValue());
                                }


                                break;


                        }
                    }
                    String sql1 = "INSERT INTO PMO(Id,Employee,Manager,Onsite) VALUES(?, ?, ?, ?)";
                    getjdbcTemplate().update(sql1, new Object[]{employee.getId(),employee.getEmployeeName(),
                            employee.getManager(),employee.getOnsite()});

                }
                file.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }    

            }



        }

myoutput我的输出

 ID     Employee       Manager   Onsite  
 1      raju           ram       offsore     

It is happening because the spreadsheet returns the date cell value as integer.这是因为电子表格将日期单元格值作为整数返回。 You need to check if your cell contains date, and work accordingly.您需要检查您的单元格是否包含日期,并进行相应的工作。 This can be done by using DateUtil class.这可以通过使用 DateUtil 类来完成。 For eg例如

HSSFWorkbook workbook = new HSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        HSSFSheet sheet = workbook.ge`enter code here`tSheetAt(0);

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext())
            {

                Cell cell = cellIterator.next();
                //Check the cell type and format accordingly
                switch (cell.getCellType())
                {
                case Cell.CELL_TYPE_NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.println(cell.getDateCellValue() + "\t\t\t\t");
                } else {
                    System.out.println(cell.getNumericCellValue() + "\t\t\t\t");
                }
                break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t\t\t\t");
                        break;
//                       case Cell.CELL_TYPE_BLANK:
//                           System.out.println("0"+ "\t\t");
//                           break;

                }
            }
            System.out.println("");
        }
        file.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }    

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM