Use POI and JavaCsv tools to read excel files (*.xls, *.xlsx, *.csv) and store them in the MySQL database

First configure maven: import related dependencies

 1 <dependency>
 2 <groupId>org.apache.poi</groupId>
 3 <artifactId>poi</artifactId>
 4 <version>3.14</version>
 5 </dependency>
 6 <dependency>
 7 <groupId>org.apache.poi</groupId>
 8 <artifactId>poi-ooxml</artifactId>
 9 <version>3.14</version>
10 </dependency>
11 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
12 <dependency>
13 <groupId>org.apache.poi</groupId>
14 <artifactId>poi-ooxml-schemas</artifactId>
15 <version>3.14</version>
16 </dependency>
17 <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
18 <dependency>
19 <groupId>org.apache.xmlbeans</groupId>
20 <artifactId>xmlbeans</artifactId>
21 <version>3.0.0</version>
22 </dependency>
23 <dependency>
24 <groupId>junit</groupId>
25 <artifactId>junit</artifactId>
26 <version>4.12</version>
27 <scope>test</scope>
28 </dependency>
29 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
30 <dependency>
31 <groupId>mysql</groupId>
32 <artifactId>mysql-connector-java</artifactId>
33 <version>5.1.47</version>
34 </dependency>
35
36 <!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv -->
37 <dependency>
38 <groupId>net.sourceforge.javacsv</groupId>
39 <artifactId>javacsv</artifactId>
40 <version>2.0</version>
41 </dependency>
42 <dependency>
43 <groupId>org.jetbrains</groupId>
44 <artifactId>annotations-java5</artifactId>
45 <version>RELEASE</version>
46 <scope>compile</scope>
47 </dependency>

Complete source code:

