首页 > 其他分享 >poi的excel导出

poi的excel导出

时间:2024-08-21 16:04:18浏览次数:7  
标签:name excel age 导出 sex Student poi new public

poi的excel导出

这个导出依赖于模板文件,可便捷设置表头样式。 也可以不使用模板,直接创建。

1.引入poi依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

2.准备模板文件

image

3.编写导出代码

测试代码直接导出到本地,如需要在浏览器中导出将输出流交给浏览器即可

public class GenerateExcel {
    public static void main(String[] args) throws IOException {
        GenerateExcelXlsx();
    }


   static void GenerateExcelXlsx() throws IOException {

       List<Student> students = new Student().stuAll();
       InputStream inputStream = GenerateExcel.class.getClassLoader().getResourceAsStream("static/测试填充excel.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        // 获取第一个sheet
        Sheet sheet = workbook.getSheetAt(0);
        int index = 2;
        for (Student item : students) {
            Row row = sheet.createRow(index);
            row.createCell(0).setCellValue(item.getName());
            row.createCell(1).setCellValue(item.getSex().toString());
            row.createCell(2).setCellValue(item.getAge());
            index += 1;
        }

       FileOutputStream outputStream = new FileOutputStream("C:\\Users\\pyb\\Desktop\\a.xlsx");

        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();

    }


    static class Student{
        private String name;
        private Integer age;
        private Character sex;

        public Student() {
        }

        public Student(String name, Character sex, Integer age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }

        List<Student> stuAll(){
             ArrayList<Student> list = new ArrayList<>();
             for (int i = 0; i < 5; i++) {
                 Student student = new Student("张三" + i, '男' ,18 );
                 list.add(student);
             }
             return list;
         }

        public String getName() {
            return name;
        }

        public Integer getAge() {
            return age;
        }

        public Character getSex() {
            return sex;
        }
    }

}

4.运行后效果图

image

5.不依赖模板直接导出

这种方式表头内容需要自己手动写,

public class GenerateExcel {
    public static void main(String[] args) throws IOException {
        GenerateExcelXlsx2();
    }

   static void GenerateExcelXlsx2() throws IOException {

        List<Student> students = new Student().stuAll();
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建一个sheet,这里面形参是内部名称,不可见
        Sheet sheet = workbook.createSheet();
        // 设置为第几个sheet,并设置用户可见名称
        workbook.setSheetName(0,"测试sheet");

        int index = 0;
        for (Student item : students) {
            Row row = sheet.createRow(index);
            row.createCell(0).setCellValue(item.getName());
            row.createCell(1).setCellValue(item.getSex().toString());
            row.createCell(2).setCellValue(item.getAge());
            index += 1;
        }

       FileOutputStream outputStream = new FileOutputStream("C:\\Users\\pyb\\Desktop\\b.xlsx");

        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();

    }


    static class Student{
        private String name;
        private Integer age;
        private Character sex;

        public Student() {
        }

        public Student(String name, Character sex, Integer age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }

        List<Student> stuAll(){
             ArrayList<Student> list = new ArrayList<>();
             for (int i = 0; i < 5; i++) {
                 Student student = new Student("张三" + i, '男' ,18 );
                 list.add(student);
             }
             return list;
         }

        public String getName() {
            return name;
        }

        public Integer getAge() {
            return age;
        }

        public Character getSex() {
            return sex;
        }
    }

}

应用模板表格中的格式

一般应用与固定位置填充完毕的计算,动态的没测试过

数据塞入完毕后加入以下代码


 // 计算公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();

总结

获取某行或者某列时候,尽量用创建方式,除非确定行和列都能被获取到(行列不做操作、修改格式的情况下内容是空,会报空指针)

标签:name,excel,age,导出,sex,Student,poi,new,public
From: https://www.cnblogs.com/pyb999/p/18371870

相关文章

