使用easyexcel实现复杂excel表格导出

news2024/9/29 23:36:36

1、问题描述

        最近在做一个自动化开发票的需求,就是把网页预览的发票导出成一个excel文件。其实这个很好实现,就是使用blob就可以实现把网页的html内容导出成一个.xls的文件就行了。

Blob把html导出为excel文件_blob导入导出excel_金斗潼关的博客-CSDN博客

这种方式其实就是利用了.xls文件可以解析html文本的功能。本质上还是一个html文件只不过是将.html另存为.xls而已。因为我的logo图片是放在服务器上的,所以导出.xls的时候需访问一次服务器上logo图片的路径,才能把logo图片加载到.xls文件里面。但是海外分公司他们导出的.xls文件里面就是加载不出logo图片,起初怀疑是因为国内与海外的网络问题,再他们尝试了vpn连接后导出的.xls也是无法加载logo图片的。我排查下来,可能是与这个证书有关系。因为要访问服务器路径肯定要过一个认证的,他们的office365打开.xls文件时没有过这个证书认证,所以他们的.xls文件自然就访问不了logo图片了。

 

 

         因为解决不了这个证书问题,所以只能使用easyexcel写一个后端的服务,连带图片内容合成.xlsx文件。

2、解决方法

        使用Springboot整合easyExcel写一个后端接口服务

官方文档:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write

GitHub:alibaba/easyexcel: 快速、简洁、解决大文件内存溢出的java处理Excel工具 (github.com) 

         不过,我看文档以及github上给的例子,貌似都是简单表格导出的,类似于这种:

         但我想导出的是这种样子的:其中表头是可以动态改变的

 

         经过我的研究,貌似easyExcel不支持这种复杂的表格样式导出。

        不过我受到了这篇文章的启发:EasyExcel实现追加写入文件_easyexcel追加写入_Lincain的博客-CSDN博客

        我们可以通过模板的形式不断的追加,即一个复杂的excel表格样式可以由若干个简单样式的模板拼接起来。         

3、我的代码

        logo.png

        pom.xml 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>easyexcelTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.8.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        引入easyexcel依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.7.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

        bo类

            把复杂的表格样式拆为若干个模板,分别用不同的实体类对应。

package com.easyexcel.bo;

import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.InputStream;

/**
 * @author Wulc
 * @date 2023/7/25 16:47
 * @description logo
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ContentRowHeight(80)
@ColumnWidth(40)
public class LogoBO {
    //logo图片
    private InputStream logoImage;
}






package com.easyexcel.bo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Wulc
 * @date 2023/7/25 16:53
 * @description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CompanyInfoBO {
    private String param;
}




package com.easyexcel.bo;

import com.alibaba.excel.annotation.write.style.ContentLoopMerge;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static com.alibaba.excel.enums.poi.HorizontalAlignmentEnum.CENTER;

/**
 * @author Wulc
 * @date 2023/7/25 16:54
 * @description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ContentStyle(horizontalAlignment = CENTER)//内容样式
public class InvoiceTitleBO {
    @ContentLoopMerge(columnExtend = 6)
    private String invoiceTitle;
}



package com.easyexcel.bo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Wulc
 * @date 2023/7/27 17:29
 * @description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class InvoiceInfoBO {
    private String param1;
    private String param2;
    private String param3;
    private String param4;
}




package com.easyexcel.bo;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.BooleanEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static com.alibaba.excel.enums.poi.HorizontalAlignmentEnum.CENTER;

/**
 * @author Wulc
 * @date 2023/7/28 10:02
 * @description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ContentStyle(horizontalAlignment = CENTER, wrapped = BooleanEnum.FALSE)//内容样式
@ColumnWidth(30)
public class ProductListBO {
    @ExcelProperty("编号")
    private String item;
    @ExcelProperty("表头1")
    private String product;
    @ExcelProperty("表头2")
    private String description;
    @ExcelProperty("表头3")
    private String quantity;
    @ExcelProperty("表头4")    //表头4会动态改变
    private String unitPrice;
    @ExcelProperty("表头5")    //表头5会动态改变
    private String amount;
}

        MyExcelUtils.java
package com.easyexcel.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.easyexcel.bo.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Wulc
 * @date 2023/7/25 17:07
 * @description
 */
