/**
* 导出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. GPU架构图

    找了几张GPU架构图,对理解图形渲染管线很有帮助

  2. java算法 第七届 蓝桥杯B组(题+答案) 6.方格填数

    6.方格填数  (结果填空) 如下的10个格子 (如果显示有问题,也可以参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案 ...

  3. CyclicBarrier的使用

    最近一直整并发这块东西,顺便写点Java并发的例子,给大家做个分享,也强化下自己记忆,如果有什么错误或者不当的地方,欢迎大家斧正. CyclicBarrier是一种多线程并发控制实用工具,和Count ...

  4. jquery offset positon 获取位置不准的解决方法

    问题: 本地开发时,由于使用了图片,而且使用了offset().top涉及到图片所在的div距离计算的部分,本地开发一切都没问题:但是部署到服务器上时却出现布局错乱,经过排查发现总是少了一个图片高度的 ...

  5. Unity3d 下websocket的使用

    今天介绍一下如何在Unity3D下使用WebSocket. 首先介绍一下什么是websocket,以及与socket,和http的区别与联系,然后介绍一下websocket的一些开源的项目. WebS ...

  6. cocoapods使用问题集锦(2017-04)

    今天公司在公司新发的电脑上边安装cocoapod发现容易忘记的几个问题,感觉需要记录下来. 问题一:系统默认ruby镜像的卸载命令行 -->     gem sources --remove h ...

  7. DevCloud for CloudStack Development

    Apache CloudStack development is not an easy task, for the simplest of deployments one requires a se ...

  8. 30-Transformation(HDU4578)-区间线段树(复杂)

    http://acm.hdu.edu.cn/showproblem.php?pid=4578 Transformation Time Limit: 15000/8000 MS (Java/Others ...

  9. [C++] Function Template - optional parameter

    Function Template

  10. [C++] c Struct VS c++ Struct

    c Struct c语言生命变量要加上struct c语言结构体内部不能有函数 C语言结构体没有共有,私有和继承