JAVA POI 直接展现EXCEL成HTML (代码,测试中)
使用POI读取EXCEL中的信息生成HTML
第一个类 ,转换EXCEL颜色
![]()
![]()
1
public class ExcelColorHelper
2![]()
![]()
{
3
public ExcelColorHelper()
4![]()
{
5
}
6![]()
7
public static String getHex(String strHex)
8![]()
{
9
if(strHex.length() > 0)
10![]()
{
11
String[] a = strHex.split(":");
12
strHex = "";
13
for(int n = 0;n < a.length;n++)
14![]()
{
15
if(a[n].length() > 0)
16![]()
{
17
if(a[n].length() < 2)
18![]()
{
19
strHex += "0" + a[n];
20
}
21
else
22![]()
23![]()
{
24
strHex += a[n].substring(0,2);
25
}
26
}
27
}
28
}
29
return strHex.length() > 0 ? strHex : null;
30![]()
31
}
32![]()
33
public static String getHex(HSSFColor c)
34![]()
{
35
return getHex(c == null ? "" : c.getHexString());
36
}
37![]()
38
public static String getHex(int nColor)
39![]()
{
40
String strHex = "";
41
if(nColor == HSSFColor.LIGHT_CORNFLOWER_BLUE.index)
42![]()
{
43
strHex = HSSFColor.LIGHT_CORNFLOWER_BLUE.hexString;
44
}
45
if(nColor == HSSFColor.ROYAL_BLUE.index)
46![]()
{
47
strHex = HSSFColor.ROYAL_BLUE.hexString;
48
}
49
if(nColor == HSSFColor.CORAL.index)
50![]()
{
51
strHex = HSSFColor.CORAL.hexString;
52
}
53
if(nColor == HSSFColor.ORCHID.index)
54![]()
{
55
strHex = HSSFColor.ORCHID.hexString;
56
}
57
if(nColor == HSSFColor.MAROON.index)
58![]()
{
59
strHex = HSSFColor.MAROON.hexString;
60
}
61
if(nColor == HSSFColor.LEMON_CHIFFON.index)
62![]()
{
63
strHex = HSSFColor.LEMON_CHIFFON.hexString;
64
}
65
if(nColor == HSSFColor.CORNFLOWER_BLUE.index)
66![]()
{
67
strHex = HSSFColor.CORNFLOWER_BLUE.hexString;
68
}
69
if(nColor == HSSFColor.WHITE.index)
70![]()
{
71
strHex = HSSFColor.WHITE.hexString;
72
}
73
if(nColor == HSSFColor.LAVENDER.index)
74![]()
{
75
strHex = HSSFColor.LAVENDER.hexString;
76
}
77
if(nColor == HSSFColor.PALE_BLUE.index)
78![]()
{
79
strHex = HSSFColor.PALE_BLUE.hexString;
80
}
81
if(nColor == HSSFColor.LIGHT_TURQUOISE.index)
82![]()
{
83
strHex = HSSFColor.LIGHT_TURQUOISE.hexString;
84
}
85
if(nColor == HSSFColor.LIGHT_GREEN.index)
86![]()
{
87
strHex = HSSFColor.LIGHT_GREEN.hexString;
88
}
89
if(nColor == HSSFColor.LIGHT_YELLOW.index)
90![]()
{
91
strHex = HSSFColor.LIGHT_YELLOW.hexString;
92
}
93
if(nColor == HSSFColor.TAN.index)
94![]()
{
95
strHex = HSSFColor.TAN.hexString;
96
}
97
if(nColor == HSSFColor.ROSE.index)
98![]()
{
99
strHex = HSSFColor.ROSE.hexString;
100
}
101
if(nColor == HSSFColor.GREY_25_PERCENT.index)
102![]()
{
103
strHex = HSSFColor.GREY_25_PERCENT.hexString;
104
}
105
if(nColor == HSSFColor.PLUM.index)
106![]()
{
107
strHex = HSSFColor.PLUM.hexString;
108
}
109
if(nColor == HSSFColor.SKY_BLUE.index)
110![]()
{
111
strHex = HSSFColor.SKY_BLUE.hexString;
112
}
113
if(nColor == HSSFColor.TURQUOISE.index)
114![]()
{
115
strHex = HSSFColor.TURQUOISE.hexString;
116
}
117
if(nColor == HSSFColor.BRIGHT_GREEN.index)
118![]()
{
119
strHex = HSSFColor.BRIGHT_GREEN.hexString;
120
}
121
if(nColor == HSSFColor.YELLOW.index)
122![]()
{
123
strHex = HSSFColor.YELLOW.hexString;
124
}
125
if(nColor == HSSFColor.GOLD.index)
126![]()
{
127
strHex = HSSFColor.GOLD.hexString;
128
}
129
if(nColor == HSSFColor.PINK.index)
130![]()
{
131
strHex = HSSFColor.PINK.hexString;
132
}
133
if(nColor == HSSFColor.GREY_40_PERCENT.index)
134![]()
{
135
strHex = HSSFColor.GREY_40_PERCENT.hexString;
136
}
137
if(nColor == HSSFColor.VIOLET.index)
138![]()
{
139
strHex = HSSFColor.VIOLET.hexString;
140
}
141
if(nColor == HSSFColor.LIGHT_BLUE.index)
142![]()
{
143
strHex = HSSFColor.LIGHT_BLUE.hexString;
144
}
145
if(nColor == HSSFColor.AQUA.index)
146![]()
{
147
strHex = HSSFColor.AQUA.hexString;
148
}
149
if(nColor == HSSFColor.SEA_GREEN.index)
150![]()
{
151
strHex = HSSFColor.SEA_GREEN.hexString;
152
}
153
if(nColor == HSSFColor.LIME.index)
154![]()
{
155
strHex = HSSFColor.LIME.hexString;
156
}
157
if(nColor == HSSFColor.LIGHT_ORANGE.index)
158![]()
{
159
strHex = HSSFColor.LIGHT_ORANGE.hexString;
160
}
161
if(nColor == HSSFColor.RED.index)
162![]()
{
163
strHex = HSSFColor.RED.hexString;
164
}
165
if(nColor == HSSFColor.GREY_50_PERCENT.index)
166![]()
{
167
strHex = HSSFColor.GREY_50_PERCENT.hexString;
168
}
169
if(nColor == HSSFColor.BLUE_GREY.index)
170![]()
{
171
strHex = HSSFColor.BLUE_GREY.hexString;
172
}
173
if(nColor == HSSFColor.BLUE.index)
174![]()
{
175
strHex = HSSFColor.BLUE.hexString;
176
}
177
if(nColor == HSSFColor.TEAL.index)
178![]()
{
179
strHex = HSSFColor.TEAL.hexString;
180
}
181
if(nColor == HSSFColor.GREEN.index)
182![]()
{
183
strHex = HSSFColor.GREEN.hexString;
184
}
185
if(nColor == HSSFColor.DARK_YELLOW.index)
186![]()
{
187
strHex = HSSFColor.DARK_YELLOW.hexString;
188
}
189
if(nColor == HSSFColor.ORANGE.index)
190![]()
{
191
strHex = HSSFColor.ORANGE.hexString;
192
}
193
if(nColor == HSSFColor.DARK_RED.index)
194![]()
{
195
strHex = HSSFColor.DARK_RED.hexString;
196
}
197
if(nColor == HSSFColor.GREY_80_PERCENT.index)
198![]()
{
199
strHex = HSSFColor.GREY_80_PERCENT.hexString;
200
}
201
if(nColor == HSSFColor.INDIGO.index)
202![]()
{
203
strHex = HSSFColor.INDIGO.hexString;
204
}
205
if(nColor == HSSFColor.DARK_BLUE.index)
206![]()
{
207
strHex = HSSFColor.DARK_BLUE.hexString;
208
}
209
if(nColor == HSSFColor.DARK_TEAL.index)
210![]()
{
211
strHex = HSSFColor.DARK_TEAL.hexString;
212
}
213
if(nColor == HSSFColor.DARK_GREEN.index)
214![]()
{
215
strHex = HSSFColor.DARK_GREEN.hexString;
216
}
217
if(nColor == HSSFColor.BROWN.index)
218![]()
{
219
strHex = HSSFColor.BROWN.hexString;
220
}
221
if(nColor == HSSFColor.BLACK.index)
222![]()
{
223
strHex = HSSFColor.BLACK.hexString;
224
}
225
return getHex(strHex);
226
}
227![]()
228
}第二个类帮助类
![]()
![]()
1
public class ExcelHelper
2![]()
![]()
{
3
public ExcelHelper()
4![]()
{
5
}
6
public static String convertString2Html(String strMsg)
7![]()
{
8
if(strMsg == null || strMsg.length() < 1)
9![]()
{
10
return "";
11
}
12
String str = "";
13
for(int i = 0;i < strMsg.length();i++)
14![]()
{
15
char c = strMsg.charAt(i);
16
if(c == '\r')
17![]()
{
18
continue;
19
}
20
else if(c == '\n')
21![]()
{
22
str += "<br>";
23
}
24
else if(c == '\t')
25![]()
{
26
str += " ";
27
}
28
else if(c == ' ')
29![]()
{
30
str += " ";
31
}
32
else
33![]()
{
34
str += c;
35
}
36
}
37
return str;
38
}
39![]()
40
public static String getCellBackgroundColorInHtml(HSSFWorkbook wb,HSSFCell cell)
41![]()
{
42
if(null == wb || null == cell)
43![]()
{
44
return null;
45
}
46
return getCellBackgroundColorInHtml( wb.getCustomPalette(),cell);
47
}
48
public static String getCellBackgroundColorInHtml(HSSFPalette pa,HSSFCell cell)
49![]()
{
50
if(null == pa || null == cell)
51![]()
{
52
return null;
53
}
54
String cBack = ExcelColorHelper.getHex(pa == null ? null : pa.getColor(cell.getCellStyle().getFillForegroundColor()));
55
if(null == cBack)
56![]()
{
57
cBack = ExcelColorHelper.getHex(pa == null ? null : pa.getColor(cell.getCellStyle().getFillBackgroundColor()));
58
}
59
if(null == cBack)
60![]()
{
61
cBack = ExcelColorHelper.getHex(cell.getCellStyle().getFillBackgroundColor());
62
}
63
return cBack;
64
}
65![]()
66
public static int getExcelHeaderColumn(String strColumnHeader)
67![]()
{
68
if(null == strColumnHeader)
69![]()
{
70
return -1;
71
}
72
int nCol = 0;
73
if(strColumnHeader.length() > 1)
74![]()
{
75
nCol = ((int) (strColumnHeader.charAt(0)) - (int) 'a' + 1) * 26 +
76
((int) (strColumnHeader.charAt(1)) - (int) 'a');
77
}
78
else
79![]()
{
80
nCol = (int) (strColumnHeader.charAt(0)) - (int) 'a';
81
}
82
return nCol;
83
}
84
}两个辅助类
![]()
![]()
1
public class ExcelCellValue
2![]()
![]()
{
3
private int m_nRowID = -1;
4
private int m_nColID =-1;
5
private Object m_objValue=null;
6
public ExcelCellValue(int nRow,int nCell,Object obj)
7![]()
{
8
m_nRowID = nRow;
9
m_nColID = nCell;
10
m_objValue= obj;
11
}
12
public int getRow()
13![]()
{
14
return m_nRowID;
15
}
16
public int getCol()
17![]()
{
18
return m_nColID;
19
}
20
public Object getValue()
21![]()
{
22
return m_objValue;
23
}
24
public boolean isMatch(int nRow,int nCell)
25![]()
{
26
return m_nRowID ==nRow && m_nColID == nCell;
27
}
28
public String toString()
29![]()
{
30
return null == m_objValue?null:m_objValue.toString();
31
}
32
}
![]()
![]()
1
public class ExcelCellValueCollection
2![]()
![]()
{
3
private java.util.Vector vect =new java.util.Vector();
4
public ExcelCellValueCollection()
5![]()
{
6
}
7
public void clear()
8![]()
{
9
vect.clear();
10
}
11
public int size()
12![]()
{
13
return vect.size();
14
}
15
public void addElement(ExcelCellValue cell)
16![]()
{
17
if(null!=cell)
18![]()
{
19
vect.addElement(cell);
20
}
21
}
22
public void removeElementAt(int nIndex)
23![]()
{
24
vect.removeElementAt(nIndex);
25
}
26
public void insertElementAt(ExcelCellValue cell,int nIndex)
27![]()
{
28
vect.insertElementAt(cell,nIndex);
29
}
30
public ExcelCellValue elementAt(int nIndex)
31![]()
{
32
return (ExcelCellValue)vect.elementAt(nIndex);
33
}
34
public Object getValueAt(int nRow,int nCell)
35![]()
{
36
for(int i =0;i<vect.size();i++)
37![]()
{
38
ExcelCellValue cell =this.elementAt(i);
39
if(null==cell)
40![]()
{
41
continue;
42
}
43
if(cell.isMatch(nRow,nCell))
44![]()
{
45
return cell.getValue();
46
}
47
}
48
return null;
49
}
50![]()
51
}
最后主类 ProcessSheet
![]()
![]()
1
public class ExcelToHtml
2![]()
![]()
{
3
private POIFSFileSystem m_excelFile = null;
4
private HSSFWorkbook m_workBook = null;
5
private HSSFPalette m_palette=null;
6
public ExcelToHtml()
7![]()
{
8
}
9![]()
10
public void open(String strFileName)
11
throws Exception
12![]()
{
13
m_excelFile = new POIFSFileSystem(new FileInputStream(strFileName));
14
m_workBook = new HSSFWorkbook(m_excelFile);
15
m_palette = m_workBook.getCustomPalette();
16
}
17
public void close()
18![]()
{
19
m_palette=null;
20
m_workBook=null;
21
m_excelFile=null;
22
}
23
public int getSheetIndexFromName(String strName)
24![]()
{
25
if(null == m_workBook)
26![]()
{
27
return -1;
28
}
29
for(int nSheet = 0; nSheet < m_workBook.getNumberOfSheets();nSheet++)
30![]()
{
31
if( m_workBook.getSheetName(nSheet).equals(strName))
32![]()
{
33
return nSheet;
34
}
35
}
36
return -1;
37
}
38
public int getNumberOfSheets()
39![]()
{
40
return null!=m_workBook?m_workBook.getNumberOfSheets():0;
41
}
42
private String getCellValue(HSSFCell cell)
43![]()
{
44
if(null == cell)
45![]()
{
46
return "";
47
}
48
int nCellType = cell.getCellType();
49
String strValue = "";
50
switch(nCellType)
51![]()
{
52
case HSSFCell.CELL_TYPE_BLANK:
53
strValue = "";
54
break;
55
case HSSFCell.CELL_TYPE_BOOLEAN:
56
strValue = cell.getBooleanCellValue() ? "true" : "false";
57
break;
58
case HSSFCell.CELL_TYPE_ERROR:
59
strValue = "Error :" + cell.getErrorCellValue();
60
break;
61
case HSSFCell.CELL_TYPE_FORMULA:
62
strValue = cell.getCellFormula();
63
break;
64
case HSSFCell.CELL_TYPE_NUMERIC:
65
strValue = get4s5r(cell.getNumericCellValue(),2);
66
break;
67
case HSSFCell.CELL_TYPE_STRING:
68
strValue = cell.getStringCellValue();
69
break;
70
default:
71
strValue = "";
72
}
73![]()
74
return strValue;
75
}
76![]()
77
public static String get4s5r(double f,long nBase)
78![]()
{
79
nBase = java.lang.Math.round(java.lang.Math.pow(10,nBase));
80
f = java.lang.Math.round(f * nBase);
81
f = f / nBase;
82
if(f == 0)
83![]()
{
84
return "0";
85
}
86
String strBak = java.text.NumberFormat.getInstance().format(f);
87
String str = "";
88
for(int i = 0;i < strBak.length();i++)
89![]()
{
90
if(strBak.charAt(i) != ',')
91![]()
{
92
str += strBak.charAt(i);
93
}
94
}
95
return str;
96
}
97![]()
98
private boolean isResideInRegion(HSSFSheet sheet,int nRow,short nCol)
99![]()
{
100
if(sheet == null)
101![]()
{
102
return false;
103
}
104
for(int nM = 0;nM < sheet.getNumMergedRegions();nM++)
105![]()
{
106
Region region = sheet.getMergedRegionAt(nM);
107
if(region.contains(nRow,nCol))
108![]()
{
109
return true;
110
}
111
}
112
return false;
113![]()
114
}
115![]()
116
private Region getTopRegion(HSSFSheet sheet,int nRow,short nCol)
117![]()
{
118
if(sheet == null)
119![]()
{
120
return null;
121
}
122
for(int nM = 0;nM < sheet.getNumMergedRegions();nM++)
123![]()
{
124
Region region = sheet.getMergedRegionAt(nM);
125
if(region.contains(nRow,nCol))
126![]()
{
127
if(region.getRowFrom() == nRow &&
128
region.getColumnFrom() == nCol)
129![]()
{
130
return region;
131
}
132
}
133
}
134
return null;
135
}
136![]()
137
public StringBuffer processSheet(int nSheetIndex,ExcelCellValueCollection cellValues)
138
throws Exception
139![]()
{
140
if(null == m_workBook)
141![]()
{
142
return null;
143
}
144
HSSFSheet sheet = m_workBook.getSheetAt(nSheetIndex);
145
if(null == sheet)
146![]()
{
147
return null;
148
}
149
StringBuffer strRet =new StringBuffer();
150
strRet.append("<table border='1'" +
151
" cellspacing='1'" +
152
" style='border-collapse: collapse'>");
153
for(int nRow = sheet.getFirstRowNum();nRow < sheet.getLastRowNum();nRow++)
154![]()
{
155
HSSFRow row = sheet.getRow(nRow);
156
if(null == row)
157![]()
{
158
continue;
159
}
160
strRet.append( "<tr height='" + row.getHeightInPoints() + "pt'>\n");
161![]()
162
for(short nCell = 0;nCell < row.getLastCellNum();nCell++)
163![]()
{
164
HSSFCell cell = row.getCell(nCell);
165
if(cell == null)
166![]()
{
167
continue;
168
}
169
String strColSpanRowSpan = "";
170
//检查Rowspan 和ColSpan
171
Region region = this.getTopRegion(sheet,nRow,nCell);
172
if(null != region)
173![]()
{
174
int nColSpan = region.getColumnTo() - region.getColumnFrom() + 1;
175
int nRowSpan = region.getRowTo() - region.getRowFrom() + 1;
176
strColSpanRowSpan = "";
177
if(nColSpan > 1)
178![]()
{
179
strColSpanRowSpan += " colspan=" + nColSpan;
180
}
181
if(nRowSpan > 1)
182![]()
{
183
strColSpanRowSpan += " rowspan=" + nRowSpan;
184
}
185
}
186
else if(this.isResideInRegion(sheet,nRow,nCell))
187![]()
{
188
continue;
189
}
190
String cBack = ExcelHelper.getCellBackgroundColorInHtml(m_palette,cell);
191
strRet.append( "<td " + strColSpanRowSpan);
192
if(null != cBack)
193![]()
{
194
strRet.append(" bgcolor=#" + cBack);
195
}
196
if(cell.getCellStyle().getAlignment() == HSSFCellStyle.ALIGN_RIGHT)
197![]()
{
198
strRet.append(" align=right");
199
}
200
else if(cell.getCellStyle().getAlignment() == HSSFCellStyle.ALIGN_CENTER)
201![]()
{
202
strRet.append(" align=center");
203
}
204![]()
205
String strCellValue = getCellValue(cell);
206
if(null!=cellValues)
207![]()
{
208
Object obj = cellValues.getValueAt(nRow,nCell);
209
if(null!=obj)
210![]()
{
211
strCellValue = obj.toString();
212
}
213
}
214
strCellValue = ExcelHelper.convertString2Html(strCellValue);
215
HSSFFont font = m_workBook.getFontAt(cell.getCellStyle().getFontIndex());
216
if(null != font)
217![]()
{
218
if(font.getBoldweight() == font.BOLDWEIGHT_BOLD)
219![]()
{
220
strCellValue = "<b>" + strCellValue + "</b>";
221
}
222
String cFore = ExcelColorHelper.getHex(font.getColor());
223
if(null == cFore)
224![]()
{
225
cFore = ExcelColorHelper.getHex(m_palette == null ? null :
226
m_palette.getColor(font.getColor()));
227
}
228
if(null != cFore)
229![]()
{
230
strCellValue = "<font style='font-size:" + font.getFontHeightInPoints() + "pt' face='" + font.getFontName() + "' color=#" + cFore + ">" + strCellValue + "</font>";
231
}
232
}
233
strRet.append( ">");
234
strRet.append( strCellValue + "</td>");
235
}
236
strRet.append( "\n");
237
}
238
strRet.append("</tr>\n");
239
strRet.append("</table>");
240
return strRet;
241
}
242![]()
243
public static void main(String[] strargvs)
244
throws Exception
245![]()
{
246
ExcelToHtml excel = new ExcelToHtml();
247
excel.open("d:\\jituan1231.xls");
248
}
249
}
没有解决问题:
EXCEL隐藏列问题
EXCEL列宽问题
第一个类 ,转换EXCEL颜色
1
public class ExcelColorHelper2