@Component
public class MyExcelUtils {
    /**
     * @author Wulc
     * @date 2023/7/25 17:11
     * @description 根据excel模板追加内容
     */
    public void appendExcelContent() throws IOException, NoSuchFieldException, IllegalAccessException {
        Resource resource = new ClassPathResource("/");
        String path = resource.getFile().getPath();

        //插入Logo
        String templatePath1 = path + "\\template1.xlsx";
        LogoBO logoBO = new LogoBO(this.getClass().getClassLoader().getResource("logo.png").openStream());
        List<LogoBO> logoBOList = new ArrayList<>();
        logoBOList.add(logoBO);
        EasyExcel.write(templatePath1, LogoBO.class).sheet().useDefaultStyle(false).needHead(false).doWrite(logoBOList);

        //插入公司信息
        String templatePath2 = path + "\\template2.xlsx";
        List<CompanyInfoBO> companyInfoBOList = new ArrayList<>();
        companyInfoBOList.add(new CompanyInfoBO("HelloWorld"));
        companyInfoBOList.add(new CompanyInfoBO("HelloSea"));
        companyInfoBOList.add(new CompanyInfoBO("HelloLand"));


        EasyExcel.write(templatePath2, CompanyInfoBO.class).withTemplate(new File(templatePath1)).sheet().useDefaultStyle(false).needHead(false).doWrite(companyInfoBOList);

        //插入标题
        String templatePath3 = path + "\\template3.xlsx";
        List<InvoiceTitleBO> invoiceTitleBOList = new ArrayList<>();
        invoiceTitleBOList.add(new InvoiceTitleBO("标题"));

        EasyExcel.write(templatePath3, InvoiceTitleBO.class).withTemplate(new File(templatePath2)).sheet().useDefaultStyle(false).needHead(false).doWrite(invoiceTitleBOList);

        //插入Invoice内容
        String templatePath4 = path + "\\template4.xlsx";
        List<InvoiceInfoBO> invoiceInfoBOList = new ArrayList<>();
        invoiceInfoBOList.add(new InvoiceInfoBO("param1", "信息1", "param6", "信息6"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param2", "信息2", "param7", "信息7"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param3", "信息3", "param8", "信息8"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param4", "信息4", "param9", "信息9"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param5", "信息5", "", ""));
        EasyExcel.write(templatePath4, InvoiceInfoBO.class).withTemplate(new File(templatePath3)).sheet().useDefaultStyle(false).needHead(false).doWrite(invoiceInfoBOList);

        //插入产品清单
        String templatePath5 = path + "\\template5.xlsx";
        //动态改变表头,我是用了反射,通过改变@ExcelProperty注解的value的值实现改变表头
        ProductListBO productListBO = new ProductListBO();
        Field field1 = productListBO.getClass().getDeclaredField("unitPrice");
        Field field2 = productListBO.getClass().getDeclaredField("amount");
        ExcelProperty excelProperty1 = field1.getAnnotation(ExcelProperty.class);
        ExcelProperty excelProperty2 = field2.getAnnotation(ExcelProperty.class);
        InvocationHandler invocationHandler1 = Proxy.getInvocationHandler(excelProperty1);
        InvocationHandler invocationHandler2 = Proxy.getInvocationHandler(excelProperty2);
        Field declaredField1 = invocationHandler1.getClass().getDeclaredField("memberValues");
        Field declaredField2 = invocationHandler2.getClass().getDeclaredField("memberValues");
        declaredField1.setAccessible(true);
        declaredField2.setAccessible(true);
        Map memberValues1 = (Map) declaredField1.get(invocationHandler1);
        Map memberValues2 = (Map) declaredField1.get(invocationHandler2);
        String[] a1 = new String[1];
        String[] a2 = new String[1];
        a1[0] = "表头44";    //设置表头的值
        a2[0] = "表头55";    //设置表头的值
        memberValues1.put("value", a1);
        memberValues2.put("value", a2);
        List<ProductListBO> productListBOList = new ArrayList<>();
        productListBOList.add(new ProductListBO("1", "产品1", "描述1", "10", "10", "200"));
        productListBOList.add(new ProductListBO("2", "产品2", "描述2", "20", "10", "300"));
        productListBOList.add(new ProductListBO("3", "产品3", "描述3", "20", "10", "300"));
        EasyExcel.write(templatePath5, productListBO.getClass()).withTemplate(new File(templatePath4)).sheet().useDefaultStyle(true).needHead(true).doWrite(productListBOList);
        
    }

}

         

        ExcelTest .java

