The Java implementation converts the data in excel xls into an sql file that can be inserted directly into the database

  • 2020-05-07 19:47:23
  • OfStack

My 1 stick style, code description 1 cut.

Nonsense not to say, directly to everyone posted code, specific code as follows:


package Tools;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import entity.Student;
public class ConvertXMSToSQL {
/**
*  from xls Get the data from the table to generate the executable sql File to insert into the database, note the need to import jxl Bag! support int,Integer,long,Long,String
* , self-expansion 
* 
* @param args
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException {
//  Get all the data in the table 
List<Student> listExcel = getAllByExcel("C:\\Users\\xxx\\Desktop\\zzz.xls");
try {
String path = "C:\\Users\\xxx\\Desktop\\convert.sql";//  File save path, name 
File file = new File(path);
BufferedWriter ow = new BufferedWriter(new FileWriter(file));
for (Student c : listExcel) {
String sql = "insert into cfg_avatar values (" + outSql(c)
+ ")";
ow.write(sql + ";" + "\n");
}//  Write content 
ow.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*  from xls Get data in 
* 
* @param file
* @return
*/
public static List<Student> getAllByExcel(String file) {
List<Student> list = new ArrayList<Student>();
try {
Workbook rwb = Workbook.getWorkbook(new File(file));
Sheet rs = rwb.getSheet(0);
int clos = rs.getColumns();//  I get all the columns 
int rows = rs.getRows();//  I get all the rows 
//  In the sample, the data is from no 3 The column first 1 line 
for (int i = 2; i < rows; i++) {
//  The per 1 All data of the row is stored listString
List<String> listString = new ArrayList<String>();
for (int j = 0; j < clos; j++) {
String str = rs.getCell(j, i).getContents();
listString.add(str);
}
Student Student = (Student) newObject(new Student(), listString);
list.add(Student);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
*  Use reflection to set the data. Limited data types can be set in this example, if not, please add your own!! 
* 
* @param obj
* @param list
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static Object newObject(Object obj, List<String> list)
throws IllegalArgumentException, IllegalAccessException {
Field[] field = obj.getClass().getDeclaredFields();
for (int i = 0; i < field.length; i++) {
Field f = field[i];
f.setAccessible(true);
if (f.getType() == String.class) {
f.set(obj, list.get(i));
}
if (f.getType() == Integer.class) {
f.set(obj, Integer.parseInt(list.get(i)));
}
if (f.getType() == int.class) {
f.set(obj, Integer.parseInt(list.get(i)));
}
if (f.getType() == Long.class) {
f.set(obj, Long.parseLong(list.get(i)));
}
if (f.getType() == long.class) {
f.set(obj, Long.parseLong(list.get(i)));
}
}
return obj;
}
/**
* 
* @param obj
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static String outSql(Object obj) throws IllegalArgumentException,
IllegalAccessException {
StringBuffer buffer = new StringBuffer();
Field[] field = obj.getClass().getDeclaredFields();
for (int i = 0; i < field.length; i++) {
Field f = field[i];
f.setAccessible(true);
if (f.getType() == String.class) {
buffer.append("'");
}
buffer.append(f.get(obj));
if (f.getType() == String.class) {
buffer.append("'");
}
if (i < field.length - 1) {
buffer.append(",");
}
}
return buffer.toString();
}
}

About Java implementation of excel xls data into directly inserted into the database sql file knowledge, this site to give you so much, hope to help you!


Related articles: