62,623
社区成员
发帖
与我相关
我的任务
分享package cn.ujjboy.TestMateril;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
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;
/**
* Title: 导出图片 <br>
* Description: <br>
* QQ: 247799110 <br>
*
* @author <a href=mailto:ujjboy@gmail.com>ujjboy</a>
*/
public class ExportImg2Xls {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("1111");
String localPath = "d://";
// 尽管是带 /down.jsp?filename=aaa.gif 类型的 也会自动跳转
String romotePath = "http://www.baidu.com/img/logo.gif";
// String romotePath = "http://192.168.93.1:8010/jsp/portal/lib/default/images/logo99.jpg";
System.out.println("下载地址" + romotePath);
String path = getFile(romotePath, localPath);// 得到图片的本地地址
System.out.println("本地地址" + path);
// 创建一个图片区域
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor;
// 加载图片
anchor = new HSSFClientAnchor(0, 0, 255, 255, (short) 0,0, (short) 2, 2);
// 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
anchor.setAnchorType(0);
patriarch.createPicture(anchor, loadPicture(path, workbook));
FileOutputStream fileOut = new FileOutputStream("ExportImg2Xls.xls");
workbook.write(fileOut);
fileOut.close();
System.out.println("Export OK~~");
}
/**
* 通过地址把文件下载下来保存到本地
*
* @param srcUrl
* @return 本地实际地址
* @throws IOException
*/
public static String getFile(String srcUrl, String localPath) throws IOException {
URL url = new URL(srcUrl);
String srcFliename = "logo.gif";// 自己用方法得到filename
String realFilePath = localPath + srcFliename;
System.out.println("正在下载" + realFilePath);
try {
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
FileOutputStream out = new FileOutputStream(new File(realFilePath));
byte buffer[] = new byte[1024];
int count = 0;
while ((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
out.flush();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("下载完成");
return realFilePath;
}
/**
* 通过图片路径得到图片
*
* @param path
* @param wb
* @return
* @throws IOException
*/
private static int loadPicture(String path, HSSFWorkbook wb) throws IOException {
int pictureIndex;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(path);
bos = new ByteArrayOutputStream();
int c;
while ((c = fis.read()) != -1)
bos.write(c);
pictureIndex = wb.addPicture(bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG);
} finally {
if (fis != null)
fis.close();
if (bos != null)
bos.close();
}
return pictureIndex;
}
}