整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

程序员必备知识干货:Excel文件转成html页面代码

信我或关注微信号:狮范儿,回复:学习,获取免费学习资源包。


Excel文件转成html页面代码

main类:启动类


package com.test;
 
import trans.toHtml;
 
public class testToHtml {
 
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
 toHtml th=new toHtml();
 // System.out.println(System.getProperty("java.library.path"));
 //-Djava.library.path=D:\jar\jacob_1.9
 th.excelToHtml("d:/excel/运维门户通讯录.xlsx", "d:/test.html");
}
 
}

代码:


package trans;
 
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 toHtml {
 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;
 }
}
需要的jar包
<<jacob.jar>>
<<toHtml.java>>
<<testToHtml.java>>

来源网络,侵权联系删除

私信我或关注微信号:狮范儿,回复:学习,获取免费学习资源包。

pire.XLS for Java 13.3.6 已发布。本次更新支持将Excel转换为独立的HTML文件,支持了保存整个Excel到HTML流,支持了给透视表的透视域字段(PivotFields)和数据域字段(DataFields)添加条件格式,并且支持取代一个单元格内部分文本的功能。同时,该版本还Excel 到 PDF和图片的转换。此外,一些已知问题也得到了修复,如修复了转换图表到图片时引导线丢失的问题。详情请阅读以下内容。

新功能:

  • 支持了将Excel转换为独立的HTML文件。
Workbook wb = new Workbook();
wb.loadFromFile("data/mytest.xlsx");
HTMLOptions.Default.isStandAloneHtmlFile(true);
wb.saveToFile("result.html", FileFormat.HTML);
  • 支持了保存整个Excel到HTML流。
Workbook wb = new Workbook();
wb.loadFromFile("data/mytest.xlsx");
FileOutputStream fileStream = new FileOutputStream("output/saveStream_result.html");
wb.saveToStream(fileStream, FileFormat.HTML);
fileStream.close();
  • 支持了给透视表的透视域字段(PivotFields)和数据域字段(DataFields)添加条件格式。
PivotTable table = (PivotTable)worksheet.getPivotTables().get(0);
PivotConditionalFormatCollection pcfs = table.getPivotConditionalFormats();
PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getColumnFields().get(0));
//PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getRowFields().get(0));
//PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getDataFields().get(0));
//PivotConditionalFormat pc = pcfs.addPivotConditionalFormat(table.getPageFields().get(0));
IConditionalFormat cf = pc.addCondition();
cf.setFormatType(ConditionalFormatType.ContainsBlanks);
cf.setFillPattern(ExcelPatternType.Solid);
cf.setBackColor(Color.Red);
  • 支持了取代一个单元格内部分文本的功能。
sheet.getCellList().get(0).textPartReplace("Office", "Spire");

问题修复:

  • 修复了转换图表到图片时引导线丢失的问题。
  • 修复了转换Excel到图片时部分数据位置下移的问题。
  • 修复了转换Excel到图片时双下划线变单下划线的问题。
  • 修复了为Excel添加页脚时程序报错java.lang.NullPointerException的问题。
  • 修复了转换Excel到PDF时程序报错Invalid sectionId of Header Footer image 的问题。
  • 修复了转换Excel到图片时进度条中的进度颜色丢失的问题。
  • 修复了转换图表到图片时X轴数据偏移的问题。
  • 修复了转换Excel到PDF时内容不正确的问题。

下载Spire.XLS for Java 13.3.6请点击:

Spire.XLS for Java | 下载

pire.XLS for Java 12.6.0 已发布。此版本支持在 Excel 转 HTML 时设置是否转出隐藏的工作表。同时,还增加了两个新特性: Excel 转 HTML 后,转出的 HTML 中当前选中的选项卡添加了有对比性的颜色边框以及工作表选项卡文本增加了两侧间距。此外,本次更新还增强了 Excel 到 HTML、 Excel 到 PDF 和 Excel 到图片的转换功能。一些已知问题也得到了修复,如复制工作表时迷你折线图丢失和设置单元格背景色为透明不生效等问题。详情请阅读一下内容。

新功能:

  • 支持在Excel转HTML时设置是否转出隐藏的工作表。
Workbook book=new Workbook();
book.loadFromFile("test.xlsx");
//false --- 转出隐藏的工作表
//true--- 不转出隐藏的工作表
book.saveToHtml("output.html",false);
  • Excel转HTML后,转出的HTML中当前选中的选项卡添加了有对比性的颜色边框。
  • Excel转HTML后,工作表选项卡文本增加了两侧间距。

问题修复:

  • 修复了更新图表数据错误的问题。
  • 修复了Excel转图片后,内容超出单元格以外的问题。
  • 修复了Excel转图片时,抛出“NullPointerException”异常的问题。
  • 修复了Excel转HTML后,工作表选项卡背景色以及文本颜色改变的问题。
  • 修复了Excel转PDF时程序挂起的问题。
  • 修复了复制工作表时,迷你折线图丢失的问题。
  • 修复了保存文档后,宏丢失的问题。
  • 修复了加载XML文件时抛出“ArrayIndexOutOfBoundsException”异常的问题。
  • 修复了Excel转HTML后,内容位置偏移的问题。。
  • 修复了图表转换到图片时设置DPI不生效的问题。
  • 修复了Excel转PDF后,部分数据值不正确的问题。
  • 修复了设置单元格背景色为透明不生效的问题。

下载Spire.XLS for Java 12.6.0 请点击:

Spire.XLS for Java | 下载