Java完成把excel xls中數據轉為可直接拔出數據庫的sql文件。本站提示廣大學習愛好者:(Java完成把excel xls中數據轉為可直接拔出數據庫的sql文件)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成把excel xls中數據轉為可直接拔出數據庫的sql文件正文
我的一向作風,代碼解釋一切。。
空話不多說了,直接給年夜家貼代碼了,詳細代碼以下所示:
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 { /** * 從xls表格中獲得數據,生成可履行的sql文件,用以拔出數據庫,留意須要引入jxl包!支撐int,Integer,long,Long,String * ,可自行擴大 * * @param args * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException { // 獲得表格中一切的數據 List<Student> listExcel = getAllByExcel("C:\\Users\\xxx\\Desktop\\zzz.xls"); try { String path = "C:\\Users\\xxx\\Desktop\\convert.sql";// 文件保留途徑、名字 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"); }// 寫入內容 ow.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 從xls中獲得數據 * * @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();// 獲得一切的列 int rows = rs.getRows();// 獲得一切的行 // 樣例中,數據從第三列第一行開端 for (int i = 2; i < rows; i++) { // 獲得的每行的一切數據存入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; } /** * 應用反射設置數據。此例中可設置的數據類型無限,沒有的請本身添加!!! * * @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(); } }
有關Java完成把excel xls中數據轉為可直接拔出數據庫的sql文件的常識,小編就給年夜家引見這麼多,願望對年夜家有所贊助!