实现功能,导出当前页面显示员工的图片,核心代码已给出,仅供参考, 如需转载请注明出处http://www.cnblogs.com/wangjianguang/p/7852060.html

我这里一页最多将50张图片导出到excel中,花费时间大概2秒钟,导出图片做限制,主要也是为了性能考虑,

我暂时没有好的办法,因为我们一个页面最多展示50条数据,如果用户想导出100条数据,那只能点击下一页,然后点击导出。。。要是大神路过,欢迎指导,感谢!

@SuppressWarnings("deprecation")
@RequestMapping("export-employeeImage")
public void exportEmployeeImage(ModelMap model, TrainerQuery query,
HttpServletRequest request,HSSFWorkbook workbook, HttpServletResponse response)
throws NoSuchFieldException, SecurityException,
IllegalStateException, ServletException, IOException,
WriteException, SQLException {
response.reset(); response.setContentType("APPLICATION/vnd.ms-excel;charset=UTF-8");
// 注意,如果去掉下面一行代码中的attachment; 那么也会使IE自动打开文件。
response.setHeader(
"Content-Disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(
"employeePhoto"+DateUtil.getExportDate()+".xls", "UTF-8"));
OutputStream os = response.getOutputStream();
String logoRealPathDir =request.getSession().getServletContext().getRealPath("//modile//employee-photo.xlsx"); //定义的模板,模板规则每行显示5个,显示6行,上下行间隔11个空格
InputStream input_document = new FileInputStream(new File(logoRealPathDir));
XSSFWorkbook input_work = new XSSFWorkbook(input_document);
//-------------------begin导出图片-----------------------------------
XSSFSheet inpub_sheet = input_work.getSheetAt(0); // 设置样式
XSSFCellStyle textStyle10 = input_work.createCellStyle();
// 设置垂直居中
textStyle10.setAlignment(HorizontalAlignment.CENTER);
textStyle10.setVerticalAlignment((short) 1);
// 设置细边框
textStyle10.setBorderBottom(BorderStyle.THIN);
textStyle10.setBorderRight(BorderStyle.THIN);
textStyle10.setWrapText(true);
XSSFFont fontText2 = input_work.createFont();
// 字体号码
fontText2.setFontHeightInPoints((short) 11);
// 字体名称
fontText2.setFontName("微软雅黑");
textStyle10.setFont(fontText2);
//为了防止速度过慢,我这里设置了只能下载当前页的数据图片,可根据实际需要调整
query.setCurrentPage(query.getTxtcurrentPage());
query.setPageSize(query.getTxtpageSize());
query.setTotalItem(query.getTotal());
List<Map> picture = trainerService.getLecturersPhotos(query); //查询数据库获取图片,可根据实际需要修改
XSSFRow input_row = null;
int rowNum=11;
int colNum=0;
int row1=2;
int row2=10;
for (int i = 0; i < picture.size(); i++) {
Map map = (Map) picture.get(i);
// if(rowNum%11==0){
input_row = inpub_sheet.getRow(rowNum); //获取excel行
// }
String trainCode = map.get("TRAINERCODE").toString(); //读取数据
String name = map.get("NAME").toString();
input_row.getCell(colNum).setCellStyle(textStyle10); //设置单元格格式
input_row.getCell(colNum).setCellValue(trainCode+"-"+name); //往单元格中填充内容
BLOB blob = (BLOB) map.get("AVATAR");
BufferedInputStream in =null;
InputStream inStream = blob.getBinaryStream(); //得到流对象
long nLen = blob.length();
int nSize = (int) nLen;
byte[] data = new byte[nSize];
inStream.read(data);
data=ImageHepler.ChangeImgSize(data,130,160); //将读取数据流转换成图片,并设置大小
inStream = new ByteArrayInputStream(data);
try {
in = new BufferedInputStream(inStream,1024);
} catch (Exception e1) {
e1.printStackTrace();
}
int pictureIdx =input_work.addPicture(in, input_work.PICTURE_TYPE_JPEG);//向excel中插入图片
XSSFDrawing drawing = inpub_sheet.createDrawingPatriarch(); //创建绘图对象
// XSSFClientAnchor的参数说明:
// 参数 说明
// dx1 第1个单元格中x轴的偏移量
// dy1 第1个单元格中y轴的偏移量
// dx2 第2个单元格中x轴的偏移量
// dy2 第2个单元格中y轴的偏移量
// col1 第1个单元格的列号
// row1 第1个单元格的行号
// col2 第2个单元格的列号
// row2 第2个单元格的行号
XSSFClientAnchor anchor= new XSSFClientAnchor(1, 1, 1, 1,(short) colNum, row1, (short) colNum+1, row2);//定位图片的位置
XSSFPicture pict = drawing.createPicture(anchor, pictureIdx);
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
pict.resize();
colNum=colNum+3;
if(colNum==15){
colNum=0;
rowNum=rowNum+13;
row1=row1+13;
row2=row2+13;
}
}
try {
input_work.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
package common.image.util;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class ImageHepler
{ /**
* 图片转换,将读取图片放大或缩小
*
* */
public static byte[] ChangeImgSize(byte[] data, int nw, int nh){
byte[] newdata = null;
try{
BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));
int w = bis.getWidth();
int h = bis.getHeight();
double sx = (double) nw / w;
double sy = (double) nh / h;
AffineTransform transform = new AffineTransform();
transform.setToScale(sx, sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
//原始颜色
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis, bid);
//转换成byte字节
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bid, "jpeg", baos);
newdata = baos.toByteArray();
}catch(IOException e){
e.printStackTrace();
}
return newdata;
}
}

