Parcourir la source

Merge branch 'f-xiaogu' into dev

gushoubang il y a 9 mois
Parent
commit
63c9ca23ee

+ 31 - 80
onemap-modules/onemap-analyse/src/main/java/com/onemap/analyse/service/impl/ReportServiceImpl.java

@@ -1,6 +1,7 @@
 package com.onemap.analyse.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
 import com.onemap.analyse.domain.FactorUseDTO;
 import com.onemap.analyse.domain.FzxzEntityDTO;
 import com.onemap.analyse.domain.FzxzReport;
@@ -20,17 +21,15 @@ import com.onemap.analyse.utils.NumberUtil;
 import com.onemap.analyse.utils.UnitsUtil;
 import com.onemap.common.core.utils.StringUtils;
 import org.apache.commons.io.FileUtils;
+import org.apache.poi.util.Units;
 import org.apache.poi.xwpf.usermodel.*;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
-import org.python.antlr.ast.Num;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
 import java.io.*;
-import java.math.BigInteger;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
@@ -109,7 +108,7 @@ public class ReportServiceImpl implements IReportService {
      *
      * @param res
      * @param reportPath
-     * @param dkIds  没有导出全部地块
+     * @param dkIds      没有导出全部地块
      */
     private void createReport(FzxzEntityDTO res, String reportPath, List<String> dkIds) {
         GeomRes geomRes = shpFileMapper.getOne(res.getGeomId());
@@ -295,80 +294,32 @@ public class ReportServiceImpl implements IReportService {
                 NpoiHelper.mergeHorizontal(table, dkList.size() + 2, 0, 3);
                 table.getRow(dkList.size() + 2).getCell(0).setParagraph(NpoiHelper.setCellText(table, "地块位置", optionIsBold));
 
-                // String mapF = "";
-                // String mapM = "";
-                //
-                // // TODO 生成位置图片
-                // // if (imgList != null && imgList.size() > 0) {
-                // //     for (int j = 0; j < imgList.size(); j++) {
-                // //         String s = imgList.get(j);
-                // //         if (s.indexOf(item.getBsm() + "_F") >= 0) {
-                // //             mapF = s;// 全局
-                // //         }
-                // //         if (s.indexOf(item.getBsm() + "_M") >= 0) {
-                // //             mapM = s;// 局部
-                // //         }
-                // //     }
-                // // }
-                // if (StringUtils.isNotEmpty(mapF)) {
-                //     FileInputStream fis = null;
-                //     try {
-                //         fis = new FileInputStream(new File(mapF));
-                //         XWPFParagraph paragraph = table.getRow(4).getCell(0).addParagraph();
-                //         paragraph.setAlignment(ParagraphAlignment.CENTER);
-                //         XWPFRun run = paragraph.createRun();
-                //         run.addPicture(fis,    // 条形码图片的位置
-                //                 Document.PICTURE_TYPE_JPEG, // 图片类型
-                //                 item.getBsm() + "_F.jpeg", // 图片名称
-                //                 2200000, // 图片的长
-                //                 1700000 // 图片的宽
-                //         );
-                //         run.addBreak(BreakType.TEXT_WRAPPING);
-                //         run.setText("(宏观位置)");
-                //     } catch (Exception e) {
-                //         System.out.println(e.toString());
-                //     } finally {
-                //         if (fis != null) {
-                //             try {
-                //                 fis.close();
-                //             } catch (IOException e) {
-                //                 throw e;
-                //             }
-                //         }
-                //     }
-                // }
-                // if (StringUtils.isNotEmpty(mapM)) {
-                //     FileInputStream fis = null;
-                //     try {
-                //         fis = new FileInputStream(new File(mapM));
-                //         XWPFParagraph paragraph = table.getRow(4).getCell(2).addParagraph();
-                //         paragraph.setAlignment(ParagraphAlignment.CENTER);
-                //         XWPFRun run = paragraph.createRun();
-                //         run.addPicture(fis,    // 条形码图片的位置
-                //                 Document.PICTURE_TYPE_JPEG, // 图片类型
-                //                 item.getBsm() + "_M.jpeg", // 图片名称
-                //                 2200000, // 图片的长
-                //                 1700000 // 图片的宽
-                //         );
-                //         run.addBreak(BreakType.TEXT_WRAPPING);
-                //         run.setText("(具体位置)");
-                //     } catch (Exception e) {
-                //         System.out.println(e.toString());
-                //         throw e;
-                //     } finally {
-                //         if (fis != null) {
-                //             try {
-                //                 fis.close();
-                //             } catch (IOException e) {
-                //                 throw e;
-                //             }
-                //         }
-                //     }
-                // }
                 // Table 图片
                 NpoiHelper.mergeHorizontal(table, dkList.size() + 3, 0, 3);
-                XWPFParagraph para6 = table.getRow(dkList.size() + 3).getCell(0).addParagraph();
-                para6.setAlignment(ParagraphAlignment.LEFT);
+                XWPFTableRow row = table.getRow(dkList.size() + 3);
+
+                // 在合并后的单元格中清除所有段落,确保没有多余的段落
+                XWPFTableCell cell = row.getCell(0);
+                while (cell.getParagraphs().size() > 0) {
+                    cell.removeParagraph(0);
+                }
+
+                // 在合并后的单元格中创建一个新的段落
+                XWPFParagraph paraImag = row.getCell(0).addParagraph();
+                paraImag.setAlignment(ParagraphAlignment.CENTER); // 居中对齐
+
+                XWPFRun run = paraImag.createRun();
+                File imageFile = new File("states.png");
+                BufferedImage bufferedImage = ImageIO.read(imageFile);
+
+                int originalWidth = bufferedImage.getWidth();
+                int originalHeight = bufferedImage.getHeight();
+
+                int useWidth = 300;
+                int useHeight = useWidth * originalHeight / originalWidth;
+
+                InputStream is = new FileInputStream(imageFile);
+                run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, null, Units.toEMU(useWidth), Units.toEMU(useHeight));
             }
             File fileDoc = new File(reportPath);
             if (fileDoc.exists()) {
@@ -479,9 +430,9 @@ public class ReportServiceImpl implements IReportService {
 
             Map<String, String> row4ItemMap = new HashMap<>();
             row4ItemMap.put("colum0", "容积率上限");
-            row4ItemMap.put("colum1", NumberUtil.double2TwoDecimal(UnitsUtil.m2ToMu((Double)rawTable.get("rjld"))) + "");
+            row4ItemMap.put("colum1", NumberUtil.double2TwoDecimal(UnitsUtil.m2ToMu((Double) rawTable.get("rjld"))) + "");
             row4ItemMap.put("colum2", "容积率下限");
-            row4ItemMap.put("colum3", NumberUtil.double2TwoDecimal(UnitsUtil.m2ToMu((Double)rawTable.get("rjlx"))) + "");
+            row4ItemMap.put("colum3", NumberUtil.double2TwoDecimal(UnitsUtil.m2ToMu((Double) rawTable.get("rjlx"))) + "");
             rows.add(row4ItemMap);
 
             Map<String, String> row5ItemMap = new HashMap<>();

+ 50 - 0
onemap-modules/onemap-analyse/src/test/java/AddImageToTable.java

@@ -0,0 +1,50 @@
+import com.onemap.analyse.utils.NpoiHelper;
+import org.apache.poi.util.Units;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+import org.apache.poi.xwpf.usermodel.XWPFRun;
+import org.apache.poi.xwpf.usermodel.XWPFTable;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.*;
+
+/**
+ * 将图片插入到表格中
+ */
+public class AddImageToTable {
+    public static void main(String[] args) {
+        try {
+            XWPFDocument document = new XWPFDocument();
+            // 创建一个有一行三列的表格
+            XWPFTable table = document.createTable(1, 3);
+            // 合并第一行的三个单元格
+            NpoiHelper.mergeHorizontal(table, 0, 0, 2);
+            // 创建段落
+            XWPFParagraph para = table.getRow(0).getCell(0).addParagraph();
+            XWPFRun run = para.createRun();
+
+            // 读取图片文件
+            File imageFile = new File("states.png");
+            BufferedImage bufferedImage = ImageIO.read(imageFile);
+
+            int originalWidth = bufferedImage.getWidth();
+            int originalHeight = bufferedImage.getHeight();
+
+            int useWidth = 300;
+            int useHeight = useWidth * originalHeight / originalWidth;
+
+            InputStream is = new FileInputStream(imageFile);
+            run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, null, Units.toEMU(useWidth), Units.toEMU(useHeight));
+
+            // 保存文档
+            FileOutputStream fos = new FileOutputStream("document_with_image.docx");
+            document.write(fos);
+            fos.close();
+            document.close();
+            System.out.println("图片成功插入文档");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 0 - 55
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/Test.java

@@ -1,55 +0,0 @@
-package com.onemap.spatial;
-
-import javax.imageio.ImageIO;
-import java.awt.*;
-import java.awt.image.BufferedImage;
-import java.io.*;
-
-public class Test {
-    public static void main(String[] args) throws IOException {
-        InputStream is1 = new FileInputStream("raster.png");
-        InputStream is2 = new FileInputStream("wkt.png");
-        BufferedImage readImg1 = ImageIO.read(is1);
-        BufferedImage readImg2 = ImageIO.read(is2);
-        BufferedImage mergeImg = mergeImg(readImg1, readImg2);
-        writeImageLocal("merge.png", mergeImg);
-
-
-    }
-
-    /**
-     * @param img1 待合并的第一张图
-     * @param img2 带合并的第二张图
-     * @return 返回合并后的BufferedImage对象
-     */
-    private static BufferedImage mergeImg(BufferedImage img1, BufferedImage img2) {
-        int w1 = img1.getWidth();
-        int h1 = img1.getHeight();
-        int h2 = img2.getHeight();
-        int w2 = img2.getWidth();
-        Graphics2D graphics2D = null;
-        // 生成新图片
-        BufferedImage destImage = null;
-        destImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
-        graphics2D = destImage.createGraphics();
-        // 插入的位置坐标需要手动尝试
-        graphics2D.drawImage(img1, 0, 0, w1, h1, null);
-        graphics2D.drawImage(img2, 0, 0, w2, h2, null);
-        graphics2D.dispose();
-        return destImage;
-    }
-
-    /**
-     * 生成新图片到本地
-     */
-    public static void writeImageLocal(String newImage, BufferedImage img) {
-        if (newImage != null && img != null) {
-            try {
-                File outputfile = new File(newImage);
-                ImageIO.write(img, "jpg", outputfile);
-            } catch (IOException e) {
-                System.out.println(e.getMessage());
-            }
-        }
-    }
-}