java中使用POI生成Excel并导出

这篇具有很好参考价值的文章主要介绍了java中使用POI生成Excel并导出。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

注:本文章中代码均为本地Demo版本,若后续代码更新将不会更新文章

需求说明及实现方式

  1. 根据从数据库查询出的数据,将其写入excel表并导出

    我的想法是通过在实体属性上写自定义注解的方式去完成。因为我们在代码中可以通过反射的方式去获取实体类中全部的注解及属性名称等等。我们可以在自定义注解中声明一个参数value,这里面就存储其标题,这样我们

  2. 数据查询type不同,则显示的标题数量不同

    在注解类中增加type参数,只有满足对应type的属性会被导出至excel中

  3. 数据查询type不同,则显示的标题不同(同一个字段)

    优化参数value,判断传入的value是否为json字符串,如果是json字符串则找到其与type对应的value

  4. 数据的格式化(时间类型格式化、数据格式化显示)

    数据格式化显示通过在注解类中增加dict参数,该参数传入json字符串。

本来我是想着通过easyExcel来完成这些功能,但是由于项目中已经引入了POI的3.9版本依赖,然后easyExcel中POI的依赖版本又高于该版本,而且不管是版本升级还是版本排除降级,总会有一个出现问题,最终也只能通过最基础的POI编写代码实现。

需求完成

依赖引入:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.33</version>
        </dependency>

java中使用POI生成Excel并导出,java,python,开发语言

通用代码

  1. ExcelExport

    • value:标题,也可为json字符串
    • dict:json字符串格式的字典,格式如User中所示
    • type:数组类型,查询数据type类型是什么值时这个字段会写入excel中。如我type = {"a"},则我在查询数据时传入的type为b则不会将这个字段写入excel中,如果传入的是a则会正常写入。
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface ExcelExport {
        String value();
        String dict() default "";
        String[] type() default {};
    }
    
  2. User

    • @ExcelExport:即自定义的注解,其中值的含义在上面已经说清楚了
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Accessors(chain = true)
    public class User {
        @ExcelExport(value = "用户名",type = {"a"})
        private String userName;
        @ExcelExport(value = "{a: '年龄',b: '年纪'}",type = {"a","b"})
        private Integer age;
        @ExcelExport(value = "性别",
                dict = "[{ value: \"0\", label: \"女\" }," +
                        "{ value: \"1\", label: \"男\" }]",
                type = {"a","b"})
        private Integer sex;
        @ExcelExport(value = "生日",type = {"b"})
        private Date birthday;
    }
    
    

版本1

版本1中未实现数据查询type不同,则显示的标题不同(同一个字段)这一功能,如需要加请看PoiExcelUtil中writeTitleCellData方法。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzj.anno.ExcelExport;
import com.lzj.entity.User;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.StringUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author:雷子杰
 * @date:2023/7/4
 */
