导航:首页 > 编程语言 > javahtml转excel

javahtml转excel

发布时间:2022-06-12 08:06:36

‘壹’ 怎么把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>

阅读全文

与javahtml转excel相关的资料

热点内容
会日语的程序员 浏览:19
网银密码加密失败怎么回事 浏览:727
android开发音乐播放器 浏览:808
ug120阵列命令快捷键 浏览:597
气动隔膜式压缩机 浏览:470
linux如何修改主机名 浏览:104
单片机光标上下移动 浏览:528
数据加密验证 浏览:108
程序员被激怒 浏览:891
winxp找不到服务器dns地址 浏览:842
以文本文件的格式保存考生文件夹 浏览:41
编译原理文法分为几类 浏览:570
JAVA基础学python要多久 浏览:74
java流量控制 浏览:936
java实现多重继承 浏览:707
票据通加密狗怎么在新系统上使用 浏览:795
航模加密狗连接电脑 浏览:473
好用的汇编语言编译器 浏览:863
自己编译安卓虚拟机 浏览:913
中国的古代算法 浏览:656