My favorites | Sign in
Project Logo
                
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
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */

package org.software.bird.som.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Class description goes here.
*
* @author <a href="mailto:cyyan@isoftstone.com">cyyan</a>
* @version $Id: ExcelUtil.java,v0.1 2007-12-6 ÏÂÎç01:46:38 cyyan Exp$
*/
public class ExcelUtil {

final public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

public static HSSFRow getSheetTitleRow(HSSFSheet sheet, int titleRowIndex) {
return sheet.getRow(titleRowIndex);
}



public static boolean isEmptyRow(HSSFRow row) {
boolean result = true;
if (row == null) {
result = true;
} else {
for (short i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
HSSFCell cell = row.getCell(i);
result &= isEmptyCell(cell);
if (!result) {
break;
}
}
}
return result;
}

public static boolean isEmptyCell(HSSFCell cell) {
String cellStr = null;
cellStr = cell2string(cell, null);
return cellStr == null || cellStr.trim().equals("");
}

/**
* ¶Ôµ¥Ôª¸ñµÄÊý¾Ýת»»³É×Ö·û´®
*
* @param cell
* @return
*/
public static String cell2string(HSSFCell cell, HSSFFormulaEvaluator evaluator) {
if (cell == null) {
return null;
}
String str = null;
final int cellType = cell.getCellType();

switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
str = "" + cell.getRichStringCellValue().getString().trim();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
str = "" + dateFormat.format(cell.getDateCellValue());
} else {
//modify by cyyan 2008-09-23 19:17:28
//excelÖÐÊýֵת»»¹ýÀ´Ê±²ÉÓÃE¼ÆÊý·¨, µ¼ÖºóÃæµÄ¹æÔòУÑéʧ°Ü;
//Ϊ´Ë½øÐаÑE¼ÆÊý·¨×ª»»ÆÕͨ¼ÆÊý·¨, ²¢ÇÒʹÓÃСÊýµãºó15λ, (15λÊÇСÊýµÄ×î´ó¾«¶È, Äܹ»±£Ö¤·Ç±¾×é¼þ´øÀ´µÄÎó²î)
// ʹÓÃ15λСÊýºó, »á´øÀ´¶à¸öĩβµÄ0, ÏàÁÚµÄÒ»ÌõÓï¾äÊÇÈ¥µôÕâЩĩβ0
str = "" + new BigDecimal(String.valueOf(cell.getNumericCellValue())).setScale(15,BigDecimal.ROUND_HALF_UP);
str = str.replaceAll("\\.0*$", "");
}
break;
case HSSFCell.CELL_TYPE_BLANK:
str = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
str = "" + cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
str = "" + cell.getErrorCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
if (evaluator == null) {
str = "" + cell.getRichStringCellValue().getString();
} else {
str = "" + evaluator.evaluate(cell).getNumberValue();
}

break;
}

return (str == null || str.trim().equals("")) ? null : str.trim();
}



public static HSSFFormulaEvaluator getFormulaEvaluator(HSSFSheet sheet, HSSFWorkbook workbook, HSSFRow row) {
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, workbook);
evaluator.setCurrentRow(row);
return evaluator;
}
}
Show details Hide details

Change log

r46 by yanchangyou on Sep 23, 2008   Diff
修改:由1.12-1版本的修改带来的潜在bug, 1.12-1只支持2位小数,
修改后能支持15位小数,
15位小数是, double类型和excel能支持的最大精度
Go to: 
Sign in to write a code review

Older revisions

r35 by yanchangyou on Aug 21, 2008   Diff
使用License 2.0 of the Apache Software
Foundation (ASF)
r8 by yanchangyou on Jul 15, 2008   Diff
Added items remotely

E:\cyyan\开发\bird 1.0\config
E:\cyyan\开发\bird 1.0\doc
E:\cyyan\开发\bird 1.0\lib
...
All revisions of this file

File info

Size: 4162 bytes, 125 lines
Hosted by Google Code