Java實現把excel xls中資料轉為可直接插入資料庫的sql檔案_java

來源:互聯網
上載者:User

我的一貫風格,代碼說明一切。。

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:

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++) {// 取得的每一行的所有資料存入listStringList<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檔案的知識,小編就給大家介紹這麼多,希望對大家有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.