『壹』 怎麼把html文件轉成excel
1.
我們打開網路上的一個包含表格內容的頁面,點擊菜單欄-文件-另存為。
2.
彈出窗口選擇保存路徑,將保存類型選為「htm
或Html格式」。
3.
在計算機上新建一個Excel文件並打開,點擊菜單欄-數據-導入外部數據-導入數據。
4.
找到剛才保存的「Html」單擊打開,再單擊「導入」。
5.
在彈出的「導入數據」窗口中你可以選擇「現有工作表」或者「新建工作表」,然後單擊確定。
『貳』 怎麼把html的table 轉換為excel
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>Inserttitlehere</title>
<scripttype="text/javascript">
vartableToExcel=(function(){
varuri='data:application/vnd.ms-excel;base64,'
,template='<htmlxmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-html40"><head><!--[ifgtemso9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
,base64=function(s){returnwindow.btoa(unescape(encodeURIComponent(s)))}
,format=function(s,c){returns.replace(/{(w+)}/g,function(m,p){returnc[p];})}
returnfunction(table,name){
if(!table.nodeType)table=document.getElementById(table)
varctx={worksheet:name||'Worksheet',table:table.innerHTML}
window.location.href=uri+base64(format(template,ctx))
}
})()
</script>
</head>
<body>
<h1>tableToExcelDemo</h1>
<p>ExportingtheW3CExampleTable</p>
<inputtype="button"onclick="tableToExcel('testTable','W3CExampleTable')"value="ExporttoExcel">
<h4>兩行三列:</h4>
<tableid='testTable'border="1">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</body>
</html>
『叄』 java excel轉html多個sheet怎麼合並到一個excel里
poi在讀取excel的時候,是按每個sheet,row,cell讀取的,分別對應sheet頁,行,單元格,你順序讀取,全都讀取到內存里,然後再按照你的格式在前台顯示就完了。
『肆』 java怎樣讀取html文件
java讀取html文件跟讀取普通文件一樣,都是使用輸入輸出流,但是java讀取html文件之後還需要解析,使用Jsoup對html進行解析。下面是一個java讀取帶表格的任意html文件,並把html文件轉換成excel的例子。
要求:要求能夠實現給出任意帶table表格的html文件,生成與表格相同內容的excel文件,附件可以作為測試文件,提供給定的roster.html文件,通過java代碼,實現生成與html頁面的table相同樣式的roster.xls文件。
首先看roster.html:
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.io.IOException;
importjxl.Workbook;
importjxl.write.Label;
importjxl.write.WritableCellFormat;
importjxl.write.WritableFont;
importjxl.write.WritableSheet;
importjxl.write.WritableWorkbook;
importjxl.write.WriteException;
importjxl.write.biff.RowsExceededException;
importorg.jsoup.Jsoup;
importorg.jsoup.nodes.Document;
importorg.jsoup.nodes.Element;
importorg.jsoup.select.Elements;
publicclassHTMLTOExcel{
publicstaticvoidmain(Stringargs[])throwsIOException{
///讀取classpath目錄下面的路徑
Stringpath=HTMLTOExcel.class.getResource("/").getPath();
path+="roster.html";
toExcel(path,"roster");
}
//得到Document並且設置編碼格式
publicstaticDocumentgetDoc(StringfileName)throwsIOException{
FilemyFile=newFile(fileName);
Documentdoc=Jsoup.parse(myFile,"GBK","");
returndoc;
}
///這個方法用於根據trs行數和sheet畫出整個表格
publicstaticvoidmergeColRow(Elementstrs,WritableSheetsheet)throwsRowsExceededException,WriteException{
int[][]rowhb=newint[300][50];
for(inti=0;i<trs.size();i++){
Elementtr=trs.get(i);
Elementstds=tr.getElementsByTag("td");
intrealColNum=0;
for(intj=0;j<tds.size();j++){
Elementtd=tds.get(j);
if(rowhb[i][realColNum]!=0){
realColNum=getRealColNum(rowhb,i,realColNum);
}
introwspan=1;
intcolspan=1;
if(td.attr("rowspan")!=""){
rowspan=Integer.parseInt(td.attr("rowspan"));
}
if(td.attr("colspan")!=""){
colspan=Integer.parseInt(td.attr("colspan"));
}
Stringtext=td.text();
drawMegerCell(rowspan,colspan,sheet,realColNum,i,text,rowhb);
realColNum=realColNum+colspan;
}
}
}
///這個方法用於根據樣式畫出單元格,並且根據rowpan和colspan合並單元格
publicstaticvoiddrawMegerCell(introwspan,intcolspan,WritableSheetsheet,intrealColNum,intrealRowNum,Stringtext,int[][]rowhb)throwsRowsExceededException,WriteException{
for(inti=0;i<rowspan;i++){
for(intj=0;j<colspan;j++){
if(i!=0||j!=0){
text="";
}
Labellabel=newLabel(realColNum+j,realRowNum+i,text);
WritableFontcountents=newWritableFont(WritableFont.TIMES,10);//設置單元格內容,字型大小12
WritableCellFormatcellf=newWritableCellFormat(countents);
cellf.setAlignment(jxl.format.Alignment.CENTRE);//把水平對齊方式指定為居中
cellf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//把垂直對齊方式指定為居
label.setCellFormat(cellf);
sheet.addCell(label);
rowhb[realRowNum+i][realColNum+j]=1;
}
}
sheet.mergeCells(realColNum,realRowNum,realColNum+colspan-1,realRowNum+rowspan-1);
}
publicstaticintgetRealColNum(int[][]rowhb,inti,intrealColNum){
while(rowhb[i][realColNum]!=0){
realColNum++;
}
returnrealColNum;
}
///根據colgroups設置表格的列寬
publicstaticvoidsetColWidth(Elementscolgroups,WritableSheetsheet){
if(colgroups.size()>0){
Elementcolgroup=colgroups.get(0);
Elementscols=colgroup.getElementsByTag("col");
for(inti=0;i<cols.size();i++){
Elementcol=cols.get(i);
Stringstrwd=col.attr("width");
if(col.attr("width")!=""){
intwd=Integer.parseInt(strwd);
sheet.setColumnView(i,wd/8);
}
}
}
}
//toExcel是根據html文件地址生成對應的xls
publicstaticvoidtoExcel(StringfileName,StringexcelName)throwsIOException{
Documentdoc=getDoc(fileName);
Stringtitle=doc.title();
///得到樣式,以後可以根據正則表達式解析css,暫且沒有找到cssparse
Elementsstyle=doc.getElementsByTag("style");
///得到Table,demo只演示輸入一個table,以後可以用循環遍歷tables集合輸入所有table
Elementstables=doc.getElementsByTag("TABLE");
if(tables.size()==0){
return;
}
Elementtable=tables.get(0);
//得到所有行
Elementstrs=table.getElementsByTag("tr");
///得到列寬集合
Elementscolgroups=table.getElementsByTag("colgroup");
try{
//文件保存到classpath目錄下面
Stringpath=HTMLTOExcel.class.getResource("/").getPath();
path+=excelName+".xls";
System.out.println(path);
WritableWorkbookbook=Workbook.createWorkbook(newFile(path));
WritableSheetsheet=book.createSheet("人事關系",0);
setColWidth(colgroups,sheet);
mergeColRow(trs,sheet);
book.write();
book.close();
}catch(RowsExceededExceptione){
e.printStackTrace();
}catch(WriteExceptione){
e.printStackTrace();
}
}
}
解析html文件的例子文檔地址:http://blog.csdn.net/androidwuyou/article/details/52636821
『伍』 java以流的形式讀取html文件,再輸出為excel文件
給你個思路吧,可以自己完成,你描述的所謂的html其實是json數據,遍歷json方法有N種,第三方的工具也很多,比如fastJson,gson,jackson等等,基本上幾句話搞定;
另外你還要將解析出來的值保存到excel中,可以使用poi這個工具,封裝了操作excel表的基本操作方法。祝你成功~
『陸』 如何用java來將excel文件轉化為html文件問題
可以導入spire.xls.jar(只需導入這一個jar即可),直接下載jar包,然後解壓將lib文件夾下的jar文件導入java程序;如果創建的是Maven程序的話,需要在pom.xml文件中配置maven路徑並指定Free Spire.XLS for Java的依賴,導入,如下配置:
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.xls</artifactId>
<version>2.2.0</version>
</dependency></dependencies>
完成導入後,參考如下代碼將excel轉為html:
import com.spire.xls.*;
public class ExcelToHtml {
public static void main(String[] args) {
//載入Excel工作表
Workbook wb = new Workbook();
wb.loadFromFile("test.xlsx");
//獲取工作表
Worksheet sheet = wb.getWorksheets().get(0);
//調用方法將Excel保存為HTML
sheet.saveToHtml("ToHtml.html");
}
}
『柒』 java操控HTML與Excel的問題
誰如果能寫出來,誰就可以打個包發布了!!
樓主啊,這個就用已經成型的吧
jxl 一個韓國程序員寫的。不難,但是是按單元格操作的。
我有兩個html文件(都是通過excel文件另存為網頁生成的)
不知道你再文件中加html標簽了沒啊?反正往excel里寫還是很簡單的!
package com.excel.excel;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class PersonExcel {
public String expordExcel(OutputStream os, List<List> list)throws Exception {
WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
//String tmptitle = "考試成績"; // 標題
WritableSheet wsheet = wbook.createSheet("第一頁", 0); // sheet名稱
// 設置excel標題
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wsheet.addCell(new Label(1, 0, "", wcfFC));//tmptitle
wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
wcfFC = new WritableCellFormat(wfont);
// 開始生成主體內容
for(int k=0;k<list.get(0).size();k++){
wsheet.addCell(new Label(k, 1, (String)list.get(0).get(k)));
}
for (int i = 0; i < list.size()-1; i++)
for(int j=0;j<list.get(i).size();j++){
wsheet.addCell(new Label(j, i+2, (String)(list.get(i+1)).get(j)));
}
// 主體內容生成結束
wbook.write(); // 寫入文件
wbook.close();
os.close();
return "success";
}
}
這是曾經用過的一個!
你可以用位元組流試一下,看看能不能讀出來!
讀出來應該可以,但是寫到excel不能!
『捌』 JSP表格轉換成excel
先通過資料庫查詢出數據,放到List里,然後把這個List發往頁面,然後遍歷這個List把數據顯示到這個表格里。 要想把數據導出到execel,很簡單,把頁面接受的這個List用jxl寫到Execel就行了。具體將List導出到Execel的類如下:
package cms.;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import cms.utlis.DbUtils;
public class ToExecelByQuery {
//方法接受兩個參數,一個是list,這個地方我用了泛型。另一個參數是HttpServletResponse response
public static void toExcelBy(List<AccessLog> list,HttpServletResponse response) {
// 創建工作表
WritableWorkbook book=null;
response.reset();
// 創建工作流
OutputStream os =null;
try {
// 設置彈出對話框
response.setContentType("application/DOWLOAD");
// 設置工作表的標題
response.setHeader("Content-Disposition",
"attachment; filename=****.xls");//設置生成的文件名字
os = response.getOutputStream();
// 初始化工作表
book = Workbook.createWorkbook(os);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
//以下是我做的導出日誌的一個模版
int nCount = list.size();
WritableSheet sheet = book.createSheet("訪問日誌", 0);
// 生成名為"商品信息"的工作表,參數0表示這是第一頁
int nI = 1;
// 表欄位名
sheet.addCell(new jxl.write.Label(0, 0, "日誌編號"));
sheet.addCell(new jxl.write.Label(1, 0, "用戶ID"));
sheet.addCell(new jxl.write.Label(2, 0, "用戶姓名"));
sheet.addCell(new jxl.write.Label(3, 0, "訪問日期"));
sheet.addCell(new jxl.write.Label(4, 0, "訪問時間"));
sheet.addCell(new jxl.write.Label(5, 0, "名片ID"));
sheet.addCell(new jxl.write.Label(6, 0, "名片名稱"));
sheet.addCell(new jxl.write.Label(7, 0, "創建日期"));
sheet.addCell(new jxl.write.Label(8, 0, "更新日期"));
// 將數據追加
for(int i=1;i<list.size();i++){
sheet.addCell(new jxl.write.Label(0, i, list.get(i).toString()));
sheet.addCell(new jxl.write.Label(1, i, list.get(i).getUserId()));
sheet.addCell(new jxl.write.Label(2, i, list.get(i).getUsername()));
sheet.addCell(new jxl.write.Label(3, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(4, i, list.get(i).getComplTime()));
sheet.addCell(new jxl.write.Label(5, i, list.get(i).getCopId()));
sheet.addCell(new jxl.write.Label(6, i, list.get(i).getFirstname()));
sheet.addCell(new jxl.write.Label(7, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(8, i, list.get(i).getUpdTim()));
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
大體思路就是這樣的,別忘了在你的項目中導入JXL必要的jar包,這個包叫jxl.jar,可以下載一個。
『玖』 java如何將html轉為excel文件
建議找一下有關java導出excel的資料,html轉pdf就試過,html轉excel沒試過
『拾』 html怎麼生成Excel
如果用java實現的話,那就需要用到jxl.jar包中提供的類方法來完成了,你可以先查看一下jxl的資料再來做。
還有一種簡單的方法就是用javascript腳本生成,例如:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script language="javascript">
function tableToExcel(tname) {
if(confirm('是否要導出到excel?')!=0)
{
window.clipboardData.setData("Text",document.all(tname).outerHTML);
try
{
ExApp = new ActiveXObject("Excel.Application")
var ExWBk = ExApp.workbooks.add()
var ExWSh = ExWBk.worksheets(1)
ExApp.DisplayAlerts = false
ExApp.visible = true
}
catch(e)
{
alert("導出沒有成功!1.您的電腦沒有安裝Microsoft Excel軟體!2.請設置Internet選項自定義級別,對沒有標記安全級別的 ActiveX控制項進行提示。")
return false
}
ExWBk.worksheets(1).Paste;
}else
{
return;
}
}
</script>
</head>
<body>
<table id="baba">
<tr>
<td>afdsfsd</td>
<td>fdfsdfds</td>
<td>fdfsadfsa</td>
<td>fdsfsadfds</td>
<td>fdsfasd</td>
<td>fdsfsad</td>
</tr>
<tr>
<td>afdsfsd</td>
<td>fdfsdfds</td>
<td>fdfsadfsa</td>
<td>fdsfsadfds</td>
<td>fdsfasd</td>
<td>fdsfsad</td>
</tr>
<tr>
<td>afdsfsd</td>
<td>fdfsdfds</td>
<td>fdfsadfsa</td>
<td>fdsfsadfds</td>
<td>fdsfasd</td>
<td>fdsfsad</td>
</tr>
</table>
<input type="button" name="anniu" onclick="tableToExcel('baba')" />
</body>
</html>