JAVA POI 直接展现EXCEL成HTML (代码,测试中)

使用POI读取EXCEL中的信息生成HTML

第一个类 ,转换EXCEL颜色
ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifpublic class ExcelColorHelper
  2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  3InBlock.gif    public ExcelColorHelper()
  4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  5ExpandedSubBlockEnd.gif    }

  6InBlock.gif
  7InBlock.gif    public static String getHex(String strHex)
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        if(strHex.length() > 0)
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 11InBlock.gif            String[] a = strHex.split(":");
 12InBlock.gif            strHex = "";
 13InBlock.gif            for(int n = 0;n < a.length;n++)
 14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 15InBlock.gif                if(a[n].length() > 0)
 16ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 17InBlock.gif                    if(a[n].length() < 2)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 19InBlock.gif                        strHex += "0" + a[n];
 20ExpandedSubBlockEnd.gif                    }

 21InBlock.gif                    else
 22InBlock.gif
 23ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 24InBlock.gif                        strHex += a[n].substring(0,2);
 25ExpandedSubBlockEnd.gif                    }

 26ExpandedSubBlockEnd.gif                }

 27ExpandedSubBlockEnd.gif            }

 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif        return strHex.length() > 0 ? strHex : null;
 30InBlock.gif
 31ExpandedSubBlockEnd.gif    }

 32InBlock.gif
 33InBlock.gif    public static String getHex(HSSFColor c)
 34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 35InBlock.gif        return getHex(c == null ? "" : c.getHexString());
 36ExpandedSubBlockEnd.gif    }

 37InBlock.gif
 38InBlock.gif    public static String getHex(int nColor)
 39ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 40InBlock.gif        String strHex = "";
 41InBlock.gif        if(nColor == HSSFColor.LIGHT_CORNFLOWER_BLUE.index)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 43InBlock.gif            strHex = HSSFColor.LIGHT_CORNFLOWER_BLUE.hexString;
 44ExpandedSubBlockEnd.gif        }

 45InBlock.gif        if(nColor == HSSFColor.ROYAL_BLUE.index)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            strHex = HSSFColor.ROYAL_BLUE.hexString;
 48ExpandedSubBlockEnd.gif        }

 49InBlock.gif        if(nColor == HSSFColor.CORAL.index)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            strHex = HSSFColor.CORAL.hexString;
 52ExpandedSubBlockEnd.gif        }

 53InBlock.gif        if(nColor == HSSFColor.ORCHID.index)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 55InBlock.gif            strHex = HSSFColor.ORCHID.hexString;
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif        if(nColor == HSSFColor.MAROON.index)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 59InBlock.gif            strHex = HSSFColor.MAROON.hexString;
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif        if(nColor == HSSFColor.LEMON_CHIFFON.index)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63InBlock.gif            strHex = HSSFColor.LEMON_CHIFFON.hexString;
 64ExpandedSubBlockEnd.gif        }

 65InBlock.gif        if(nColor == HSSFColor.CORNFLOWER_BLUE.index)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            strHex = HSSFColor.CORNFLOWER_BLUE.hexString;
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif        if(nColor == HSSFColor.WHITE.index)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            strHex = HSSFColor.WHITE.hexString;
 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif        if(nColor == HSSFColor.LAVENDER.index)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            strHex = HSSFColor.LAVENDER.hexString;
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif        if(nColor == HSSFColor.PALE_BLUE.index)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 79InBlock.gif            strHex = HSSFColor.PALE_BLUE.hexString;
 80ExpandedSubBlockEnd.gif        }

 81InBlock.gif        if(nColor == HSSFColor.LIGHT_TURQUOISE.index)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 83InBlock.gif            strHex = HSSFColor.LIGHT_TURQUOISE.hexString;
 84ExpandedSubBlockEnd.gif        }

 85InBlock.gif        if(nColor == HSSFColor.LIGHT_GREEN.index)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 87InBlock.gif            strHex = HSSFColor.LIGHT_GREEN.hexString;
 88ExpandedSubBlockEnd.gif        }

 89InBlock.gif        if(nColor == HSSFColor.LIGHT_YELLOW.index)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 91InBlock.gif            strHex = HSSFColor.LIGHT_YELLOW.hexString;
 92ExpandedSubBlockEnd.gif        }

 93InBlock.gif        if(nColor == HSSFColor.TAN.index)
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95InBlock.gif            strHex = HSSFColor.TAN.hexString;
 96ExpandedSubBlockEnd.gif        }

 97InBlock.gif        if(nColor == HSSFColor.ROSE.index)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99InBlock.gif            strHex = HSSFColor.ROSE.hexString;
