在做的项目中遇到需要处理Excel的多种输出文件,包括csv, xls, xlsx这三种文件, 所以想先将后两种合适都转换到csv格式再一起解析. 于是就去找到了下面这两个类, 两个好像都是apache官方的例子.来源链接:
XLS2CSV: html/api/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java.html">http://www.docjar.com/html/api/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java.html
XLSX2CSV: https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java
上面两个地址中后面一个好像是被墙的,我也是翻过了墙才勉强访问到.为了方便大家我把代码贴到下面,
不过跟上面的有一点区别: 我自己在两个类中都多加了一个构造器,把功能封装到只要传入来源文件和输出文件路径就可以用了
使用方法示例请看代码最后面的main方法.
依赖的包在本页最下面提供...
XLS2CSV:
1/**//* ====================================================================
2Licensed to the Apache Software Foundation (ASF) under one or more
3contributor license agreements. See the NOTICE file distributed with
4this work for additional information regarding copyright ownership.
5The ASF licenses this file to You under the Apache License, Version 2.0
6(the "License"); you may not use this file except in compliance with
7the License. You may obtain a copy of the License at
8
9http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16==================================================================== */
17package cn.jayslong.convert;
18
19import java.io.FileInputStream;
20import java.io.FileNotFoundException;
21import java.io.IOException;
22import java.io.PrintStream;
23import java.util.ArrayList;
24
25import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
26import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
27import org.apache.poi.hssf.eventusermodel.HSSFListener;
28import org.apache.poi.hssf.eventusermodel.HSSFRequest;
29import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
30import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;
31import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
32import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
33import org.apache.poi.hssf.model.HSSFFormulaParser;
34import org.apache.poi.hssf.record.BOFRecord;
35import org.apache.poi.hssf.record.BlankRecord;
36import org.apache.poi.hssf.record.BoolErrRecord;
37import org.apache.poi.hssf.record.BoundSheetRecord;
38import org.apache.poi.hssf.record.FormulaRecord;
39import org.apache.poi.hssf.record.LabelRecord;
40import org.apache.poi.hssf.record.LabelSSTRecord;
41import org.apache.poi.hssf.record.NoteRecord;
42import org.apache.poi.hssf.record.NumberRecord;
43import org.apache.poi.hssf.record.RKRecord;
44import org.apache.poi.hssf.record.Record;
45import org.apache.poi.hssf.record.SSTRecord;
46import org.apache.poi.hssf.record.StringRecord;
47import org.apache.poi.hssf.usermodel.HSSFWorkbook;
48import org.apache.poi.poifs.filesystem.POIFSFileSystem;
49
50/** *//**
51 * A XLS -> CSV processor, that uses the MissingRecordAware Ev