package com.easyexcel;

import com.easyexcel.util.MyExcelUtils;
import com.easyexcel.util.MyExcelUtils2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

/**
 * @author Wulc
 * @date 2023/7/28 20:08
 * @description
 */
@SpringBootTest(classes = SpringbootApplication.class)
@RunWith(SpringRunner.class)
public class ExcelTest {
    @Autowired
    private MyExcelUtils myExcelUtils;

    @Test
    public void test1() throws IOException, NoSuchFieldException, IllegalAccessException {
        myExcelUtils.appendExcelContent();
    }
}

测试一下。

 你会发现在target目录下的test-classes(因为是@test单元测试启动的,所以构建编译文件都放在target/test-classes下面)有5个.xlsx文件,分别对应MyExcelUtils.java中的template1~5。其中template1~4为过程文件,就是你每个模板依次拼接得到的中间文件。template5为你最终想要的。在实际开发中,可以在生成了template5后就把template1~4文件给删掉,只保留一个template5.xlsx供下载,下载好template5后再把template5.xlsx也给删掉就行了。

修改一下appendExcelContent()方法,返回值改为File类型,并且合成了template5后再把template1~4删掉

MyExcelUtils.java

package com.easyexcel.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.easyexcel.bo.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Wulc
 * @date 2023/7/25 17:07
 * @description
 */