{3
public ExcelColorHelper()4

{5
}6

7
public static String getHex(String strHex)8

{9
if(strHex.length() > 0)10

{11
String[] a = strHex.split(":");12
strHex = "";13
for(int n = 0;n < a.length;n++)14

{15
if(a[n].length() > 0)16

{17
if(a[n].length() < 2)18

{19
strHex += "0" + a[n];20
}21
else22

23

{24
strHex += a[n].substring(0,2);25
}26
}27
}28
}29
return strHex.length() > 0 ? strHex : null;30

31
}32

33
public static String getHex(HSSFColor c)34

{35
return getHex(c == null ? "" : c.getHexString());36
}37

38
public static String getHex(int nColor)39

{40
String strHex = "";41
if(nColor == HSSFColor.LIGHT_CORNFLOWER_BLUE.index)42

{43
strHex = HSSFColor.LIGHT_CORNFLOWER_BLUE.hexString;44
}45
if(nColor == HSSFColor.ROYAL_BLUE.index)46

{47
strHex = HSSFColor.ROYAL_BLUE.hexString;48
}49
if(nColor == HSSFColor.CORAL.index)50

{51
strHex = HSSFColor.CORAL.hexString;52
}53
if(nColor == HSSFColor.ORCHID.index)54

{55
strHex = HSSFColor.ORCHID.hexString;56
}57
if(nColor == HSSFColor.MAROON.index)58

{59
strHex = HSSFColor.MAROON.hexString;60
}61
if(nColor == HSSFColor.LEMON_CHIFFON.index)62

{63
strHex = HSSFColor.LEMON_CHIFFON.hexString;64
}65
if(nColor == HSSFColor.CORNFLOWER_BLUE.index)66

{67
strHex = HSSFColor.CORNFLOWER_BLUE.hexString;68
}69
if(nColor == HSSFColor.WHITE.index)70

{71
strHex = HSSFColor.WHITE.hexString;72
}73
if(nColor == HSSFColor.LAVENDER.index)74

{75
strHex = HSSFColor.LAVENDER.hexString;76
}77
if(nColor == HSSFColor.PALE_BLUE.index)78

{79
strHex = HSSFColor.PALE_BLUE.hexString;80
}81
if(nColor == HSSFColor.LIGHT_TURQUOISE.index)82

{83
strHex = HSSFColor.LIGHT_TURQUOISE.hexString;84
}85
if(nColor == HSSFColor.LIGHT_GREEN.index)86

{87
strHex = HSSFColor.LIGHT_GREEN.hexString;88
}89
if(nColor == HSSFColor.LIGHT_YELLOW.index)90

{91
strHex = HSSFColor.LIGHT_YELLOW.hexString;92
}93
if(nColor == HSSFColor.TAN.index)94

{95
strHex = HSSFColor.TAN.hexString;96
}97
if(nColor == HSSFColor.ROSE.index)98

{99
strHex = HSSFColor.ROSE.hexString;100
}101
if(nColor == HSSFColor.GREY_25_PERCENT.index)102

{103
strHex = HSSFColor.GREY_25_PERCENT.hexString;104
}105
if(nColor == HSSFColor.PLUM.index)106

{107
strHex = HSSFColor.PLUM.hexString;108
}109
if(nColor == HSSFColor.SKY_BLUE.index)110

{111
strHex = HSSFColor.SKY_BLUE.hexString;112
}113
if(nColor == HSSFColor.TURQUOISE.index)114

{115
strHex = HSSFColor.TURQUOISE.hexString;116
}117
if(nColor == HSSFColor.BRIGHT_GREEN.index)118

{119
strHex = HSSFColor.BRIGHT_GREEN.hexString;120
}121
if(nColor == HSSFColor.YELLOW.index)122

{123
strHex = HSSFColor.YELLOW.hexString;124
}125
if(nColor == HSSFColor.GOLD.index)126

{127
strHex = HSSFColor.GOLD.hexString;128
}129
if(nColor == HSSFColor.PINK.index)130

{131
strHex = HSSFColor.PINK.hexString;132
}133
if(nColor == HSSFColor.GREY_40_PERCENT.index)134

{135
strHex = HSSFColor.GREY_40_PERCENT.hexString;136
}137
if(nColor == HSSFColor.VIOLET.index)138

{139
strHex = HSSFColor.VIOLET.hexString;140
}141
if(nColor == HSSFColor.LIGHT_BLUE.index)142

{143
strHex = HSSFColor.LIGHT_BLUE.hexString;144
}145
if(nColor == HSSFColor.AQUA.index)146

{147
strHex = HSSFColor.AQUA.hexString;148
}149
if(nColor == HSSFColor.SEA_GREEN.index)150

{151
strHex = HSSFColor.SEA_GREEN.hexString;152
}153
if(nColor == HSSFColor.LIME.index)154

{155
strHex = HSSFColor.LIME.hexString;156
}157
if(nColor == HSSFColor.LIGHT_ORANGE.index)158

{159
strHex = HSSFColor.LIGHT_ORANGE.hexString;160
}161
if(nColor == HSSFColor.RED.index)162

{163
strHex = HSSFColor.RED.hexString;164
}165
if(nColor == HSSFColor.GREY_50_PERCENT.index)166

{167
strHex = HSSFColor.GREY_50_PERCENT.hexString;168
}169
if(nColor == HSSFColor.BLUE_GREY.index)170

{171
strHex = HSSFColor.BLUE_GREY.hexString;172
}173
if(nColor == HSSFColor.BLUE.index)174

{175
strHex = HSSFColor.BLUE.hexString;176
}177
if(nColor == HSSFColor.TEAL.index)178

{179
strHex = HSSFColor.TEAL.hexString;180
}181
if(nColor == HSSFColor.GREEN.index)182

{183
strHex = HSSFColor.GREEN.hexString;184
}185
if(nColor == HSSFColor.DARK_YELLOW.index)186

{187
strHex = HSSFColor.DARK_YELLOW.hexString;188
}189
if(nColor == HSSFColor.ORANGE.index)190

{191
strHex = HSSFColor.ORANGE.hexString;192
}193
if(nColor == HSSFColor.DARK_RED.index)194

{195
strHex = HSSFColor.DARK_RED.hexString;196
}197
if(nColor == HSSFColor.GREY_80_PERCENT.index)198

{199
strHex = HSSFColor.GREY_80_PERCENT.hexString;200
}201
if(nColor == HSSFColor.INDIGO.index)202

{203
strHex = HSSFColor.INDIGO.hexString;204
}205
if(nColor == HSSFColor.DARK_BLUE.index)206

{207
strHex = HSSFColor.DARK_BLUE.hexString;208
}209
if(nColor == HSSFColor.DARK_TEAL.index)210

{211
strHex = HSSFColor.DARK_TEAL.hexString;212
}213
if(nColor == HSSFColor.DARK_GREEN.index)214

{215
strHex = HSSFColor.DARK_GREEN.hexString;216
}217
if(nColor == HSSFColor.BROWN.index)218

{219
strHex = HSSFColor.BROWN.hexString;220
}221
if(nColor == HSSFColor.BLACK.index)222

{223
strHex = HSSFColor.BLACK.hexString;224
}225
return getHex(strHex);226
}227

228
}1
public class ExcelHelper2