  • 基于NPOI封装支持多级表头灵活读写的《Soul.XLS》库
    一、先上效果图二、上代码dotnetaddpackageSoul.XLS--version1.1.0//构造一个多级表头varcolumns=newExcelColumnCollection("学生信息"){newExcelColumn("学生信息"){Items=new[]{newExcelColumn("个人信息")......
  • 在Vue中的三种不同的模块导出方式:分别暴露、统一暴露和默认暴露。
     一,分别暴露:通过在模块中使用export关键字来分别导出多个变量、函数或组件。例如:test.jsexportconstperson={name:"bruce",age:33}exportfunctiongetPerson(){console.log(11)}在另一个模块中,可以使用解构赋值来导入这些分别暴露的变量、函数或组件:import......
  • 泊松自助法(Poisson Bootstrap Sampling):大型数据集上的自助抽样
    自助抽样可以根据收集的样本推断总体的统计特征(如均值、十分位数、置信区间)。泊松自助抽样(PoissonBootstrapSampling)是一种用于统计分析中的重采样技术,特别是在机器学习和数据科学中用于模型评估和误差估计。这种方法的一个特点是保留了样本中数据点出现的自然波动,而不是像传......
  • [Paper Reading] HandDiff: 3D Hand Pose Estimation with Diffusion on Image-Point
    HandDiff:3DHandPoseEstimationwithDiffusiononImage-PointCloudHandDiff:3DHandPoseEstimationwithDiffusiononImage-PointCloud时间:CVPR2024机构:ETH&CMU&SungkyunkwanUniversityTL;DR将手部分3D点预测任务建模为点云生成任务,提出Handiff算法,以手部......
  • C# read excel file via ExcelDataReader
    Install-PackageExcelDataReader usingExcelDataReader;usingSystem.Text;usingSystem.IO;namespaceConsoleApp50{internalclassProgram{staticvoidMain(string[]args){ReadExcel("aaa.xls");......
  • 洛谷P3528 [POI2011] PAT-Sticks && 数据结构之堆
    传送门:P3528[POI2011]PAT-Sticks与买桂花同载酒,终不似,少年游这是现在为止洛谷上的最优解!!翻译题目描述小约翰尼的爷爷奶奶送给他一份生日礼物。这份礼物是一盒长度和颜色各异的木棍。约翰尼想知道,在他得到的这组木棍中,是否存在三根木棍能够组成一个三边颜色各不相同的三......
  • Excel/WPS 取消合并单元格,并填充
     WPS实现 Excel实现https://baijiahao.baidu.com/s?id=1799704002472022803&wfr=spider&for=pc然后我们就需要在公式编辑里面,输入公式=C2按CTRL+回车,就可以填充所有的结果不是 CTRL+回车是,向下填充 ......
  • 一款专为内网办公环境设计的操作系统,集成了Word、Excel、PPT、PDF编辑器,内网聊天、白
    前言在当今数字化办公时代,企业面临着多样化的办公需求。现有软件往往存在一些痛点,如操作复杂、兼容性差、资源消耗高,以及在内网环境下的通讯和文件共享不便。这些限制不仅影响了工作效率,也制约了企业的数字化转型。因此,一款能够处理这些问题的软件显得尤为迫切。介绍GodoOS......
  • 免费Excel工作表同类数据合并工具
       下载地址:https://pan.quark.cn/s/81b1aeb45e4c在Excel表格中,手动将多行同类数据合并为一行存在诸多困难和复杂的操作,容易出现错漏且难以保证数据合并的完全正确性,需要人工反复复核。作者因深受其苦,开发了一个具有特定功能的小工具。**重要亮点**-**手动合并数据的......
  • P1543 [POI2004] SZP 题解
    P1543[POI2004]SZP题解传送门。题目简述有\(n\)个人,每个人都会监视另一个人,要求选出尽可能多的同学,使得选出的每一名同学都必定会被监视到。且选出的同学不可再监视其他人。思路简述因为任意一个人只能被另一个人管,那么就想到,如果没人管的同学就不能被选(不被监视)。若某......