java解析excel信息(excel打开时报破损警告)

工具包:jsoup-1.13.1.jar

 

 链接:https://pan.baidu.com/s/1npwaTaBk7YiWx4MbxIb5Zw

提取码:bgmz

代码:

EXCELUTIL:

  1 package com.util;
  2 
  3 import org.jsoup.Jsoup;
  4 import org.jsoup.nodes.Document;
  5 import org.jsoup.nodes.Element;
  6 import org.jsoup.select.Elements;
  7 
  8 import java.io.IOException;
  9 import java.io.InputStream;
 10 import java.io.Serializable;
 11 import java.nio.charset.StandardCharsets;
 12 import java.util.ArrayList;
 13 import java.util.HashMap;
 14 import java.util.List;
 15 import java.util.Map;
 16 import java.util.regex.Matcher;
 17 import java.util.regex.Pattern;
 18 
 19 public class ExcelUtil {
 20     /*
 21      * Student{number,name,gender,clazz,grades}
 22      *            ——》{学号、姓名、性别、班级、成绩列表}
 23      *Grades{code,name,type,credit,score}
 24      *            ——》{课程代码、课程名、类型(必/选),学分,分数}
 25      * */
 26     public static List<Student> readGrades(InputStream inputStream) throws IOException {
 27         final Document document = Jsoup.parse(inputStream, StandardCharsets.UTF_8.name(), "");
 28         inputStream.close();
 29         final Pattern coursePattern = Pattern.compile("(?<code>\\d{6}[A-Z])(?<name>.+)\\[(?<type>.+)]\\[(?<credit>.+)学分]");
 30         final Map<String, Integer> nameToIndex = new HashMap<>();
 31         final Map<Integer, Course> indexToCourse = new HashMap<>();
 32         try {
 33             final Element table = document.getElementsByTag("table").get(0);
 34             final Elements trs = table.getElementsByTag("tr");
 35             final Element titleRow = trs.get(5);
 36             final Elements titleTds = titleRow.getElementsByTag("td");
 37             for (int i = 0; i < titleTds.size(); i++) {
 38                 final String text = titleTds.get(i).text();
 39                 final Matcher matcher = coursePattern.matcher(text);
 40                 if (matcher.matches()) {
 41                     indexToCourse.put(i, new Course(
 42                             matcher.group("code"),
 43                             matcher.group("name"),
 44                             matcher.group("type"),
 45                             Float.parseFloat(matcher.group("credit"))
 46                     ));
 47                 } else {
 48                     nameToIndex.put(text, i);
 49                 }
 50             }
 51             final List<Student> students = new ArrayList<>();
 52             for (int i = 6; i < trs.size(); i++) {
 53                 final Elements tds = trs.get(i).getElementsByTag("td");
 54                 final Student student = new Student();
 55                 student.setNumber(tds.get(nameToIndex.get("学号")).text());
 56                 if (student.getNumber().isEmpty()) {
 57                     continue;
 58                 }
 59                 student.setName(tds.get(nameToIndex.get("姓名")).text());
 60                 student.setGender(tds.get(nameToIndex.get("性别")).text());
 61                 student.setClazz(tds.get(nameToIndex.get("班级")).text());
 62                 final List<Student.Grade> grades = new ArrayList<>();
 63                 for (int j = 0; j < tds.size(); j++) {
 64                     if (indexToCourse.containsKey(j)) {
 65                         grades.add(new Student.Grade(indexToCourse.get(j), Float.parseFloat(tds.get(j).text())));
 66                     }
 67                 }
 68                 student.setGrades(grades);
 69                 students.add(student);
 70             }
 71             return students;
 72         } catch (IndexOutOfBoundsException e) {
 73             return null;
 74         }
 75     }
 76 
 77     public static class Course {
 78         public Course(String code, String name, String type, float credit) {
 79             this.code = code;
 80             this.name = name;
 81             this.type = type;
 82             this.credit = credit;
 83         }
 84 
 85         public Course(Course course) {
 86             this.code = course.code;
 87             this.name = course.name;
 88             this.type = course.type;
 89             this.credit = course.credit;
 90         }
 91         protected String code;
 92         protected String name;
 93         protected String type;
 94         protected float credit;
 95 
 96         public String getCode() {
 97             return code;
 98         }
 99 
100         public void setCode(String code) {
101             this.code = code;
102         }
103 
104         public String getName() {
105             return name;
106         }
107 
108         public void setName(String name) {
109             this.name = name;
110         }
111 
112         public String getType() {
113             return type;
114         }
115 
116         public void setType(String type) {
117             this.type = type;
118         }
119 
120         public float getCredit() {
121             return credit;
122         }
123 
124         public void setCredit(float credit) {
125             this.credit = credit;
126         }
127     }
128     public static class Student implements Serializable {
129         private String number;
130         private String name;
131         private String gender;
132         private String clazz;
133         private List<Grade> grades;
134 
135         @Override
136         public String toString() {
137             return "Student{" +
138                     "number='" + number + '\'' +
139                     ", name='" + name + '\'' +
140                     ", gender='" + gender + '\'' +
141                     ", clazz='" + clazz + '\'' +
142                     ", grades=" + grades +
143                     '}';
144         }
145 
146         public static class Grade extends Course implements Serializable {
147             public Grade(Course course, float score) {
148                 super(course);
149                 this.score = score;
150             }
151 
152             private float score;
153 
154             public float getScore() {
155                 return score;
156             }
157 
158             public void setScore(float score) {
159                 this.score = score;
160             }
161 
162             @Override
163             public String toString() {
164                 return "Grade{" +
165                         "score=" + score +
166                         '}';
167             }
168         }
169 
170         public String getNumber() {
171             return number;
172         }
173 
174         public void setNumber(String number) {
175             this.number = number;
176         }
177 
178         public String getName() {
179             return name;
180         }
181 
182         public void setName(String name) {
183             this.name = name;
184         }
185 
186         public String getGender() {
187             return gender;
188         }
189 
190         public void setGender(String gender) {
191             this.gender = gender;
192         }
193 
194         public String getClazz() {
195             return clazz;
196         }
197 
198         public void setClazz(String clazz) {
199             this.clazz = clazz;
200         }
201 
202         public List<Grade> getGrades() {
203             return grades;
204         }
205 
206         public void setGrades(List<Grade> grades) {
207             this.grades = grades;
208         }
209     }
210 }
ExcelUtil