{3
public ExcelHelper()4

{5
}6
public static String convertString2Html(String strMsg)7

{8
if(strMsg == null || strMsg.length() < 1)9

{10
return "";11
}12
String str = "";13
for(int i = 0;i < strMsg.length();i++)14

{15
char c = strMsg.charAt(i);16
if(c == '\r')17

{18
continue;19
}20
else if(c == '\n')21

{22
str += "<br>";23
}24
else if(c == '\t')25

{26
str += " ";27
}28
else if(c == ' ')29

{30
str += " ";31
}32
else33

{34
str += c;35
}36
}37
return str;38
}39

40
public static String getCellBackgroundColorInHtml(HSSFWorkbook wb,HSSFCell cell)41

{42
if(null == wb || null == cell)43

{44
return null;45
}46
return getCellBackgroundColorInHtml( wb.getCustomPalette(),cell);47
}48
public static String getCellBackgroundColorInHtml(HSSFPalette pa,HSSFCell cell)49

{50
if(null == pa || null == cell)51

{52
return null;53
}54
String cBack = ExcelColorHelper.getHex(pa == null ? null : pa.getColor(cell.getCellStyle().getFillForegroundColor()));55
if(null == cBack)56

{57
cBack = ExcelColorHelper.getHex(pa == null ? null : pa.getColor(cell.getCellStyle().getFillBackgroundColor()));58
}59
if(null == cBack)60

{61
cBack = ExcelColorHelper.getHex(cell.getCellStyle().getFillBackgroundColor());62
}63
return cBack;64
}65

66
public static int getExcelHeaderColumn(String strColumnHeader)67

{68
if(null == strColumnHeader)69

{70
return -1;71
}72
int nCol = 0;73
if(strColumnHeader.length() > 1)74

{75
nCol = ((int) (strColumnHeader.charAt(0)) - (int) 'a' + 1) * 26 +76
((int) (strColumnHeader.charAt(1)) - (int) 'a');77
}78
else79

{80
nCol = (int) (strColumnHeader.charAt(0)) - (int) 'a';81
}82
return nCol;83
}84
}1
public class ExcelCellValue2


