⑴ java如何將有圖片的html轉為word
如果你只是想要不依賴網路的存儲圖片的話,可以試試把圖片轉base64
⑵ java中如何導出word
如果電腦里裝的有WORD,可以通過JS新建一個WORD,並把頁面文件拷貝進去。我這有操作Excel的,word的差不多。<input type="button" onclick="javascript:method1('tableExcel');" value="導入到EXCEL"> <SCRIPT LANGUAGE="javascript"> function method1(tableid) {//整個表格拷貝到EXCEL中 var curTbl = document.getElementById(tableid); var oXL = new ActiveXObject("Excel.Application"); //創建AX對象excel var oWB = oXL.Workbooks.Add(); //獲取workbook對象 var oSheet = oWB.ActiveSheet; //激活當前sheet var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); //把表格中的內容移到TextRange中 sel.select(); //全選TextRange中內容 sel.execCommand("Copy"); //復制TextRange中內容 oSheet.Paste(); //粘貼到活動的EXCEL中 oXL.Visible = true; //設置excel可見屬性 } function method2(tableid) //讀取表格中每個單元到EXCEL中 { var curTbl = document.getElementById(tableid); var oXL = new ActiveXObject("Excel.Application"); //創建AX對象excel var oWB = oXL.Workbooks.Add(); //獲取workbook對象 var oSheet = oWB.ActiveSheet; //激活當前sheet var Lenr = curTbl.rows.length; //取得表格行數 for (i = 0; i < Lenr; i++) { var Lenc = curTbl.rows(i).cells.length; //取得每行的列數 for (j = 0; j < Lenc; j++) { oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText; //賦值 } } oXL.Visible = true; //設置excel可見屬性 } function getXlsFromTbl(inTblId, inWindow) { try { var allStr = ""; var curStr = ""; //alert("getXlsFromTbl"); if (inTblId != null && inTblId != "" && inTblId != "null") { curStr = getTblData(inTblId, inWindow); } if (curStr != null) { allStr += curStr; } else { alert("你要導出的表不存在!"); return; } var fileName = getExcelFileName(); doFileExport(fileName, allStr); } catch(e) { alert("導出發生異常:" + e.name + "->" + e.description + "!"); } } function getTblData(inTbl, inWindow) { var rows = 0; //alert("getTblData is " + inWindow); var tblDocument = document; if (!!inWindow && inWindow != "") { if (!document.all(inWindow)) { return null; } else { tblDocument = eval(inWindow).document; } } var curTbl = tblDocument.getElementById(inTbl); var outStr = ""; if (curTbl != null) { for (var j = 0; j < curTbl.rows.length; j++) { //alert("j is " + j); for (var i = 0; i < curTbl.rows[j].cells.length; i++) { //alert("i is " + i); if (i == 0 && rows > 0) { outStr += " \t"; rows -= 1; } outStr += curTbl.rows[j].cells[i].innerText + "\t"; if (curTbl.rows[j].cells[i].colSpan > 1) { for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) { outStr += " \t"; } } if (i == 0) { if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) { rows = curTbl.rows[j].cells[i].rowSpan - 1; } } } outStr += "\r\n"; } } else { outStr = null; alert(inTbl + "不存在!"); } return outStr; } function getExcelFileName() { var d = new Date(); var curYear = d.getYear(); var curMonth = "" + (d.getMonth() + 1); var curDate = "" + d.getDate(); var curHour = "" + d.getHours(); var curMinute = "" + d.getMinutes(); var curSecond = "" + d.getSeconds(); if (curMonth.length == 1) { curMonth = "0" + curMonth; } if (curDate.length == 1) { curDate = "0" + curDate; } if (curHour.length == 1) { curHour = "0" + curHour; } if (curMinute.length == 1) { curMinute = "0" + curMinute; } if (curSecond.length == 1) { curSecond = "0" + curSecond; } var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_" + curHour + curMinute + curSecond + ".csv"; //alert(fileName); return fileName; } function doFileExport(inName, inStr) { var xlsWin = null; if (!!document.all("glbHideFrm")) { xlsWin = glbHideFrm; } else { var width = 6; var height = 4; var openPara = "left=" + (window.screen.width / 2 - width / 2) + ",top=" + (window.screen.height / 2 - height / 2) + ",scrollbars=no,width=" + width + ",height=" + height; xlsWin = window.open("", "_blank", openPara); } xlsWin.document.write(inStr); xlsWin.document.close(); xlsWin.document.execCommand('Saveas', true, inName); xlsWin.close(); } </SCRIPT>]
⑶ java freemarker引擎 導出word文檔
要生成的數據,在java後台必須是用Map格式,這個你檢查一下有沒有問題。如果有list,也要放到map裡面去。
HashMap<String,Object>dataMap=newHashMap<String,Object>();
Configurationconf=newConfiguration();
Templatet=conf.getTemplate(fileName);
FiledocFile=newFile(filePath+"/"+docName);
out=newOutputStreamWriter(newFileOutputStream(docFile),"UTF-8");
t.process(dataMap,out);
⑷ 請問下java中導出圖片怎麼做
package com.xolt;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{
//先把讀進來的圖片放到一個ByteArrayOutputStream中,以便產生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
ImageIO.write(bufferImg1,"jpg",byteArrayOut1);
//創建一個工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("poi picT");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);
anchor1.setAnchorType(2);
//插入圖片
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("c:/workbook.xls");
//寫入excel文件
wb.write(fileOut);
fileOut.close();
}catch(IOException io){
io.printStackTrace();
System.out.println("io erorr : "+ io.getMessage());
} finally
{
if (fileOut != null)
{
try {
fileOut.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
poi中圖片到到excel的方法 你需要准備poi包 試試看看
⑸ java 如何將html(包含表格,圖片)轉換為word
你用IE瀏覽器打開你的HTML文件,然後點擊菜單欄 文件→使用 Microsoft Office Word 編輯,之後系統會自動打開 Word 並顯示HTML文件的內容,這是保存即可。
如果找不到「使用 Microsoft Office Word 編輯」的話,點擊菜單欄 工具→Internet 選項→程序→ HTML 編輯器 → Microsoft Office Word → 確定。
⑹ 用java怎麼將word文檔轉成圖片格式
可以使用Spire.Doc for Java在Java中利用代碼進行轉換。需要在 Java 程序中添加Free Spire.Doc.jar文件作為依賴項。可以從這個鏈接下載 JAR 文件;如果使用Maven,則可以通過在 pom.xml 文件中添加以下代碼導入 JAR 文件。
repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository></repositories><dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.3</version>
</dependency></dependencies>
Java代碼如下:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertWordToOtherFormats {
public static void main(String[] args) throws IOException {
//創建Document對象
Document doc = new Document();
//載入Word文檔
doc.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");
//將指定頁保存為BufferedImage
BufferedImage image= doc.saveToImages(0, ImageType.Bitmap);
//將圖片數據保存為PNG格式文檔
File file= new File("output/ToPNG.png");
ImageIO.write(image, "PNG", file);
//將Word保存為SVG格式
doc.saveToFile("output/ToSVG.svg",FileFormat.SVG);
//將Word保存為RTF格式
doc.saveToFile("output/ToRTF.rtf",FileFormat.Rtf);
//將Word保存為XPS格式
doc.saveToFile("output/ToXPS.xps",FileFormat.XPS);
//將Word保存為XML格式
doc.saveToFile("output/ToXML.xml",FileFormat.Xml);
//將Word保存為TXT格式
doc.saveToFile("output/ToTXT.txt",FileFormat.Txt);
}
}