信我或关注微信号:狮范儿,回复:学习,获取免费学习资源包。
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>>
来源网络,侵权联系删除
私信我或关注微信号:狮范儿,回复:学习,获取免费学习资源包。
o 语言没有内置 abs() 标准函数来计算整数的绝对值,这里的绝对值是指负数、正数的非负表示。
我最近为了解决 Advent of Code 2017 上边的 Day 20 难题,自己实现了一个 abs() 函数。如果你想学点新东西或试试身手,可以去一探究竟。
Go 实际上已经在 math 包中实现了 abs() : math.Abs ,但对我的问题并不适用,因为它的输入输出的值类型都是 float64,我需要的是 int64。通过参数转换是可以使用的,不过将 float64 转为 int64 会产生一些开销,且转换值很大的数会发生截断,这两点都会在文章说清楚。
帖子 Pure Go math.Abs outperforms assembly version 讨论了针对浮点数如何优化 math.Abs,不过这些优化的方法因底层编码不同,不能直接应用在整型上。
文章中的源码和测试用例在 cavaliercoder/go-abs
对我来说取绝对值最简单的函数实现是:输入参数 n 大于等于 0 直接返回 n,小于零则返回 -n(负数取反为正),这个取绝对值的函数依赖分支控制结构来计算绝对值,就命名为:abs.WithBranch
成功返回 n 的绝对值,这就是 Go v1.9.x math.Abs 对 float64 取绝对值的实现。不过当进行类型转换(int64 to float64)再取绝对值时,1.9.x 是否做了改进?我们可以验证一下:
上边的代码中,将 n 先从 int64 转成 float64,通过 math.Abs 取到绝对值后再转回 int64,多次转换显然会造成性能开销。可以写一个基准测试来验证一下:
$ go test -bench=. goos: darwin goarch: amd64 pkg: github.com/cavaliercoder/abs BenchmarkWithBranch-8 2000000000 0.30 ns/op BenchmarkWithStdLib-8 2000000000 0.79 ns/op PASS ok github.com/cavaliercoder/abs 2.320s
测试结果:0.3 ns/op, WithBranch 要快两倍多,它还有一个优势:在将 int64 的大数转化为 IEEE-754 标准的 float64 不会发生截断(丢失超出精度的值)
举个例子:abs.WithBranch(-9223372036854775807) 会正确返回 9223372036854775807。但 WithStdLib(-9223372036854775807) 则在类型转换区间发生了溢出,返回 -9223372036854775808,在大的正数输入时, WithStdLib(9223372036854775807) 也会返回不正确的负数结果。
不依赖分支控制的方法取绝对值的方法对有符号整数显然更快更准,不过还有更好的办法吗?
我们都知道不依赖分支控制的方法的代码破坏了程序的运行顺序,即 pipelining processors 无法预知程序的下一步动作。
Hacker’s Delight 第二章介绍了一种无分支控制的方法,通过 Two’s Complement 计算有符号整数的绝对值。
为计算 x 的绝对值,先计算 x >> 63 ,即 x 右移 63 位(获取最高位符号位),如果你对熟悉无符号整数的话, 应该知道如果 x 是负数则 y 是 1,否者 y 为 0
接着再计算 (x ⨁ y) - y :x 与 y 异或后减 y,即是 x 的绝对值。
可以直接使用高效的汇编实现,代码如下:
我们先命名这个函数为 WithASM,分离命名与实现,函数体使用 Go 的汇编 实现,上边的代码只适用于 AMD64 架构的系统,我建议你的文件名加上 _amd64.s 的后缀。
WithASM 的基准测试结果:
$ go test -bench=. goos: darwin goarch: amd64 pkg: github.com/cavaliercoder/abs BenchmarkWithBranch-8 2000000000 0.29 ns/op BenchmarkWithStdLib-8 2000000000 0.78 ns/op BenchmarkWithASM-8 2000000000 1.78 ns/op PASS ok github.com/cavaliercoder/abs 6.059s
这就比较尴尬了,这个简单的基准测试显示无分支控制结构高度简洁的代码跑起来居然很慢:1.78 ns/op,怎么会这样呢?
我们需要知道 Go 的编译器是怎么优化执行 WithASM 函数的,编译器接受 -m 参数来打印出优化的内容,在 go build 或 go test中加上 -gcflags=-m 使用:
运行效果:
$ go tool compile -m abs.go # github.com/cavaliercoder/abs ./abs.go:11:6: can inline WithBranch ./abs.go:21:6: can inline WithStdLib ./abs.go:22:23: inlining call to math.Abs
对于我们这个简单的函数,Go 的编译器支持 function inlining,函数内联是指在调用我们函数的地方直接使用这个函数的函数体来代替。举个例子:
实际上会被编译成:
根据编译器的输出,可以看出 WithBranch 和 WithStdLib 在编译时候被内联了,但是 WithASM 没有。对于 WithStdLib,即使底层调用了 math.Abs 但编译时依旧被内联。
因为 WithASM 函数没法内联,每个调用它的函数会在调用上产生额外的开销:为 WithASM 重新分配栈内存、复制参数及指针等等。
如果我们在其他函数中不使用内联会怎么样?可以写个简单的示例程序:
重新编译,我们会看到编译器优化内容变少了:
$ go tool compile -m abs.go abs.go:22:23: inlining call to math.Abs
基准测试的结果:
$ go test -bench=. goos: darwin goarch: amd64 pkg: github.com/cavaliercoder/abs BenchmarkWithBranch-8 1000000000 1.87 ns/op BenchmarkWithStdLib-8 1000000000 1.94 ns/op BenchmarkWithASM-8 2000000000 1.84 ns/op PASS ok github.com/cavaliercoder/abs 8.122s
可以看出,现在三个函数的平均执行时间几乎都在 1.9 ns/op 左右。
你可能会觉得每个函数的调用开销在 1.5ns 左右,这个开销的出现否定了我们 WithBranch 函数中的速度优势。
我从上边学到的东西是, WithASM 的性能要优于编译器实现类型安全、垃圾回收和函数内联带来的性能,虽然大多数情况下这个结论可能是错误的。当然,这其中是有特例的,比如提升 SIMD 的加密性能、流媒体编码等。
Go 编译器无法内联由汇编实现的函数,但是内联我们重写后的普通函数是很容易的:
编译结果说明我们的方法被内联了:
$ go tool compile -m abs.go ... abs.go:26:6: can inline WithTwosComplement
但是性能怎么样呢?结果表明:当我们启用函数内联时,性能与 WithBranch 很相近了:
$ go test -bench=. goos: darwin goarch: amd64 pkg: github.com/cavaliercoder/abs BenchmarkWithBranch-8 2000000000 0.29 ns/op BenchmarkWithStdLib-8 2000000000 0.79 ns/op BenchmarkWithTwosComplement-8 2000000000 0.29 ns/op BenchmarkWithASM-8 2000000000 1.83 ns/op PASS ok github.com/cavaliercoder/abs 6.777s
现在函数调用的开销消失了,WithTwosComplement 的实现要比 WithASM 的实现好得多。来看看编译器在编译 WithASM 时做了些什么?
使用 -S 参数告诉编译器打印出汇编过程:
编译器在编译 WithASM 和 WithTwosComplement 时,做的事情太像了,编译器在这时才有正确配置和跨平台的优势,可加上 GOARCH=386 选项再次编译生成兼容 32 位系统的程序。
最后关于内存分配,上边所有函数的实现都是比较理想的情况,我运行 go test -bench=. -benchme,观察对每个函数的输出,显示并没有发生内存分配。
WithTwosComplement 的实现方式在 Go 中提供了较好的可移植性,同时实现了函数内联、无分支控制的代码、零内存分配与避免类型转换导致的值截断。基准测试没有显示出无分支控制比有分支控制的优势,但在理论上,无分支控制的代码在多种情况下性能会更好。
最后,我对 int64 的 abs 实现如下:
func abs(n int64) int64 { y := n >> 63 return (n ^ y) - y }
via: http://cavaliercoder.com/blog/optimized-abs-for-int64-in-go.html
作者:Ryan Armstrong 译者:wuYinBest 校对:rxcai
本文由 GCTT 原创编译,Go语言中文网 荣誉推出
Java中将PDF转换为HTML可以使用开源库Apache PDFBox来实现。以下是一个简单的示例,说明如何使用PDFBox进行转换:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Entities;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
public class PdfToHtmlConverter {
public static void convertPdfToHtml(String pdfFilePath, String htmlOutputPath) {
try (PDDocument document = PDDocument.load(new File(pdfFilePath))) {
if (!document.isEncrypted()) {
PDFRenderer pdfRenderer = new PDFRenderer(document);
// 创建一个HTML文档对象
Document htmlDocument = Jsoup.parseBodyFragment("<html><head><title>Converted PDF</title></head><body></body></html>");
for (int page = 0; page < document.getNumberOfPages(); ++page) {
// 从PDF页面创建一个BufferedImage
BufferedImage image = pdfRenderer.renderImageWithDPI(page, 96, ImageType.RGB);
// 将图片转为Base64并添加到HTML中(这里假设我们只转换为图片形式的HTML)
String base64Img = getBase64Image(image);
Element imgElement = new Element("img").attr("src", "data:image/png;base64," + base64Img);
htmlDocument.body().appendChild(imgElement);
// 如果需要文本形式的HTML,则需解析图像中的文本,但这通常更为复杂,PDFBox本身并不直接提供纯文本HTML转换
}
// 输出HTML到文件
Files.write(Paths.get(htmlOutputPath), htmlDocument.html().getBytes(StandardCharsets.UTF_8));
} else {
System.err.println("The PDF file is encrypted and cannot be converted directly.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getBase64Image(BufferedImage image) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
byte[] imageData = os.toByteArray();
return Base64.getEncoder().encodeToString(imageData);
}
public static void main(String[] args) {
convertPdfToHtml("input.pdf", "output.html");
}
}
// 注意:上述代码仅演示了如何将PDF每一页转换为PNG图片,并嵌入到HTML中。
// 要获得纯文本和格式的HTML转换,可能需要更复杂的逻辑来解析PDF的文本布局和样式。
实际上,PDFBox并不直接支持将PDF内容以保持原始格式的方式完美转换成HTML。如果你需要更高级的转换,例如保留文本、表格和列表等格式,你可能需要结合其他工具或服务,或者编写自定义的PDF解析逻辑来模拟HTML结构。
*请认真填写需求信息,我们会在24小时内与您取得联系。