100ExpandedSubBlockEnd.gif        }

101InBlock.gif        if(nColor == HSSFColor.GREY_25_PERCENT.index)
102ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
103InBlock.gif            strHex = HSSFColor.GREY_25_PERCENT.hexString;
104ExpandedSubBlockEnd.gif        }

105InBlock.gif        if(nColor == HSSFColor.PLUM.index)
106ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
107InBlock.gif            strHex = HSSFColor.PLUM.hexString;
108ExpandedSubBlockEnd.gif        }

109InBlock.gif        if(nColor == HSSFColor.SKY_BLUE.index)
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
111InBlock.gif            strHex = HSSFColor.SKY_BLUE.hexString;
112ExpandedSubBlockEnd.gif        }

113InBlock.gif        if(nColor == HSSFColor.TURQUOISE.index)
114ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
115InBlock.gif            strHex = HSSFColor.TURQUOISE.hexString;
116ExpandedSubBlockEnd.gif        }

117InBlock.gif        if(nColor == HSSFColor.BRIGHT_GREEN.index)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            strHex = HSSFColor.BRIGHT_GREEN.hexString;
120ExpandedSubBlockEnd.gif        }

121InBlock.gif        if(nColor == HSSFColor.YELLOW.index)
122ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
123InBlock.gif            strHex = HSSFColor.YELLOW.hexString;
124ExpandedSubBlockEnd.gif        }

125InBlock.gif        if(nColor == HSSFColor.GOLD.index)
126ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
127InBlock.gif            strHex = HSSFColor.GOLD.hexString;
128ExpandedSubBlockEnd.gif        }

129InBlock.gif        if(nColor == HSSFColor.PINK.index)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131InBlock.gif            strHex = HSSFColor.PINK.hexString;
132ExpandedSubBlockEnd.gif        }

133InBlock.gif        if(nColor == HSSFColor.GREY_40_PERCENT.index)
134ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
135InBlock.gif            strHex = HSSFColor.GREY_40_PERCENT.hexString;
136ExpandedSubBlockEnd.gif        }

137InBlock.gif        if(nColor == HSSFColor.VIOLET.index)
138ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
139InBlock.gif            strHex = HSSFColor.VIOLET.hexString;
140ExpandedSubBlockEnd.gif        }

141InBlock.gif        if(nColor == HSSFColor.LIGHT_BLUE.index)
142ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
143InBlock.gif            strHex = HSSFColor.LIGHT_BLUE.hexString;
144ExpandedSubBlockEnd.gif        }

145InBlock.gif        if(nColor == HSSFColor.AQUA.index)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            strHex = HSSFColor.AQUA.hexString;
148ExpandedSubBlockEnd.gif        }

149InBlock.gif        if(nColor == HSSFColor.SEA_GREEN.index)
150ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
151InBlock.gif            strHex = HSSFColor.SEA_GREEN.hexString;
152ExpandedSubBlockEnd.gif        }

153InBlock.gif        if(nColor == HSSFColor.LIME.index)
154ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
155InBlock.gif            strHex = HSSFColor.LIME.hexString;
156ExpandedSubBlockEnd.gif        }

