◎筱米加步枪◎.Blog

Happy coding

Java操作Excel文件之使用JXL 学习笔记

百科上对于JXL的说明:

通过java操作excel表格的工具类库   

支持Excel 95-2000的所有版本   

生成Excel 2000标准格式   

支持字体、数字、日期操作   

能够修饰单元格属性   

支持图像和图表   

应该说以上功能已经能够大致满足我们的需要。最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。

前几天学习了下,总结备忘下:贴个关于Excel操作类的代码:(需要加入jxl.jar包)

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
/**
 *
 * <pre>
 * MS Office Excel 文件操作对象
 * 注:利用第三方包jxl.jar进行操作
 * 注:暂不支持excel2007版本新格式xlsx后缀名
 * 提供操作内容:
 * 1.创建Excel文件
 * 2.读取Excel文件
 * 3.导出(写入)Excel文件
 * 4.判断是否为Excel文件
 * </pre>
 *
 * @author 陈书挺
 * @create 2010-6-12
 * @version v1.0
 *
 * <pre>
 * 修改时间    修改人   修改原因
 * 2010-6-12  陈书挺   新建类
 * 2010-6-13  陈书挺   修改createExcel(String)方法 | 增加createExcel(String, String...)方法
 * </pre>
 *
 */
public class ExcelOperator {
     
    /*
     * Excel文件后缀名
     */
    private static final String EXCEL_POSTFIX = ".xls";
     
    /**
     * 创建指定的文件路径的一个Excel文件<br>
     * 默认创建3个工作表分别为:Sheet1|Sheet2|Sheet3<br>
     * 注:只支持xls格式的文件
     * @param filePath    文件路径
     * @throws Exception
     */
    public void createExcel(String filePath) throws Exception{
        this.createExcel(filePath, "Sheet1" , "Sheet2" , "Sheet3");
    }
     
    /**
     * 创建指定的文件路径的一个Excel文件,并指定工作表名<br>
     * 注:只支持xls格式的文件
     * @param filePath    文件路径
     * @param sheetNames  工作表名数组
     * @throws Exception
     */
    public void createExcel(String filePath , String...sheetNames) throws Exception{
        if(!this.isExcel(filePath)){
            throw new Exception("对不起,文件必须是Excel文件,必须是xls格式的文件");
        }
        File file = new File(filePath);
        //工厂方法创建一个可写入的工作薄(WorkBook)
        WritableWorkbook workBook = Workbook.createWorkbook(file);
        //创建一个可写入的工作表(Sheet) 得到的对象为WritableSheet对象             
        //工作表名   工作表在工作簿中的位置
        for (int i = 0; i < sheetNames.length; i++) {
            workBook.createSheet(sheetNames[i], i);
        }
        workBook.write();
        workBook.close();
    }
     
    /**
     * 读取指定文件路径的Excel文件,以集合方式返回<br>
     * 说明:读取所有工作表的内容
     * @param filePath  文件路径
     * @return  List集合,List集合中的元素表示一个工作表
     *          List中存储String[][]数组,一个数组表示存储工作表的内容
     *          返回文件中的所有内容
     * @throws Exception
     */
    public List<String [][]> readExcel(String filePath) throws Exception{
        if(!this.isExcel(filePath)){
            throw new Exception("对不起,文件必须是Excel文件,必须是xls格式的文件");
        }
        List<String [][]> contents = new ArrayList<String[][]>();
        File file = new File(filePath);
        //创建工作薄
        Workbook workBook = Workbook.getWorkbook(file);
        //获取工作表
        Sheet [] sheets = workBook.getSheets();
        if(sheets!=null && sheets.length>0){
            for (int i = 0; i < sheets.length; i++) {
                String[][] content = this.readExcel(filePath, i);
                contents.add(content);
            }
        }
        workBook.close();
        return contents;
    }
     
    /**
     * 将指定内容写入指定的Excel文件,并指定相应的工作表名
     * @param filePath   文件路径
     * @param contents   数据内容
     * @param sheetName  工作表名
     * @throws Exception
     */
    public void writeExcel(String filePath , String [][] contents ,String sheetName) throws Exception{
        if(!this.isExcel(filePath)){
            throw new Exception("对不起,文件必须是Excel文件,必须是xls格式的文件");
        }
        File file = new File(filePath);
        //工厂方法创建一个可写入的工作薄(WorkBook)
        WritableWorkbook workBook = Workbook.createWorkbook(file);
        WritableSheet sheet = workBook.createSheet(sheetName, 0);
        for (int i = 0; i < contents.length; i++) {
            for (int j = 0; j < contents[i].length; j++) {
                sheet.addCell(new Label(j,i,contents[i][j]));
            }
        }
        workBook.write();
        workBook.close();
    }
     
    /**
     * 将指定内容写入指定的Excel文件,并指定相应的工作表名
     * @param filePath   文件路径
     * @param contents   数据内容
     * @param sheetName  工作表名
     * @throws Exception
     */
    public void writeExcel(String filePath , Vector<Vector<String>> contents , String sheetName) throws Exception{
        this.writeExcel(filePath, StringUtils.toStringArray(contents), sheetName);
    }
     
    /**
     * 读取指定文件路径、指定工作表索引的Excel文件
     *
     * @param filePath   Excel文件路径
     * @param sheetIndex 工作表索引 从0开始
     * @return 与Excel结构相似的二维数组,二维数组中存储的是表格中的内容
     * @throws Exception
     */
    public String [][] readExcel(String filePath , int sheetIndex) throws Exception{
        if(!this.isExcel(filePath)){
            throw new Exception("文件必须是Excel文件,必须是xls格式的文件");
        }
        File file = new File(filePath);
        //创建工作薄
        Workbook workBook = Workbook.getWorkbook(file);
        //获取工作表
        Sheet [] sheets = workBook.getSheets();
        if(sheetIndex>sheets.length-1){
            throw new Exception("工作表索引["+sheetIndex+"]超出范围");
        }
         
        //获取指定工作表
        Sheet sheet = sheets[sheetIndex];
        //获取当前工作薄的行数
        int rows = sheet.getRows();
        //获取当前工作薄的列数
        int columns = sheet.getColumns();
        //存储Excel中的数据
        String [][] content = new String[rows][columns];
        this.initArrays(content);
        for (int i = 0; i < rows; i++) {
            //得到当前行的所有单元格
            Cell [] cells = sheet.getRow(i);
            if(cells!=null && cells.length>0){
                for (int j= 0; j < cells.length; j++) {
                    String cellContent = cells[j].getContents();
                    content[i][j]=cellContent;
                }
            }
        }
        workBook.close();
        return content;
    }
     
    /**
     * 判断指定文件的路径是否是Excel文件
     * @param filePath  文件的路径
     * @return   是否是Excel文件
     *          true:是Excel文件
     *          false:不是Excel文件
     */
    public boolean isExcel(String filePath){
        //获取文件后缀名
        String postfix = FileUtils.getPostfix(filePath);
        if(!postfix.equals(ExcelOperator.EXCEL_POSTFIX)){
            return false;
        }
        return true;
    }
     
    /*
     * 初始化二维数组内容为""
     * @param arrays 要初始化二维数组
     */
    private void initArrays(String [][] arrays){
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays[i].length; j++) {
                arrays[i][j]="";
            }
        }
    }
}