1. 程式人生 > >java導入excle表格,並且對表格進行相應的修改,並對表格數據進行整理,最後導出本地表格等一系列

java導入excle表格,並且對表格進行相應的修改,並對表格數據進行整理,最後導出本地表格等一系列

new oid ace ava txt文件 輸入 lld 字符串 能量

1.首先創建一個java項目

  完成效果如下圖所示

2.導入以下jar包

3.代碼如下

  其中行和列的操作是根據需求自動劃分的

復制代碼
1 public class auto_date {
2 private static List<List<String>> readExcel(File file) throws Exception {
3 // 創建輸入流,讀取Excel
4 InputStream is = new FileInputStream(file.getAbsolutePath());
5 // jxl提供的Workbook類
6 Workbook wb = Workbook.getWorkbook(is);

7 // 只有一個sheet,直接處理
8 //創建一個Sheet對象
9 Sheet sheet = wb.getSheet(0);
10 // 得到所有的行數
11 int rows = sheet.getRows();
12 // 所有的數據
13 List<List<String>> allData = new ArrayList<List<String>>();
14 // 越過第一行 它是列名稱
15 for (int j = 1; j < rows; j++) {
16 List<String> oneData = new ArrayList<String>();
17 // 得到每一行的單元格的數據
18 Cell[] cells = sheet.getRow(j);
19 for (int k = 0; k < cells.length; k++) {
20 oneData.add(cells[k].getContents().trim());
21 }
22 // 存儲每一條數據
23 allData.add(oneData);
24 // 打印出每一條數據
25 //System.out.println(oneData);
26 }
27 return allData;
28 }
29 public static void main(String[] args) {
30 File file = new File("F://m//1.xls");
31 //42列
32 //3337行
33 try {
34 List<List<String>> allData=readExcel(file);
35 //System.out.println("總數:"+allData.size());//總行數
36 /*
37
創建excle表格
38 */
39 // 第一步,創建一個webbook,對應一個Excel文件
40 HSSFWorkbook wb = new HSSFWorkbook();
41 // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
42 HSSFSheet sheet = wb.createSheet("小麥特性表");
43 // 第三步,在sheet中添加表頭第0行,註意老版本poi對Excel的行數列數有限制short
44 HSSFRow row = sheet.createRow((int) 0);
45 // 第四步,創建單元格,並設置值表頭 設置表頭居中
46 //HSSFCellStyle style = wb.createCellStyle();
47 //style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式
48 // HSSFRow row1 = sheet.createRow(0);
49 HSSFCell cell = row.createCell((short) 0);
50 cell.setCellValue("所有小麥特征表");
51 sheet.addMergedRegion(new CellRangeAddress(0,0,0,20));
52 HSSFRow row2 = sheet.createRow(1);
53 row2.createCell(0).setCellValue("品種名稱");
54 row2.createCell(1).setCellValue("生態類型");
55 row2.createCell(2).setCellValue("生育期");
56 row2.createCell(3).setCellValue("苗性");
57 row2.createCell(4).setCellValue("葉色");
58 row2.createCell(5).setCellValue("分蘗力");
59 row2.createCell(6).setCellValue("株型");
60 row2.createCell(7).setCellValue("株高");
61 row2.createCell(8).setCellValue("株高");
62 row2.createCell(9).setCellValue("穗形");
63 row2.createCell(10).setCellValue("芒");
64 row2.createCell(11).setCellValue("殼色");
65 row2.createCell(12).setCellValue("粒色");
66 row2.createCell(13).setCellValue("硬度");
67 row2.createCell(14).setCellValue("籽粒飽滿度");
68 row2.createCell(15).setCellValue("畝穗數");
69 row2.createCell(16).setCellValue("穗粒數");
70 row2.createCell(17).setCellValue("千粒重");
71 row2.createCell(18).setCellValue("熟相");
72 row2.createCell(19).setCellValue("抗倒性");
73 row2.createCell(20).setCellValue("抗旱性");
74 row2.createCell(21).setCellValue("抗寒性1");
75 row2.createCell(22).setCellValue("抗寒性2");
76 row2.createCell(23).setCellValue("粗蛋白質");
77 row2.createCell(24).setCellValue("濕面筋");
78 row2.createCell(25).setCellValue("降落數值");
79 row2.createCell(26).setCellValue("沈澱指數");
80 row2.createCell(27).setCellValue("吸水量");
81 row2.createCell(28).setCellValue("形成時間");
82 row2.createCell(29).setCellValue("穩定時間");
83 row2.createCell(30).setCellValue("弱化度");
84 row2.createCell(31).setCellValue("出粉率");
85 row2.createCell(32).setCellValue("延伸性");
86 row2.createCell(33).setCellValue("拉伸能量");
87 row2.createCell(34).setCellValue("拉伸面積");
88 row2.createCell(35).setCellValue("最大拉伸阻力");
89 row2.createCell(36).setCellValue("容重");
90 row2.createCell(37).setCellValue("節水性指數1");
91 row2.createCell(38).setCellValue("節水性指數2");
92 row2.createCell(39).setCellValue("節水性1");
93 row2.createCell(40).setCellValue("節水性2");
94 row2.createCell(41).setCellValue("抗病性1");
95 row2.createCell(42).setCellValue("抗病性2");
96
97
98
99
100
101 /
102 導出txt文件
103
/
104 // File writename = new File("G://2.txt"); // 相對路徑,如果沒有則要建立一個新的output。txt文件
105 // writename.createNewFile(); // 創建新文件
106 // BufferedWriter out = new BufferedWriter(new FileWriter(writename));
107 // out.write("寫入文件\r\n"); // \r\n即為換行
108
109
110
111 //System.out.println(allData);//輸出全部
112 for (int i = 0; i < allData.size(); i++) {
113 List<String> list=allData.get(i);
114 HSSFRow row3 = sheet.createRow(i+2);
115 HSSFCell col0 = row3.createCell(0);
116 col0.setCellValue(list.get(0));
117 /

118 導出txt文件
119
/
120 // out.write("**"+"\r\n");
121 // out.write("第 "+i+" 個"+"品種名稱: "+list.get(0)+"\r\n");
122 //*****
123 //System.out.println(allData.get(i));//逐條輸出
124 //System.out.println(list.get(0));
125 for (int j = 0; j <list.size(); j++) {
126 String str=list.get(j);
127
128 String newstr=str.replace(",", "。");
129 String newstr1=newstr.replace(";","。");
130 String newstr2=newstr1.replace("、","。");
131 String newstr3=newstr2.replace(";","。");
132 String newstr4=newstr3.replace(",","。");
133 //System.out.println(newstr1);
134 String[] temp;
135 String delimeter = "。"; // 指定分割字符
136 temp = newstr4.split(delimeter); // 分割字符串
137 // 普通 for 循環
138 for(int q =0; q < temp.length ; q++){
139 String disease="";
140 //生態類型
141 HSSFCell col11 = row3.createCell(1);
142 //生育周期
143 HSSFCell col12 = row3.createCell(2);
144 //苗性
145 HSSFCell col13 = row3.createCell(3);
146 //葉色
147 HSSFCell col14 = row3.createCell(4);
148 //分蘗力
149 HSSFCell col15 = row3.createCell(5);
150 //穗形
151 HSSFCell col16 = row3.createCell(6);
152 //芒
153 HSSFCell col17 = row3.createCell(7);
154 //殼色
155 HSSFCell col18 = row3.createCell(8);
156 //芒
157 HSSFCell col19 = row3.createCell(9);
158 //殼
159 HSSFCell col110 = row3.createCell(10);
160 //粒色
161 HSSFCell col111 = row3.createCell(11);
162 //硬度
163 HSSFCell col112 = row3.createCell(12);
164 //籽粒飽滿度
165 HSSFCell col113 = row3.createCell(13);
166 //畝穗數
167 HSSFCell col114 = row3.createCell(14);
168 //穗粒數
169 HSSFCell col115 = row3.createCell(15);
170 //千粒重
171 HSSFCell col116 = row3.createCell(16);
172 //熟相
173 HSSFCell col117 = row3.createCell(17);
174 //抗倒性
175 HSSFCell col118 = row3.createCell(18);
176 //抗旱性
177 HSSFCell col119 = row3.createCell(19);
178 //抗寒性
179 HSSFCell col120 = row3.createCell(20);
180 HSSFCell col121 = row3.createCell(21);
181 HSSFCell col122 = row3.createCell(22);
182 HSSFCell col123 = row3.createCell(23);
183 HSSFCell col124 = row3.createCell(24);
184 HSSFCell col125 = row3.createCell(25);
185 HSSFCell col126 = row3.createCell(26);
186 HSSFCell col127 = row3.createCell(27);
187 HSSFCell col128 = row3.createCell(28);
188 HSSFCell col129 = row3.createCell(29);
189 HSSFCell col130 = row3.createCell(30);
190 HSSFCell col131 = row3.createCell(31);
191 HSSFCell col132 = row3.createCell(32);
192 HSSFCell col133 = row3.createCell(33);
193 HSSFCell col134 = row3.createCell(34);
194 HSSFCell col135 = row3.createCell(35);
195 HSSFCell col136 = row3.createCell(36);
196 HSSFCell col137 = row3.createCell(37);
197 HSSFCell col138 = row3.createCell(38);
198 HSSFCell col139 = row3.createCell(39);
199 HSSFCell col140 = row3.createCell(40);
200 HSSFCell col141 = row3.createCell(41);
201 HSSFCell col142 = row3.createCell(42);
202 HSSFCell col143 = row3.createCell(43);
203 for(int r =0; r < temp.length ; r++){
204 if (temp[r].contains("春性")==true||temp[r].contains("冬性")==true) {
205
206 col11.setCellValue(temp[r]);
207 }
208 if (temp[r].contains("生育期")==true) {
209 col12.setCellValue(temp[r]);
210 }
211 if (temp[r].contains("幼苗")==true) {
212 col13.setCellValue(temp[r]);
213 }
214 if (temp[r].contains("葉色")==true) {
215 col14.setCellValue(temp[r]);
216 }
217 if (temp[r].contains("分蘗力")==true) {
218 col15.setCellValue(temp[r]);
219 }
220 if (temp[r].contains("株型")==true) {
221 col16.setCellValue(temp[r]);
222 }
223 if (temp[r].contains("株高")==true) {
224 col17.setCellValue(temp[r]);
225 }
226 if (temp[r].contains("穗紡錘")==true||temp[r].contains("穗長方")==true||temp[r].contains("穗棍棒")==true
227 ||temp[r].contains("紡錘型")==true||temp[r].contains("棍棒形")==true
228 ||temp[r].contains("穗型長方形")==true||temp[r].contains("長方穗型")==true) {
229 col19.setCellValue(temp[r]);
230 }
231 if (temp[r].contains("芒")==true) {
232 col110.setCellValue(temp[r]);
233 }
234 if (temp[r].contains("殼")==true) {
235 col111.setCellValue(temp[r]);
236 }
237 if (temp[r].contains("紅粒")==true||temp[r].contains("藍粒")==true||
238 temp[r].contains("白粒")==true||temp[r].contains("黑粒")==true||temp[r].contains("粒紅")==true
239 ||temp[r].contains("粒黑")==true||temp[r].contains("粒白")==true) {
240 col112.setCellValue(temp[r]);
241 }
242 if (temp[r].contains("硬質")==true) {
243 col113.setCellValue(temp[r]);
244 }
245 if (temp[r].contains("飽滿")==true) {
246 col114.setCellValue(temp[r]);
247 }
248 if (temp[r].contains("畝穗數")==true) {
249 col115.setCellValue(temp[r]);
250 }
251 if (temp[r].contains("穗粒數")==true) {
252 col116.setCellValue(temp[r]);
253 }
254 if (temp[r].contains("千粒重")==true) {
255 col117.setCellValue(temp[r]);
256 }
257 if (temp[r].contains("熟相")==true) {
258 col118.setCellValue(temp[r]);
259 }
260 if (temp[r].contains("抗倒性")==true) {
261 col119.setCellValue(temp[r]);
262 }
263 if (temp[r].contains("抗旱性")==true) {
264 col120.setCellValue(temp[r]);
265 }
266 if (temp[r].contains("抗寒性")==true) {
267 col121.setCellValue(temp[r]);
268 }
269 // if (temp[r].contains("抗寒性")==true) {
270 // col122.setCellValue(temp[r]);
271 // }
272 if (temp[r].contains("粗蛋白質")==true) {
273 col123.setCellValue(temp[r]);
274 }
275 if (temp[r].contains("濕面筋")==true) {
276 col124.setCellValue(temp[r]);
277 }
278 if (temp[r].contains("降落數值")==true) {
279 col125.setCellValue(temp[r]);
280 }
281 if (temp[r].contains("沈澱指數")==true) {
282 col126.setCellValue(temp[r]);
283 }
284 if (temp[r].contains("吸水量")==true||temp[r].contains("吸水率")==true) {
285 col127.setCellValue(temp[r]);
286 }
287 if (temp[r].contains("形成時間")==true) {
288 col128.setCellValue(temp[r]);
289 }
290 if (temp[r].contains("穩定時間")==true) {
291 col129.setCellValue(temp[r]);
292 }
293 if (temp[r].contains("弱化度")==true) {
294 col130.setCellValue(temp[r]);
295 }
296 if (temp[r].contains("出粉率")==true) {
297 col131.setCellValue(temp[r]);
298 }
299 if (temp[r].contains("延伸性")==true) {
300 col132.setCellValue(temp[r]);
301 }
302 if (temp[r].contains("拉伸能量")==true) {
303 col133.setCellValue(temp[r]);
304 }
305 if (temp[r].contains("拉伸面積")==true) {
306 col134.setCellValue(temp[r]);
307 }
308 if (temp[r].contains("最大拉伸阻力")==true) {
309 col135.setCellValue(temp[r]);
310 }
311 if (temp[r].contains("容重")==true) {
312 col136.setCellValue(temp[r]);
313 }
314 if (temp[r].contains("節水指數")==true) {
315 col137.setCellValue(temp[r]);
316 }
317 // if (temp[r].contains("節水指數2")==true) {
318 // col138.setCellValue(temp[r]);
319 // }
320 if (temp[r].contains("節水性")==true) {
321 col139.setCellValue(temp[r]);
322 }
323 // if (temp[r].contains("節水性2")==true) {
324 // col140.setCellValue(temp[r]);
325 // }
326 /
327 各種病癥
328
/
329 if(temp[r].contains("抗病鑒定")==true){
330 disease+=temp[r]+":";
331 }
332 if(temp[r].contains("葉銹病")==true){
333 disease+=temp[r]+",";
334 }
335 if(temp[r].contains("條銹病")==true){
336 disease+=temp[r]+",";
337 }
338 if(temp[r].contains("×××病")==true){
339 disease+=temp[r]+",";
340 }
341 if(temp[r].contains("赤黴病")==true){
342 disease+=temp[r]+",";
343 }
344 if(temp[r].contains("抗條銹病")==true){
345 disease+=temp[r];
346 }
347 if(temp[r].contains("紋枯病")==true){
348 disease+=temp[r];
349 }
350 if(temp[r].contains("黃花葉病")==true){
351 disease+=temp[r];
352 }
353 if(temp[r].contains("根腐病")==true){
354 disease+=temp[r];
355 }
356 if(temp[r].contains("稈銹病")==true){
357 disease+=temp[r];
358 }
359 if(temp[r].contains("株期感病")==true){
360 disease+=temp[r];
361 }
362 if(temp[r].contains("株期抗病")==true){
363 disease+=temp[r];
364 }
365 if(temp[r].contains("抗赤黴")==true){
366 disease+=temp[r];
367 }
368 if(temp[r].contains("抗赤黴")==true){
369 disease+=temp[r];
370 }
371 if(true){
372 col141.setCellValue(disease);
373 //System.out.println(disease);
374 }
375
376
377
378 // if (temp[r].contains("抗病性2")==true) {
379 // col142.setCellValue(temp[r]);
380 // }
381
382
383 /

384 導出txt文件
385
/
386 // out.write("屬性: "+temp[r]+"\r\n");
387
388 //System.out.println(temp[r]);//輸出字符串
389 }
390 }
391 }
392 //System.out.println(list.get(1));
393 }
394
395
396
397 // 第五步,將文件存到指定位置
398 FileOutputStream fout = new FileOutputStream("F://m//2.xls");
399 wb.write(fout);
400 fout.close();
401 /*
402
關閉txt流
403 */
404 // out.flush(); // 把緩存區內容壓入文件
405 // out.close(); // 最後記得關閉文件
406 } catch (Exception e) {
407 // TODO Auto-generated catch block
408 e.printStackTrace();
409 }
410 }
411 }
復制代碼

  

4.做成效果如下圖所示

java導入excle表格,並且對表格進行相應的修改,並對表格數據進行整理,最後導出本地表格等一系列