POI操作EXCELの挿入画像2003,2007
7378 ワード
もっと読む
必要なカバンは poi-ooxml
下記はダウンロードアドレスです。
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22poi-ooxml%22
直接コードをつけて、言うべきことは全部コメントに書いてあります。
必要なカバンは poi-ooxml
下記はダウンロードアドレスです。
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22poi-ooxml%22
直接コードをつけて、言うべきことは全部コメントに書いてあります。
package com.excel;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
//Excel 2003
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//Excel 2007
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestExcel {
public static void main(String[] args) throws IOException {
// Excel
String picPath="http://pic.nipic.com/2007-11-09/2007119122712983_2.jpg";
XSSFWorkbook xWb = new XSSFWorkbook();
TestExcel.insertImage(xWb,picPath);
TestExcel.insertImage(picPath);
FileOutputStream fileOut = null;
//
fileOut = new FileOutputStream("F:/1/excel2007x"
+ System.currentTimeMillis() + ".xlsx");
xWb.write(fileOut);
fileOut.close();
System.out.println("well Done ! ");
}
// 2003 excel
public static void insertImage(String picPath){
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
try {
// Excel
//HSSFWorkbook wb=new HSSFWorkbook(new FileInputStream(new File("F:/16.jpg")));
HSSFWorkbook wb=new HSSFWorkbook();
//
HSSFSheet sheet1 = wb.createSheet("test picture");
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
for(int i=0;i<2;i++){
//new URL
URL url = new URL(picPath);
//
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// "GET"
conn.setRequestMethod("GET");
// 5
conn.setConnectTimeout(5 * 1000);
//
InputStream inStream = conn.getInputStream();
// ByteArrayOutputStream , ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
// File
bufferImg = ImageIO.read(inStream);
//bufferImg = ImageIO.read(new File("F:/16.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
// , 1023 255, ,
HSSFClientAnchor anchor = new HSSFClientAnchor(500, 120, 1023, 255,
(short) 0, 0+i*30, (short) 10, 20+i*20);
anchor.setAnchorType(3);
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
byteArrayOut.close();
inStream.close();
conn.disconnect();
}
fileOut = new FileOutputStream("F:/1/excel2003h"
+ System.currentTimeMillis() + ".xls");
// excel
wb.write(fileOut);
fileOut.close();
} catch (IOException io) {
io.printStackTrace();
System.out.println("erorr : " + io.getMessage());
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 2007 excel
public static void insertImage(XSSFWorkbook wb,String picPath) {
BufferedImage bufferImg = null;
try {
//new URL
URL url = new URL(picPath);
//
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// "GET"
conn.setRequestMethod("GET");
// 5
conn.setConnectTimeout(5 * 1000);
//
InputStream inStream = conn.getInputStream();
//
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//
bufferImg = ImageIO.read(inStream);
// bufferImg = ImageIO.read(new File("F:/16.jpg"));
// png、gif jpg png OK , , ,
ImageIO.write(bufferImg, "jpg", byteArrayOut);
// excel Sheet , ,
XSSFSheet sheet=wb.createSheet("sheet0");
// 2003 HSSFPatriarch
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
// , , 10000 。 , 2003 HSSF
XSSFClientAnchor anchor = new XSSFClientAnchor(50*10000, 0, 100, 100,
2, 10, 12, 25);
// 0、2、3 , ,
//anchor.setAnchorType(2);
// png、gif
patriarch
.createPicture(anchor, wb.addPicture(
byteArrayOut.toByteArray(),
XSSFWorkbook.PICTURE_TYPE_JPEG));
byteArrayOut.close();
inStream.close();
conn.disconnect();
} catch (FileNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
}
}
}