Test:

 1 package com.util;
 2 
 3 import com.util.ExcelUtil;
 4 
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.IOException;
 8 import java.util.List;
 9 
10 public class Test {
11     /*
12      * Student{number,name,gender,clazz,grades}
13      *            ——》{学号、姓名、性别、班级、成绩列表}
14      *Grades{code,name,type,credit,score}
15      *            ——》{课程代码、课程名、类型(必/选),学分,分数}
16      * */
17     public static void main(String[] args) {
18         try {
19             final String filename = "E:\\578095023\\FileRecv\\综测系统\\Excel表格获取\\信2007-1班+2020学年第1学期成绩.xls";
20             List<ExcelUtil.Student> students = ExcelUtil.readGrades(new FileInputStream(filename));
21             if (students != null) {
22                 for(ExcelUtil.Student student:students)
23                 {
24                     for(ExcelUtil.Student.Grade grade:student.getGrades())
25                     {
26                         System.out.println("AAAAAAAAAAAAAAAAAA");
27                         System.out.println(grade.getCode());
28                         System.out.println(grade.getName());
29                         System.out.println(grade.getCredit());
30                         System.out.println(grade.getScore());
31                         System.out.println(grade.getType());
32                         
33                     }
34                 }
35 
36             }
37         } catch (IOException e) {
38             e.printStackTrace();
39         }
40     }
41 }
TEST

 

posted @ 2021-04-10 16:26  博二爷  阅读(195)  评论(0编辑  收藏  举报