2017-04-12-读写excel
Xplorist Lv6

2017-04-12-读写excel

知识点1:

网上查资料的能力,自己研究api的能力

调用添加第三方jar包工具库并研究其api的能力

poi方式解析Excel的核心思想

将每一行的数据看做是一个类的对象,每一列的数据看做是一个对象的属性。

面向对象的思想是多么的重要和方便,数据通过对象的方式可以存储进程序中进行处理,然后通过程序再将数据导出。

后面的对象关系映射也是利用这个原理。

面向对象很奇妙的应用。

将某种格式的文件读取出来,存进到对象中,那么就要对这种文件的层次分析清楚,Excel文件的层次是,将每个处理单位都一般对于着jar包中的一个类的对象。

Excel文件–》excel的一张表格sheet–》sheet中的行raw–》raw中的单元格cell

读取,一层一层地读取,

写入,一层一层地写入

凭感觉能够写出来,但是理性分析之后效率会加倍

实例1:

读取excel在控制台中显示,然后截取部分数据导出到另外一个excel文件中

工程代码百度网盘链接:

链接:http://pan.baidu.com/s/1kVx9Hyf 密码:bn4y

核心代码:

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
package com.share.eg01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 读取excel数据,导出excel数据 
 * 
 * @author brx
 */
public class ExcelDataCapture {
    public static void main(String[] args) {
        ExcelDataCapture c = new ExcelDataCapture();
        List<Student> list = null;
        List<Student> listOut = null;

        try {
            list = c.readXlsx();
            listOut = new ArrayList<Student>();
            System.out.println();

            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
                if (i >= 5 && i <= 15 || i == 0) {
                    listOut.add(list.get(i));
                }
            }
            c.writeXlsx(listOut);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 读出excel
    public List<Student> readXlsx() throws Exception {
        File file = new File("D:\\1myJavaWSpace\\JavaSE_04_12_解析excel\\lib\\java阶段性考试成绩.xlsx");
        InputStream is = new FileInputStream(file);
        XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is);
        List<Student> list = new ArrayList<Student>();
        Student student = null;
        for (int i = 0; i < hssfWorkbook.getNumberOfSheets(); i++) {
            XSSFSheet hssfSheet = hssfWorkbook.getSheetAt(i);
            if (hssfSheet == null) {
                continue;
            }
            for (int j = 0; j <= hssfSheet.getLastRowNum(); j++) {
                XSSFRow hssfRow = hssfSheet.getRow(j);
                if (hssfRow != null) {
                    student = new Student();
                    XSSFCell order = hssfRow.getCell(0);
                    XSSFCell name = hssfRow.getCell(1);
                    XSSFCell choice = hssfRow.getCell(2);
                    XSSFCell judge = hssfRow.getCell(3);
                    XSSFCell blank = hssfRow.getCell(4);
                    XSSFCell comprehehsion = hssfRow.getCell(5);
                    XSSFCell paperSum = hssfRow.getCell(6);
                    XSSFCell machineSum = hssfRow.getCell(7);
                    XSSFCell examSum = hssfRow.getCell(8);

                    if (j == 0) {
                        student.setOrder(order.toString());
                        student.setName(name.toString());
                        student.setChoice(choice.toString());
                        student.setJudge(judge.toString());
                        student.setBlank(blank.toString());
                        student.setComprehehsion(comprehehsion.toString());
                        student.setPaperSum(paperSum.toString());
                        student.setMachineSum(machineSum.toString());
                        student.setExamSum(examSum.toString());
                    } else {
                        student.setOrder(order.getRawValue());
                        student.setName(name.toString());
                        student.setChoice(getStringValue(choice.getRawValue()) + "\t");
                        student.setJudge(getStringValue(judge.getRawValue()));
                        student.setBlank(getStringValue(blank.getRawValue()));
                        student.setComprehehsion(getStringValue(comprehehsion.getRawValue()) + "\t");
                        student.setPaperSum(getStringValue(paperSum.getRawValue()) + "\t");
                        student.setMachineSum(getStringValue(machineSum.getRawValue()) + "\t");
                        student.setExamSum(getStringValue(examSum.getRawValue()) + "\t");
                    }

                    list.add(student);
                }
            }
        }
        return list;
    }
    //用于截取double类型数值的字符串,使精确只保留一位小数,控制台打印的格式就能够统一了
    public static String getStringValue(String str) {
        if (str.contains(".")) {
            int i = str.lastIndexOf(".");
            str = str.substring(0, i + 2);
        }
        return str;
    }

