时间:2021-07-01 10:21:17 帮助过:33人阅读
插件需要下载的可以点击此处下载:http://download.csdn.net/detail/s592652578/8446163
package com.odon.common.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public classExlToHtm {
//单例模式
private static ExlToHtm exlToHtm=null;
public static ExlToHtm getExlToHtm(){
if(exlToHtm==null)
exlToHtm=new ExlToHtm();
return exlToHtm;
}
public static final int EXCEL_HTML = 44;
public static final ActiveXComponent app = new ActiveXComponent("Excel.Application");// 启动exl
/**
* EXCEL转HTML
* @param xlsfile EXCEL文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public synchronized void excelToHtml(String xlsfile, Stringhtmlfile){
// ActiveXComponentapp = new ActiveXComponent("Excel.Application"); // 启动exl
try {
app.setProperty("Visible",newVariant(false));
Dispatchexcels = app.getProperty("Workbooks").toDispatch();
Dispatchexcel = 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,newObject[] {
htmlfile,newVariant(EXCEL_HTML)}, newint[1]);
Variantf = newVariant(false);
Dispatch.call(excel,"Close",f);
}catch (Exception e){
e.printStackTrace();
}finally{
app.invoke("Quit", new Variant[] {});
}
}
/**
* 修改css样式
* @param filePath
* @param cssPath
*/
public static void readCss(StringfilePath,String cssPath) {
BufferedReaderbr = null;
Stringline = null;
StringBufferbuf = newStringBuffer();
Stringcontent = null;
Filetxt=newFile(cssPath);
FileOutputStreamfos = null;
try {
fos= newFileOutputStream(txt);
// 根据文件路径创建缓冲输入流
br= newBufferedReader(newFileReader(filePath));
// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
while ((line = br.readLine())!= null){
// 此处根据实际需要修改某些行的内容
if(line.indexOf(":.5pt")>-1){
line= line.replace(":.5pt", ":1pt");
line= line + "\r";
}
buf.append(line);
}
//判断文件是否存在
if(!txt.exists()){
txt.createNewFile();
}
content= buf.toString();
byte bytes[]=new byte[1024];
bytes=content.getBytes(); //新加的
intb=content.length(); //改
fos.write(bytes,0,b);
}catch(Exception e) {
e.printStackTrace();
}finally{
// 关闭流
if (br != null) {
try {
br.close();
}catch(IOException e) {
br= null;
}
}
if( fos != null){
try {
fos.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}
}
}