java excel怎么读取到html
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java excel怎么读取到html相关的知识,希望对你有一定的参考价值。
一般都是用jacob的,这里不能发地址,自己搜一下,要下1.9的版本1、我们解开下载的jacob_1.9.zip,在文件夹中找到jacob.dll和jacob.jar两个文件
2、将压缩包解压后,Jacob.jar添加到Libraries中;
3、将Jacob.dll放至“WINDOWS\SYSTEM32”下面。
需要注意的是:
【使用IDE启动Web服务器时,系统读取不到Jacob.dll,例如用MyEclipse启动Tomcat,就需要将dll文件copy到MyEclipse安装目录的“jre\bin”下面。
一般系统没有加载到Jacob.dll文件时,报错信息为:“java.lang.UnsatisfiedLinkError: no jacob in java.library.path”】
使用Jacob转换Word,Excel为html
JAVA代码:
Java代码
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 TransformFiletoHtml
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;
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 TransformFiletoHtml
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;
调用JAVA代码:
Java代码
public class Test1
public static void main(String[] args)
// TODO Auto-generated method stub
TransformFiletoHtml trans = new TransformFiletoHtml();
trans.wordToHtml("D:\\sinye.doc", "D:\\sinye.html");
public class Test1
public static void main(String[] args)
// TODO Auto-generated method stub
TransformFiletoHtml trans = new TransformFiletoHtml();
trans.wordToHtml("D:\\sinye.doc", "D:\\sinye.html");
只写了一个测试word转html的,excel转html的同理,在TransformFiletoHtml类中,写了两个方法,一个是删除文件夹的方法(delFolder()),一个是删除文件夹下所有文件的方法(delAllFile())。追问
我是想要开始读取Excel的数据,然后在写入到html中
参考技术A 直接parse html 到jxl.追问能说清楚点吗??、
谢谢了。。。
xml reader parse html tag 所对应的element/txt type
jxl 用来读xml
不大确定你要干嘛。
java读取excel时间格式出现数字怎么处理
在Excel中的日期格式,比如2009-12-24将其转化为数字格式时变成了40171,在用java处理的时候,读取的也将是40171。如果使用POI处理Excel中的日期类型的单元格时,如果仅仅是判断它是否为日期类型的话,最终会以NUMERIC类型来处理。
正确的处理方法是先判断单元格的类型是否则NUMERIC类型,然后再判断单元格是否为日期格式,如果是的话,创建一个日期格式,再将单元格的内容以这个日期格式显示出来。如果单元格不是日期格式,那么则直接得到NUMERIC的值就行了。
具体代码如下:
主要是判断NUMERIC 的时候 同事判断下 单元格是不是日期格式 如果是 日期格式直接 转成日期格式字符串返回值就ok了。
if (0 == cell.getCellType()) //判断是否为日期类型 if(HSSFDateUtil.isCellDateFormatted(cell)) //用于转化为日期格式 Date d = cell.getDateCellValue(); DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); str[k] = formater.format(d); else // 用于格式化数字,只保留数字的整数部分 DecimalFormat df = new DecimalFormat("########"); str[k] = df.format(cell.getNumericCellValue());
JSch连接SSH问题Exception:Algorithm negotiation fail
使用Jenkins配置完远程SSH服务器,发生无法连接问题,查看Log后找到:
[SSH] Exception:Algorithm negotiation fail
com.jcraft.jsch.JSchException: Algorithm negotiation fail
at com.jcraft.jsch.Session.receive_kexinit(Session.java:520)
at com.jcraft.jsch.Session.connect(Session.java:286)
at com.jcraft.jsch.Session.connect(Session.java:150)
at org.jvnet.hudson.plugins.SSHSite.createSession(SSHSite.java:141)
at org.jvnet.hudson.plugins.SSHSite.executeCommand(SSHSite.java:151)
at org.jvnet.hudson.plugins.SSHBuildWrapper.executePreBuildScript(SSHBuildWrapper.java:75)
at org.jvnet.hudson.plugins.SSHBuildWrapper.setUp(SSHBuildWrapper.java:59)
at hudson.model.Build$BuildExecution.doRun(Build.java:154)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:533)
at hudson.model.Run.execute(Run.java:1754)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:89)
at hudson.model.Executor.run(Executor.java:240)
Finished: FAILURE
原因分析:OpenSSH和 JSch支持的交换算法不同,需要一方打开另一方支持的交换算法。
OpenSSH enables only the following key exchange algorithms by default:
- curve25519-sha256@libssh.org
- ecdh-sha2-nistp256
- ecdh-sha2-nistp384
- ecdh-sha2-nistp521
- diffie-hellman-group-exchange-sha256
- diffie-hellman-group14-sha1
Where as JSch claims to support these algorithms for key exchange:
- diffie-hellman-group-exchange-sha1
- diffie-hellman-group1-sha1
解决办法:
在SSH的配置文件/etc/ssh/sshd_config增加以下两行,让SSH支持相应的算法和MACs。
KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-ripemd160-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,umac-128@openssh.com,hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96
完成后重启SSH即可解决问题。
参考技术A正确的处理方法是先判断单元格的类型是否则NUMERIC类型,判断单元格是否为日期格式,如果是的话,创建一个日期格式,将单元格的内容以这个日期格式显示出来。如果单元格不是日期格式,那么则直接得到NUMERIC的值就行了。
在Excel中的日期格式,比如2009-12-24将其转化为数字格式时变成了40171,在用java处理的时候,读取的也将是40171。如果使用POI处理Excel中的日期类型的单元格时,如果仅仅是判断它是否为日期类型的话,最终会以NUMERIC类型来处理。
三维引用样式
如果要分析同一工作簿中多张工作表上的相同单元格或单元格区域中的数据,就要用到三维引用。三维引用包含单元格或区域引用,前面加上工作表名称的范围。Excel 使用存储在引用开始名和结束名之间的任何工作表。例如,=SUM(Sheet2:Sheet13!B5) 将计算包含在 B5 单元格内所有值的和,单元格取值范围是从工作表2 到工作表 13。
以上内容参考:百度百科-单元格
参考技术B java读取excel时间格式出现数字的处理方法:Excel存储日期、时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化
1、数值格式(CELL_TYPE_NUMERIC):
1.纯数值格式:getNumericCellValue() 直接获取数据
2.日期格式:处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式
1).判断是否是日期格式:HSSFDateUtil.isCellDateFormatted(cell)
2).判断是日期或者时间
cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")
OR: cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")
3.自定义日期格式:处理yyyy年m月d日,h时mm分,yyyy年m月等含文字的日期格式
判断cell.getCellStyle().getDataFormat()值,解析数值格式
yyyy年m月d日----->31
m月d日---->58
h时mm分--->32
举例说明:
private String parseExcel(Cell cell)
String result = new String();
switch (cell.getCellType())
case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) // 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm"))
sdf = new SimpleDateFormat("HH:mm");
else // 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = cell.getDateCellValue();
result = sdf.format(date);
else if (cell.getCellStyle().getDataFormat() == 58)
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
result = sdf.format(date);
else
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General"))
format.applyPattern("#");
result = format.format(value);
break;
case HSSFCell.CELL_TYPE_STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
return result;
参考技术C 那就全部按照字符串读取,然后用Integer.parseInt()方法和new SimpleDateFormat().format()方法操作获得的字符串,没报异常就是对应的数字或时间,全报异常就是字符串本回答被提问者采纳 参考技术D 那就全部按照字符串读取,然后用Integer.parseInt()方法和new SimpleDateFormat().format()方法操作获得的字符串,没报异常就是对应的数字或时间,全报异常就是字符串
以上是关于java excel怎么读取到html的主要内容,如果未能解决你的问题,请参考以下文章