信我或關注微信號:獅范兒,回復:學習,獲取免費學習資源包。
Excel文件轉成html頁面代碼
main類:啟動類
package com.test; import trans.toHtml; public class testToHtml { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub toHtml th=new toHtml(); // System.out.println(System.getProperty("java.library.path")); //-Djava.library.path=D:\jar\jacob_1.9 th.excelToHtml("d:/excel/運維門戶通訊錄.xlsx", "d:/test.html"); } }
代碼:
package trans; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class toHtml { int WORD_HTML = 8; int WORD_TXT = 7; int EXCEL_HTML = 44; /** * WORD?HTML * @param docfile WORD ? ?· * @param htmlfile ? HTML · */ public void wordToHtml(String docfile, String htmlfile) { ActiveXComponent app = new ActiveXComponent("Word.Application"); // word try { app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]); Variant f = new Variant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); } } /** * EXCEL?HTML * @param xlsfile EXCEL ? ?· * @param htmlfile ? HTML · */ public void excelToHtml(String xlsfile, String htmlfile) { ActiveXComponent app = new ActiveXComponent("Excel.Application"); // excel try { app.setProperty("Visible", new Variant(false)); Dispatch excels = app.getProperty("Workbooks").toDispatch(); Dispatch excel = Dispatch.invoke(excels,"Open",Dispatch.Method,new Object[] { xlsfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(EXCEL_HTML) }, new int[1]); Variant f = new Variant(false); Dispatch.call(excel, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); } } /** * /? ? * @param folderPath ? ?· * @param htmlfile ? HTML · */ public void delFolder(String folderPath) { try { delAllFile(folderPath); //? String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); //? ? } catch (Exception e) {e.printStackTrace();} } /** * /? ? ? * @param path ? ?· */ public boolean delAllFile(String path) { boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFile(path + "/" + tempList[i]);// ? ? ? delFolder(path + "/" + tempList[i]);// ? ? flag = true; } } return flag; } } 需要的jar包 <<jacob.jar>> <<toHtml.java>> <<testToHtml.java>>
來源網絡,侵權聯系刪除
私信我或關注微信號:獅范兒,回復:學習,獲取免費學習資源包。
pire.XLS for Java 13.3.6 已發布。本次更新支持將Excel轉換為獨立的HTML文件,支持了保存整個Excel到HTML流,支持了給透視表的透視域字段(PivotFields)和數據域字段(DataFields)添加條件格式,并且支持取代一個單元格內部分文本的功能。同時,該版本還Excel 到 PDF和圖片的轉換。此外,一些已知問題也得到了修復,如修復了轉換圖表到圖片時引導線丟失的問題。詳情請閱讀以下內容。
新功能:
Workbook wb = new Workbook();
wb.loadFromFile("data/mytest.xlsx");
HTMLOptions.Default.isStandAloneHtmlFile(true);
wb.saveToFile("result.html", FileFormat.HTML);
Workbook wb = new Workbook();
wb.loadFromFile("data/mytest.xlsx");
FileOutputStream fileStream = new FileOutputStream("output/saveStream_result.html");
wb.saveToStream(fileStream, FileFormat.HTML);
fileStream.close();
PivotTable table = (PivotTable)worksheet.getPivotTables().get(0);
PivotConditionalFormatCollection pcfs = table.getPivotConditionalFormats();
PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getColumnFields().get(0));
//PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getRowFields().get(0));
//PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getDataFields().get(0));
//PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getPageFields().get(0));
IConditionalFormat cf = pc.addCondition();
cf.setFormatType(ConditionalFormatType.ContainsBlanks);
cf.setFillPattern(ExcelPatternType.Solid);
cf.setBackColor(Color.Red);
sheet.getCellList().get(0).textPartReplace("Office", "Spire");
問題修復:
下載Spire.XLS for Java 13.3.6請點擊:
Spire.XLS for Java | 下載
pire.XLS for Java 12.6.0 已發布。此版本支持在 Excel 轉 HTML 時設置是否轉出隱藏的工作表。同時,還增加了兩個新特性: Excel 轉 HTML 后,轉出的 HTML 中當前選中的選項卡添加了有對比性的顏色邊框以及工作表選項卡文本增加了兩側間距。此外,本次更新還增強了 Excel 到 HTML、 Excel 到 PDF 和 Excel 到圖片的轉換功能。一些已知問題也得到了修復,如復制工作表時迷你折線圖丟失和設置單元格背景色為透明不生效等問題。詳情請閱讀一下內容。
新功能:
Workbook book=new Workbook();
book.loadFromFile("test.xlsx");
//false --- 轉出隱藏的工作表
//true--- 不轉出隱藏的工作表
book.saveToHtml("output.html",false);
問題修復:
下載Spire.XLS for Java 12.6.0 請點擊:
Spire.XLS for Java | 下載
*請認真填寫需求信息,我們會在24小時內與您取得聯系。