① 用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);
}
}
② java如何將flex界面中的餅圖直接導出到word文檔中
FLEX只是一個前台展現,它也是訪問過後台資料庫取得的數據才展現出來的。在FLEX頁面上寫一個按鈕,調用後台JAVA去訪問資料庫取得相對的數據,然後用JAVA的方式導出成WORD文件。具體的需求還要看您了,反正核心是取得要的數據做JAVA導出,FLEX上就提供觸發導出的操作而已
③ JAVA編輯WORD文件插入圖片
試試這個代碼,需要添加spire.doc jar依賴
importcom.spire.doc.Document;
importcom.spire.doc.FileFormat;
importcom.spire.doc.Section;
importcom.spire.doc.documents.*;
importcom.spire.doc.fields.DocPicture;
publicclassInsertImage{
publicstaticvoidmain(String[]args){
//實例化Document對象
Documentdoc=newDocument();
//載入文檔
doc.loadFromFile("C:\Users\Administrator\Desktop\test.docx");
//獲取第一個section
Sectionsection=doc.getSections().get(0);
//添加一個段落
Paragraphpara=section.addParagraph();
//添加圖片到段落
DocPicturepicture=para.appendPicture("C:\Users\Administrator\Desktop\Cartoon.png");
//設置文字環繞方式(居於文字上方)
picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
//指定圖片的相對位置
picture.setHorizontalOrigin(HorizontalOrigin.Page);
picture.setHorizontalPosition(250f);
picture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
picture.setVerticalPosition(150f);
//設置圖片大小
picture.setWidth(80f);
picture.setHeight(80f);
//保存到文檔
doc.saveToFile("output/InsertImage.docx",FileFormat.Docx);
}
}
生成的Word:
④ 在java中在頁面點擊一個按鈕將頁面的數據導出word,包過圖片,請問該怎麼做啊,誰有實例,幫個忙吧。
推薦用PageOffice
用jacob只能是權宜之計,有很多毛病啊,比如:只能用在windows平台上,linux不行;伺服器上要安裝office;伺服器上自動化調用office經常崩潰、死進程,阻塞web頁面;tomcat進程需要提高交互許可權;另外代碼介面比vba還麻煩啊
⑤ 怎麼用java導出word
java導出word代碼如下:
package com.bank.util;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class WordTools {
public void createDocContext(String file) throws DocumentException,
IOException {
// 設置紙張大小
Document document = new Document(PageSize.A4);
// 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
// 設置中文字體
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 標題字體風格
Font titleFont = new Font(bfChinese, 12, Font.BOLD);
// 正文字體風格
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
Paragraph title = new Paragraph("標題");
// 設置標題格式對齊方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
String contextString = "iText是一個能夠快速產生PDF文件的java類庫。"
+ " \n"// 換行
+ "iText的java類對於那些要產生包含文本,"
+ "表格,圖形的只讀文檔是很有用的。它的類庫尤其與java Servlet有很好的給合。"
+ "使用iText與PDF能夠使你正確的控制Servlet的輸出。";
Paragraph context = new Paragraph(contextString);
// 正文格式左對齊
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 離上一段落(標題)空的行數
context.setSpacingBefore(5);
// 設置第一行空的列數
context.setFirstLineIndent(20);
document.add(context);
//利用類FontFactory結合Font和Color可以設置各種各樣字體樣式
/**
* Font.UNDERLINE 下劃線,Font.BOLD 粗體
*/
Paragraph underline = new Paragraph("下劃線的實現", FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE,
new Color(0, 0, 255)));
document.add(underline);
// 設置 Table 表格
Table aTable = new Table(3);
int width[] = {25,25,50};
aTable.setWidths(width);//設置每列所佔比例
aTable.setWidth(90); // 占頁面寬度 90%
aTable.setAlignment(Element.ALIGN_CENTER);//居中顯示
aTable.setAlignment(Element.ALIGN_MIDDLE);//縱向居中顯示
aTable.setAutoFillEmptyCells(true); //自動填滿
aTable.setBorderWidth(1); //邊框寬度
aTable.setBorderColor(new Color(0, 125, 255)); //邊框顏色
aTable.setPadding(0);//襯距,看效果就知道什麼意思了
aTable.setSpacing(0);//即單元格之間的間距
aTable.setBorder(2);//邊框
//設置表頭
/**
* cell.setHeader(true);是將該單元格作為表頭信息顯示;
* cell.setColspan(3);指定了該單元格佔3列;
* 為表格添加表頭信息時,要注意的是一旦表頭信息添加完了之後, \
* 必須調用 endHeaders()方法,否則當表格跨頁後,表頭信息不會再顯示
*/
Cell haderCell = new Cell("表格表頭");
haderCell.setHeader(true);
haderCell.setColspan(3);
aTable.addCell(haderCell);
aTable.endHeaders();
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據", fontChinese ));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(255, 0, 0));
cell.setRowspan(2);
aTable.addCell(cell);
aTable.addCell(new Cell("#1"));
aTable.addCell(new Cell("#2"));
aTable.addCell(new Cell("#3"));
aTable.addCell(new Cell("#4"));
Cell cell3 = new Cell(new Phrase("一行三列數據", fontChinese ));
cell3.setColspan(3);
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
document.add(aTable);
document.add(new Paragraph("\n"));
//添加圖片
// Image img=Image.getInstance("http://127.0.0.1:8080/testSystem/images/1_r1_c1.png");
// img.setAbsolutePosition(0, 0);
// img.setAlignment(Image.RIGHT);//設置圖片顯示位置
// img.scaleAbsolute(12,35);//直接設定顯示尺寸
// img.scalePercent(50);//表示顯示的大小為原尺寸的50%
// img.scalePercent(25, 12);//圖像高寬的顯示比例
// img.setRotation(30);//圖像旋轉一定角度
// document.add(img);
document.close();
}
public static void main(String[] args){
WordTools b=new WordTools();
try {
b.createDocContext("d:/demo.doc");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⑥ java中如何將有圖片的頁面導出成Word文檔
http://blog.163.com/ask_rent@yeah/blog/static/139893663201102471721745/
可以參考一下
⑦ java如何生成含圖片的word文檔
pageoffice很不錯,你有時間網上搜pageoffice的示例代碼參考下吧。
⑧ 怎麼用java導出word文檔
java導出word大致有6種解決方案:
1:Jacob是Java-COM Bridge的縮寫,它在Java與微軟的COM組件之間構建一座橋梁。使用Jacob自帶的DLL動態鏈接庫,並通過JNI的方式實現了在Java平台上對COM程序的調用。DLL動態鏈接庫的生成需要windows平台的支持。該方案只能在windows平台實現,是其局限性。
2:Apache POI包括一系列的API,它們可以操作基於MicroSoft OLE 2 Compound Document Format的各種格式文件,可以通過這些API在Java中讀寫Excel、Word等文件。他的excel處理很強大,對於word還局限於讀取,目前只能實現一些簡單文件的操作,不能設置樣式。
3:Java2word是一個在java程序中調用 MS Office Word 文檔的組件(類庫)。該組件提供了一組簡單的介面,以便java程序調用他的服務操作Word 文檔。 這些服務包括: 打開文檔、新建文檔、查找文字、替換文字,插入文字、插入圖片、插入表格,在書簽處插入文字、插入圖片、插入表格等。填充數據到表格中讀取表格數據 ,1.1版增強的功能: 指定文本樣式,指定表格樣式。如此,則可動態排版word文檔。是一種不錯的解決方案。
4:iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉化為PDF文件。功能強大。
5:JSP輸出樣式,該方案實現簡單,但是處理樣式有點缺陷,簡單的導出可以使用。
6:用XML做就很簡單了。Word從2003開始支持XML格式,大致的思路是先用office2003或者2007編輯好word的樣式,然後另存為xml,將xml翻譯為FreeMarker模板,最後用java來解析FreeMarker模板並輸出Doc。經測試這樣方式生成的word文檔完全符合office標准,樣式、內容控制非常便利,列印也不會變形,生成的文檔和office中編輯文檔完全一樣。
⑨ 用java技術,導出word,其中的數據是從資料庫中取出來的,含有圖片,請問用什麼能做到,poi jacob。。。
poi jacob不太好用,樓主可以用PageOffice解決這個問題。
生成文件的時候替換模板標簽數據,插入圖片或合並多個word文檔都可以實現的。
⑩ java中怎麼將word文檔怎麼生成圖片
public class CreateWordDemo
{
public void createDocContext(String file)
throws DocumentException,IOException {
//
設置紙張大小
Document document = new
Document(PageSize.A4);
//
建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中
RtfWriter2.getInstance(document, new
FileOutputStream(file));
document.open();
//
設置中文字體
BaseFont bfChinese =
BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//
標題字體風格
Font titleFont = new Font(bfChinese, 12,
Font.BOLD);
//
正文字體風格
Font contextFont = new Font(bfChinese, 10,
Font.NORMAL);
Paragraph title = new
Paragraph("標題");
//
設置標題格式對齊方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
String contextString =
"iText是一個能夠快速產生PDF文件的java類庫。"
+ " \n"//
換行
+
"iText的java類對於那些要產生包含文本,"
+ "表格,圖形的只讀文檔是很有用的。它的類庫尤其與java
Servlet有很好的給合。"
+
"使用iText與PDF能夠使你正確的控制Servlet的輸出。";
Paragraph context = new
Paragraph(contextString);
//
正文格式左對齊
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
//
離上一段落(標題)空的行數
context.setSpacingBefore(5);
//
設置第一行空的列數
context.setFirstLineIndent(20);
document.add(context);
//
利用類FontFactory結合Font和Color可以設置各種各樣字體樣式Paragraph underline = new Paragraph("下劃線的實現",
FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18,
Font.UNDERLINE, new Color(0, 0,
255)));
document.add(underline);
// 設置 Table
表格
Table aTable = new
Table(3);
int width[] = { 25, 25, 50
};
aTable.setWidths(width);//
設置每列所佔比例
aTable.setWidth(90); // 占頁面寬度
90%
aTable.setAlignment(Element.ALIGN_CENTER);//
居中顯示
aTable.setAlignment(Element.ALIGN_MIDDLE);//
縱向居中顯示
aTable.setAutoFillEmptyCells(true); //
自動填滿
aTable.setBorderWidth(1); //
邊框寬度
aTable.setBorderColor(new Color(0, 125, 255)); //
邊框顏色
aTable.setPadding(2);//
襯距,看效果就知道什麼意思了
aTable.setSpacing(3);//
即單元格之間的間距
aTable.setBorder(2);//
邊框
//
設置表頭Cell haderCell = new
Cell("表格表頭");
haderCell.setHeader(true);
haderCell.setColspan(3);
aTable.addCell(haderCell);
aTable.endHeaders();
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,
Color.GREEN);
Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據",
fontChinese));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(255, 0,
0));
cell.setRowspan(2);
aTable.addCell(cell);
aTable.addCell(new
Cell("#1"));
aTable.addCell(new
Cell("#2"));
aTable.addCell(new
Cell("#3"));
aTable.addCell(new
Cell("#4"));
Cell cell3 = new Cell(new Phrase("一行三列數據",
fontChinese));
cell3.setColspan(3);
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
document.add(aTable);
document.add(new
Paragraph("\n"));
//
添加圖片 Image.getInstance即可以放路徑又可以放二進制位元組流
Image img =
Image.getInstance("d:\\img01800.jpg");
img.setAbsolutePosition(0,
0);
img.setAlignment(Image.RIGHT);//
設置圖片顯示位置
img.scaleAbsolute(60, 60);//
直接設定顯示尺寸
//
img.scalePercent(50);//表示顯示的大小為原尺寸的50%
// img.scalePercent(25,
12);//圖像高寬的顯示比例
//
img.setRotation(30);//圖像旋轉一定角度
document.add(img);
document.close();
}public static void main(String[] args)
{
CreateWordDemo word = new
CreateWordDemo();
String file =
"d:/demo1.doc";
try
{
word.createDocContext(file);
} catch (DocumentException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}