{3
private int m_nRowID = -1;4
private int m_nColID =-1;5
private Object m_objValue=null;6
public ExcelCellValue(int nRow,int nCell,Object obj)7

{8
m_nRowID = nRow;9
m_nColID = nCell;10
m_objValue= obj;11
}12
public int getRow()13

{14
return m_nRowID;15
}16
public int getCol()17

{18
return m_nColID;19
}20
public Object getValue()21

{22
return m_objValue;23
}24
public boolean isMatch(int nRow,int nCell)25

{26
return m_nRowID ==nRow && m_nColID == nCell;27
}28
public String toString()29

{30
return null == m_objValue?null:m_objValue.toString();31
}32
}1
public class ExcelCellValueCollection2


{3
private java.util.Vector vect =new java.util.Vector();4
public ExcelCellValueCollection()5

{6
}7
public void clear()8

{9
vect.clear();10
}11
public int size()12

{13
return vect.size();14
}15
public void addElement(ExcelCellValue cell)16

{17
if(null!=cell)18

{19
vect.addElement(cell);20
}21
}22
public void removeElementAt(int nIndex)23

{24
vect.removeElementAt(nIndex);25
}26
public void insertElementAt(ExcelCellValue cell,int nIndex)27

{28
vect.insertElementAt(cell,nIndex);29
}30
public ExcelCellValue elementAt(int nIndex)31

{32
return (ExcelCellValue)vect.elementAt(nIndex);33
}34
public Object getValueAt(int nRow,int nCell)35

{36
for(int i =0;i<vect.size();i++)37

{38
ExcelCellValue cell =this.elementAt(i);39
if(null==cell)40

{41
continue;42
}43
if(cell.isMatch(nRow,nCell))44

{45
return cell.getValue();46
}47
}48
return null;49
}50

51
}最后主类 ProcessSheet
1
public class ExcelToHtml2


