㈠ 請問下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中怎麼將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();
}
}
}
㈢ 怎麼用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統計圖
目前沒有成熟的直接可以寫入word的方法,只能通過先生成圖片,然後再寫入
㈤ java 把office word,ppt轉化為圖片
從一個大神那裡學來的,已測試無誤
package com;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class ExportPPT {
public static void main(String[] args) {
// 讀入PPT文件
File file = new File("D:\\UPH.ppt");
doPPTtoImage(file);
}
public static boolean doPPTtoImage(File file) {
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
return false;
}
try {
FileInputStream is = new FileInputStream(file);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
TextRun[] truns = slide[i].getTextRuns();
for (int k = 0; k < truns.length; k++) {
RichTextRun[] rtruns = truns[k].getRichTextRuns();
for (int l = 0; l < rtruns.length; l++) {
rtruns[l].setFontIndex(1);
rtruns[l].setFontName("宋體");
}
}
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.BLUE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
// 這里設置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑
File path = new File("D:/images");
if (!path.exists()) {
path.mkdir();
}
FileOutputStream out = new FileOutputStream(path + "/" + (i + 1)
+ ".jpg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
System.out.println("success!!");
return true;
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
}
return false;
}
// function 檢查文件是否為PPT
public static boolean checkFile(File file) {
boolean isppt = false;
String filename = file.getName();
String suffixname = null;
if (filename != null && filename.indexOf(".") != -1) {
suffixname = filename.substring(filename.lastIndexOf("."));
if (suffixname.equals(".ppt")) {
isppt = true;
}
return isppt;
} else {
return isppt;
}
}
}
㈥ 在java中在頁面點擊一個按鈕將頁面的數據導出word,包過圖片,請問該怎麼做啊,誰有實例,幫個忙吧。
推薦用PageOffice
用jacob只能是權宜之計,有很多毛病啊,比如:只能用在windows平台上,linux不行;伺服器上要安裝office;伺服器上自動化調用office經常崩潰、死進程,阻塞web頁面;tomcat進程需要提高交互許可權;另外代碼介面比vba還麻煩啊
㈦ 用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 WORD 轉圖片 方法 有代碼更好!
給你說個思路吧!
用java將word轉換為圖片,道理就是用java讀取word然後寫到圖片上,就像生成驗證碼似的
其中會遇到的瓶頸是:1、word裡面插入的圖片,這個你要分開來判斷。2、對於word多少頁生成一張圖片。還是整個word生成一個圖片。
希望可以幫到你。
㈨ 用Java從word中提取出圖片
因為太長了, 我放我空間了
思路是2003以後, word就可存為xml, 二進制數據按base64編碼
然後可以按解析xml文檔方式獲取圖片數據
然後對它進行解碼--
㈩ 怎麼用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();
}
}
}