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 class ExlToHtm {
//单例模式
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", new Variant( 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, new Object[] {
htmlfile, new Variant(EXCEL_HTML)}, newint [1]);
Variantf = new Variant( 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 = new StringBuffer();
Stringcontent = null ;
Filetxt= new File(cssPath);
FileOutputStreamfos = null ;
try {
fos= new FileOutputStream(txt);
// 根据文件路径创建缓冲输入流
br= new BufferedReader( new FileReader(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(); //新加的
int b=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();
}
}
}
}
}
查看更多关于Exl转换成html的方法_html/css_WEB-ITnose的详细内容...