-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImport.java
More file actions
171 lines (156 loc) · 4.37 KB
/
Copy pathImport.java
File metadata and controls
171 lines (156 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import dbc.DataBaseConnection;
public class Import {
public static void main(String[] args) {
// TODO Auto-generated method stub
XSSFWorkbook hw;
try {
hw = new XSSFWorkbook(new FileInputStream("province.xlsx"));
for(int i = 1;i <= 12;i++) {
XSSFSheet hsheet = hw.getSheetAt(i + 2);
if(hsheet != null) {
String date = "2017";
if(i < 10) {
date = date + "0" + i;
} else {
date = date + i;
}
importTotalData(hsheet,date);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void importTotalData(XSSFSheet hsheet,String date) {
int rows = hsheet.getPhysicalNumberOfRows();
int flag = 0;
int province = 0;
String subject = "0";
for (int i = 2; i < 37; i++) {
if(flag == 1)
break;
province = i - 2;
XSSFRow hrow = hsheet.getRow(i);
for (int j = 1; j <= 24; j++) {
XSSFCell incomecell = hrow.getCell(j);
String income = getCellValue(incomecell);
if(income.equals("")) {
income = "0.0";
}
j++;
XSSFCell tbcell = hrow.getCell(j);
String tongbizengfu = getCellValue(tbcell);
if(tongbizengfu.equals("")) {
tongbizengfu = "0.0";
}
if(j < 3) {
subject = "0";
} else if(j < 5) {
subject = "2";
} else if(j < 7) {
subject = "3";
} else if(j < 9) {
subject = "1";
} else if(j < 11) {
subject = "11";
} else if(j < 13) {
subject = "12";
} else if(j < 15) {
subject = "14";
} else if(j < 17) {
subject = "15";
} else if(j < 19) {
subject = "13";
} else if(j < 21) {
subject = "31";
} else if(j < 23) {
subject = "32";
} else if(j < 25) {
subject = "33";
}
//System.out.println(income+ " " +tongbizengfu +" " +subject + " " + province);
importdata(subject,province + "",tongbizengfu,income,date);
}
}
}
public static void importdata(String subject,String province,String tongbiIncrease,String income,String month) {
try {
double tongbizengfuDouble = Double.parseDouble(tongbiIncrease) * 100;
tongbiIncrease = String.format("%.1f", tongbizengfuDouble);
} catch(Exception e) {
e.printStackTrace();
}
try {
double incomeDouble = Double.parseDouble(income);
income = String.format("%.1f", incomeDouble);
} catch(Exception e) {
e.printStackTrace();
}
String sql = "insert into `provincesubjecttotal_copy`(`province`,`subject`,`income`,`tongbiIncrease`,`month`) values(?,?,?,?,?)";
Connection conn = DataBaseConnection.getConnection();
PreparedStatement prep = null;
try {
prep = conn.prepareStatement(sql);
prep.setString(1,province);
prep.setString(2, subject);
prep.setString(3, income);
prep.setString(4, tongbiIncrease);
prep.setString(5, month);
prep.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (prep != null)
prep.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
DataBaseConnection.returnConnection(conn);
}
}
public static String getCellValue(XSSFCell cell) {
String value = "";
try {
if (cell != null) {
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_FORMULA:
try {
value = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
try {
value = String.valueOf(cell.getRichStringCellValue());
} catch (Exception ex) {
value = "error";
}
}
break;
case XSSFCell.CELL_TYPE_NUMERIC:
value = String.valueOf(cell.getNumericCellValue());
break;
case XSSFCell.CELL_TYPE_STRING:
value = String.valueOf(cell.getRichStringCellValue());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
value = "";
}
if(value == null)
value = "";
return value;
}
}