1+ package info .xiaomo .core .untils ;
2+
3+ import info .xiaomo .core .other .CountryModel ;
4+ import org .apache .poi .hssf .usermodel .*;
5+ import org .apache .poi .poifs .filesystem .POIFSFileSystem ;
6+
7+ import java .io .*;
8+ import java .text .DecimalFormat ;
9+ import java .text .SimpleDateFormat ;
10+ import java .util .ArrayList ;
11+ import java .util .Arrays ;
12+ import java .util .Date ;
13+ import java .util .List ;
14+
15+ public class ExcelUtil {
16+
17+ public static void main (String [] args ) throws Exception {
18+ List <List <String >> listData = getListData ("E:\\ thinkpage_cities.xls" , 1 );
19+ List <CountryModel > list = new ArrayList <>();
20+ for (List <String > listDatum : listData ) {
21+ CountryModel model = new CountryModel ();
22+ model .setCityName (listDatum .get (0 ));
23+ model .setCityNameEN (listDatum .get (1 ));
24+ list .add (model );
25+ }
26+ System .out .println (list );
27+ }
28+
29+ public static List <List <String >> getListData (String path , int ignoreRows ) throws IOException {
30+ File file = new File (path );
31+ String [][] result = getData (file , ignoreRows );
32+ List <List <String >> lists = new ArrayList <>();
33+ for (String [] aResult : result ) {
34+ List <String > list = new ArrayList <>();
35+ for (String anAResult : aResult ) {
36+ if (!anAResult .equals ("" )) {
37+ list .add (anAResult );
38+ }
39+ }
40+ lists .add (list );
41+ }
42+ return lists ;
43+ }
44+
45+ /**
46+ * 读取Excel的内容,第一维数组存储的是一行中格列的值,二维数组存储的是多少个行
47+ *
48+ * @param file 读取数据的源Excel
49+ * @param ignoreRows 读取数据忽略的行数,比喻行头不需要读入 忽略的行数为1
50+ * @return 读出的Excel中数据的内容
51+ * @throws FileNotFoundException
52+ * @throws IOException
53+ */
54+ private static String [][] getData (File file , int ignoreRows )
55+ throws IOException {
56+ List <String []> result = new ArrayList <>();
57+ int rowSize = 0 ;
58+ BufferedInputStream in = new BufferedInputStream (new FileInputStream (
59+ file ));
60+ // 打开HSSFWorkbook
61+ POIFSFileSystem fs = new POIFSFileSystem (in );
62+ HSSFWorkbook wb = new HSSFWorkbook (fs );
63+ HSSFCell cell ;
64+ for (int sheetIndex = 0 ; sheetIndex < wb .getNumberOfSheets (); sheetIndex ++) {
65+ HSSFSheet st = wb .getSheetAt (sheetIndex );
66+ // 第一行为标题,不取
67+ for (int rowIndex = ignoreRows ; rowIndex <= st .getLastRowNum (); rowIndex ++) {
68+ HSSFRow row = st .getRow (rowIndex );
69+ if (row == null ) {
70+ continue ;
71+ }
72+ int tempRowSize = row .getLastCellNum () + 1 ;
73+ if (tempRowSize > rowSize ) {
74+ rowSize = tempRowSize ;
75+ }
76+ String [] values = new String [rowSize ];
77+ Arrays .fill (values , "" );
78+ boolean hasValue = false ;
79+ for (short columnIndex = 0 ; columnIndex <= row .getLastCellNum (); columnIndex ++) {
80+ String value = "" ;
81+ cell = row .getCell (columnIndex );
82+ if (cell != null ) {
83+ switch (cell .getCellType ()) {
84+ case HSSFCell .CELL_TYPE_STRING :
85+ value = cell .getStringCellValue ();
86+ break ;
87+ case HSSFCell .CELL_TYPE_NUMERIC :
88+ if (HSSFDateUtil .isCellDateFormatted (cell )) {
89+ Date date = cell .getDateCellValue ();
90+ if (date != null ) {
91+ value = new SimpleDateFormat ("yyyy-MM-dd" )
92+ .format (date );
93+ } else {
94+ value = "" ;
95+ }
96+ } else {
97+ value = new DecimalFormat ("0" ).format (cell
98+ .getNumericCellValue ());
99+ }
100+ break ;
101+ case HSSFCell .CELL_TYPE_FORMULA :
102+ // 导入时如果为公式生成的数据则无值
103+ if (!cell .getStringCellValue ().equals ("" )) {
104+ value = cell .getStringCellValue ();
105+ } else {
106+ value = cell .getNumericCellValue () + "" ;
107+ }
108+ break ;
109+ case HSSFCell .CELL_TYPE_BLANK :
110+ break ;
111+ case HSSFCell .CELL_TYPE_ERROR :
112+ value = "" ;
113+ break ;
114+ case HSSFCell .CELL_TYPE_BOOLEAN :
115+ value = (cell .getBooleanCellValue () ? "Y"
116+ : "N" );
117+ break ;
118+ default :
119+ value = "" ;
120+ }
121+ }
122+ if (columnIndex == 0 && value .trim ().equals ("" )) {
123+ break ;
124+ }
125+ values [columnIndex ] = rightTrim (value );
126+ hasValue = true ;
127+ }
128+
129+ if (hasValue ) {
130+ result .add (values );
131+ }
132+ }
133+ }
134+ in .close ();
135+ String [][] returnArray = new String [result .size ()][rowSize ];
136+ for (int i = 0 ; i < returnArray .length ; i ++) {
137+ returnArray [i ] = result .get (i );
138+ }
139+ return returnArray ;
140+ }
141+
142+ /**
143+ * 去掉字符串右边的空格
144+ *
145+ * @param str 要处理的字符串
146+ * @return 处理后的字符串
147+ */
148+ public static String rightTrim (String str ) {
149+ if (str == null ) {
150+ return "" ;
151+ }
152+ int length = str .length ();
153+ for (int i = length - 1 ; i >= 0 ; i --) {
154+ if (str .charAt (i ) != 0x20 ) {
155+ break ;
156+ }
157+ length --;
158+ }
159+ return str .substring (0 , length );
160+ }
161+ }
0 commit comments