Excel Mysql database to dynamically create tables, insert data into Excel and dynamically created Mysql Table

1. its argument is the table name, excel file name and the sheet name.

2. nulls in the null-excel sql is stored in the database.

The number of columns in the sheet, dynamically create a database table name, database field named name1, name2 ......

4. The value of incoming excel table dynamically created.

The first step to query all data specified target excel table: Create List <String> type of Return method, passing parameters to excel file name and the sheet name, using Workbook to open the file, you need to add poi rely on the use of the Workbook can open excel 2003 and 2007 release version, to give the number of rows and columns, and a for loop, adding the contents of each plane in excel Gerry unit inside the list.

public static List<String> getAllByExcel(String file, String sheet){
    List<String> list = new ArrayList<String>();
    DBhelper db = new DBhelper();
    try {
        Workbook rwb = Workbook.getWorkbook(new File(file));
        Sheet rs=rwb.getSheet(sheet);
        clos=rs.getColumns();//得到所有列
        int rows =rs.getRows();
        String str[] = new String[clos+1];
        for(int n=0;n<clos;n++){
            str[n]="name"+n;
        }
        for(int i=0;i<rows;i++){//i行数从第0行开始 j 列数
            for(int j=0;j<clos;j++){
                String arr[ ]=new String[clos+1];
                arr[j] =rs.getCell(j,i).getContents();
                System.out.println(str[j]+":"+arr[j]);  //字段名加数据内容
                list.add( arr[j]); //得到表中所有数据
            }
        }
        rwb.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;
}

The second step to create a dynamic table: write a method to create a table of mass participation for the table name, call the database connection method, write sql statement, sql using stitching, for circulation to create the database table fields, the number of cycles to get the number of columns in the second step with "name" + i in the form of a field can be named name1, name2 ...., pay attention to the normative language when writing sql, sql statement to be able to assemble into fully capable of running to execute sql statements using PrepareStatement. ps.executeUpdate (sql).

String sql= null;
 try {
    sql = "create table if not exists "+tablename+" ";
    sql+="(";
    for(int i=1;i<=clos;i++){
        sql+="name"+i +" varchar(100)";//表字段
        if(i<clos){
            sql+=",";
        }
    }
    sql+=")DEFAULT CHARSET=utf8;";
    PreparedStatement ps=null;
    ps=db.conn.prepareStatement(sql);
    ps.executeUpdate(sql);
    ps.close();
} catch (Exception e) {
    System.out.println("建表失败"+e.getMessage());
}

The third step is to add data mysql statement: Write the insert method, parameter is the table name, splicing sql statement, insert into tablename (name1, name2 ...) values ​​(NULLIF (, ''), ...?), Because you want to the null value into a null value, use the NULLIF null values ​​into the database (null).

try {
    sql="insert into "+tablename+"(";
    for(int i=1;i<=clos;i++){
        sql+="name"+i;
        if(i<clos){
            sql+=",";
        }
    }
    sql+=")values(";
    for(int i=1;i<=clos;i++){
        sql+="NULLIF(?,'')";
        if(i<clos){
            sql+=",";
        }
    }
    sql+=");";
    db.conn.close();
} catch (Exception e) {
    e.printStackTrace();
}

The fourth step in the data list and the sql statement combined data is inserted into mysql achieved, the loop list, execute sql statement insert, insert data in each cycle, the cycle number of the number of columns in excel using ps.setString (j + 1, list.get (i ++)), j columns for the first record, i is the i-th list of values.

List<String> listExcel=ExcelService.getAllByExcel("D://xx.xlsx","Sheet1");//读excel表中的数据
ExcelService.CreateTable(tableName);//创建数据库表
String sql=ExcelService.insert(tableName);
for(int i=0;i<listExcel.size();){
    PreparedStatement ps=null;
    try {
        ps=db.conn.prepareStatement(sql);
        if(listExcel!=null){
            for(int j=0;j<ExcelService.clos;j++){
                ps.setString(j+1,listExcel.get(i++));
            }
        }
        ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Published 36 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_27182767/article/details/84348258
Recommended