Java implementation to convert data in Excel XLS into SQL files that can be inserted directly into the database _java

Source: Internet
Author: User
Tags first row stringbuffer

My usual style, the code explains everything.

Nonsense not much said, directly to everyone to paste code, the 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 {/** * Gets the data from the XLS table, generates the executable SQL file to insert the database, and notices the need to introduce the JXL package! Support Int,integer,long,long,string *, expandable * * @param args * @throws illegalargumentexception * @throws Illegalaccessexcepti On */public static void main (string[] args) throws IllegalArgumentException, illegalaccessexception {//Get all the data in the table list& Lt
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 ();}} /** * Get data from XLS * * @param file * @return/public static list<student> Getallbyexcel (String file) {List<stude
nt> list = new arraylist<student> ();
try {Workbook RWB = Workbook.getworkbook (new file);
Sheet rs = rwb.getsheet (0); int clos = Rs.getcolumns ()//Get all columns int rows = rs.getrows ()//Get all row//samples, the data starts with the first row in the third column for (int i = 2; i < rows; i++  {///all data obtained for each row is deposited in 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; /** * uses reflection to set data.
The data type that can be set in this example is limited, please add!!! yourself * @param obj * @param list * @return * @throws illegalargumentexception * @throws illegalaccessexception/Public stat IC 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.par
Seint (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 Strin G Outsql (Object obj) throws IllegalArgumentException, illegalaccessexception {stringbuffer buffer = new StringBuffer (); F
ield[] 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 the data into the Excel XLS can be directly inserted into the database SQL file knowledge, small set to introduce so many people, I hope to help!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.