157InBlock.gif        if(nColor == HSSFColor.LIGHT_ORANGE.index)
158ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
159InBlock.gif            strHex = HSSFColor.LIGHT_ORANGE.hexString;
160ExpandedSubBlockEnd.gif        }

161InBlock.gif        if(nColor == HSSFColor.RED.index)
162ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
163InBlock.gif            strHex = HSSFColor.RED.hexString;
164ExpandedSubBlockEnd.gif        }

165InBlock.gif        if(nColor == HSSFColor.GREY_50_PERCENT.index)
166ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
167InBlock.gif            strHex = HSSFColor.GREY_50_PERCENT.hexString;
168ExpandedSubBlockEnd.gif        }

169InBlock.gif        if(nColor == HSSFColor.BLUE_GREY.index)
170ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
171InBlock.gif            strHex = HSSFColor.BLUE_GREY.hexString;
172ExpandedSubBlockEnd.gif        }

173InBlock.gif        if(nColor == HSSFColor.BLUE.index)
174ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
175InBlock.gif            strHex = HSSFColor.BLUE.hexString;
176ExpandedSubBlockEnd.gif        }

177InBlock.gif        if(nColor == HSSFColor.TEAL.index)
178ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
179InBlock.gif            strHex = HSSFColor.TEAL.hexString;
180ExpandedSubBlockEnd.gif        }

181InBlock.gif        if(nColor == HSSFColor.GREEN.index)
182ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
183InBlock.gif            strHex = HSSFColor.GREEN.hexString;
184ExpandedSubBlockEnd.gif        }

185InBlock.gif        if(nColor == HSSFColor.DARK_YELLOW.index)
186ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
187InBlock.gif            strHex = HSSFColor.DARK_YELLOW.hexString;
188ExpandedSubBlockEnd.gif        }

189InBlock.gif        if(nColor == HSSFColor.ORANGE.index)
190ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
191InBlock.gif            strHex = HSSFColor.ORANGE.hexString;
192ExpandedSubBlockEnd.gif        }

193InBlock.gif        if(nColor == HSSFColor.DARK_RED.index)
194ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
195InBlock.gif            strHex = HSSFColor.DARK_RED.hexString;
196ExpandedSubBlockEnd.gif        }

197InBlock.gif        if(nColor == HSSFColor.GREY_80_PERCENT.index)
198ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
199InBlock.gif            strHex = HSSFColor.GREY_80_PERCENT.hexString;
200ExpandedSubBlockEnd.gif        }

201InBlock.gif        if(nColor == HSSFColor.INDIGO.index)
202ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
203InBlock.gif            strHex = HSSFColor.INDIGO.hexString;
204ExpandedSubBlockEnd.gif        }

205InBlock.gif        if(nColor == HSSFColor.DARK_BLUE.index)
206ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
207InBlock.gif            strHex = HSSFColor.DARK_BLUE.hexString;
208ExpandedSubBlockEnd.gif        }

209InBlock.gif        if(nColor == HSSFColor.DARK_TEAL.index)
210ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
211InBlock.gif            strHex = HSSFColor.DARK_TEAL.hexString;
212ExpandedSubBlockEnd.gif        }

213InBlock.gif        if(nColor == HSSFColor.DARK_GREEN.index)
214ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
215InBlock.gif            strHex = HSSFColor.DARK_GREEN.hexString;
216ExpandedSubBlockEnd.gif        }

217InBlock.gif        if(nColor == HSSFColor.BROWN.index)
218ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
219InBlock.gif            strHex = HSSFColor.BROWN.hexString;
220ExpandedSubBlockEnd.gif        }

221InBlock.gif        if(nColor == HSSFColor.BLACK.index)
222ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
223InBlock.gif            strHex = HSSFColor.BLACK.hexString;
224ExpandedSubBlockEnd.gif        }

225InBlock.gif        return getHex(strHex);
226ExpandedSubBlockEnd.gif    }

