/**
* 导出word
* @throws Exception
*/
@Override
public byte[] WordExport(
List<VbLibGlobalAnalyListVoRes> vbLibGlobalAnalyList, String picId) throws Exception {
//Blank Document
CustomXWPFDocument document= null;
ByteArrayOutputStream os = null;
try{
os=new ByteArrayOutputStream();
document= new CustomXWPFDocument ();
//添加标题
XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText("漏洞风险分析");
titleParagraphRun.setColor("000000");
titleParagraphRun.setFontSize(20); //工作经历表格
XWPFTable ComTable = document.createTable();
//列宽自动分割
CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
comTableWidth.setType(STTblWidth.DXA);
comTableWidth.setW(BigInteger.valueOf(9072)); //表格第一行
XWPFTableRow comTableRowOne = ComTable.getRow(0);
comTableRowOne.getCell(0).setText("编号");
comTableRowOne.addNewTableCell().setText("漏洞名称");
comTableRowOne.addNewTableCell().setText("SG编号");
comTableRowOne.addNewTableCell().setText("受影响资产数量");
comTableRowOne.addNewTableCell().setText("漏洞描述");
comTableRowOne.addNewTableCell().setText("发布日期"); for (int i=0;i<vbLibGlobalAnalyList.size();i++) {
VbLibGlobalAnalyListVoRes voRes = vbLibGlobalAnalyList.get(i);
//表格第二行
XWPFTableRow comTableRowTwo = ComTable.createRow();
comTableRowTwo.getCell(0).setText(i+1+"");
comTableRowTwo.getCell(1).setText(voRes.getVbName());
comTableRowTwo.getCell(2).setText(voRes.getSgvbCode());
comTableRowTwo.getCell(3).setText(voRes.getInfluenceNum());
comTableRowTwo.getCell(4).setText(voRes.getVbDesc());
comTableRowTwo.getCell(5).setText(voRes.getFoundTime());
} /**
* 插入图片
*/
//下载mongdb图片
byte[] fileBody = fileDao.getBody(picId);
XWPFParagraph pargraph = document.createParagraph();
document.addPictureData(fileBody, XWPFDocument.PICTURE_TYPE_PNG);
document.createPicture(document.getAllPictures().size()-1, 600, 395, pargraph);
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr); document.write(os);
return os.toByteArray();
}catch (Exception e) {
throw e;
}finally{
if(null!=os){
os.close();
}
if(null!=document){
document.close();
}
}
}
/**
* 导出pdf
*/
@Override
public byte[] PdfExport(
List<VbLibGlobalAnalyListVoRes> vbLibGlobalAnalyList, String picId) throws Exception {
ByteArrayOutputStream bos =null;
Document document=null;
try {
bos = new ByteArrayOutputStream();
/** 实例化文档对象 */
document = new Document(PageSize.A4, 50, 50, 50, 50);
/** 创建 PdfWriter 对象 */
PdfWriter.getInstance(document,// 文档对象的引用
bos);//文件的输出路径+文件的实际名称
document.open();// 打开文档
/** pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包 */
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", false);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);//加入document:
/** 创建章节对象 */
// Paragraph title1 = new Paragraph("漏洞风险分析表", FontChinese);
Chapter chapter1 = new Chapter(1);
chapter1.setNumberDepth(1);
/** 创建章节中的小节 */
Paragraph title11 = new Paragraph("漏洞风险分析表", FontChinese);
Section section1 = chapter1.addSection(title11);
/** 创建表格对象(包含行列矩阵的表格) */
Table t = new Table(6);// X行6列
/* t.setBorderColor(new Color(220, 255, 100));
t.setPadding(5);
t.setSpacing(5);
t.setBorderWidth(1);*/
Cell c1 = new Cell(new Paragraph("编号", FontChinese));
t.addCell(c1);
c1 = new Cell(new Paragraph("漏洞名称", FontChinese));
t.addCell(c1);
c1 = new Cell(new Paragraph("SG编号", FontChinese));
t.addCell(c1);
c1 = new Cell(new Paragraph("受影响资产数量", FontChinese));
t.addCell(c1);
c1 = new Cell(new Paragraph("漏洞描述", FontChinese));
t.addCell(c1);
c1 = new Cell(new Paragraph("发布日期", FontChinese));
t.addCell(c1);
// 第二行开始不需要new Cell()
for (int i = 0; i < vbLibGlobalAnalyList.size(); i++) {
VbLibGlobalAnalyListVoRes voRes = vbLibGlobalAnalyList.get(i);
t.addCell(i+1+"");
t.addCell(voRes.getVbName());
t.addCell(voRes.getSgvbCode());
t.addCell(voRes.getInfluenceNum());
t.addCell(voRes.getVbDesc());
t.addCell(voRes.getFoundTime());
}
section1.add(t); //创建章节对象
// Paragraph title2 = new Paragraph("漏洞风险分析图", FontChinese);
Chapter chapter2 = new Chapter(1);
chapter2.setNumberDepth(1);
//创建章节中的小节
Paragraph title12 = new Paragraph("漏洞风险分析图", FontChinese);
Section section2 = chapter2.addSection(title12);
// 添加图片 byte[] fileBody = fileDao.getBody(picId); Image img = Image.getInstance(fileBody);//图片的地址
// img.setAbsolutePosition(mmTopx(0), mmTopx(1));
img.scalePercent(50f);
document.add(chapter1);
section2.add(img);
document.add(chapter2);
document.close();
return bos.toByteArray();
} catch (Exception e) {
throw e;
}finally{
if(null!=document){
document.close();
}
if(null!=bos){
bos.close();
}
}
} /**
* 导出excel
*
* @throws Exception
*/
@Override
public byte[] excelExport(List<VbLibGlobalAnalyListVoRes> list,String picId) throws Exception {
ByteArrayOutputStream os = null;
XSSFWorkbook wb = null;
try{
// 第一步,创建一个webbook,对应一个Excel文件
wb = new XSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet("漏洞库分析");
// sheet.setColumnWidth(0, 1000*255);
sheet.setDefaultColumnWidth(16);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
XSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中 XSSFCellStyle styleCell = wb.createCellStyle();
styleCell.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
styleCell.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框 styleCell.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
styleCell.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
styleCell.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
styleCell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
styleCell.setWrapText(true);// 设置自动换行
styleCell.setFillForegroundColor(IndexedColors.AQUA.getIndex());// 设置背景色
styleCell.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 字体样式
XSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
styleCell.setFont(font); XSSFCellStyle styleHeffCell = wb.createCellStyle(); styleHeffCell.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 styleHeffCell.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
styleHeffCell.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
styleHeffCell.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
styleHeffCell.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
styleHeffCell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
styleHeffCell.setWrapText(true);// 设置自动换行 XSSFCell cell = row.createCell((short) 0);
cell.setCellValue("编号");
cell.setCellStyle(styleCell); cell = row.createCell((short) 1);
cell.setCellValue("漏洞名称");
cell.setCellStyle(styleCell); cell = row.createCell((short) 2);
cell.setCellValue("SG编号");
cell.setCellStyle(styleCell); cell = row.createCell((short) 3);
cell.setCellValue("受影响资产数量");
cell.setCellStyle(styleCell); cell = row.createCell((short) 4);
cell.setCellValue("漏洞描述");
cell.setCellStyle(styleCell); cell = row.createCell((short) 5);
cell.setCellValue("发布日期");
cell.setCellStyle(styleCell); // 第五步,写入实体数据 for (int i = 0; i < list.size(); i++) { row = sheet.createRow((int) i + 1);
VbLibGlobalAnalyListVoRes vbLibGlobalAnalyListVoRes = list.get(i);
// 第四步,创建单元格,并设置值
XSSFCell hssfCell = row.createCell((short) 0);
hssfCell.setCellValue(i + 1);
hssfCell.setCellStyle(styleHeffCell); hssfCell = row.createCell((short) 1);
hssfCell.setCellValue(vbLibGlobalAnalyListVoRes.getVbName());
hssfCell.setCellStyle(styleHeffCell); hssfCell = row.createCell((short) 2);
hssfCell.setCellValue(vbLibGlobalAnalyListVoRes.getSgvbCode());
hssfCell.setCellStyle(styleHeffCell); hssfCell = row.createCell((short) 3);
hssfCell.setCellValue(vbLibGlobalAnalyListVoRes.getInfluenceNum());
hssfCell.setCellStyle(styleHeffCell); hssfCell = row.createCell((short) 4);
hssfCell.setCellValue(vbLibGlobalAnalyListVoRes.getVbDesc());
hssfCell.setCellStyle(styleHeffCell); hssfCell = row.createCell((short) 5);
hssfCell.setCellValue(vbLibGlobalAnalyListVoRes.getFoundTime());
hssfCell.setCellStyle(styleHeffCell); }
/**
* 插入图片
*/
//下载图片
byte[] fileBody = fileDao.getBody(picId);
// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
// 八个参数,前四个表示图片离起始单元格和结束单元格边缘的位置,
// 后四个表示起始和结束单元格的位置,如下表示从第2列到第12列,从第1行到第15行,需要注意excel起始位置是0
int size = list.size();
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,
(short) 0, (short) size+1, (short) 6, (short) size+20);
anchor.setAnchorType(3);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(
fileBody, XSSFWorkbook.PICTURE_TYPE_PNG)); os = new ByteArrayOutputStream();
wb.write(os);
return os.toByteArray();
}catch(Exception e){
throw e;
}finally{
if(os != null){
os.close();
}
if(wb != null){
wb.close();
} }
}

  Controller调用方法

 /**
* 下载
* @param request
* @param response
* @param fileName
* @param fileContent
* @throws Exception
*
* @author linan
*/
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, byte[] fileContent)throws Exception{ String userAgent = request.getHeader("user-agent").toUpperCase();
if (null != userAgent && -1 != userAgent.indexOf("MSIE") || userAgent.indexOf("TRIDENT")!=-1) {
// win10 ie edge 浏览器 和其他系统的ie
fileName = URLEncoder.encode(fileName, "UTF-8");
fileName = fileName.replace("+", "%20");// 处理空格变“+”的问
fileName = fileName.replaceAll("%28", "\\(");
fileName = fileName.replaceAll("%29", "\\)");
} else {
// fe
fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
} response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + fileContent.length);
response.setContentType("application/octet-stream");
response.getOutputStream().write(fileContent);
}

  Html直接get调用

导出包含图片的excel、word、pdf 笔记的更多相关文章

  1. PHPExcel 导出包含图片excel

    <?php // 这里用的PHPExcel版本号为1.8.0 // 下载地址https://github.com/PHPOffice/PHPExcel 下载ZIP压缩包 // 下载后将Class ...

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

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

  3. java 导出blob图片到excel

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

  4. 数据字典生成工具(生成Excel, Word,PDF,html)

    转自:http://www.cnblogs.com/yanweidie/p/3838765.html 数据字典生成工具之旅系列文章导航 数据字典生成工具之旅系列文章导航 宣传语 数据字典生成工具.数据 ...

  5. 导出带有图片的excel

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

  6. php导出word(可包含图片)

    为大家介绍一个 php 生成 导出word(可包含图片)的代码,有需要的朋友可以参考下. 之前介绍过php生成word的例子,只是不能包含图片与链接. 今天 为大家介绍一个 php 生成 导出word ...

  7. java通过freemarker导出包含富文本图片的word文档

    废话不多说,进入正题! 本文重点在于:对富文本图片的导出(基础的freemarker+word模板导出这里不做详细解说哈) 参考文章:http://www.cnblogs.com/liaofeifig ...

  8. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  9. Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片

    将Office文件转换为PDF文件,提取Office文件中的所有图片 1.Office系列---将Office文件(Word.PPT.Excel)转换为PDF文件 1.1 基于Office实现的解决方 ...

