java导入excle表格,并且对表格进行相应的修改,并对表格数据进行整理,最后导出本地表格等一系列操作...

1.首先创建一个java项目

  完成效果如下图所示

2.导入以下jar包

3.代码如下

  其中行和列的操作是根据需求自动划分的

复制代码 1 public class auto_date { 2 private static List<List> 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> allData = new ArrayList<List>(); 14 // 越过第一行 它是列名称 15 for (int j = 1; j < rows; j++) { 16 List oneData = new ArrayList(); 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> 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 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工程师朋友们加入Java群: 891219277
复制代码

群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

转载于:https://juejin.im/post/5bfd0615f265da616e4c218a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值