227InBlock.gif
228ExpandedBlockEnd.gif}
第二个类帮助类
ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifpublic class ExcelHelper
 2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 3InBlock.gif    public ExcelHelper()
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 5ExpandedSubBlockEnd.gif    }

 6InBlock.gif    public static String convertString2Html(String strMsg)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        if(strMsg == null || strMsg.length() < 1)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            return "";
11ExpandedSubBlockEnd.gif        }

12InBlock.gif        String str = "";
13InBlock.gif        for(int i = 0;i < strMsg.length();i++)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            char c = strMsg.charAt(i);
16InBlock.gif            if(c == '\r')
17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
18InBlock.gif                continue;
19ExpandedSubBlockEnd.gif            }

20InBlock.gif            else if(c == '\n')
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                str += "<br>";
23ExpandedSubBlockEnd.gif            }

24InBlock.gif            else if(c == '\t')
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                str += "&nbsp;&nbsp;&nbsp;&nbsp;";
27ExpandedSubBlockEnd.gif            }

28InBlock.gif            else if(c == ' ')
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                str += "&nbsp;";
31ExpandedSubBlockEnd.gif            }

32InBlock.gif            else
33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
34InBlock.gif                str += c;
35ExpandedSubBlockEnd.gif            }

36ExpandedSubBlockEnd.gif        }

37InBlock.gif        return str;
38ExpandedSubBlockEnd.gif    }

39InBlock.gif
40InBlock.gif    public static String getCellBackgroundColorInHtml(HSSFWorkbook wb,HSSFCell cell)
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        if(null == wb || null == cell)
43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
44InBlock.gif            return null;
45ExpandedSubBlockEnd.gif        }

46InBlock.gif        return getCellBackgroundColorInHtml( wb.getCustomPalette(),cell);
47ExpandedSubBlockEnd.gif    }

48InBlock.gif    public static String getCellBackgroundColorInHtml(HSSFPalette pa,HSSFCell cell)
49ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
50InBlock.gif        if(null == pa || null == cell)
51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
52InBlock.gif            return null;
53ExpandedSubBlockEnd.gif        }

54InBlock.gif        String cBack = ExcelColorHelper.getHex(pa == null ? null : pa.getColor(cell.getCellStyle().getFillForegroundColor()));
55InBlock.gif            if(null == cBack)
56ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
57InBlock.gif                cBack = ExcelColorHelper.getHex(pa == null ? null : pa.getColor(cell.getCellStyle().getFillBackgroundColor()));
58ExpandedSubBlockEnd.gif            }

59InBlock.gif            if(null == cBack)
60ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
61InBlock.gif                cBack = ExcelColorHelper.getHex(cell.getCellStyle().getFillBackgroundColor());
62ExpandedSubBlockEnd.gif            }

63InBlock.gif            return cBack;
64ExpandedSubBlockEnd.gif    }

65InBlock.gif
66InBlock.gif    public static int getExcelHeaderColumn(String strColumnHeader)
67ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
68InBlock.gif        if(null == strColumnHeader)
69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
70InBlock.gif            return -1;
71ExpandedSubBlockEnd.gif        }

72InBlock.gif        int nCol = 0;
73InBlock.gif        if(strColumnHeader.length() > 1)
74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
75InBlock.gif            nCol = ((int) (strColumnHeader.charAt(0)) - (int'a' + 1* 26 +
76InBlock.gif                ((int) (strColumnHeader.charAt(1)) - (int'a');
77ExpandedSubBlockEnd.gif        }

78InBlock.gif        else
79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
80InBlock.gif            nCol = (int) (strColumnHeader.charAt(0)) - (int'a';
81ExpandedSubBlockEnd.gif        }

82InBlock.gif        return nCol;
83ExpandedSubBlockEnd.gif    }

84ExpandedBlockEnd.gif}
两个辅助类
ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifpublic class ExcelCellValue
 2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 3InBlock.gif    private int m_nRowID = -1;
 4InBlock.gif    private int m_nColID =-1;
 5InBlock.gif    private Object m_objValue=null;
 6InBlock.gif    public ExcelCellValue(int nRow,int nCell,Object obj)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        m_nRowID = nRow;
 9InBlock.gif        m_nColID = nCell;
