1、java将excel转湍恬擒舍换成html需要基于jacob依赖包来实现,引入jacob包在pom.xml加入媪青怍牙<dependency> <groupId>com.jacob</groupId> <artifactId>jacob</artifactId> <version>1.15</version></dependency>还需要下载jacob.dll文件,放入jdk的bin目录中
2、在F:\apps\test(自己指定一个目录用来测试)中新建一个excel文件,自己按照需要添加几条数据
3、新建一个ExcelToHtmlTe衡痕贤伎st测试类String xlsfile = new String("F:\\apps\\test\\test.xlsx"); //需要转换的excel文件String htmlfile = new String("F:\\apps\\test\\test.html"); //转换的html文件ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动exceltry { 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(44)}, new int[1]); Variant f = new Variant(false); Dispatch.call(excel, "Close", f);} catch (Exception e) { e.printStackTrace();} finally { app.invoke("Quit", new Variant[]{});}System.out.println("转换成功!");
4、运行测试类方法excelToHtml,打印结果提示转换成功
5、去测试目录查看生成好的html文件,还有一个文件夹test.files,是html依赖的文件
6、使用浏览器打开test.html文件,可以看到excel里面的内容已经成功展示在html页面上