{3
private POIFSFileSystem m_excelFile = null;4
private HSSFWorkbook m_workBook = null;5
private HSSFPalette m_palette=null;6
public ExcelToHtml()7

{8
}9

10
public void open(String strFileName)11
throws Exception12

{13
m_excelFile = new POIFSFileSystem(new FileInputStream(strFileName));14
m_workBook = new HSSFWorkbook(m_excelFile);15
m_palette = m_workBook.getCustomPalette();16
}17
public void close()18

{19
m_palette=null;20
m_workBook=null;21
m_excelFile=null;22
}23
public int getSheetIndexFromName(String strName)24

{25
if(null == m_workBook)26

{27
return -1;28
}29
for(int nSheet = 0; nSheet < m_workBook.getNumberOfSheets();nSheet++)30

{31
if( m_workBook.getSheetName(nSheet).equals(strName))32

{33
return nSheet;34
}35
}36
return -1;37
}38
public int getNumberOfSheets()39

{40
return null!=m_workBook?m_workBook.getNumberOfSheets():0;41
}42
private String getCellValue(HSSFCell cell)43

{44
if(null == cell)45

{46
return "";47
}48
int nCellType = cell.getCellType();49
String strValue = "";50
switch(nCellType)51

{52
case HSSFCell.CELL_TYPE_BLANK:53
strValue = "";54
break;55
case HSSFCell.CELL_TYPE_BOOLEAN:56
strValue = cell.getBooleanCellValue() ? "true" : "false";57
break;58
case HSSFCell.CELL_TYPE_ERROR:59
strValue = "Error :" + cell.getErrorCellValue();60
break;61
case HSSFCell.CELL_TYPE_FORMULA:62
strValue = cell.getCellFormula();63
break;64
case HSSFCell.CELL_TYPE_NUMERIC:65
strValue = get4s5r(cell.getNumericCellValue(),2);66
break;67
case HSSFCell.CELL_TYPE_STRING:68
strValue = cell.getStringCellValue();69
break;70
default:71
strValue = "";72
}73

74
return strValue;75
}76

77
public static String get4s5r(double f,long nBase)78

{79
nBase = java.lang.Math.round(java.lang.Math.pow(10,nBase));80
f = java.lang.Math.round(f * nBase);81
f = f / nBase;82
if(f == 0)83

{84
return "0";85
}86
String strBak = java.text.NumberFormat.getInstance().format(f);87
String str = "";88
for(int i = 0;i < strBak.length();i++)89

{90
if(strBak.charAt(i) != ',')91

{92
str += strBak.charAt(i);93
}94
}95
return str;96
}97

98
private boolean isResideInRegion(HSSFSheet sheet,int nRow,short nCol)99

{100
if(sheet == null)101

{102
return false;103
}104
for(int nM = 0;nM < sheet.getNumMergedRegions();nM++)105

{106
Region region = sheet.getMergedRegionAt(nM);107
if(region.contains(nRow,nCol))108

{109
return true;110
}111
}112
return false;113

114
}115

116
private Region getTopRegion(HSSFSheet sheet,int nRow,short nCol)117

{118
if(sheet == null)119

{120
return null;121
}122
for(int nM = 0;nM < sheet.getNumMergedRegions();nM++)123

{124
Region region = sheet.getMergedRegionAt(nM);125
if(region.contains(nRow,nCol))126

{127
if(region.getRowFrom() == nRow &&128
region.getColumnFrom() == nCol)129

{130
return region;131
}132
}133
}134
return null;135
}136

137
public StringBuffer processSheet(int nSheetIndex,ExcelCellValueCollection cellValues)138
throws Exception139

{140
if(null == m_workBook)141

{142
return null;143
}144
HSSFSheet sheet = m_workBook.getSheetAt(nSheetIndex);145
if(null == sheet)146

{147
return null;148
}149
StringBuffer strRet =new StringBuffer();150
strRet.append("<table border='1'" +151
" cellspacing='1'" +152
" style='border-collapse: collapse'>");153
for(int nRow = sheet.getFirstRowNum();nRow < sheet.getLastRowNum();nRow++)154

{155
HSSFRow row = sheet.getRow(nRow);156
if(null == row)157

{158
continue;159
}160
strRet.append( "<tr height='" + row.getHeightInPoints() + "pt'>\n");161

162
for(short nCell = 0;nCell < row.getLastCellNum();nCell++)163

{164
HSSFCell cell = row.getCell(nCell);165
if(cell == null)166

{167
continue;168
}169
String strColSpanRowSpan = "";170
//检查Rowspan 和ColSpan171
Region region = this.getTopRegion(sheet,nRow,nCell);172
if(null != region)173

{174
int nColSpan = region.getColumnTo() - region.getColumnFrom() + 1;175
int nRowSpan = region.getRowTo() - region.getRowFrom() + 1;176
strColSpanRowSpan = "";177
if(nColSpan > 1)178

{179
strColSpanRowSpan += " colspan=" + nColSpan;180
}181
if(nRowSpan > 1)182

{183
strColSpanRowSpan += " rowspan=" + nRowSpan;184
}185
}186
else if(this.isResideInRegion(sheet,nRow,nCell))187

{188
continue;189
}190
String cBack = ExcelHelper.getCellBackgroundColorInHtml(m_palette,cell);191
strRet.append( "<td " + strColSpanRowSpan);192
if(null != cBack)193

{194
strRet.append(" bgcolor=#" + cBack);195
}196
if(cell.getCellStyle().getAlignment() == HSSFCellStyle.ALIGN_RIGHT)197

{198
strRet.append(" align=right");199
}200
else if(cell.getCellStyle().getAlignment() == HSSFCellStyle.ALIGN_CENTER)201

{202
strRet.append(" align=center");203
}204

205
String strCellValue = getCellValue(cell);206
if(null!=cellValues)207

{208
Object obj = cellValues.getValueAt(nRow,nCell);209
if(null!=obj)210

{211
strCellValue = obj.toString();212
}213
}214
strCellValue = ExcelHelper.convertString2Html(strCellValue);215
HSSFFont font = m_workBook.getFontAt(cell.getCellStyle().getFontIndex());216
if(null != font)217

{218
if(font.getBoldweight() == font.BOLDWEIGHT_BOLD)219

{220
strCellValue = "<b>" + strCellValue + "</b>";221
}222
String cFore = ExcelColorHelper.getHex(font.getColor());223
if(null == cFore)224

{225
cFore = ExcelColorHelper.getHex(m_palette == null ? null :226
m_palette.getColor(font.getColor()));227
}228
if(null != cFore)229

{230
strCellValue = "<font style='font-size:" + font.getFontHeightInPoints() + "pt' face='" + font.getFontName() + "' color=#" + cFore + ">" + strCellValue + "</font>";231
}232
}233
strRet.append( ">");234
strRet.append( strCellValue + "</td>");235
}236
strRet.append( "\n");237
}238
strRet.append("</tr>\n");239
strRet.append("</table>");240
return strRet;241
}242

243
public static void main(String[] strargvs)244
throws Exception245

{246
ExcelToHtml excel = new ExcelToHtml();247
excel.open("d:\\jituan1231.xls");248
}249
}没有解决问题:
EXCEL隐藏列问题
EXCEL列宽问题

浙公网安备 33010602011771号