获取Excel部分数据并很据项目要求计算适宜性等级综合指数判断该地区的土壤适宜性
代码运行前请先导入jxl架包,以下代码仅供学习参考:
下图为项目中的Excel:
ExcelTest02类代码如下:
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | // 读取Excel的类 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; public class ExcelTest02 { /* *该代码需要先获得excel里面的pH、有机质、氯含量和质地等值 *然后根据公式IAI(pH)=PH×权重(pH)计算出各个的IAI然后相加 *根据IAI(总)的值判断该地图土壤是否适宜种植烟草 */ static double IAI1; static double IAI2; static double IAI3; static double IAI4; static double IAI5; public static void main(String args[]) { try { System.out.println( "begini" ); Workbook book = Workbook.getWorkbook( new File( "a.xls" )); // 获得第一个工作表对象 Sheet sheet = book.getSheet( 0 ); // 得到第一列第一行的单元格 try { File file = new File( "d:/IAI.txt" ); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter( new FileWriter(file)); for ( int i = 2 ; i < 4896 ; i++) { Cell cell1 = sheet.getCell( 23 , i); // PH Cell cell2 = sheet.getCell( 24 , i); // 有机质 Cell cell3 = sheet.getCell( 33 , i); // 氯含量 Cell cell4 = sheet.getCell( 22 , i); // 质地 Cell cell5 = sheet.getCell( 4 , i); // 省 Cell cell6 = sheet.getCell( 7 , i); // 县 Cell cell7 = sheet.getCell( 8 , i); // 乡 Cell cell8 = sheet.getCell( 9 , i); // 村 Cell cell9 = sheet.getCell( 10 , i); // 组 String ph = cell1.getContents(); double phWeight = 0.1235 ; computePhValue(TypeConversion(ph), phWeight); String youjizhi = cell2.getContents(); double youjizhiWeight = 0.2075 ; computeYoujizhiValue(TypeConversion(youjizhi), youjizhiWeight); String lv = cell3.getContents(); double lvWeight = 0.1112 ; computeLvValue(TypeConversion(lv), lvWeight); String zhidi = cell4.getContents(); double zhidiWeight = 0.3057 ; computeZhidiValue(zhidi, zhidiWeight); double houduWeight = 0.2521 ; houdu(houduWeight); String sheng = cell5.getContents(); String xian = cell6.getContents(); String xiang = cell7.getContents(); String cun = cell8.getContents(); String zu = cell9.getContents(); String shiyixing = null ; double IAI = IAI1 + IAI2 + IAI3 + IAI4 + IAI5; if (IAI < 70.0 ) { shiyixing = "不适宜" ; } if (IAI >= 70.0 && IAI < 80.0 ) { shiyixing = "次适宜" ; } if (IAI >= 80.0 && IAI < 88.0 ) { shiyixing = "适宜" ; } if (IAI >= 88.0 ) { shiyixing = "最适宜" ; } System.out.println( "第" + (i + 1 ) + "行" + (IAI1 + IAI2 + IAI3 + IAI4 + IAI5)); output.write( "第" + (i + 1 ) + "行" + sheng + xian + xiang + cun + zu + "\t" + "IAI:" + (IAI1 + IAI2 + IAI3 + IAI4 + IAI5) + "\t" + shiyixing + "\n" ); output.newLine(); } output.close(); } catch (Exception ex) { System.out.println(ex); } book.close(); System.out.println( "end" ); } catch (Exception e) { System.out.println(e); } } /** * * @param * @return */ public static double computePhValue( double ph, double phWeight) { double result = 0d; if (ph == 0 ) { return result; } if (ph < 4.5 ) { IAI1 = 60.00 * phWeight; } if (ph == 4.5 ) { IAI1 = 68.75 * phWeight; } if (ph > 4.5 && ph < 5.0 ) { IAI1 = ( 30 * ph - 66.25 ) * phWeight; } if (ph == 5.0 ) { IAI1 = 83.75 * phWeight; } if (ph > 5.0 && ph < 5.5 ) { IAI1 = ( 32.5 * ph - 78.75 ) * phWeight; } if (ph == 5.5 || (ph > 5.5 && ph < 6.5 ) || ph == 6.5 ) { IAI1 = 100 * phWeight; } if (ph > 6.5 && ph < 7.0 ) { IAI1 = (- 16.24 * ph + 205.56 ) * phWeight; } if (ph == 7.0 ) { IAI1 = 91.88 * phWeight; } if (ph > 7.0 && ph < 7.5 ) { IAI1 = (- 23.76 * ph + 258.2 ) * phWeight; } if (ph == 7.5 ) { IAI1 = 80.00 * phWeight; } if (ph > 7.5 && ph < 8.0 ) { IAI1 = (- 77.14 * ph + 658.55 ) * phWeight; } if (ph == 8.0 ) { IAI1 = 41.43 * phWeight; } if (ph > 8.0 ) { IAI1 = 8.75 * phWeight; } return result; } public static double computeYoujizhiValue( double youjizhi, double youjizhiWeight) { double result = 0d; if (youjizhi == 0 ) { return result; } if (youjizhi < 10 ) { IAI2 = 58.75 * youjizhiWeight; } if (youjizhi > 10 && youjizhi < 15 ) { IAI2 = ( 4.37 * youjizhi + 15.05 ) * youjizhiWeight; } if (youjizhi == 15 ) { IAI2 = 80.60 * youjizhiWeight; } if (youjizhi > 15 && youjizhi < 20 ) { IAI2 = ( 2 * youjizhi + 50.6 ) * youjizhiWeight; } if (youjizhi == 20 ) { IAI2 = 90.60 * youjizhiWeight; } if (youjizhi > 20 && youjizhi < 25 ) { IAI2 = ( 1.88 * youjizhi + 53 ) * youjizhiWeight; } if (youjizhi == 25 ) { IAI2 = 100 * youjizhiWeight; } if (youjizhi > 25 && youjizhi < 30 ) { IAI2 = ( 2.12 * youjizhi + 47 ) * youjizhiWeight; } if (youjizhi == 30 ) { IAI2 = 89.40 * youjizhiWeight; } if (youjizhi > 30 && youjizhi < 35 ) { IAI2 = (- 2.5 * youjizhi + 164.4 ) * youjizhiWeight; } if (youjizhi == 35 ) { IAI2 = 76.90 * youjizhiWeight; } if (youjizhi > 35 && youjizhi < 40 ) { IAI2 = (- 3.38 * youjizhi + 195.2 ) * youjizhiWeight; } if (youjizhi == 40 ) { IAI2 = 60.00 * youjizhiWeight; } if (youjizhi > 40 ) { IAI2 = 48.8 * youjizhiWeight; } return result; } public static double computeLvValue( double lv, double lvWeight) { double result = 0d; if (lv == 0 ) { return result; } if (lv == 5 ) { IAI3 = 87.50 * lvWeight; } if (lv > 5 && lv < 10 ) { IAI3 = ( 2.5 * lv + 75 ) * lvWeight; } if (lv == 10 ) { IAI3 = 100 * lvWeight; } if (lv > 10 && lv < 20 ) { IAI3 = (- 0.625 * lv + 106.25 ) * lvWeight; } if (lv == 20 ) { IAI3 = 93.75 * lvWeight; } if (lv > 20 && lv < 30 ) { IAI3 = (- 1.625 * lv + 126.25 ) * lvWeight; } if (lv == 30 ) { IAI3 = 77.50 * lvWeight; } if (lv > 30 && lv < 40 ) { IAI3 = (- 1.25 * lv + 115 ) * lvWeight; } if (lv == 40 ) { IAI3 = 65.00 * lvWeight; } if (lv > 40 && lv < 50 ) { IAI3 = (- 6.5 * lv + 325 ) * lvWeight; } if (lv > 50 ) { IAI3 = 0 ; } return result; } public static void computeZhidiValue(String zhidi, double zhidiWeight) { if ( "沙土" .equals(zhidi)) { IAI4 = 91.40 * zhidiWeight; } if ( "壤沙土" .equals(zhidi)) { IAI4 = 100 * zhidiWeight; } if ( "沙壤土" .equals(zhidi)) { IAI4 = 100 * zhidiWeight; } if ( "壤土" .equals(zhidi)) { IAI4 = 91.40 * zhidiWeight; } if ( "粉沙壤土" .equals(zhidi)) { IAI4 = 82.10 * zhidiWeight; } if ( "粘壤土" .equals(zhidi)) { IAI4 = 72.10 * zhidiWeight; } if ( "粘土" .equals(zhidi)) { IAI4 = 48.50 * zhidiWeight; } } //由于土层厚度在给出的数据里面并没有,这里选随机数 public static void houdu( double houduWeight) { int len = 5 ; int lens = 5 ; double sum = 0 ; int [] houdu = new int [len]; double [] IAI5s = new double [lens]; for ( int i = 0 ; i < len; i++) { houdu[i] = ( int ) (Math.random() * 70 + 20 ); for ( int j = 0 ; j < lens; j++) { if (houdu[j] < 20 ) { IAI5s[j] = 32.85 * houduWeight; } if (houdu[j] > 20 && houdu[j] < 30 ) { IAI5s[j] = ( 1.855 * houdu[j] - 4.25 ) * houduWeight; } if (houdu[j] == 30 ) { IAI5s[j] = 51.40 * houduWeight; } if (houdu[j] > 30 && houdu[j] < 50 ) { IAI5s[j] = ( 1.18 * houdu[j] + 16 ) * houduWeight; } if (houdu[j] == 50 ) { IAI5s[j] = 75.00 * houduWeight; } if (houdu[j] > 50 && houdu[j] < 70 ) { IAI5s[j] = ( 0.715 * houdu[j] + 39.25 ) * houduWeight; } if (houdu[j] == 70 ) { IAI5s[j] = 89.30 * houduWeight; } if (houdu[j] > 70 ) { IAI5s[j] = 100 * houduWeight; } } sum = sum + IAI5s[i]; IAI5 = sum / 5 ; } } public static double TypeConversion(String value) { double values = 0 ; if (value != "" ) { values = Double.parseDouble(value.trim()); } else { values = 0 ; } return values; } } |
运行结果如下图所示:
生成的文本内容如下:
注意下:由于没有土层厚度的数据,所以在程序中采用了随机数选取几组数据求平均值,故每次生成的文本内容都不一样
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix