21xrx.com
2024-09-19 08:19:24 Thursday
登录
文章检索 我的文章 写文章
Java实现导出Excel工具类
2023-06-15 20:41:41 深夜i     --     --
Java Excel 导出工具类

在日常的开发中,经常需要将数据导出成Excel表格的形式,方便用户进行查看和分析。Java语言提供了丰富的工具类库来实现Excel的导出操作,本文将介绍一种通用的导出Excel工具类的实现方式。

实现步骤:

1. 引入POI和相关依赖

在Maven中,我们可以通过以下依赖引入POI工具库和其他相关依赖:


  org.apache.poi

  poi

  4.1.2

  org.apache.poi

  poi-ooxml

  4.1.2

  org.apache.poi

  poi-ooxml-schemas

  4.1.2

  org.apache.poi

  poi-examples

  4.1.2

  org.apache.poi

  poi-excelant

  4.1.2

  commons-beanutils

  commons-beanutils

  1.9.3

2. 编写导出Excel的工具类

我们可以参考以下代码,实现一个通用的导出Excel的工具类:


public class ExcelExportUtil {

  private List list;

  /**

   * Instantiates a new Excel export util.

   */

  public ExcelExportUtil()

  

  /**

   * Instantiates a new Excel export util.

   *

   * @param list the list

   */

  public ExcelExportUtil(List list)

    this.list = list;

  

  /**

   * Export.

   *

   * @param fileName the file name

   * @param sheetName the sheet name

   * @param fields the fields

   * @param fieldNames the field names

   * @param response the response

   * @throws Exception the exception

   */

  public void export(String fileName, String sheetName,

            String[] fields, String[] fieldNames,

            HttpServletResponse response) throws Exception {

    Workbook workbook = new XSSFWorkbook();

    Sheet sheet = workbook.createSheet(sheetName);

    Row headerRow = sheet.createRow(0);

    for (int i = 0; i < fieldNames.length; i++) {

      headerRow.createCell(i).setCellValue(fieldNames[i]);

    }

    for (int i = 0; i < list.size(); i++) {

      Object obj = list.get(i);

      Row row = sheet.createRow(i + 1);

      for (int j = 0; j < fields.length; j++) {

        String fieldName = fields[j];

        Object fieldValue = PropertyUtils.getProperty(obj, fieldName);

        if (fieldValue != null) {

          row.createCell(j).setCellValue(fieldValue.toString());

        } else {

          row.createCell(j).setCellValue("");

        }

      }

    }

    response.setContentType("application/vnd.ms-excel");

    response.setHeader("Content-disposition", "attachment; filename=" + fileName);

    response.flushBuffer();

    workbook.write(response.getOutputStream());

  }

}

3. 使用导出Excel的工具类

在使用导出Excel工具类时,我们只需要按照以下步骤进行即可:

- 创建待导出的数据集合List

- 创建字段数组fields和字段名称数组fieldNames,保证二者的顺序一致

- 创建导出文件的名称fileName和工作表名称sheetName

- 调用ExcelExportUtil 类的export方法,传入前面的参数即可

下面是一个例子:


List studentList = new ArrayList<>();

studentList.add(new Student(1L, "Tom", 90.2));

studentList.add(new Student(2L, "Jerry", 80.5));

studentList.add(new Student(3L, "Lily", 86.0));

String[] fields = "name";

String[] fieldNames = "学号";

String fileName = "学生信息.xlsx";

String sheetName = "学生信息";

HttpServletResponse response = ... // 获取HttpServletResponse对象

ExcelExportUtil excelExportUtil = new ExcelExportUtil<>(studentList);

excelExportUtil.export(fileName, sheetName, fields, fieldNames, response);

文章

代码案例关键词:Apache POI、Workbook、Sheet、Row、PropertyUtils

使用步骤关键词:数据集合、字段、导出文件、工作表名、HttpResponseServletResponse

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章