随机推荐

  1. 【原】Coursera—Andrew Ng机器学习—Week 9 习题—异常检测

    [1]异常检测 [2]高斯分布 [3]高斯分布 [4] 异常检测 [5]特征选择 [6] [7]多变量高斯分布 Answer: ACD B 错误.需要矩阵Σ可逆,则要求m>n  测验1 Answ ...

  2. Linux下Spark框架配置(Python)

    简述  Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapReduce所具有的优点:但不同于MapRedu ...

  3. Linux实战教学笔记15:磁盘原理

    第十五节 磁盘原理 标签(空格分隔): Linux实战教学笔记 1,知识扩展 非脚本方式的一条命令搞定批量创建用户并设置随机10位字母数字组合密码. 1.1 sed的高级用法 [root@chensi ...

  4. java算法 第七届 蓝桥杯B组(题+答案) 2.生日蜡烛

    2.生日蜡烛  (结果填空) 某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛.现在算起来,他一共吹熄了236根蜡烛.请问,他从多少岁开始过生日party的?请填写他开 ...

  5. OPEN LDAP

    一.安装OPENLDAP 安装完成后退出 编辑两个文本,输入以下内容,并把文本文件改名为ldap01.ldif和ldap02.ldif. ldap01.ldif: dn: dc=maxcrc,dc=c ...

  6. cdoj31-饭卡(card) (01背包)

    http://acm.uestc.edu.cn/#/problem/show/31 饭卡(card) Time Limit: 3000/1000MS (Java/Others)     Memory ...

  7. 关于在64位win7下运行Virtualbox安装系统时出错(提示VBoxDD.DLL错误)的解决方

    安装没有问题,安装了最新版VirtualBox-4.3.18-96516-Win,一点运行想安装系统时就出错. 这是提示的错误: 运行Virtualbox去安装系统时出错:Failed to open ...

  8. Openssl errstr命令

    一.简介 errstr命令用于查询错误代码 二.语法 errstr [-stats] <errno> 选项 -stats:打印哈希表状态 errno:错误号 三.实例 1.查看错误信息 : ...

  9. LWIP数据包管理

  10. mdb

    计划开发高性能KV数据库, 学习MongoDB leveldb innodb, 练手贴+日记贴: http://bbs.chinaunix.net/thread-4244870-1-1.html 超高 ...