10InBlock.gif        m_objValue= obj;
11ExpandedSubBlockEnd.gif    }

12InBlock.gif    public int getRow()
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        return m_nRowID;
15ExpandedSubBlockEnd.gif    }

16InBlock.gif    public int getCol()
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif        return m_nColID;
19ExpandedSubBlockEnd.gif    }

20InBlock.gif    public Object getValue()
21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
22InBlock.gif        return m_objValue;
23ExpandedSubBlockEnd.gif    }

24InBlock.gif    public boolean isMatch(int nRow,int nCell)
25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
26InBlock.gif        return m_nRowID ==nRow && m_nColID == nCell;
27ExpandedSubBlockEnd.gif    }

28InBlock.gif    public String toString()
29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
30InBlock.gif        return null == m_objValue?null:m_objValue.toString();
31ExpandedSubBlockEnd.gif    }

32ExpandedBlockEnd.gif}

ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifpublic class ExcelCellValueCollection
 2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 3InBlock.gif    private java.util.Vector vect =new java.util.Vector();
 4InBlock.gif    public ExcelCellValueCollection()
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 6ExpandedSubBlockEnd.gif    }

 7InBlock.gif    public void clear()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        vect.clear();
10ExpandedSubBlockEnd.gif    }

11InBlock.gif    public int size()
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        return vect.size();
14ExpandedSubBlockEnd.gif    }

15InBlock.gif    public void addElement(ExcelCellValue cell)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17InBlock.gif        if(null!=cell)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            vect.addElement(cell);
20ExpandedSubBlockEnd.gif        }

21ExpandedSubBlockEnd.gif    }

22InBlock.gif    public void removeElementAt(int nIndex)
23ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
24InBlock.gif        vect.removeElementAt(nIndex);
25ExpandedSubBlockEnd.gif    }

26InBlock.gif    public void insertElementAt(ExcelCellValue cell,int nIndex)
27ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
28InBlock.gif        vect.insertElementAt(cell,nIndex);
29ExpandedSubBlockEnd.gif    }

30InBlock.gif    public ExcelCellValue elementAt(int nIndex)
31ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
32InBlock.gif        return (ExcelCellValue)vect.elementAt(nIndex);
33ExpandedSubBlockEnd.gif    }

34InBlock.gif    public Object getValueAt(int nRow,int nCell)
35ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
36InBlock.gif        for(int i =0;i<vect.size();i++)
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            ExcelCellValue cell =this.elementAt(i);
39InBlock.gif            if(null==cell)
40ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
41InBlock.gif                continue;
42ExpandedSubBlockEnd.gif            }

43InBlock.gif            if(cell.isMatch(nRow,nCell))
44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
45InBlock.gif                return cell.getValue();
46ExpandedSubBlockEnd.gif            }

47ExpandedSubBlockEnd.gif        }

48InBlock.gif        return null;
49ExpandedSubBlockEnd.gif    }

50InBlock.gif
51ExpandedBlockEnd.gif}