    // 导出excel
    public void writeXlsx(List<Student> listOut) throws Exception {
        File file = new File("D:\\1myJavaWSpace\\JavaSE_04_12_解析excel\\lib\\java阶段性考试成绩(5-15).xlsx");
        OutputStream os = new FileOutputStream(file);
        file.createNewFile();
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet sheet = xssfWorkbook.createSheet();
        Student student = null;
        XSSFCell[] cells = new XSSFCell[9];

        for (int i = 0; i < listOut.size(); i++) {
            XSSFRow row = sheet.createRow(i);
            student = listOut.get(i);
            for (int j = 0; j < 9; j++) {
                cells[j] = row.createCell(j);
            }
            cells[0].setCellValue(student.getOrder());
            cells[1].setCellValue(student.getName());
            cells[2].setCellValue(student.getChoice());
            cells[3].setCellValue(student.getJudge());
            cells[4].setCellValue(student.getBlank());
            cells[5].setCellValue(student.getComprehehsion());
            cells[6].setCellValue(student.getPaperSum());
            cells[7].setCellValue(student.getMachineSum());
            cells[8].setCellValue(student.getExamSum());
        }

        xssfWorkbook.write(os);
        System.out.println("导出成功");

    }
}

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
package com.share.eg01;

public class Student {
    private String order;//排名
    private String name;//姓名
    private String choice;//选择题
    private String judge;//判断
    private String blank;//填空
    private String comprehehsion;//阅读程序
    private String paperSum;//卷面总分
    private String machineSum;//上机总分
    private String examSum;//考试总分
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String order, String name, String choice, String judge, String blank, String comprehehsion,
            String paperSum, String machineSum, String examSum) {
        super();
        this.order = order;
        this.name = name;
        this.choice = choice;
        this.judge = judge;
        this.blank = blank;
        this.comprehehsion = comprehehsion;
        this.paperSum = paperSum;
        this.machineSum = machineSum;
        this.examSum = examSum;
    }
    public String getOrder() {
        return order;
    }
    public void setOrder(String order) {
        this.order = order;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getChoice() {
        return choice;
    }
    public void setChoice(String choice) {
        this.choice = choice;
    }
    public String getJudge() {
        return judge;
    }
    public void setJudge(String judge) {
        this.judge = judge;
    }
    public String getBlank() {
        return blank;
    }
    public void setBlank(String blank) {
        this.blank = blank;
    }
    public String getComprehehsion() {
        return comprehehsion;
    }
    public void setComprehehsion(String comprehehsion) {
        this.comprehehsion = comprehehsion;
    }
    public String getPaperSum() {
        return paperSum;
    }
    public void setPaperSum(String paperSum) {
        this.paperSum = paperSum;
    }
    public String getMachineSum() {
        return machineSum;
    }
    public void setMachineSum(String machineSum) {
        this.machineSum = machineSum;
    }
    public String getExamSum() {
        return examSum;
    }
    public void setExamSum(String examSum) {
        this.examSum = examSum;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return ""+order+"\t"+name+"\t"+choice+"\t"+judge+"\t"+blank+"\t"+comprehehsion+"\t"+paperSum+"\t"+machineSum+"\t"+examSum+"";
    }
    
    
}
 评论