信我或關注微信號:獅范兒,回復:學習,獲取免費學習資源包。
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>>
來源網絡,侵權聯系刪除
私信我或關注微信號:獅范兒,回復:學習,獲取免費學習資源包。
請耐心慢慢刷新到頁面完整顯示出來
https://fjxasdf.github.io/daogou/
原文章https://www.toutiao.com/i6711294610594857476/
1.顯示商品圖片
2.沒有顯示全部商品,只顯示熱門商品,避免數據、圖片加載太慢
3.樣式稍微美化了
4.上傳到GitHub,不過GitHub很卡,要刷新等好久才能看到
import xlrd from datetime import date,datetime import json file = '精選優質商品清單(內含優惠券).xls' def read_excel(): wb = xlrd.open_workbook(filename=file)#打開文件 # print(wb.sheet_names())#獲取所有表格名字 sheet1 = wb.sheet_by_index(0)#通過索引獲取表格 # sheet2 = wb.sheet_by_name('Page1')#通過名字獲取表格 # print(sheet1) # print(sheet2) # print(sheet1.name) #表 名 rows = sheet1.nrows #多少行 # print(sheet1.ncols) #多少列 # rows = sheet1.row_values(1)#獲取行內容 category0 = sheet1.col_values(4)#獲取列內容(類目) del category0[0] category = sorted(set(category0),key=category0.index) #類目列表->去重 data = [] data_hot = [] # print(rows) # print(cols) for i,v in enumerate(category): category[i] = v.replace("/", "、")#吧"/"替換"、" data.append([category[i],[]]) jsonData = json.dumps(category, ensure_ascii=False) with open('./daogou/category.json', 'w',encoding="utf-8") as f: f.write(jsonData)#保存分類json for i,v in enumerate(data): for x in range(rows): if v[0] == (sheet1.cell(x,4).value.replace("/", "、")): xo = sheet1.row_values(x) y = [xo[1],xo[2],xo[4],xo[6],xo[21]] #選擇保存商品名稱、圖片、鏈接等 data[i][1].append(y) if x>0 and float(sheet1.row_values(x)[9]) >= 1: #選擇傭金多的作為熱門 xo = sheet1.row_values(x) y = [xo[1],xo[2],xo[4],xo[6],xo[21]] data_hot.append(y) data_hot = data_hot[:24] jsonData_hot = json.dumps(data_hot, ensure_ascii=False) with open('./daogou/results_hot.json', 'w',encoding="utf-8") as f: f.write(jsonData_hot)#保存熱門商品json for i,v in enumerate(data): jsonData = json.dumps(v[1], ensure_ascii=False) with open('./daogou/'+v[0]+'.json', 'w',encoding="utf-8") as f: f.write(jsonData)#保存每個分類商品json # jsonData1 = json.dumps(data, ensure_ascii=False) # with open('results.json', 'w',encoding="utf-8") as f: # f.write(jsonData1) # print(sheet1.cell(0,0).value)#獲取表格里的內容,第一行第一個格內容 # print(sheet1.cell_value(0,0))#獲取表格里的內容,第一行第一個格內容 #print(sheet1.row(0)[0].value)#獲取表格里的內容,第一行第一個格內容 if __name__ == '__main__': read_excel()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="../js/jquery.min.js"></script> <link rel="stylesheet" href="../font/Alibaba-PuHuiTi-Regular.css"> <script> var navo = ''; var info = ''; var data_total;//總數據 function color16(){//十六進制顏色隨機 var r = Math.floor(Math.random()*256); var g = Math.floor(Math.random()*256); var b = Math.floor(Math.random()*256); var color = '#'+r.toString(16)+g.toString(16)+b.toString(16); return color; } function nav_href(title,id){//錨跳轉 if ($('#'+title).length>0) { location.href="#"+title; }else{ var div_title = "<div id='"+title+"' style='float:left;'>"; var div_content = "<div class='pro_img' style='background:#ff5000'><b style='color:#FFF'>"+title+"</b><\/div>"; $('#load').show(); $.get('./'+title+'.json', function(data) { $.each(data, function(index, val) { div_content+="<a style='background:"+color16()+"' target='_blank' href='"+val[4]+"' class='pro_img'><img onload='$(this).show()' src="+val[1]+" style='width:100%;height:100%;display:none;' />"+val[0]+"<span class='money'>¥"+val[3]+"</span><span class='title'>"+val[0].substring(0,25)+"...</span><\/a>"; if ((index+1)==data.length) { var div_footer ="</div><br>"; infoo=div_title+div_content+div_footer; $('#content').append(infoo); location.href="#"+title; $('#load').hide(); } }); },'json'); } } function get_data(){ $.get('./category.json', function(data) {//導航 $.each(data, function(index, val) { // console.log(val[0]); navo+="<a class='nav' href=javascript:;nav_href(\'"+val+"\',"+index+")>"+val+"</a> "; }); $('#nav').html(navo); $.get('./results_hot.json', function(data) {//熱門商品 var div_title = "<div id='熱門商品' style='float:left;'>"; var div_content = "<div class='pro_img' style='background:#ff5000'><b style='color:#FFF'>熱門商品</b></span><\/div>"; $.each(data, function(index, val) { // console.log(val); div_content+="<a style='background:"+color16()+"' target='_blank' href='"+val[4]+"' class='pro_img'><img onload='$(this).show()' src="+val[1]+" style='width:100%;height:100%;display:none;' />"+val[0]+"<span class='money'>¥"+val[3]+"</span><span class='title'>"+val[0].substring(0,25)+"...</span><\/a>" // if (index==3) {return false} }); var div_footer ="</div><br>"; info+=div_title+div_content+div_footer; $('#content').html(info); $('#load').hide(0); }); },'json'); } </script> <style> #body{ /*border: 1px solid #eee;*/ width: 1110px; margin:0 auto; } a.nav{ text-decoration: none; margin-right: 10px; padding: 0 5px; /*padding-bottom: 10px;*/ display: inline-block; color: #000; font-weight: 700; } #nav{ display: inline-block; } *,html,body{ font-family:"Alibaba-PuHuiTi-Regular"; } #content{ margin-top: 10px; display: inline-block; } .money{ width: 100%; /* padding: 0px 10px; */ background: #ffffffcc; position: absolute; left: 0; bottom: 50px; height: 30px; line-height: 30px; color: #F40; font-weight: 700; /* border-radius: 0 8px 0 0; */ text-align: left; font-size: 18px; } .title{ width: 100%; font-weight: 700; /* padding: 0px 10px; */ background: #ffffffcc; position: absolute; left: 0; bottom: 0; height: 50px; line-height: normal; color: #000; /* border-radius: 0 8px 0 0; */ text-align: left; font-size: 15px; } .pro_img{ position: relative; float: left; width: 220px; height: 220px; line-height: 220px; text-align: center; border: 1px solid #eee; cursor: pointer; font-size: 30px; /*white-space:normal; */ overflow:hidden; /*超過部分不顯示*/ text-overflow:ellipsis; /*超過部分用點點表示*/ /* white-space:nowrap;/*不換行*/ } .input1{ width: 300px; height: 30px; border: 1px solid #888; border-radius: 10px 0 0 10px; outline-style: none ; } .button1{ margin-left: -7px; width: 50px; height: 34px; border-radius:0 10px 10px 0 ; outline-style: none ; } #search1{ /*width: 350px;*/ /*margin:0 auto;*/ } #load{ position: fixed; width: 100%; height: 100%; top: 0; left: 0; background: #000000ad; z-index: 999; text-align: center; vertical-align: middle; } #load>span{ display: inline-block; vertical-align: middle; height: 100%; } .load{ display: inline-block; vertical-align: middle; font-size: 50px; color: #FFF; } </style> </head> <body onload="get_data()"> <!-- loading --> <div id="load"> <span ></span> <div class="load">加載中...</div> </div> <div id="body"> <h1>網站僅學習交流!!網站中的商品信息均來自于互聯網。</h1> <!-- <div id="search1"> <input type="text" class="input1"> <button class="button1">搜索</button><b> 網站僅學習交流!!網站僅學習交流!!網站僅學習交流!!網站僅學習交流!!網站僅學習交流!!</b> </div> --> <div id="nav"></div> <div id="content"></div> </div> </body> </html>
?Excel表格可是幾乎每天都能用的到,不是自己編輯發給同事,就是別人發來的,而且有時候不僅是對Excel表格進行編輯,還需要進行格式轉換,最常見的就是Excel轉PDF。幾天你那小編給大家講一個不常見的【Excel轉HTML】,有需要的小伙伴趕緊看過來吧,只需要一個PDF轉換器即可。
??第一步,打開已經安裝好的極強PDF轉換器,點擊頁面左側的【其他文檔格式轉換】,看見了,有一個【文件轉HTML】選項,沒錯點它。然后點擊【添加文件】,把需要進行轉換的Excel表格文件加進來。?
??第二步,在頁面下方勾選【自定義】選項,先把文件存儲為設置好,然后點擊【開始轉換】。?
??第三步,稍等片刻,轉換完成之后會有一個如下圖所示的提示框,點擊【確定】即可。?
??第四步,下面就預覽一下Excel轉HTML的成果如何,如下圖所示還不錯吧。?
??極強PDF轉換器不僅是PDF轉換器,還可以實現其他文件的格式轉換,還等什么,趕緊來本站免費下載吧。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。