最后主类 ProcessSheet
ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifpublic class ExcelToHtml
  2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  3InBlock.gif    private POIFSFileSystem m_excelFile = null;
  4InBlock.gif    private HSSFWorkbook m_workBook = null;
  5InBlock.gif    private HSSFPalette m_palette=null;
  6InBlock.gif    public ExcelToHtml()
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  8ExpandedSubBlockEnd.gif    }

  9InBlock.gif
 10InBlock.gif    public void open(String strFileName)
 11InBlock.gif        throws Exception
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        m_excelFile = new POIFSFileSystem(new FileInputStream(strFileName));
 14InBlock.gif        m_workBook = new HSSFWorkbook(m_excelFile);
 15InBlock.gif        m_palette = m_workBook.getCustomPalette();
 16ExpandedSubBlockEnd.gif    }

 17InBlock.gif    public void close()
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 19InBlock.gif        m_palette=null;
 20InBlock.gif        m_workBook=null;
 21InBlock.gif        m_excelFile=null;
 22ExpandedSubBlockEnd.gif    }

 23InBlock.gif    public int getSheetIndexFromName(String strName)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 25InBlock.gif        if(null == m_workBook)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 27InBlock.gif            return -1;
 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif        for(int nSheet = 0; nSheet < m_workBook.getNumberOfSheets();nSheet++)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif           if( m_workBook.getSheetName(nSheet).equals(strName))
 32ExpandedSubBlockStart.gifContractedSubBlock.gif           dot.gif{
 33InBlock.gif               return nSheet;
 34ExpandedSubBlockEnd.gif           }

 35ExpandedSubBlockEnd.gif        }

 36InBlock.gif        return -1;
 37ExpandedSubBlockEnd.gif    }

 38InBlock.gif    public int getNumberOfSheets()
 39ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 40InBlock.gif        return null!=m_workBook?m_workBook.getNumberOfSheets():0;
 41ExpandedSubBlockEnd.gif    }

 42InBlock.gif    private String getCellValue(HSSFCell cell)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 44InBlock.gif        if(null == cell)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            return "";
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif        int nCellType = cell.getCellType();
 49InBlock.gif        String strValue = "";
 50InBlock.gif        switch(nCellType)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52InBlock.gif            case HSSFCell.CELL_TYPE_BLANK:
 53InBlock.gif                strValue = "";
 54InBlock.gif                break;
 55InBlock.gif            case HSSFCell.CELL_TYPE_BOOLEAN:
 56InBlock.gif                strValue = cell.getBooleanCellValue() ? "true" : "false";
 57InBlock.gif                break;
 58InBlock.gif            case HSSFCell.CELL_TYPE_ERROR:
 59InBlock.gif                strValue = "Error :" + cell.getErrorCellValue();
 60InBlock.gif                break;
 61InBlock.gif            case HSSFCell.CELL_TYPE_FORMULA:
 62InBlock.gif                strValue = cell.getCellFormula();
 63InBlock.gif                break;
 64InBlock.gif            case HSSFCell.CELL_TYPE_NUMERIC:
 65InBlock.gif                strValue = get4s5r(cell.getNumericCellValue(),2);
 66InBlock.gif                break;
 67InBlock.gif            case HSSFCell.CELL_TYPE_STRING:
 68InBlock.gif                strValue = cell.getStringCellValue();
 69InBlock.gif                break;
 70InBlock.gif            default:
 71InBlock.gif                strValue = "";
 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif
 74InBlock.gif        return strValue;
 75ExpandedSubBlockEnd.gif    }

 76InBlock.gif
 77InBlock.gif    public static String get4s5r(double f,long nBase)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 79InBlock.gif        nBase = java.lang.Math.round(java.lang.Math.pow(10,nBase));
 80InBlock.gif        f = java.lang.Math.round(f * nBase);
 81InBlock.gif        f = f / nBase;
 82InBlock.gif        if(f == 0)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 84InBlock.gif            return "0";
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif        String strBak = java.text.NumberFormat.getInstance().format(f);
 87InBlock.gif        String str = "";
 88InBlock.gif        for(int i = 0;i < strBak.length();i++)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 90InBlock.gif            if(strBak.charAt(i) != ',')
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 92InBlock.gif                str += strBak.charAt(i);
 93ExpandedSubBlockEnd.gif            }

 94ExpandedSubBlockEnd.gif        }

 95InBlock.gif        return str;
 96ExpandedSubBlockEnd.gif    }

 97InBlock.gif
 98InBlock.gif    private boolean isResideInRegion(HSSFSheet sheet,int nRow,short nCol)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
100InBlock.gif        if(sheet == null)
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
102InBlock.gif            return false;
103ExpandedSubBlockEnd.gif        }

