请教java导出多张图片到Excel问题!
目前我遇到一个难题,就是将多张图片导出到Excel.目前我图片是导出来了,是通过SQL查询出来的结果集,然后遍历结果集循环画图片,但始终是只导出一张图片.求高手指导.if...
目前我遇到一个难题,就是将多张图片导出到Excel.目前我图片是导出来了,是通过SQL查询出来的结果集,然后遍历结果集循环画图片,但始终是只导出一张图片.求高手指导.
if(dataset.size()>0 || dataset!=null){
HSSFClientAnchor anchor = null;
ByteArrayOutputStream byteArrayOutputStream = null;
for(int i=0;i<dataset.size();i++){
byteArrayOutputStream = new ByteArrayOutputStream();
String imgPathString=dataset.get(0).get("图片").toString();
BufferedImage bufferedImage = ImageIO.read(new File(pathString));
ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
anchor = new HSSFClientAnchor(5,0,10,15,(short) 9,i+1,(short) 10,2);//i+1是第一行,按道理说第一次循环是1,第二次是2,如果有2行数据的话,会把第2行的图片导出来,第一行的好像被覆盖了一样,就是这个问题.我要每一行对应的图片都导出来.
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
patriarch.createPicture(anchor, wb.addPicture(byteArrayOutputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
}
} 展开
if(dataset.size()>0 || dataset!=null){
HSSFClientAnchor anchor = null;
ByteArrayOutputStream byteArrayOutputStream = null;
for(int i=0;i<dataset.size();i++){
byteArrayOutputStream = new ByteArrayOutputStream();
String imgPathString=dataset.get(0).get("图片").toString();
BufferedImage bufferedImage = ImageIO.read(new File(pathString));
ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
anchor = new HSSFClientAnchor(5,0,10,15,(short) 9,i+1,(short) 10,2);//i+1是第一行,按道理说第一次循环是1,第二次是2,如果有2行数据的话,会把第2行的图片导出来,第一行的好像被覆盖了一样,就是这个问题.我要每一行对应的图片都导出来.
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
patriarch.createPicture(anchor, wb.addPicture(byteArrayOutputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
}
} 展开
展开全部
package tei;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcelImage {
static List<BufferedImage> images = new ArrayList<>();
static {
try {
images.add(ImageIO.read(new File("C:/t/1.jpg")));
images.add(ImageIO.read(new File("C:/t/2.jpg")));
images.add(ImageIO.read(new File("C:/t/3.jpg")));
images.add(ImageIO.read(new File("C:/t/4.jpg")));
images.add(ImageIO.read(new File("C:/t/5.jpg")));
images.add(ImageIO.read(new File("C:/t/6.jpg")));
images.add(ImageIO.read(new File("C:/t/7.jpg")));
images.add(ImageIO.read(new File("C:/t/8.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileOutputStream fileOut = null;
try {
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
// HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
short i = 0;
for (BufferedImage image : images) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1 + i, (short) 2, 2 + i);
anchor.setAnchorType(0);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
i++;
}
HSSFRow row = sheet1.createRow(10);
short s = 10;
HSSFCell cell = row.createCell(s);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setStrikeout(true);
style.setFont(font);
cell.setCellStyle(style);
cell.setCellValue("aaaaa");
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();
}
}
}
}
}
简单的写了下。 我机器上可以运行
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcelImage {
static List<BufferedImage> images = new ArrayList<>();
static {
try {
images.add(ImageIO.read(new File("C:/t/1.jpg")));
images.add(ImageIO.read(new File("C:/t/2.jpg")));
images.add(ImageIO.read(new File("C:/t/3.jpg")));
images.add(ImageIO.read(new File("C:/t/4.jpg")));
images.add(ImageIO.read(new File("C:/t/5.jpg")));
images.add(ImageIO.read(new File("C:/t/6.jpg")));
images.add(ImageIO.read(new File("C:/t/7.jpg")));
images.add(ImageIO.read(new File("C:/t/8.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileOutputStream fileOut = null;
try {
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
// HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
short i = 0;
for (BufferedImage image : images) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1 + i, (short) 2, 2 + i);
anchor.setAnchorType(0);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
i++;
}
HSSFRow row = sheet1.createRow(10);
short s = 10;
HSSFCell cell = row.createCell(s);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setStrikeout(true);
style.setFont(font);
cell.setCellStyle(style);
cell.setCellValue("aaaaa");
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();
}
}
}
}
}
简单的写了下。 我机器上可以运行
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询