本文目錄一覽:
編寫程序,將一個Java文件轉換為HTML一個文件
java中將java文件轉換為html一個文件,先使用file類讀取java文件,然後使用string進行分割、替換等操作,輸出html後綴名的文件,如下代碼:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Change {
String textHtml = “”;
String color = “#00688B”;
//讀取文件
public void ReadFile(String filePath) {
BufferedReader bu = null;
InputStreamReader in = null;
try {
File file = new File(filePath);
if (file.isFile() file.exists()) {
in = new InputStreamReader(new FileInputStream(file));
bu = new BufferedReader(in);
String lineText = null;
textHtml = “htmlbody”;
while ((lineText = bu.readLine()) != null) {
lineText = changeToHtml(lineText);
lineText += “/br”;
textHtml += lineText;
}
textHtml += “/html/body”;
} else {
System.out.println(“文件不存在”);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bu.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//輸出文件
public void writerFile(String writepath) {
File file = new File(writepath);
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(file));
System.out.println(textHtml);
output.write(textHtml);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//文件轉換
public String changeToHtml(String text) {
text = text.replace(“”, “”);
text = text.replace(” “, ” “);
text = text.replace(“”, “”);
text = text.replace(“”, “”);
text = text.replace(“\””, “””);
text = text.replace(” “, ” “);
text = text.replace(“public”, “bfont color='”+color+”‘public/font/b”);
text = text.replace(“class”, “bfont color='”+color+”‘class/font/b”);
text = text.replace(“static”, “bfont color='”+color+”‘static/font/b”);
text = text.replace(“void”, “bfont color='”+color+”‘void/font/b”);
String t = text.replace(“//”, “font color=green//”);
if (!text.equals(t)) {
System.out.println(“t:”+t);
text = t + “/font”;
}
return text;
}
public static void main(String[] args) {
System.out.println(“第一個參數為讀取文件路徑,第二個參數為生成文件路徑”);
if(args.length1){
System.out.println(“請a href=”;tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3P16znjKBn1uWPvnzPWcY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DsPjTdnWTvPjT3n1T4n1ckn1b” target=”_blank” class=”baidu-highlight”輸入文件/a路徑”);
return ;
}else if(args.length2){
System.out.println(“請輸入生成文件”);
return;
}
Change c = new Change();
c.ReadFile(args[0]);
c.writerFile(args[1]);
}
}
如何在java中實現自動生成html
創建一個StringBuilder對象,通過append方法來為其添加html語句。
StringBuilder sb = new StringBuilder();
Properties fileProperties = getProperties(“file”);
Properties sqlProperties = getProperties(“sql”);
PrintStream printStream = new PrintStream(new FileOutputStream(
“report.html”));
sb.append(“html”);
sb.append(“head”);
sb.append(“title每日運營報表/title”);
sb.append(“meta http-equiv=\”Content-Type\” content=\”text/html; charset=utf-8\” /”);
sb.append(“style type=\”text/css\””);
sb.append(“TABLE{border-collapse:collapse;border-left:solid 1 #000000; border-top:solid 1 #000000;padding:5px;}”);
sb.append(“TH{border-right:solid 1 #000000;border-bottom:solid 1 #000000;}”);
sb.append(“TD{font:normal;border-right:solid 1 #000000;border-bottom:solid 1 #000000;}”);
sb.append(“/style/head”);
sb.append(“body bgcolor=\”#FFF8DC\””);
sb.append(“div align=\”center\””);
sb.append(“br/”);
sb.append(“br/”);
ListMapString, Object result1 = getRpt(sqlProperties
.getProperty(“sql1”));
for (Map.EntryString, Object m : result1.get(0).entrySet()) {
sb.append(fileProperties.getProperty(“file1”));
sb.append(m.getValue());
}
sb.append(“br/br/”);
輸出
sb.append(“/div/body/html”);
printStream.println(sb.toString());
怎樣用Java把word文檔轉換為html文檔
可以通過Spire.Doc for Java進行轉換。
首先需要安裝Spire.Doc for Java。可在 Java 程序中添加 Spire.Doc for Java 文件作為依賴項。JAR 文件可以從此鏈接下載。 如果您使用 Maven,則可以將以下代碼添加到項目的 pom.xml 文件中,從而輕鬆地在應用程序中導入 JAR 文件。
repositories
repository
idcom.e-iceblue/id
namee-iceblue/name
url;/url
/repository/repositoriesdependencies
dependency
groupIde-iceblue/groupId
artifactIdspire.doc/artifactId
version5.2.3/version
/dependency/dependencies
Java代碼如下:
mport com.spire.doc.*;public class WordtoHtml {
public static void main(String[] args) {
//實例化Document類的對象
Document doc = new Document();
//加載Word文檔
doc.loadFromFile(“inputfile.docx”);
//保存為HTML格式
doc.saveToFile(“ToHtml.html”,FileFormat.Html);
doc.dispose();
}
}
希望對您有幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/237776.html