104InBlock.gif        for(int nM = 0;nM < sheet.getNumMergedRegions();nM++)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
106InBlock.gif            Region region = sheet.getMergedRegionAt(nM);
107InBlock.gif            if(region.contains(nRow,nCol))
108ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
109InBlock.gif                return true;
110ExpandedSubBlockEnd.gif            }

111ExpandedSubBlockEnd.gif        }

112InBlock.gif        return false;
113InBlock.gif
114ExpandedSubBlockEnd.gif    }

115InBlock.gif
116InBlock.gif    private Region getTopRegion(HSSFSheet sheet,int nRow,short nCol)
117ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
118InBlock.gif        if(sheet == null)
119ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
120InBlock.gif            return null;
121ExpandedSubBlockEnd.gif        }

122InBlock.gif        for(int nM = 0;nM < sheet.getNumMergedRegions();nM++)
123ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
124InBlock.gif            Region region = sheet.getMergedRegionAt(nM);
125InBlock.gif            if(region.contains(nRow,nCol))
126ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
127InBlock.gif                if(region.getRowFrom() == nRow &&
128InBlock.gif                   region.getColumnFrom() == nCol)
129ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
130InBlock.gif                    return region;
131ExpandedSubBlockEnd.gif                }

132ExpandedSubBlockEnd.gif            }

133ExpandedSubBlockEnd.gif        }

134InBlock.gif        return null;
135ExpandedSubBlockEnd.gif    }

136InBlock.gif
137InBlock.gif    public StringBuffer processSheet(int nSheetIndex,ExcelCellValueCollection cellValues)
138InBlock.gif        throws Exception
139ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
140InBlock.gif        if(null == m_workBook)
141ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
142InBlock.gif            return null;
143ExpandedSubBlockEnd.gif        }

144InBlock.gif        HSSFSheet sheet = m_workBook.getSheetAt(nSheetIndex);
145InBlock.gif        if(null == sheet)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            return null;
148ExpandedSubBlockEnd.gif        }

149InBlock.gif        StringBuffer strRet =new StringBuffer();
150InBlock.gif        strRet.append("<table border='1'" +
151InBlock.gif            " cellspacing='1'" +
152InBlock.gif            " style='border-collapse: collapse'>");
153InBlock.gif        for(int nRow = sheet.getFirstRowNum();nRow < sheet.getLastRowNum();nRow++)
154ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
155InBlock.gif            HSSFRow row = sheet.getRow(nRow);
156InBlock.gif            if(null == row)
157ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
158InBlock.gif                continue;
159ExpandedSubBlockEnd.gif            }

160InBlock.gif            strRet.append( "<tr height='" + row.getHeightInPoints() + "pt'>\n");
161InBlock.gif
162InBlock.gif            for(short nCell = 0;nCell < row.getLastCellNum();nCell++)
163ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
164InBlock.gif                HSSFCell cell = row.getCell(nCell);
165InBlock.gif                if(cell == null)
166ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
167InBlock.gif                    continue;
168ExpandedSubBlockEnd.gif                }

169InBlock.gif                String strColSpanRowSpan = "";
170InBlock.gif                //检查Rowspan 和ColSpan
171InBlock.gif                Region region = this.getTopRegion(sheet,nRow,nCell);
172InBlock.gif                if(null != region)
173ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
174InBlock.gif                    int nColSpan = region.getColumnTo() - region.getColumnFrom() + 1;
175InBlock.gif                    int nRowSpan = region.getRowTo() - region.getRowFrom() + 1;
176InBlock.gif                    strColSpanRowSpan = "";
177InBlock.gif                    if(nColSpan > 1)
178ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
179InBlock.gif                        strColSpanRowSpan += " colspan=" + nColSpan;
180ExpandedSubBlockEnd.gif                    }

181InBlock.gif                    if(nRowSpan > 1)
182ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
183InBlock.gif                        strColSpanRowSpan += " rowspan=" + nRowSpan;
184ExpandedSubBlockEnd.gif                    }

