Bläddra i källkod

1.新增导出功能

WoNiu 4 år sedan
förälder
incheckning
b15424a423
1 ändrade filer med 49 tillägg och 25 borttagningar
  1. 49 25
      src/main/java/com/galaxis/manatee/util/ExcelUtils.java

+ 49 - 25
src/main/java/com/galaxis/manatee/util/ExcelUtils.java

@@ -1,5 +1,6 @@
 package com.galaxis.manatee.util;
 
+import java.awt.image.BufferedImage;
 import java.io.*;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
@@ -18,6 +19,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletResponse;
 
 import com.galaxis.manatee.entity.chuanyun.dto.ExcelColumn;
@@ -28,13 +30,9 @@ import org.apache.commons.lang.CharUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.math.NumberUtils;
 import org.apache.poi.hssf.usermodel.*;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.IndexedColors;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.Picture;
+import org.apache.poi.util.IOUtils;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -92,24 +90,50 @@ public class ExcelUtils {
             for (int j = 0; j < values[i].length; j++) {
                 //生成图片
                 if (j==2){
-                    URL url = new URL(values[i][j]);
-                    //打开链接
-                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
-                    //设置请求方式为"GET"
-                    conn.setRequestMethod("GET");
-                    //超时响应时间为5秒
-                    conn.setConnectTimeout(5 * 1000);
-                    //通过输入流获取图片数据
-                    InputStream inStream = conn.getInputStream();
-                    //得到图片的二进制数据,以二进制封装得到数据,具有通用性
-                    byte[] data = readInputStream(inStream);
-                    //anchor主要用于设置图片的属性
-                    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 250,(short) 2, i, (short) 3, i+1);
-                    //Sets the anchor type (图片在单元格的位置)
-                    //0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
-                    anchor.setAnchorType(0);
-                    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
-                    patriarch.createPicture(anchor, wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_PNG));
+//                    URL url = new URL(values[i][j]);
+//                    //打开链接
+//                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+//                    //设置请求方式为"GET"
+//                    conn.setRequestMethod("GET");
+//                    //超时响应时间为5秒
+//                    conn.setConnectTimeout(5 * 1000);
+//                    //通过输入流获取图片数据
+//                    InputStream inStream = conn.getInputStream();
+//                    //得到图片的二进制数据,以二进制封装得到数据,具有通用性
+//                    byte[] data = readInputStream(inStream);
+//                    log.warn(data.toString());
+//                    //anchor主要用于设置图片的属性
+//                    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 250,(short) 2, i, (short) 3, i+1);
+//                    //Sets the anchor type (图片在单元格的位置)
+//                    //0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
+//                    anchor.setAnchorType(0);
+//                    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+//                    patriarch.createPicture(anchor, wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_PNG));
+//                    Sheet first=wb.createSheet("first");
+                    Drawing drawing=sheet.createDrawingPatriarch();
+
+                    CreationHelper creationHelper=wb.getCreationHelper();
+
+                    ClientAnchor clientAnchor=creationHelper.createClientAnchor();
+                    clientAnchor.setCol1(2);
+                    clientAnchor.setRow1(i);
+                    //获取图片
+                    BufferedInputStream in = new BufferedInputStream(new URL("https://lmichelin.fr/content/images/size/w2000/2019/05/5c1bb7dd5e7cc9678fcdc39f_Hello-World-Header.png").openStream());
+                    BufferedImage bufferedImage= ImageIO.read(in);
+
+
+                    InputStream inputStream=new BufferedInputStream(new URL("https://lmichelin.fr/content/images/size/w2000/2019/05/5c1bb7dd5e7cc9678fcdc39f_Hello-World-Header.png").openStream());
+                    byte[] bytes= IOUtils.toByteArray(inputStream);
+                    int helloWorldIndex=wb.addPicture(bytes,Workbook.PICTURE_TYPE_PNG);
+                    inputStream.close();
+
+                    //获取图片尺寸,用于修改单元格大小
+//                    int width=bufferedImage.getWidth();
+                    int height=bufferedImage.getHeight();
+                    inputStream.close();
+                    Picture picture = drawing.createPicture(clientAnchor,helloWorldIndex);
+                    picture.resize();
+                    row.setHeight((short)(height*15));
                 }else{
                     //将内容按顺序赋给对应的列对象
                     row.createCell(j).setCellValue(values[i][j]);