首先进行maven的配置:导入相关依赖

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>

完整源码:

1 public interface ReadFile {
2 //返回值是不包含表头的所有数据
3 public ArrayList<ArrayList<String>> readExcelFile(String path) throws IOException, SQLException;
4 //返回值是有错误的数据
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 //相关变量
21 private int MAX_CELL_NUM; //excel文件的最大单元格数
22 private PreparedStatement preparedStatement=null;
23 private Connection conn =null;
24 //--------------------------------------------------
25 public ArrayList<ArrayList<String>> readExcelFile(String path) throws IOException, SQLException {
26 String DROP_TABLE_1=""; //删除源数据表sql语句
27 String DROP_TABLE_2=""; //删除备份表sql语句
28 String CREATE_TABLE_1_SQL=""; //创建源数据表语句
29 String CREATE_TABLE_2_SQL=""; //创建备份表语句
30 String tableName_1=""; //源数据表名
31 String tableName_2=""; //备份表名
32 ArrayList<String> tableField = new ArrayList<String>(); //数据表字段名
33 ArrayList<ArrayList<String>> tableValue = new ArrayList<ArrayList<String>>(); //数据表的值
34 System.out.println(path);
35 File excel=new File(path);
36 String[] split = excel.getName().split("\\."); //.是特殊字符,需要转义!
37 System.out.println(split[0]+" "+split[1]);
38 tableName_1=split[0]; //给源数据表名赋值
39 tableName_2=split[0]+split[1]; //给备份表名赋值
40 System.out.println("源数据表名:" + tableName_1);
41 System.out.println("备份表名: " + tableName_2);
42 Workbook wb; //新建文件
43 FileInputStream fileStream = new FileInputStream(excel); //文件流对象
44 //根据文件后缀(xls/xlsx)进行判断
45 if ( "xls".equals(split[1])){
46 //xls和csv文件
47 wb = new HSSFWorkbook(fileStream);
48 }else{
49 //xlsx文件
50 wb = new XSSFWorkbook(new FileInputStream(excel));
51 }
52 //开始解析
53 Sheet sheet = wb.getSheetAt(0); //读取sheet 0
54 MAX_CELL_NUM=getMaxCellNumExcel(sheet);
55 int firstRowIndex = sheet.getFirstRowNum(); //获取第一行索引
56 int lastRowIndex = sheet.getLastRowNum(); //获取最后一行索引
57 for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { //遍历行
58 Row row = sheet.getRow(rIndex); //获取行索引
59 ArrayList<String> tempTableValue = new ArrayList<String>(); //暂存一行的值,之后放到tableValue
60 if (row != null) {
61 int lastCellIndex = MAX_CELL_NUM; //行的最后一个单元格
62 for (int cIndex = 0; cIndex < lastCellIndex; cIndex++) { //遍历列(单元格)
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("第"+ String.valueOf(rIndex+1)+"行的值:"+ " "+tempTableValue);
72 tableValue.add(tempTableValue);
73 }
74 // System.out.println(tempTableValue);
75 System.out.println("-----------------------------");
76 }
77 System.out.println(tableValue); //输出表格的所有值
78 tableField= tableValue.get(0); //把表头信息放到tableFiled
79 tableValue.remove(0); //之后把所有值的这个list的第一行数据去掉
80 System.out.println("数据表字段: " + tableField);
81 wb.close();
82
83 //---------------------------------------------------------------------
84 //下面开始插入数据库:
85 //获取数据库连接
86 conn = JDBCUtil.getConnection();
87 if(conn!=null){
88 System.out.println("数据库连接成功!");
89 }else {
90 System.out.println("数据库连接失败!");
91 }
92 //根据表头和表名写SQL建表语句----------源数据表
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 //备份表语句
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("源数据表的建表语句: "+"\n" + CREATE_TABLE_1_SQL);
103 System.out.println("备份表的建表语句: " +"\n" + 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 //读CSV文件
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=""; //删除源数据表sql语句
124 String DROP_TABLE_2=""; //删除备份表sql语句
125 String CREATE_TABLE_1_SQL=""; //创建源数据表语句
126 String CREATE_TABLE_2_SQL=""; //创建备份表语句
127 ArrayList<String> tableField = new ArrayList<String>(); //数据表字段名
128 ArrayList<ArrayList<String>> tableValue = new ArrayList<ArrayList<String>>(); //数据表的值
129 String tableName_1=""; //源数据表名
130 String tableName_2=""; //备份表名
131 try {
132 // 创建CSV读对象
133 CsvReader csvReader = new CsvReader(path,',', Charset.forName(code));
134 String[] split1 = path.split("\\."); //.是特殊字符,需要转义!
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("输出源表名:"+tableName_1);
141 System.out.println("输出备份表名:"+tableName_2);
142 // 跳过表头
143 // csvReader.readHeaders();
144 boolean flag=true;
145 while (csvReader.readRecord()){
146 // 读一整行
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>(); //暂存一行的值,之后放到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); //之后把所有值的这个list的第一行数据去掉
172 System.out.println("数据表字段: " + 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 //生成源表建表语句
180 CREATE_TABLE_1_SQL=getCsvSQL(CREATE_TABLE_1_SQL,tableField);
181 //生成备份表建表语句
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 //获取excel文件建表SQL语句的方法
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,"+"\n";
208 }else{
209 SQL=SQL + "`" + item + "`" +" text COLLATE utf8_general_ci "+"\n";
210 }
211 }
212 return SQL+") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;";
213 }
214 //获取CSV文件的建表sql语句
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,"+"\n";
223 }else{
224 SQL=SQL + "`" + item + "`" +" text COLLATE utf8_general_ci "+"\n";
225 }
226 }
227 return SQL+") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;";
228 }
229 //--------------------------------------------------
230 //插入数据库 xls和xlsx文件
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("\n");
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("数据表长度: " + 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 //关闭数据库连接
287 }
288 }
289 }
290 //--------------------------------------------------
291 //获取excel表一行最大的单元格数目的方法
292 public int getMaxCellNumExcel(Sheet sheet){
293 int resNum=0;
294 int firstRowIndex = sheet.getFirstRowNum(); //获取第一行索引
295 int lastRowIndex = sheet.getLastRowNum(); //获取最后一行索引
296 for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { //遍历行
297 Row row = sheet.getRow(rIndex); //获取行索引
298
299 if(row.getLastCellNum()>resNum){
300 resNum=row.getLastCellNum();
301 }
302 }
303 return resNum;
304 }
305 //获取csv表一行最大的单元格数目的方法
306 public int getMaxCellNumCsv(ArrayList<String> tableField){
307 int resNum=0;
308 resNum=tableField.size();
309 return resNum;
310 }
311

 

作者:​​靠谱杨​​​,

更多日常分享尽在我的VX公众号:小杨的挨踢IT生活

使用POI、JavaCsv工具读取excel文件(*.xls , *.xlsx , *.csv)存入MySQL数据库_java