1 public interface ReadFile {
2 //The return value is all data excluding the header
3 public ArrayList<ArrayList<String>> readExcelFile(String path) throws IOException, SQLException;
4 //The return value is incorrect data
5 public ArrayList<ArrayList<String>> readCsvFile(String path,String code) throws IOException, SQLException;
6}
 1 import com.csvreader.CsvReader;
  2 import com.reliable.dao.ReadFile;
  3 import com.reliable.util.JDBCUtil;
  4 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5 import org.apache.poi.ss.usermodel.Cell;
  6 import org.apache.poi.ss.usermodel.Row;
  7 import org.apache.poi.ss.usermodel.Sheet;
  8 import org.apache.poi.ss.usermodel.Workbook;
  9 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 10 import java.io.File;
 11 import java.io.FileInputStream;
 12 import java.io.IOException;
 13 import java.nio.charset.Charset;
 14 import java.sql.Connection;
 15 import java.sql.PreparedStatement;
 16 import java.sql.SQLException;
 17 import java.util.ArrayList;
 18
 19 public class ReadFileImpl implements ReadFile {
 20 //Related variables
 21 private int MAX_CELL_NUM; //Maximum number of cells in excel file
 22 private PreparedStatement preparedStatement=null;
 23 private Connection conn =null;
 twenty four     //----------------------------------------------- ---
 25 public ArrayList<ArrayList<String>> readExcelFile(String path) throws IOException, SQLException {
 26 String DROP_TABLE_1=""; //Delete the source data table sql statement
 27 String DROP_TABLE_2=""; //Delete backup table sql statement
 28 String CREATE_TABLE_1_SQL=""; //Create source data table statement
 29 String CREATE_TABLE_2_SQL=""; //Create backup table statement
 30 String tableName_1=""; //Source data table name
 31 String tableName_2=""; //Backup table name
 32 ArrayList<String> tableField = new ArrayList<String>(); //Data table field name
 33 ArrayList<ArrayList<String>> tableValue = new ArrayList<ArrayList<String>>(); //The value of the data table
 34 System.out.println(path);
 35 File excel=new File(path);
 36 String[] split = excel.getName().split("\."); //. is a special character and needs to be escaped!
 37 System.out.println(split[0] + " " + split[1]);
 38 tableName_1=split[0]; //Assign a value to the source data table name
 39 tableName_2=split[0] + split[1]; //Assign a value to the backup table name
 40 System.out.println("Source data table name:" + tableName_1);
 41 System.out.println("Backup table name: " + tableName_2);
 42 Workbook wb; //Create new file
 43 FileInputStream fileStream = new FileInputStream(excel); //File stream object
 44 //Judge based on file suffix (xls/xlsx)
 45 if ( "xls".equals(split[1])){
 46 //xls and csv files
 47 wb = new HSSFWorkbook(fileStream);
 48 }else{
 49 //xlsx file
 50 wb = new XSSFWorkbook(new FileInputStream(excel));
 51 }
 52 //Start parsing
 53 Sheet sheet = wb.getSheetAt(0); //Read sheet 0
 54 MAX_CELL_NUM=getMaxCellNumExcel(sheet);
 55 int firstRowIndex = sheet.getFirstRowNum(); //Get the first row index
 56 int lastRowIndex = sheet.getLastRowNum(); //Get the last row index
 57 for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex + + ) { //Traverse rows
 58 Row row = sheet.getRow(rIndex); //Get the row index
 59 ArrayList<String> tempTableValue = new ArrayList<String>(); //Temporarily store the value of a row and then put it in tableValue
 60 if (row != null) {
 61 int lastCellIndex = MAX_CELL_NUM; //The last cell of the row
 62 for (int cIndex = 0; cIndex < lastCellIndex; cIndex + + ) { //Traverse columns (cells)
 63 Cell cell = row.getCell(cIndex,Row.RETURN_BLANK_AS_NULL);
 64 cell.setCellType(Cell.CELL_TYPE_STRING);
 65 if ((cell == null)) {
 66 tempTableValue.add("NULL");
 67 } else {
 68 tempTableValue.add(cell.getStringCellValue());
 69 }
 70 }
 71 System.out.println("th" + String.valueOf(rIndex + 1) + "The value of the row: " + " " + tempTableValue);
 72 tableValue.add(tempTableValue);
 73}
 74 // System.out.println(tempTableValue);
 75 System.out.println("--------------------------------");
 76 }
 77 System.out.println(tableValue); //Output all values of the table
 78 tableField= tableValue.get(0); //Put the header information into tableFiled
 79 tableValue.remove(0); //Then remove the first row of data from this list of all values
 80 System.out.println("Data table field: " + tableField);
 81 wb.close();
 82
 83 //-------------------------------------------------- -----------------------
 84 //Start inserting into the database below:
 85 //Get database connection
 86 conn = JDBCUtil.getConnection();
 87 if(conn!=null){
 88 System.out.println("Database connection successful!");
 89 } else {
 90 System.out.println("Database connection failed!");
 91 }
 92 //Write SQL table creation statement based on table header and table name ----------source data table
 93 DROP_TABLE_1="DROP TABLE IF EXISTS " + tableName_1 + ";";
 94 DROP_TABLE_2="DROP TABLE IF EXISTS " + tableName_2 + ";";
 95 CREATE_TABLE_1_SQL="CREATE TABLE " + "`" + tableName_1 + "`" + "(";
 96 CREATE_TABLE_1_SQL = getExcelSQL(CREATE_TABLE_1_SQL,tableField);
 97 // System.out.println(CREATE_TABLE_1_SQL);
 98 //Backup table statement
 99 CREATE_TABLE_2_SQL= "CREATE TABLE " + "`" + tableName_2 + "`" + "(";
100 CREATE_TABLE_2_SQL = getExcelSQL(CREATE_TABLE_2_SQL,tableField);
101
102 System.out.println("Create table statement of source data table: " + "\\
" + CREATE_TABLE_1_SQL);
103 System.out.println("Create table statement for backup table: " + "\\
" + CREATE_TABLE_2_SQL);
104 preparedStatement = conn.prepareStatement(DROP_TABLE_1);
105 preparedStatement.executeUpdate();
106 preparedStatement= conn.prepareStatement(CREATE_TABLE_1_SQL);
107 preparedStatement.executeUpdate();
108 preparedStatement = conn.prepareStatement(DROP_TABLE_2);
109 preparedStatement.executeUpdate();
110 preparedStatement= conn.prepareStatement(CREATE_TABLE_2_SQL);
111 preparedStatement.executeUpdate();
112 conn.setAutoCommit(false);
113 conn.commit();
114 JDBCUtil.release(conn,preparedStatement);
115 insertExcelTable(tableValue,tableName_1);
116 insertExcelTable(tableValue,tableName_2);
117 return tableValue;
118 }
119 //Read CSV file
120 public ArrayList<ArrayList<String>> readCsvFile(String path,String code) throws SQLException {
121 ArrayList<ArrayList<String>> WrongData=new ArrayList<ArrayList<String>>();
122 System.out.println(path);
123 String DROP_TABLE_1=""; //Delete the source data table sql statement
124 String DROP_TABLE_2=""; //Delete backup table sql statement
125 String CREATE_TABLE_1_SQL=""; //Create source data table statement
126 String CREATE_TABLE_2_SQL=""; //Create backup table statement
127 ArrayList<String> tableField = new ArrayList<String>(); //Data table field name
128 ArrayList<ArrayList<String>> tableValue = new ArrayList<ArrayList<String>>(); //The value of the data table
129 String tableName_1=""; //Source data table name
130 String tableName_2=""; //Backup table name
131 try {
132 //Create CSV reading object
133 CsvReader csvReader = new CsvReader(path,',', Charset.forName(code));
134 String[] split1 = path.split("\."); //. is a special character and needs to be escaped!
135 // System.out.println(split1[0] + " " + split1[1]);
136 String[] split2 = split1[0].split("\\");
137 // System.out.println(split2[split2.length-1]);
138 tableName_1=split2[split2.length-1];
139 tableName_2=split2[split2.length-1] + split1[1];
140 System.out.println("Output source table name:" + tableName_1);
141 System.out.println("Output backup table name:" + tableName_2);
142 // Skip header
143 // csvReader.readHeaders();
144 boolean flag=true;
145 while (csvReader.readRecord()){
146 // Read a whole line
147 String resString="";
148 resString = csvReader.getRawRecord();
149 resString=resString.replace(",",", ");
150 System.out.println(resString);
151 if(flag==true){
152 tableField.add(resString);
153 flag=false;
154 }
155 // System.out.println(resString);
156 String[] resString_list=resString.split(",");
157 String[] tableField_list=tableField.get(0).split(",");
158 ArrayList<String> tempTableValue = new ArrayList<String>(); //Temporarily store the value of a row and then put it in tableValue
159 if(resString_list.length != tableField_list.length){
160 tempTableValue.add(resString);
161 WrongData.add(tempTableValue);
162 } else {
163 tempTableValue.add(resString);
164 tableValue.add(tempTableValue);
165 }
166 }
167 } catch (IOException e) {
168 e.printStackTrace();
169 }
170 System.out.println(tableValue);
171 tableValue.remove(0); //Then remove the first row of data from this list of all values
172 System.out.println("Data table field: " + tableField);
173 //name1,sex1,age1
174 // System.out.println(tableValue.get(0).get(0));
175 DROP_TABLE_1="DROP TABLE IF EXISTS " + tableName_1 + ";";
176 DROP_TABLE_2="DROP TABLE IF EXISTS " + tableName_2 + ";";
177 CREATE_TABLE_1_SQL="CREATE TABLE " + tableName_1 + "(" ;
178 CREATE_TABLE_2_SQL="CREATE TABLE " + tableName_2 + "(" ;
179 //Generate source table creation statement
180 CREATE_TABLE_1_SQL=getCsvSQL(CREATE_TABLE_1_SQL,tableField);
181 //Generate backup table creation statement
182 CREATE_TABLE_2_SQL=getCsvSQL(CREATE_TABLE_2_SQL,tableField);
183 System.out.println(CREATE_TABLE_1_SQL);
184 System.out.println(CREATE_TABLE_2_SQL);
185 conn=JDBCUtil.getConnection();
186 preparedStatement = conn.prepareStatement(DROP_TABLE_1);
187 preparedStatement.executeUpdate();
188 preparedStatement= conn.prepareStatement(CREATE_TABLE_1_SQL);
189 preparedStatement.executeUpdate();
190 preparedStatement = conn.prepareStatement(DROP_TABLE_2);
191 preparedStatement.executeUpdate();
192 preparedStatement= conn.prepareStatement(CREATE_TABLE_2_SQL);
193 preparedStatement.executeUpdate();
194 conn.setAutoCommit(false);
195 conn.commit();
196 JDBCUtil.release(conn,preparedStatement);
197 insertCsvTable(tableValue,tableName_1);
198 insertCsvTable(tableValue,tableName_2);
199 return WrongData;
200 }
201 //-------------------------------------------------- ---
202 //Method to obtain the SQL statement for creating a table in an excel file
203 public String getExcelSQL(String SQL,ArrayList<String> tableField) {
204 for (int i =0 ;i<tableField.size();i + + ){
205 String item= tableField.get(i);
206 if(i!=tableField.size()-1){
207 SQL=SQL + "`" + item + "`" + " text COLLATE utf8_general_ci," + "\\
";
208 }else{
209 SQL=SQL + "`" + item + "`" + " text COLLATE utf8_general_ci " + "\\
";
210 }
211 }
212 return SQL + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;";
213 }
214 //Get the table creation sql statement of the CSV file
215 public String getCsvSQL(String SQL,ArrayList<String> tableField){
216 // System.out.println(tableField);
217 String [] tempField=tableField.get(0).split("\,");
218 for (int i =0 ;i<tempField.length;i + + )
219 {
220 String item= tempField[i];
221 if(i!=tempField.length-1){
222 SQL=SQL + "`" + item + "`" + " text COLLATE utf8_general_ci," + "\\
";
223 }else{
224 SQL=SQL + "`" + item + "`" + " text COLLATE utf8_general_ci " + "\\
";
225 }
226 }
227 return SQL + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;";
228 }
229 //-------------------------------------------------- ---
230 //Insert database xls and xlsx files
231 public void insertExcelTable(ArrayList<ArrayList<String>> tableValue, String tableName){
232 conn=JDBCUtil.getConnection();
233 String INSERT_TABLE_SQL="INSERT INTO " + tableName + " VALUES(";
234 for (ArrayList<String> item1 : tableValue){
235 String tempString=new String();
236 for (int i =0 ;i<item1.size();i + + ){
237 if(i!=item1.size()-1){
238 tempString=tempString + """ + item1.get(i) + """ + ",";
239 }
240 else{
241 tempString=tempString + """ + item1.get(i) + """;
242 }
243 }
244 INSERT_TABLE_SQL=INSERT_TABLE_SQL + tempString + ");";
245 System.out.println(INSERT_TABLE_SQL);
246 try{
247 preparedStatement= conn.prepareStatement(INSERT_TABLE_SQL);
248 preparedStatement.executeUpdate();
249 conn.setAutoCommit(false);
250 conn.commit();
251 System.out.println("\\
");
252 }catch(SQLException e){
253 e.printStackTrace();
254 }
255 INSERT_TABLE_SQL="INSERT INTO " + tableName + " VALUES(";
256 }
257 }
258 public void insertCsvTable(ArrayList<ArrayList<String>> tableValue, String tableName){
259 conn=JDBCUtil.getConnection();
260 System.out.println("Data table length: " + tableValue.size());
261 for (int i=0 ;i<tableValue.size();i + + ){
262 String INSERT_TABLE_SQL="INSERT INTO " + tableName + " VALUES( ";
263 String tempString=new String();
264 String[] tempValue={};
265 tempValue=tableValue.get(i).get(0).split("\,");
266 // System.out.println(tempValue[tempValue.length-1]);
267 for (int k=0; k<tempValue.length; k + + )
268 {
269 if(k!=tempValue.length-1){
270 tempString=tempString + """ + tempValue[k] + """ + ",";
271 }
272 else{
273 tempString=tempString + """ + tempValue[k] + """;
274 }
275 }
276 INSERT_TABLE_SQL=INSERT_TABLE_SQL + tempString + ");";
277 System.out.println(INSERT_TABLE_SQL);
278 try{
279 preparedStatement= conn.prepareStatement(INSERT_TABLE_SQL);
280 preparedStatement.executeUpdate();
281 conn.setAutoCommit(false);
282 conn.commit();
283 }catch(SQLException e){
284 e.printStackTrace();
285 }finally{
286 //Close database connection
287 }
288 }
289 }
290 //-------------------------------------------------- ---
291 //Method to get the maximum number of cells in a row of excel table
292 public int getMaxCellNumExcel(Sheet sheet){
293 int resNum=0;
294 int firstRowIndex = sheet.getFirstRowNum(); //Get the first row index
295 int lastRowIndex = sheet.getLastRowNum(); //Get the last row index
296 for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex + + ) { //Traverse rows
297 Row row = sheet.getRow(rIndex); //Get row index
298
299 if(row.getLastCellNum()>resNum){
300 resNum=row.getLastCellNum();
301 }
302 }
303 return resNum;
304 }
305 //Method to get the maximum number of cells in a row of a csv table
306 public int getMaxCellNumCsv(ArrayList<String> tableField){
307 int resNum=0;
308 resNum=tableField.size();
309 return resNum;
310 }
311 }