@Component
public class MyExcelUtils {
    /**
     * @author Wulc
     * @date 2023/7/25 17:11
     * @description 根据excel模板追加内容
     */
    public File appendExcelContent() throws IOException, NoSuchFieldException, IllegalAccessException {
        Resource resource = new ClassPathResource("/");
        String path = resource.getFile().getPath();

        //插入Logo
        String templatePath1 = path + "\\template1.xlsx";
        LogoBO logoBO = new LogoBO(this.getClass().getClassLoader().getResource("logo.png").openStream());
        List<LogoBO> logoBOList = new ArrayList<>();
        logoBOList.add(logoBO);
        EasyExcel.write(templatePath1, LogoBO.class).sheet().useDefaultStyle(false).needHead(false).doWrite(logoBOList);

        //插入公司信息
        String templatePath2 = path + "\\template2.xlsx";
        List<CompanyInfoBO> companyInfoBOList = new ArrayList<>();
        companyInfoBOList.add(new CompanyInfoBO("HelloWorld"));
        companyInfoBOList.add(new CompanyInfoBO("HelloSea"));
        companyInfoBOList.add(new CompanyInfoBO("HelloLand"));


        EasyExcel.write(templatePath2, CompanyInfoBO.class).withTemplate(new File(templatePath1)).sheet().useDefaultStyle(false).needHead(false).doWrite(companyInfoBOList);

        //插入标题
        String templatePath3 = path + "\\template3.xlsx";
        List<InvoiceTitleBO> invoiceTitleBOList = new ArrayList<>();
        invoiceTitleBOList.add(new InvoiceTitleBO("标题"));

        EasyExcel.write(templatePath3, InvoiceTitleBO.class).withTemplate(new File(templatePath2)).sheet().useDefaultStyle(false).needHead(false).doWrite(invoiceTitleBOList);

        //插入Invoice内容
        String templatePath4 = path + "\\template4.xlsx";
        List<InvoiceInfoBO> invoiceInfoBOList = new ArrayList<>();
        invoiceInfoBOList.add(new InvoiceInfoBO("param1", "信息1", "param6", "信息6"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param2", "信息2", "param7", "信息7"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param3", "信息3", "param8", "信息8"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param4", "信息4", "param9", "信息9"));
        invoiceInfoBOList.add(new InvoiceInfoBO("param5", "信息5", "", ""));
        EasyExcel.write(templatePath4, InvoiceInfoBO.class).withTemplate(new File(templatePath3)).sheet().useDefaultStyle(false).needHead(false).doWrite(invoiceInfoBOList);

        //插入产品清单
        String templatePath5 = path + "\\template5.xlsx";
        //动态改变表头,我是用了反射,通过改变@ExcelProperty注解的value的值实现改变表头
        ProductListBO productListBO = new ProductListBO();
        Field field1 = productListBO.getClass().getDeclaredField("unitPrice");
        Field field2 = productListBO.getClass().getDeclaredField("amount");
        ExcelProperty excelProperty1 = field1.getAnnotation(ExcelProperty.class);
        ExcelProperty excelProperty2 = field2.getAnnotation(ExcelProperty.class);
        InvocationHandler invocationHandler1 = Proxy.getInvocationHandler(excelProperty1);
        InvocationHandler invocationHandler2 = Proxy.getInvocationHandler(excelProperty2);
        Field declaredField1 = invocationHandler1.getClass().getDeclaredField("memberValues");
        Field declaredField2 = invocationHandler2.getClass().getDeclaredField("memberValues");
        declaredField1.setAccessible(true);
        declaredField2.setAccessible(true);
        Map memberValues1 = (Map) declaredField1.get(invocationHandler1);
        Map memberValues2 = (Map) declaredField1.get(invocationHandler2);
        String[] a1 = new String[1];
        String[] a2 = new String[1];
        a1[0] = "表头44";    //设置表头的值
        a2[0] = "表头55";    //设置表头的值
        memberValues1.put("value", a1);
        memberValues2.put("value", a2);
        List<ProductListBO> productListBOList = new ArrayList<>();
        productListBOList.add(new ProductListBO("1", "产品1", "描述1", "10", "10", "200"));
        productListBOList.add(new ProductListBO("2", "产品2", "描述2", "20", "10", "300"));
        productListBOList.add(new ProductListBO("3", "产品3", "描述3", "20", "10", "300"));
        EasyExcel.write(templatePath5, productListBO.getClass()).withTemplate(new File(templatePath4)).sheet().useDefaultStyle(true).needHead(true).doWrite(productListBOList);
        new File(templatePath1).delete();
        new File(templatePath2).delete();
        new File(templatePath3).delete();
        new File(templatePath4).delete();
        return new File(templatePath5);
    }

}


ExcelController.java
package com.easyexcel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.easyexcel.entity.DemoData;
import com.easyexcel.util.MyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @author Wulc
 * @date 2023/7/20 16:02
 * @description
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    private MyExcelUtils myExcelUtils;

    @GetMapping("/exceldownload")
    public void exceldownload(HttpServletResponse response) throws IOException, NoSuchFieldException, IllegalAccessException {
        File file = myExcelUtils.appendExcelContent();
        try {
            // 获取文件名
            String filename = file.getName();
            // 获取文件后缀名
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            // 将文件写入输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            // 清空response
            response.reset();
            // 设置response的Header
            response.setCharacterEncoding("UTF-8");
            //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
            //attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"
            // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            // 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        file.delete();  //下载完后,把文件删除
    }

}

 

 4、总结

        在使用过程中发现easyExcel不支持Object类型,每一行数据对应一个实体类,每个单元格对应该实体类的一个属性,但这个属性必须是确切的类型,不支持Object属性。

解决EasyExcel不支持解析List以及实体类对象问题_easyexcel list_小熊学Java的博客-CSDN博客

这就造成了,如果是复杂的表格样式,比如第一行是图片,第二行是文字,你就必须定义两个实体类分别对应第一行与第二行的内容。不能一个实体类定义Object属性搞定,这个就不是很方便,希望easyExcel开发者下一版中可以支持Object类型。 

5、参考资料

EasyExcel自定义各种合并策略 - 掘金

IDEA打包时clean报错Failed to delete_柠檬气泡水~的博客-CSDN博客 

EasyExcel实现追加写入文件_easyexcel追加写入_Lincain的博客-CSDN博客 

easyexcel 自适应(行宽, 行高)_easyexcel自适应行高__Mr丶s的博客-CSDN博客 

记录java使用EasyExcel进行单元格内换行操作_easyexcel 换行_阿任_的博客-CSDN博客 

Java反射动态修改注解的值_修改注解的value值_ChihoiTse的博客-CSDN博客 

SpringBoot实现文件下载的几种方式_spring boot 文件下载_user2025的博客-CSDN博客 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/804112.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【我们一起60天准备考研算法面试(大全)-第二十七天 27/60】【真分数】

专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客&#xff0c;如有问题交流&#xff0c;欢迎评论区留言&#xff0c;一定尽快回复&#xff01;&#xff08;大家可以去看我的专栏&#xff0c;是所有文章的目录&#xff09;   文章字体风格&#xff1a; 红色文字表示&#…

HTTP——一、了解Web及网络基础

HTTP 一、使用HTTP协议访问Web二、HTTP的诞生1、为知识共享而规划Web2、Web成长时代3、驻足不前的HTTP 三、网络基础TCP/IP1、TCP/IP协议族2、TCP/IP的分层管理3、TCP/IP 通信传输流 四、与HTTP关系密切的协议&#xff1a;IP、TCP和DNS1、负责传输的 IP 协议2、确保可靠性的TCP…

搭建简单的chatbot并部署到HuggingFace上

调用ChatGPT接口完成聊天任务 下面的代码调用ChatGPT的ChatCompletion接口实现聊天任务&#xff0c;生成的结果如下图打印的信息所示。而且&#xff0c;在封装Conversation class中&#xff0c;message一直使用append进行追加&#xff0c;即每次调用ChatCompletion接口时都传入…

【C++入门到精通】C++入门 —— 类和对象(构造函数、析构函数)

目录 一、类的6个默认成员函数 二、构造函数 ⭕构造函数概念 ⭕构造函数的特点 ⭕常见构造函数的几种类型 三、析构函数 ⭕析构函数概念 ⭕析构函数的特点 ⭕常见析构函数的几种类型 四、温馨提示 前言 这一篇文章是上一篇的续集&#xff08;这里有上篇链接&#xff09;…

qt服务器 网络聊天室

widget.cpp #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);//给服务器指针实例化空间server new QTcpServer(this); }Widget::~Widget() {delete ui; }//启动…

C++ malloc/free和new/delete

1.malloc和free malloc是开辟内存单元的库函数&#xff1b; malloc不会调用构造函数&#xff1b; free只是释放malloc所开辟的空间&#xff1b; free不会调用析构函数。 #include <iostream> using namespace std; class A { public:A(int i0) { cout << "A&…

BUG分析以及BUG定位

一般来说bug大多数存在于3个模块&#xff1a; 1、前台界面&#xff0c;包括界面的显示&#xff0c;兼容性&#xff0c;数据提交的判断&#xff0c;页面的跳转等等&#xff0c;这些bug基本都是一眼可见的&#xff0c;不太需要定位&#xff0c;当然也不排除一些特殊情况&#xf…

25.6 matlab里面的10中优化方法介绍—— 遗传算法(matlab程序)

1.简述 遗传算法&#xff08;Genetic Algorithm, GA&#xff09;是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型&#xff0c;是一种通过模拟自然进化过程搜索最优解&#xff08;所找到的解是全局最优解&#xff09;的方法。 参数编码、初始群体的设定…

「乐天世界」VoxEdit 创作大赛

&#x1f389;参加激动人心的乐天世界 VoxEdit 大赛&#xff01;&#x1f3a8; 召集所有体素艺术家和韩国文化爱好者&#xff01;您准备好展示自己的体素设计技能&#xff0c;用自己的独特风格为乐天世界心爱的吉祥物 Lotty 赋予生命了吗&#xff1f;让我们看看您的想象力和设计…

Acwing.91 最短Hamilton路径(动态规划)

题目 给定一张n个点的带权无向图&#xff0c;点从0~n-1标号&#xff0c;求起点0到终点n-1的最短Hamilton路径。Hamilton路径的定义是从0到n-1不重不漏地经过每个点恰好一次。 输入格式 第—行输入整数n。 接下来n行每行n个整数&#xff0c;其中第i行第j个整数表示点i到j的距…

使用go与智能合约交互之abi调用

上一篇文章&#xff0c;我们讲解了go如何使用函数选择器的方式进行智能合约的调用&#xff0c;接下来让我们一起学习一下如何使用abi的方式进行智能合约的调用 本系列课程&#xff1a; 第一节&#xff1a;使用go与智能合约交互之函数选择器调用 第二节&#xff1a;使用go与智能…

堆喷射的小例子

引自&#xff1a;https://blog.csdn.net/lixiangminghate/article/details/53413863 照着作者的意思&#xff0c;自己的测试代码&#xff1a; #include <iostream> #include <windows.h> #include <stdio.h>class base {char m_buf[8]; public:virtual int…

上传图片到腾讯云对象存储桶cos

1、首先登录腾讯云官网控制台 进入对象存储页面 2、找到跨越访问CIRS设置 配置规则 点击添加规则 填写信息 3、书写代码 这里用VUE3书写 <template><div><input type"file" change"handleFileChange" /></div> </template&g…

JS学习之ES6

一、ES简介 ES6是一个泛指&#xff0c;指EDMAJavaScript之后的版本。它是JS的语言标准。 Nodejs 简介&#xff1a;它是一个工具&#xff0c;主攻服务器&#xff0c;使得利用JS也可以完成服务器代码的编写。 安装&#xff1a; 安装Nodejs的同时&#xff0c;会附带一个npm命令…

QT--day4(定时器事件、鼠标事件、键盘事件、绘制事件、实现画板、QT实现TCP服务器)

QT实现tcpf服务器代码&#xff1a;&#xff08;源文件&#xff09; #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);//给服务器指针实例化空间server new QTc…

无涯教程-jQuery - show( )方法函数

show()方法仅显示匹配元素中的每个元素(如果隐藏)。此方法还有另一种形式&#xff0c;可以控制动画的速度。 show( ) - 语法 selector.show( ); show( ) - 示例 以下是一个简单的示例&#xff0c;简单说明了此方法的用法- <html><head><title>The jQuer…

PostgreSQL-Centos7源码安装

卸载服务器上的pg13 本来是想删除原来的postgis重新源码安装就行,但是yum安装的PostgreSQL不能直接使用,会提示以下问题: 之前服务是用yum安装的,现在需要删除 -- 删除数据的postgis插件 drop extension postgis; drop extension postgis cascade;删除相关安装包 # 查询…

Ubuntu Server版 之 apache系列 常用配置 以及 隐藏 版本号 IP、Port 搭建服务案例

查看版本 旧的 用 httpd -v 新的 用 apache2 -v 配置检测 旧的 httpd -t 新的 apachectl configtest window用的apache 是 httpd -t Linux 中 apachectl configtest 主配置文件 之前旧版apache 是httpd 现在都改成 apache2 /etc/apache2/apache2.conf window中 httpd.con…

leetcode 2141. Maximum Running Time of N Computers(N台计算机的最大运行时间)

有n台电脑&#xff0c;数组batteries代表每块电池的电量。 每台电脑每次只能放入一块电池&#xff0c;然后电池可以任意交换&#xff0c;但电池不能充电。 所有电脑必须同时运行。 问n台电脑最多可以同时运行几分钟。 思路&#xff1a; 乍一看很复杂&#xff0c;复杂的电池交…

使用Feign出现空指针异常

说明&#xff1a;本文记录一次偶然出现的空指针异常&#xff0c;在微服务架构中&#xff0c;一个服务在调用另一个服务时&#xff0c;出现了空指针异常。 业务描述&#xff1a;在做订单超时功能时&#xff0c;大家都知道&#xff0c;可以使用RabbitMQ延迟队列&#xff0c;下单…