Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
信我或關注微信號:獅范兒,回復:學習,獲取免費學習資源包。
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小時內與您取得聯系。