In the project you are working on, you may need to process multiple Excel output files, including csv, xls, and xlsx files, so I want to convert the last two methods to the csv format and parse them together. so I found the following two classes, both of which are official apache examples. source link:
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
One of the above two addresses seems to be behind the wall, and I barely accessed it after I crossed the wall. In order to help you paste the code below,
However, there is a difference: I added a constructor to both classes to encapsulate the function and use it as long as it is passed in to the source file and the output file path.
For usage examples, see the main method at the end of the Code.
Dependent packages are provided at the bottom of this page...
XLS2CSV:
1/** // * ========================================== ==========================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed
4 this 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 T in compliance
7the License. You may obtain a copy of the License
8
Http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "as is" BASIS,
13 without warranties or conditions of any kind, either express or implied.
14See the License for the specific language governing permissions and
15 limitations under the License.
16 ================================================ ===================================== */
17 package cn. jayslong. convert;
18
19 import java. io. FileInputStream;
20 import java. io. FileNotFoundException;
21 import java. io. IOException;
22 import java. io. PrintStream;
23 import java. util. ArrayList;
24
25 import org. apache. poi. hssf. eventusermodel. FormatTrackingHSSFListener;
26 import org. apache. poi. hssf. eventusermodel. HSSFEventFactory;
27 import org. apache. poi. hssf. eventusermodel. HSSFListener;
28 import org. apache. poi. hssf. eventusermodel. HSSFRequest;
29 import org. apache. poi. hssf. eventusermodel. MissingRecordAwareHSSFListener;
30 import org. apache. poi. hssf. eventusermodel. EventWorkbookBuilder. SheetRecordCollectingListener;
31 import org. apache. poi. hssf. eventusermodel. dummyrecord. LastCellOfRowDummyRecord;
32 import org. apache. poi. hssf. eventusermodel. dummyrecord. MissingCellDummyRecord;
33 import org. apache. poi. hssf. model. HSSFFormulaParser;
34 import org. apache. poi. hssf. record. BOFRecord;
35 import org. apache. poi. hssf. record. BlankRecord;
36 import org. apache. poi. hssf. record. BoolErrRecord;
37 import org. apache. poi. hssf. record. BoundSheetRecord;
38 import org. apache. poi. hssf. record. FormulaRecord;
39 import org. apache. poi. hssf. record. LabelRecord;
40 import org. apache. poi. hssf. record. LabelSSTRecord;
41 import org. apache. poi. hssf. record. NoteRecord;
42 import org. apache. poi. hssf. record. NumberRecord;
43 import org. apache. poi. hssf. record. RKRecord;
44 import org. apache. poi. hssf. record. Record;
45 import org. apache. poi. hssf. record. SSTRecord;
46 import org. apache. poi. hssf. record. StringRecord;
47 import org. apache. poi. hssf. usermodel. HSSFWorkbook;
48 import org. apache. poi. poifs. filesystem. POIFSFileSystem;
49
50 /***//**
51 * a xls-> CSV processor, that uses the MissingRecordAware EventModel code
52 * ensure it outputs all columns and rows.
53 *
54 * @ author Nick Burch
55 */
56 public class XLS2CSV implements HSSFListener {
57 private int minColumns;
58 private POIFSFileSystem fs;
59 private PrintStream output;
60
61 private int lastRowNumber;
62 private int lastColumnNumber;
63
64/*** // ** shocould we output the formula, or the value it has? */
65 private boolean outputFormulaValues = true;
66
67/**** // ** For parsing Formulas */
68 private SheetRecordCollectingListener workbookBuildingListener;
69 private HSSFWorkbook stubWorkbook;
70
71 // Records we pick up as we process
72 private SSTRecord sstRecord;
73 private FormatTrackingHSSFListener formatListener;
74
75/*** // ** So we known which sheet were on */
76 private int sheetIndex =-1;
77 private BoundSheetRecord [] orderedBSRs;
78 private ArrayList boundSheetRecords = new ArrayList ();
79
80 // For handling formulas with string results
81 private int nextRow;
82 private int nextColumn;
83 private boolean outputNextStringRecord;
84
85 private final String OUTPUT_CHARSET = "GBK ";
86
87 /***//**
88 * Creates a new XLS-> CSV converter
89 *
90 * @ param fs
91 * The POIFSFileSystem to process
92 * @ param output
93 * The PrintStream to output the CSV
94 * @ param minColumns
95 * The minimum number of columns to output, or-1 for no minimum
96 */
97 public XLS2CSV (POIFSFileSystem fs, PrintStream output, int minColumns ){
98 this. fs = fs;
99 this. output = output;
100 this. minColumns = minColumns;
101}
102
103 public XLS2CSV (String inputFilePath, String outputFilePath) throws Exception {
104 fs = new POIFSFileSystem (new FileInputStream (inputFilePath ));
105 output = new PrintStream (outputFilePath, OUTPUT_CHARSET );
106 minColumns =-1;
107}
108
109 /***//**
110 * Creates a new XLS-> CSV converter
111 *
112 * @ param filename
113 * The file to process
114 * @ param minColumns
115 * The minimum number of columns to output, or-1 for no minimum
116 * @ t