185ExpandedSubBlockEnd.gif                }

186InBlock.gif                else if(this.isResideInRegion(sheet,nRow,nCell))
187ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
188InBlock.gif                    continue;
189ExpandedSubBlockEnd.gif                }

190InBlock.gif                String cBack = ExcelHelper.getCellBackgroundColorInHtml(m_palette,cell);
191InBlock.gif                strRet.append( "<td " + strColSpanRowSpan);
192InBlock.gif                if(null != cBack)
193ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
194InBlock.gif                    strRet.append(" bgcolor=#" + cBack);
195ExpandedSubBlockEnd.gif                }

196InBlock.gif                if(cell.getCellStyle().getAlignment() == HSSFCellStyle.ALIGN_RIGHT)
197ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
198InBlock.gif                    strRet.append(" align=right");
199ExpandedSubBlockEnd.gif                }

200InBlock.gif                else if(cell.getCellStyle().getAlignment() == HSSFCellStyle.ALIGN_CENTER)
201ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
202InBlock.gif                    strRet.append(" align=center");
203ExpandedSubBlockEnd.gif                }

204InBlock.gif
205InBlock.gif                String strCellValue = getCellValue(cell);
206InBlock.gif                if(null!=cellValues)
207ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
208InBlock.gif                    Object obj = cellValues.getValueAt(nRow,nCell);
209InBlock.gif                    if(null!=obj)
210ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
211InBlock.gif                        strCellValue = obj.toString();
212ExpandedSubBlockEnd.gif                    }

213ExpandedSubBlockEnd.gif                }

214InBlock.gif                strCellValue = ExcelHelper.convertString2Html(strCellValue);
215InBlock.gif                HSSFFont font = m_workBook.getFontAt(cell.getCellStyle().getFontIndex());
216InBlock.gif                if(null != font)
217ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
218InBlock.gif                    if(font.getBoldweight() == font.BOLDWEIGHT_BOLD)
219ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
220InBlock.gif                        strCellValue = "<b>" + strCellValue + "</b>";
221ExpandedSubBlockEnd.gif                    }

222InBlock.gif                    String cFore = ExcelColorHelper.getHex(font.getColor());
223InBlock.gif                    if(null == cFore)
224ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
225InBlock.gif                        cFore = ExcelColorHelper.getHex(m_palette == null ? null :
226InBlock.gif                            m_palette.getColor(font.getColor()));
227ExpandedSubBlockEnd.gif                    }

228InBlock.gif                    if(null != cFore)
229ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
230InBlock.gif                        strCellValue = "<font style='font-size:" + font.getFontHeightInPoints() + "pt' face='" + font.getFontName() + "' color=#" + cFore + ">" + strCellValue + "</font>";
231ExpandedSubBlockEnd.gif                    }

232ExpandedSubBlockEnd.gif                }

233InBlock.gif                strRet.append( ">");
234InBlock.gif                strRet.append( strCellValue + "</td>");
235ExpandedSubBlockEnd.gif            }

236InBlock.gif            strRet.append( "\n");
237ExpandedSubBlockEnd.gif        }

238InBlock.gif        strRet.append("</tr>\n");
239InBlock.gif        strRet.append("</table>");
240InBlock.gif        return strRet;
241ExpandedSubBlockEnd.gif    }

242InBlock.gif
243InBlock.gif    public static void main(String[] strargvs)
244InBlock.gif        throws Exception
245ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
246InBlock.gif        ExcelToHtml excel = new ExcelToHtml();
247InBlock.gif        excel.open("d:\\jituan1231.xls");
248ExpandedSubBlockEnd.gif    }

249ExpandedBlockEnd.gif}

没有解决问题:
    EXCEL隐藏列问题
    EXCEL列宽问题

转载于:https://www.cnblogs.com/JeasonZhao/archive/2006/03/01/340267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值