public class One {
    public static void main(String[] args) throws IOException {
        List<User> userList = new ArrayList<>();
        userList.add(new User("lzj1",1,1,new Date()));
        userList.add(new User("lzj2",2,0,new Date()));
        userList.add(new User("lzj3",3,1,new Date()));
        userList.add(new User("lzj4",4,0,new Date()));

        //声明XSSF对象
        XSSFWorkbook xssfSheets = new XSSFWorkbook();
        //创建sheet
        XSSFSheet userSheet = xssfSheets.createSheet("user");

        //创建标题字体
        XSSFFont titleFont = xssfSheets.createFont();
        titleFont.setBold(true);//加粗
        titleFont.setFontName("微软雅黑");
        titleFont.setFontHeightInPoints((short) 12);//字体大小
        //创建通用字体
        XSSFFont commonFont = xssfSheets.createFont();
        commonFont.setBold(false);//加粗
        commonFont.setFontName("微软雅黑");
        commonFont.setFontHeightInPoints((short) 12);//字体大小

        // 创建标题行单元格样式
        CellStyle titleCellStyle = xssfSheets.createCellStyle();
        titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框线
        titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平对齐方式
        titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直对齐方式
        titleCellStyle.setFont(titleFont);//字体样式
        titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//单元格前景色
        titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充单元格

        //创建通用行单元格样式
        CellStyle commonCellStyle = xssfSheets.createCellStyle();
        commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        commonCellStyle.setFont(commonFont);
        commonCellStyle.setWrapText(true);//自动换行

        //获取实体类中全部属性
        Field[] fields = User.class.getDeclaredFields();
        //当前行
        int currentRow = 0;
        //当前列
        int currentColumn = 0;
        //行高
        float rowHeight = 40.1f;
        //列宽
        int columnWidth = 33 * 256;
        //创建行
        XSSFRow row = userSheet.createRow(currentRow);
        当前行+1
        //currentRow++;

        //创建标题行
        // 遍历每个字段
        for (Field field : fields) {
            // 检查字段是否带有Explanation注解
            if (field.isAnnotationPresent(ExcelExport.class)) {
                // 获取Explanation注解实例
                ExcelExport explanation = field.getAnnotation(ExcelExport.class);
                // 获取注解中的解释
                String value = explanation.value();

                //创建单元格,传入值,设置单元格样式
                XSSFCell cell = row.createCell(currentColumn);
                cell.setCellValue(value);
                cell.setCellStyle(titleCellStyle);

                //设置行高度
                row.setHeightInPoints(rowHeight);
                //设置列的宽度
                userSheet.setColumnWidth(currentColumn,columnWidth);
                //当前列+1
                currentColumn++;
            }
        }
        //重置当前列
        currentColumn = 0;

        //创建数据行
        for (User user : userList) {
            //每次循环时重置列
            currentColumn = 0;
            //当前行+1
            currentRow++;
            //创建行
            row = userSheet.createRow(currentRow);
            for (Field field : fields) {
                if (field.isAnnotationPresent(ExcelExport.class)) {
                    try {
                        //解除private限制
                        field.setAccessible(true);

                        // 获取Explanation注解实例
                        ExcelExport explanation = field.getAnnotation(ExcelExport.class);
                        // 获取属性的值
                        Object value = field.get(user);

                        //日期类型格式化
                        if (value != null && field.getType() == Date.class){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            value = sdf.format(value);
                        }
                        //获取对应字典
                        String dict = explanation.dict();
                        if (!StringUtils.isEmpty(dict) && value != null){
                            //JSONObject jsonObject = JSON.parseObject(dict);
                            List<String> list = JSON.parseArray(dict, String.class);
                            for (String item : list) {
                                JSONObject jsonObject = JSON.parseObject(item);
                                if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
                                    value = jsonObject.getString("label");
                                    break;
                                }
                            }
                            //value = jsonObject.get(value.toString());
                        }
                        //创建单元格,传入值,设置单元格样式
                        XSSFCell cell = row.createCell(currentColumn);
                        cell.setCellValue(value == null?"":value.toString());
                        cell.setCellStyle(commonCellStyle);

                        //设置行高度
                        row.setHeightInPoints(rowHeight);
                        //当前列+1
                        currentColumn++;

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        // 将生成的excel文件输出流转为字节数组
        byte[] bytes = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        xssfSheets.write(outputStream);
        outputStream.close();
        bytes = outputStream.toByteArray();

        //读取字节数组为文件输入流
        InputStream inputStream = new ByteArrayInputStream(bytes);
        inputStream.close();


        //在声明一个输出流将文件下载到本地
        File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        //将bytes中的内容写入
        bufferedOutputStream.write(bytes);
        //刷新输出流,否则不会写出数据
        bufferedOutputStream.flush();
        bufferedOutputStream.close();


    }
}

版本2

版本二相比与版本1,其主要优势是将POI相关操作都封装进了PoiExcelUtil中。

  1. PoiExcelUtil

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.lzj.anno.ExcelExport;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.xssf.usermodel.*;
    import org.springframework.util.StringUtils;
    
    import java.lang.reflect.Field;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    /**
     * <p>
     *
     * </p>
     *
     * @author:雷子杰
     * @date:2023/7/6
     */
    public class PoiExcelUtil {
    
        /**
         * 获取标题字体
         * @param xssfWorkbook
         * @return
         */
        public static XSSFFont getTitleFont(XSSFWorkbook xssfWorkbook){
            //创建标题字体
            XSSFFont titleFont = xssfWorkbook.createFont();
            titleFont.setBold(true);//加粗
            titleFont.setFontName("微软雅黑");
            titleFont.setFontHeightInPoints((short) 12);//字体大小
    
            return titleFont;
        }
    
        /**
         * 获取通用字体
         * @param xssfWorkbook
         * @return
         */
        public static XSSFFont getCommonFont(XSSFWorkbook xssfWorkbook){
            //创建通用字体
            XSSFFont commonFont = xssfWorkbook.createFont();
            commonFont.setBold(false);//加粗
            commonFont.setFontName("微软雅黑");
            commonFont.setFontHeightInPoints((short) 12);//字体大小
    
            return commonFont;
        }
    
        /**
         * 获取标题单元格样式
         * @param xssfWorkbook
         * @param xssfFont
         * @return
         */
        public static CellStyle getTitleCellStyle(XSSFWorkbook xssfWorkbook , XSSFFont xssfFont){
            // 创建标题行单元格样式
            CellStyle titleCellStyle = xssfWorkbook.createCellStyle();
            titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框线
            titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
            titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
            titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
            titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平对齐方式
            titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直对齐方式
            titleCellStyle.setFont(xssfFont);//字体样式
            titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//单元格前景色
            titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充单元格
    
            return titleCellStyle;
        }
    
        /**
         * 获取通用单元格样式
         * @param xssfWorkbook
         * @param xssfFont
         * @return
         */
        public static CellStyle getCommonCellStyle(XSSFWorkbook xssfWorkbook, XSSFFont xssfFont){
            //创建通用行单元格样式
            CellStyle commonCellStyle = xssfWorkbook.createCellStyle();
            commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
            commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
            commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
            commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
            commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
            commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
            commonCellStyle.setFont(xssfFont);
            commonCellStyle.setWrapText(true);//自动换行
    
            return commonCellStyle;
        }
    
        /**
         * 写入单个单元格数据
         * @param row 行对象
         * @param xssfSheet sheet对象
         * @param value 单元格的值
         * @param cellStyle 单元格样式
         * @param rowHeight 行高
         * @param columnWidth 列宽
         */
        public static void writeCellData(XSSFRow row, XSSFSheet xssfSheet , Object value ,CellStyle cellStyle,Integer currentColumn,Float rowHeight,Integer columnWidth){
    
            //创建单元格,传入值,设置单元格样式
            XSSFCell cell = row.createCell(currentColumn);
            cell.setCellValue(value == null ? "" : value.toString());
            cell.setCellStyle(cellStyle);
            //设置行高度
            row.setHeightInPoints(rowHeight);
            //设置列的宽度
            xssfSheet.setColumnWidth(currentColumn,columnWidth);
        }
    
        /**
         *
         * @param row 行对象
         * @param xssfSheet sheet对象
         * @param cellStyle 单元格样式
         * @param fields 反射获取得到的实体对象的全部属性
         * @param currentColumn 当前列
         * @param rowHeight 行高
         * @param columnWidth 列宽
         * @param type 类型
         */
        public static void writeTitleCellData(XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Integer currentColumn,Float rowHeight,Integer columnWidth,String type){
            //创建标题行
            // 遍历每个字段
            for (Field field : fields) {
                // 检查字段是否带有ExcelExport注解
                if (field.isAnnotationPresent(ExcelExport.class)) {
                    // 获取Explanation注解实例
                    ExcelExport explanation = field.getAnnotation(ExcelExport.class);
    
                    //判断是否是需要写入的数据类型
                    String[] typeArray = explanation.type();
                    Set<String> set = new HashSet<>(Arrays.asList(typeArray));
                    if (!set.contains(type)){
                     continue;
                    }
                    // 获取注解中的解释
                    String value = explanation.value();
                    //判断value是否是json格式数据
                    boolean isJson = true;
                    try{
                        Object parse = JSON.parse(value);
                    }catch (Exception e){
                        isJson = false;
                    }
                    if (isJson == true){//如果是json格式数据,则给他对应对应类型的值
                        JSONObject jsonObject = JSON.parseObject(value);
                        value = jsonObject.getString(type);
                    }
    
                    //写入单元格数据
                    PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
                    //当前列+1
                    currentColumn++;
                }
            }
        }
    
        /**
         * 将集合数据全部写入单元格
         * @param list 需要写入excel的集合数据
         * @param currentRow 当前行
         * @param currentColumn 当前列
         * @param row 行对象
         * @param xssfSheet sheet对象
         * @param cellStyle 单元格样式
         * @param fields 反射获取得到的实体对象的全部属性
         * @param rowHeight 行高
         * @param columnWidth 列宽
         * @param type 类型
         * @param <T>
         */
        public static <T> void writeCommonRowCellData(List<T> list,Integer currentRow ,Integer currentColumn, XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Float rowHeight,Integer columnWidth,String type){
            //创建数据行
            for (T obj : list) {
                //每次循环时重置列
                currentColumn = 0;
                //当前行+1
                currentRow++;
                //创建行
                row = xssfSheet.createRow(currentRow);
                for (Field field : fields) {
                    // 检查字段是否带有ExcelExport注解
                    if (field.isAnnotationPresent(ExcelExport.class)) {
                        try {
                            //解除private限制
                            field.setAccessible(true);
                            // 获取Explanation注解实例
                            ExcelExport explanation = field.getAnnotation(ExcelExport.class);
    
                            //判断是否是需要写入的数据类型
                            String[] typeArray = explanation.type();
                            Set<String> set = new HashSet<>(Arrays.asList(typeArray));
                            if (!set.contains(type)){
                                continue;
                            }
    
                            // 获取属性的值
                            Object value = field.get(obj);
                            //日期类型格式化
                            if (value != null && field.getType() == Date.class){
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                value = sdf.format(value);
                            }
                            //获取对应字典
                            String dict = explanation.dict();
                            if (!StringUtils.isEmpty(dict) && value != null){
                                List<String> parseArray = JSON.parseArray(dict, String.class);
                                for (String item : parseArray) {
                                    JSONObject jsonObject = JSON.parseObject(item);
                                    if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
                                        value = jsonObject.getString("label");
                                        break;
                                    }
                                }
                            }
                            //写入单元格数据
                            PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
                            //当前列+1
                            currentColumn++;
    
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
    }
    
    
  2. Two

    import com.lzj.entity.User;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.xssf.usermodel.*;
    
    import java.io.*;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    /**
     * <p>
     *
     * </p>
     *
     * @author:雷子杰
     * @date:2023/7/4
     */
    public class Two {
        public static void main(String[] args) throws IOException {
            List<User> userList = new ArrayList<>();
            userList.add(new User("lzj1",1,1,new Date()));
            userList.add(new User("lzj2",2,0,new Date()));
            userList.add(new User("lzj3",3,1,new Date()));
            userList.add(new User("lzj4",4,0,new Date()));
    
            //声明XSSF对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
            //创建sheet
            XSSFSheet userSheet = xssfWorkbook.createSheet("user");
    
            //创建标题字体
            XSSFFont titleFont = PoiExcelUtil.getTitleFont(xssfWorkbook);
            //创建通用字体
            XSSFFont commonFont = PoiExcelUtil.getCommonFont(xssfWorkbook);
            // 创建标题行单元格样式
            CellStyle titleCellStyle = PoiExcelUtil.getTitleCellStyle(xssfWorkbook,titleFont);
            //创建通用行单元格样式
            CellStyle commonCellStyle = PoiExcelUtil.getCommonCellStyle(xssfWorkbook,commonFont);
            //获取实体类中全部属性
            Field[] fields = User.class.getDeclaredFields();
            //当前行
            int currentRow = 0;
            //当前列
            int currentColumn = 0;
            //行高
            float rowHeight = 40.1f;
            //列宽
            int columnWidth = 33 * 256;
            //创建行
            XSSFRow row = userSheet.createRow(currentRow);
            //创建标题行
            PoiExcelUtil.writeTitleCellData(row,userSheet,titleCellStyle,fields,currentColumn,rowHeight,columnWidth,"b");
            //重置当前列
            currentColumn = 0;
            //创建数据行
            PoiExcelUtil.writeCommonRowCellData(userList,currentRow,currentColumn,row,userSheet,commonCellStyle,fields,rowHeight,columnWidth,"b");
    
    
            // 将生成的excel文件输出流转为字节数组
            byte[] bytes = null;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            xssfWorkbook.write(outputStream);
            outputStream.close();
            bytes = outputStream.toByteArray();
    
            //读取字节数组为文件输入流
            InputStream inputStream = new ByteArrayInputStream(bytes);
            inputStream.close();
    
    
            //在声明一个输出流将文件下载到本地
            File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            //将bytes中的内容写入
            bufferedOutputStream.write(bytes);
            //刷新输出流,否则不会写出数据
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
    
        }
    }
    

结果展示

这是我初始化时的数据

java中使用POI生成Excel并导出,java,python,开发语言

下面的是我type参数不同时的数据,均是以版本2来进行的写入导出。type参数修改位置如下:

java中使用POI生成Excel并导出,java,python,开发语言

type参数为a

java中使用POI生成Excel并导出,java,python,开发语言

type参数为b

java中使用POI生成Excel并导出,java,python,开发语言

总结

在项目开发过程中总会遇到各式各样的问题,只有不断的学习,不断的积累,自身水平才能提高。文章来源地址https://www.toymoban.com/news/detail-586128.html

到了这里,关于java中使用POI生成Excel并导出的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java poi导入Excel、导出excel

    java poi导入Excel、导出excel ReadPatientExcelUtil PoiUtils FileUtils

    2024年02月15日
    浏览(26)
  • Java Poi导出Excel表格详解

    一、导出下面的表格 二、流程详解         1、导出excel需要先将数据准备好         2、创建工作傅对象SXSSFWorkbook         3、使用工作傅对象创建sheet对象(工作页)         4、使用sheet对象创建行对象row(行对象)         5、使用row对象创建cell对象(单元格

    2024年02月10日
    浏览(33)
  • java poi实现Excel多级表头导出

    最近碰到一个导出,比较繁琐,也查询了许多博客,在其中一篇博客的基础上修改,实现了最终想要的效果。话不多说,直接上效果图 1.主代码: 2.合并单元格 3.设置表头单元格的宽度 4.填充数据(注:我这里的数据格式是ListMapString, Object类型,可以根据自己的实际情况来封

    2024年02月03日
    浏览(32)
  • Java原生POI实现的Excel导入导出(简单易懂)

    首先是Controller入口方法 这个接口在postman上传参是下面这样的: 注意里面的参数名称要和接口上的一致,不然会拿不到值 还有file那里key的类型要选file类型的,这样就可以在后面value里面选择文件 然后是Service方法 首先是Controller入口 strJson是用来接受其它参数的,一般导出的

    2024年02月11日
    浏览(32)
  • Java excel poi 使用HSSFWorkbook 导出的excel wps能打开office打不开问题解决 Excel无法打开xx.xlsx,因为文件格式或扩展名无效......

    1.在开发代码中涉及到报表导出 xlsx文件 office打不开问题 JavaPOI导出Excel有三种形式,他们分别是 1.HSSFWorkbook 2.XSSFWorkbook 3.SXSSFWorkbook。 pom文件如下 检查创建sheet代码如下 代码中用了 HSSFworkbook 去创建Sheet 导致office打不开原因就在这里 HSSFworkbook 解释如下: HSSFWorkbook:是操作Exc

    2024年02月16日
    浏览(25)
  • Java POI (2)—— Excel文件的上传与导出(实例演示)

             这里是一个demo的流程图,下面按照这个流程图做了一个简单的实现,有部分判断没有加上,实际操作中,可以根据自己的需求进行增加或者修改。并且此处还是在接受文件传入后将文件进行了下载,保存到本地的操作,这个要按照具体情况具体分析,看需求是否

    2024年02月11日
    浏览(33)
  • Java POI导出Excel时,合并单元格没有边框的问题

    今天用POI导出Excel的时候,发现导出的单元格确少边框,最后发现有2个方案可以解决。 CellRangeAddress的4个参数分别表示:起始行号,终止行号, 起始列号,终止列号

    2024年02月14日
    浏览(31)
  • 【Java】使用 HSSFWorkbook 生成 Excel 并导出步骤

    1、含义:excel的工作簿 2、创建工作簿 3、创建 excel 的工作表 4、创建单元格样式 1、含义:excel 的工作表 2、创建行(第一行从 0 开始) 3、设置单元格宽度 1、含义:单元格样式 2、属性设置 1、含义:excel 的行 2、创建行对应的单元格(第一个单元格从 0 开始) 3、属性设置

    2024年02月03日
    浏览(31)
  • Java根据excel模版导出Excel(easyexcel、poi)——含项目测试例子拿来即用

    一般列表导出以及个性化样式设置请看下面的文章: JAVA导出Excel通用工具类——第一篇:详细介绍POI 导出excel的多种复杂情况,包括动态设置筛选、动态合并横向(纵向)单元格等多种复杂情况. JAVA导出Excel通用工具——第二篇:使用EasyExcel导出excel的多种情况的例子介绍.

    2024年04月29日
    浏览(29)
  • java poi导出excel单元格设置自定义背景颜色(任意颜色)

    一、思考过程(看代码的移步第二点) 现有方法 现有资料多为使用 IndexedColors 设置颜色, 但是IndexedColors能设置的颜色有限 ,而需求中所要颜色都是花里胡哨的,需要真正的自定义; 而颜色的本质是rgb ,所以只要我们能自己设置rgb的值就能获取任意想要的颜色了; 源码分

    2023年04月10日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包