/**
* 导出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. Spring、Springboot常用注解:@Qualifier(不定时更新)

    1.@Qualifier 出现场景: 老项目中有多个实现类实现同一个接口时,或者一个项目中有多个数据源时,spring容器不知道该注入哪个实现类或者使用哪个数据源,该注解就派上用场. 1)多实现类实现 ...

  2. .net core 2.0的一次奇特经历

    环境:.net core SDK版本 2.0.0-preview1-005977 VS 2017 version 15.3.0 preview 3.0 问题描述:今天在迁移Job的项目中,中午吃饭的时 ...

  3. PHP - 引用计数

    引用计数以及是否是引用变量,一个神奇的函数,查看当前引用计数: <?php xdebug_debug_zval('a'); 以上例程会输出: a: (refcount=1, is_ref=0)= ...

  4. Elasticsearch-PHP 安装

    安装 Elasticsearch-PHP只有三个要求你需要担心: PHP 5.3.9 或更高版本(查看更多信息) Composer ext-curl: Libcurl的PHP扩展 其它的依赖会通过Co ...

  5. python+Django创建第一个项目

    1.首先搭建好环境 1.1 安装pyhton,Linux系统中,python是系统自带的所以就不用安装 1.2 安装Django框架 使用pip安装: pip install django 1.3 检 ...

  6. 选择性导出excel表中内容

    package com.huawei.utils; import java.io.FileNotFoundException;import java.io.FileOutputStream;impor ...

  7. 高性能Web服务器Nginx的配置与部署研究(8)核心模块之事件模块

    一.事件模块的作用是什么? 用来设置Nginx处理链接请求. 二.相关指令 1. accept_mutex 含义:设置是否使用连接互斥锁进行顺序的accept()系统调用. 语法:accept_mut ...

  8. 143. Reorder List(List)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  9. porwedesigner 去掉引号

    PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大.小写 若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入Tool ...

  10. HUST软件测试1504班: 第0周作业成绩

    说明 本次公布的成绩包含三次作业的结果: 毕博平台课前测试题 第0周作业1:开设博客 第0周作业2:博客阅读和思考 如果同学对作业结果存在异议,可以: 在线平台的第一周在线答疑中创建话题申诉. 或直接 ...