代码运行前请先导入jxl架包,以下代码仅供学习参考:
下图为项目中的Excel:
ExcelTest02类代码如下:
// 读取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; } }
运行结果如下图所示:
生成的文本内容如下:
注意下:由于没有土层厚度的数据,所以在程序中采用了随机数选取几组数据求平均值,故每次生成的文本内容都不一样
获取Excel部分数据并很据项目要求计算适宜性等级综合指数判断该地区的土壤适宜性,布布扣,bubuko.com
时间: 2024-10-24 05:20:11