|
@@ -0,0 +1,152 @@
|
|
|
+package com.mooctest.util;
|
|
|
+
|
|
|
+
|
|
|
+import com.mooctest.data.FinalReportDTO;
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class Report2xls {
|
|
|
+ private static String [] theads = {"报告ID","bug分类","级别","复现程度","描述",
|
|
|
+ "图片1","图片2","图片3","图片4","图片5","图片6","图片7","图片8",
|
|
|
+ "图片9","图片10","图片11","图片12","图片13","图片14","图片15"
|
|
|
+ ,"图片16","图片17","图片18","图片19"};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void report2excel (List<FinalReportDTO> data){
|
|
|
+ FileOutputStream fileOut = null; // 为了输出excel 文件
|
|
|
+ BufferedImage bufferImg = null;
|
|
|
+ HSSFCellStyle desCellStyle ;
|
|
|
+ HSSFCellStyle lineCellStyle ;
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ int maxImgLength = max(getMaxImgs(data),10); // 获得最大的图片列数
|
|
|
+ // 限定列数
|
|
|
+// int cols = 6+maxImgLength;
|
|
|
+
|
|
|
+ HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
+ HSSFSheet sheet1 = wb.createSheet("sheet1");
|
|
|
+ for (int i =5;i<maxImgLength;i++){
|
|
|
+ sheet1.setColumnWidth((short)i,(short)60*256);
|
|
|
+
|
|
|
+ }
|
|
|
+ sheet1.setColumnWidth((short)4,(short)30*256); // 设置描述列的宽度
|
|
|
+ desCellStyle = wb.createCellStyle();
|
|
|
+ desCellStyle.setWrapText(true);
|
|
|
+ lineCellStyle = wb.createCellStyle();
|
|
|
+ lineCellStyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置文字居中,垂直居中
|
|
|
+ sheet1.setDefaultRowHeight((short)(30*256));
|
|
|
+ HSSFPatriarch patriarch = sheet1.createDrawingPatriarch(); // 创建的用来画图的
|
|
|
+ HSSFClientAnchor anchor;
|
|
|
+ Cell cell1,cell2,cell3,cell4,cell0;
|
|
|
+
|
|
|
+ int tempWidth;
|
|
|
+ int tempHeight;
|
|
|
+ int fitSize [] ;
|
|
|
+ Row thead = sheet1.createRow(0);
|
|
|
+// thead.setHeight((short)256);
|
|
|
+ thead.setHeight((short)480);
|
|
|
+ for(int i =0;i<4+maxImgLength&&i<20;i++){
|
|
|
+ thead.createCell(i).setCellValue(theads[i]);
|
|
|
+ }
|
|
|
+// sheet1.setDefaultRowHeight((short)(30*256));
|
|
|
+ for (int rowNum = 0; rowNum < data.size()&&rowNum<588; rowNum++) {
|
|
|
+ Row row = sheet1.createRow(rowNum+1);
|
|
|
+ //row.setHeight((short)(30*256));
|
|
|
+ if(rowNum<580){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ FinalReportDTO dto = data.get(rowNum);
|
|
|
+ cell0 = row.createCell(0);
|
|
|
+ cell0.setCellValue(rowNum+1);
|
|
|
+ cell0.setCellStyle(lineCellStyle);
|
|
|
+ cell1 = row.createCell(1);
|
|
|
+ cell1.setCellValue(ReportUtil.category2String.get(dto.getCategory()));
|
|
|
+ cell1.setCellStyle(lineCellStyle);
|
|
|
+ cell2 = row.createCell(2);
|
|
|
+ cell2.setCellValue(ReportUtil.severity2String.get(dto.getSeverity()));
|
|
|
+ cell2.setCellStyle(lineCellStyle);
|
|
|
+ cell3 = row.createCell(3);
|
|
|
+ cell3.setCellValue(ReportUtil.recurrent2String.get(dto.getRecurrent()));
|
|
|
+ cell3.setCellStyle(lineCellStyle);
|
|
|
+ cell4 = row.createCell(4);
|
|
|
+ cell4.setCellStyle(desCellStyle);
|
|
|
+ cell4.setCellValue(dto.getDescription());
|
|
|
+ if(dto.getImgUrls()!=null){
|
|
|
+ row.setHeight((short)(30*256)); // 说明有图片,设置高度
|
|
|
+ for(int photoIndex = 0;photoIndex<maxImgLength&&photoIndex<dto.getImgUrls().length;photoIndex++){
|
|
|
+ URL url = new URL(dto.getImgUrls()[photoIndex]);
|
|
|
+ URLConnection connection = url.openConnection();
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ bufferImg = ImageIO.read(connection.getInputStream());
|
|
|
+ tempWidth = bufferImg.getWidth();
|
|
|
+ tempHeight = bufferImg.getHeight();
|
|
|
+ fitSize = getFitHeightAndWidth(tempHeight,tempWidth);
|
|
|
+ ImageIO.write(bufferImg, "jpg", byteArrayOut);
|
|
|
+ anchor = new HSSFClientAnchor(fitSize[1]*4, fitSize[0], 0, 0,(short) (5+photoIndex), (rowNum+1), (short) (6+photoIndex), rowNum+2);
|
|
|
+ //插入图片 1
|
|
|
+ patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
|
|
|
+ bufferImg = null;
|
|
|
+ byteArrayOut = new ByteArrayOutputStream();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fileOut = new FileOutputStream("excel.xls");
|
|
|
+ wb.write(fileOut);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得报告内的最大图片数量
|
|
|
+ * @param reports
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static int getMaxImgs (List<FinalReportDTO> reports ){
|
|
|
+ int res =0;
|
|
|
+ for (FinalReportDTO dto : reports){
|
|
|
+ if(dto.getImgUrls()!=null)
|
|
|
+ res = max(res,dto.getImgUrls().length);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int max(int a, int b) {
|
|
|
+ return a>b?a:b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 0 is Height and 1 is width
|
|
|
+ * @param height
|
|
|
+ * @param width
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static int[] getFitHeightAndWidth(int height,int width){
|
|
|
+ //默认格子的大小是正方形的。
|
|
|
+ int [] res = new int[2];
|
|
|
+ int max = Math.max(height,width);
|
|
|
+ int min = Math.min(height,width);
|
|
|
+ min = (int)( (min*1.0/max)*255 );
|
|
|
+ if(height>width){
|
|
|
+ res[0] = 0;
|
|
|
+ res[1] = 255-min;
|
|
|
+ }else {
|
|
|
+ res[1] = 0;
|
|
|
+ res[0] = 255-min;
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|