Package cn. hnedu. util;
Import java. io. File;
Import java. io. FileInputStream;
Import java. text. DecimalFormat;
Import java. util. ArrayList;
Import java. util. Collections;
Import java. util. HashMap;
Import java. util. HashSet;
Import java. util. Iterator;
Import java. util. List;
Import java. util. Map;
Import java. util. Set;
Import oracle.net. aso. r;
Import org. apache. openjpa. lib. conf. StringValue;
Import org. apache. poi. hssf. usermodel. HSSFCell;
Import org. apache. poi. hssf. usermodel. HSSFCellStyle;
Import org. apache. poi. hssf. usermodel. HSSFFont;
Import org. apache. poi. hssf. usermodel. HSSFPalette;
Import org. apache. poi. hssf. usermodel. HSSFRichTextString;
Import org. apache. poi. hssf. usermodel. HSSFRow;
Import org. apache. poi. hssf. usermodel. HSSFSheet;
Import org. apache. poi. hssf. usermodel. HSSFWorkbook;
Import org. apache. poi. hssf. util. HSSFColor;
Import org. apache. poi. poifs. filesystem. POIFSFileSystem;
Import org. apache. poi. ss. usermodel. Sheet;
Import org. apache. poi. ss. util. CellRangeAddress;
Import bsh.org. objectweb. asm. Label;
/**
* Poi-3.7
*
* @ Author Administrator
*
*/
Public class ExcelUtils {
Public ExcelUtils (){
}
Private int sumWidth;
Public static void main (String args []) {
ExcelUtils eu = new ExcelUtils ();
// String filepath = "E: \ myproject \ eipplugin \ test.xls ";
String filepath = "D: \ 1.xls ";
HSSFWorkbook workbook = eu. readExcelFile (filepath );
String exceltitle = eu. getFirstRowContent (workbook, 0 );
// System. out. println (exceltitle );
// StringBuffer htmlsource = eu. excelToHtmlSource (workbook, 0 );
StringBuffer htmlsource = eu. excelToHtmlJs (workbook, 0 );
StringBuffer htmlbuf = new StringBuffer ("");
Htmlbuf. append (eu. headerHtmlStart (exceltitle ));
Htmlbuf. append (htmlsource );
Htmlbuf. append (eu. headerHtmlEnd ());
// Htmlbuf. append (eu. excelToHtmlSource (workbook, 0 ));
Htmlbuf. append (eu. bodyHtml ());
Htmlbuf. append (eu. bodyHtmlEnd ());
Try {
FileUtils. writeFile ("E: \ excel \ \ excel3.html", htmlbuf );
} Catch (Exception e ){
E. printStackTrace ();
}
// System. out. println (htmlsource );
}
Public StringBuffer headerHtmlStart (String title ){
StringBuffer sb = new StringBuffer ("");
Sb. append ("Sb. append ("Sb
. Append ("<meta http-equiv =" Content-Type "content =" text/html; charset = gb2312 "/> ");
Sb. append ("<META HTTP-EQUIV =" pragma "CONTENT =" no-cache "> ");
Sb
. Append ("<META HTTP-EQUIV =" Cache-Control "CONTENT =" no-cache, must-revalidate "> ");
Sb. append ("<META HTTP-EQUIV =" expires "CONTENT =" 0 "> ");
Sb. append ("<title>" + title + "</title> ");
// Sb. append ("<link href =" js/style.css "rel =" stylesheet "type =" text/css "/> ");
Sb. append ("<SCRIPT type =" text/javascript "src =" js/jquery-1.3.2.min.js "> </SCRIPT> ");
Sb. append ("<script type =" text/javascript "src =" js/scripts-pack.js "> </script> ");
Sb. append ("<script type =" text/javascript "src =" js/jquery-ui-1.7.2.custom.min.js "> </script> ");
Sb. append ("<script type =" text/javascript "src =" js/jquery. chromatable. js "> </script> ");
Return sb;
}
Public StringBuffer headerHtmlEnd (){
StringBuffer sb = new StringBuffer ("");
Sb. append ("Sb. append ("<body>"); // refresh the page
Return sb;
}
Public StringBuffer bodyHtml (){
StringBuffer sb = new StringBuffer ("");
Sb. append ("<a id =" deldata "href =" # "> View All results </a> ");
Sb. append ("<br/> ");
Sb. append ("<table id = exceltitletable width =" "+ this. getSumWidth () + "" border = "0" cellspacing = "0" cellpadding = "0"> ");
Sb. append ("</table> ");
Sb. append ("<table id = exceltable width =" "+ this. getSumWidth () + "" border = "0" cellspacing = "0" cellpadding = "0"> ");
Sb. append ("<thead>"); // width = "" + this. getSumWidth () + "" width = "" + this. getSumWidth () + ""
Sb. append ("</thead> ");
Sb. append ("<tbody> ");
Sb. append ("</tbody> ");
Sb. append ("</table> ");
Return sb;
}
Public StringBuffer bodyHtml (HSSFWorkbook workbook, int sheetindex ){
Int trwidth = this. getTrWidth (workbook, sheetindex );
StringBuffer sb = new StringBuffer ("");
Sb. append ("<a id =" deldata "href =" # "> View All results </a> ");
Sb. append ("<br/> ");
Sb
. Append ("<table id = exceltitletable border =" 0 "cellspacing =" 0 "cellpadding =" 0 "> ");
Sb. append ("</table> ");
Sb. append ("<table id = exceltable ");
Sb. append ("border =" 0 "cellspacing =" 0 "cellpadding =" 0 "> ");
Sb. append ("<thead> ");
Sb. append ("</thead> ");
Sb. append ("<tbody> ");
Sb. append ("</tbody> ");
Sb. append ("</table> ");
Return sb;
}
Public StringBuffer bodyHtmlEnd (){
StringBuffer sb = new StringBuffer ("");
Sb. append ("</body> ");
Sb. append ("Return sb;
}
Private int getTdWidth (Sheet sheet, int startCol, int endCol ){
Int tdwidth = 0;
For (int I = startCol; I <= endCol; I ++ ){
Int tempwidth = sheet. getColumnWidth (I)/32;
Tdwidth = tdwidth + tempwidth;
}
Return tdwidth;
}
Public StringBuffer excelToHtmlJs (HSSFWorkbook workbook, int sheetindex ){
StringBuffer sb = new StringBuffer ("");
HSSFSheet sheet = workbook. getSheetAt (sheetindex );
Int lastRowNum = sheet. getLastRowNum ();
System. out. println ("lastRowNum =" + lastRowNum );
Map <String, String> map [] = getRowSpanColSpanMap (sheet );
HSSFRow row1 = null;
HSSFCell cell1 = null;
Int lastColNums = 0;
For (int rowNum = sheet. getFirstRowNum (), I = 0; rowNum <= lastRowNum; rowNum ++, I ++ ){
Row1 = (HSSFRow) sheet. getRow (rowNum );
If (! "". Equals (row1 )){
LastColNums = row1.getLastCellNum ();
Int sumWidth = 0; // calculates the total width of the header.
System. out. println ("lastColNums:" + lastColNums );
}
// Break;
For (int colNum = 0, j = 0; colNum <lastColNums; colNum ++, j ++ ){
Cell1 = row1.getCell (colNum );
Int tdwidth = sheet. getColumnWidth (colNum)/32;
// Calculate the total width of the header
// System. out. println ("tdwidth =" + tdwidth );
SumWidth + = tdwidth;
This. setSumWidth (sumWidth );
System. out. println ("tdwidth =" + tdwidth );
}
Break;
}
Sb. append ("<