java 导出blob图片到excel的更多相关文章

  1. 导出带图片的Excel——OOXML文件分析

    需求: 普通js导出文件excel具有兼容性问题,通过js-xsl导出文件API未找到导出图片的方案,实例过少,因此针对07年后以.xlsx后缀的excel文件,通过修改后缀.zip参考文件模板来实现 ...

  2. 导出包含图片的excel、word、pdf 笔记

    /** * 导出word * @throws Exception */ @Override public byte[] WordExport( List<VbLibGlobalAnalyList ...

  3. Java导出带格式的Excel数据到Word表格

    前言 在Word中创建报告时,我们经常会遇到这样的情况:我们需要将数据从Excel中复制和粘贴到Word中,这样读者就可以直接在Word中浏览数据,而不用打开Excel文档.在本文中,您将学习如何使用 ...

  4. 如何通过Java导出带格式的 Excel 数据到 Word 表格

    在Word中制作报表时,我们经常需要将Excel中的数据复制粘贴到Word中,这样则可以直接在Word文档中查看数据而无需打开另一个Excel文件.但是如果表格比较长,内容就会存在一定程度的丢失,无法 ...

  5. java 导出百万数据到excel

    @RequestMapping("export") public void write(HttpServletRequest request,HttpServletResponse ...

  6. java导出csv文件使用Excel打开乱码问题

    写一个csv文件,发现使用 notpad++ 打开是没有问题的,但是使用 Excel 打开之后显示乱码 刚开始的代码是这样子的: ByteArrayOutputStream os = new Byte ...

  7. 导出带有图片的excel

    public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream("d:\ ...

  8. JAVA将Excel中的报表导出为图片格式(一)问题背景

    如题所示,先抛出一个问题,如何使用JAVA将Excel中的报表导出为图片格式? 首先说一下这个问题的背景,也就是为什么博主会碰到这个问题 随着微信,易信之流大行其道,企业内部的办公交流.绩效考评甚至考 ...

  9. Java 导出EXCEL

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

随机推荐

  1. zoj1494 暴力模拟 简单数学问题

    Climbing Worm Time Limit: 2 Seconds      Memory Limit:65536 KB An inch worm is at the bottom of a we ...

  2. There is no getter for property named xxx' in 'class java.lang.xxx'

    在xxxMapper.xml我们使用sql片段来提高sql代码的复用性,当时新手传入参数时常常出现这样的错误: There is no getter for property named xxx' i ...

  3. How to Add Columns to a DataGrid through Binding and Map Its Cell Values

    How to Add Columns to a DataGrid through Binding and Map Its Cell Values Lance Contreras, 7 Nov 2013 ...

  4. DOS命令(系统错误5,拒绝访问)的解决方法

    在用DOS命令启动MySQL服务时,出现(系统错误5,拒绝访问)的错误提示,这是由于我们操作的权限不足造成的,需要以管理员身份启动,解决问题方法如下: 1."Windows+S"- ...

  5. web 导出 csv

    public void ProcessRequest(HttpContext context)        {            //DownloadFile("教程.csv" ...

  6. IIS ApplicationPoolIdentity(配置IIS讀寫網站文件)

    原创地址:http://www.cnblogs.com/jfzhu/p/4067297.html 转载请注明出处 从IIS 7.5开始,Application Pool Identity的Built- ...

  7. C# 6.0 $"Hello {csdn}"

    "hello $world"的格式化字符串是指把字符串中一个单词,以一个标示开头.可以代换为单词所指的变量. 这个在jq有,而C#string的格式只能用格式的字符占位符,格式的字 ...

  8. hibernate利用mysql的自增张id属性实现自增长id和手动赋值id并存

    我们知道在mysql中如果设置了表id为自增长属性的话,insert语句中如果对id赋值(值没有被用到过)了,则插入的数据的id会为用户设置的值,并且该表的id的最大值会重新计算,以插入后表的id最大 ...

  9. Linux下文件打包与解压缩

    Linux上存在的文件后缀 文件后缀名 说明 *.zip zip程序打包压缩的文件 *.rar rar程序压缩的文件 *.7z 7zip程序压缩的文件 *.tar tar程序打包,未压缩的文件 *.g ...

  10. 我的第一个python web开发框架(12)——工具函数包说明(三)

    mail_helper.py是邮件操作包,用来发送邮件的. #!/usr/bin/evn python # coding=utf-8 import smtplib from email.mime.te ...