java poi批量导出excel表格

1.前言

现在提供了很多方便的框架操作excel进行导入导出。
框架的底层还是对poi框架进行了封装。

2.项目环境

前端使用的是layui框架,后台是springboot框架,部署在阿里云linux服务器上。

3.maven依赖

导入maven 依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi-examples</artifactId>
	    <version>4.1.2</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi</artifactId>
	    <version>4.1.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi-ooxml</artifactId>
	    <version>4.1.2</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi-ooxml-schemas</artifactId>
	    <version>4.1.2</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi-excelant</artifactId>
	    <version>4.1.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi-scratchpad</artifactId>
	    <version>4.1.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-math3</artifactId>
	    <version>3.6.1</version>
	</dependency>

4. 代码演示

1.首先创建工作簿,相当于整体excel文件,是整个文件的骨架

	private HSSFWorkbook workbook = new HSSFWorkbook();

2.再通过workbook 对象 创建sheet对象

 private HSSFSheet sheet = workbook.createSheet();

对应于excel表格的工作空间
在这里插入图片描述
3.接下来就到数据单元的绘制
可以分为两个部分:
表头:数据是固定的
列数据:是动态数据
每个数据都由一个单元格组成,一行可以有多个单元格
在这里插入图片描述

3.1.创建表头


	private static final String[] HEAD_NAME = {"id","姓名","公司","职位","电话","邮箱","更多信息"};

	.....省略
	
		//===========表头操作=====================
        for (int i = 0; i < HEAD_NAME.length; i++) {
        	//设置宽度
            sheet.setColumnWidth(i,4000);
            if (i == 0) {
                //创建一行表头
                headRow = sheet.createRow(i);
                headRow.setHeight((short) 800);
            }
            //创建单元格数据
            HSSFCell cell = headRow.createCell(i);
            //给单元格赋值
            cell.setCellValue(HEAD_NAME[i]);
        }

3.2 绘制数据

			//===========数据操作=====================
        for (int i = 0; i < appointment.size(); i++) {
        	//从i+1行开始绘制
            HSSFRow row = sheet.createRow(i+1);
            
            //jackson反序列化
            Appointment value = objectMapper.convertValue(appointment.get(i), Appointment.class);
            
			//为每个数据创建一个单元格
            HSSFCell cell = row.createCell(0);
            cell.setCellValue(value.getAId());

            HSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(value.getName());

            HSSFCell cell3 = row.createCell(2);
            cell3.setCellValue(value.getCompany());

            HSSFCell cell4 = row.createCell(3);
            cell4.setCellValue(value.getPost());

            HSSFCell cell5 = row.createCell(4);
            cell5.setCellValue(value.getPhone());

            HSSFCell cell6 = row.createCell(5);
            cell6.setCellValue(value.getEmail());

            HSSFCell cell7 = row.createCell(6);
            cell7.setCellValue(value.getMessage());
        }

5.整体代码

controller层代码

	@PostMapping("/export")
    public Object export(@RequestParam String appointment){
        HashMap result = new HashMap();
        try {
            ObjectMapper mapper = new ObjectMapper();
            List list = mapper.readValue(appointment,List.class);
            String fileName = new ExcelUtil().export(list, mapper);
            if (!fileName.isEmpty()) {
            	//必须将路径返回给前端,才能配合前端下载到本地
                result.put("msg","excel/"+fileName);
                result.put("code", 200);
                result.put("desc", "导出成功");
                return result;
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        result.put("code", 500);
        result.put("desc", "导出失败");
        return result;
    }

业务层代码

package com.hniu.zs_manage.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hniu.zs_manage.entity.appointment.Appointment;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @Description: excel 导出工具类
 * @Author: songbiao
 */
@Component
public class ExcelUtil {

    private static final String[] HEAD_NAME = {"id","姓名","公司","职位","电话","邮箱","更多信息"};

    private static FileOutputStream FILE_OUTPUT_STREAM = null;

    private static Logger logger = LogUtil.getLOG(ExcelUtil.class);

    private static final File FILE_URL = new File("/cbim/ManagerSystem/excel");

    private static String fileName = "";

    private HSSFWorkbook workbook = new HSSFWorkbook();

    private HSSFSheet sheet = workbook.createSheet();

    private static HSSFFont headFont = null;
    private static HSSFCellStyle headStyle = null;
    private static HSSFFont dataFont = null;
    private static HSSFCellStyle dataStyle = null;

    private static HSSFRow headRow = null;

    public ExcelUtil() {
        //头字体样式
        HSSFFont headFont = workbook.createFont();
        headFont.setFontName("宋体");
        headFont.setFontHeightInPoints((short) 18);

        //列头
        HSSFCellStyle headStyle = workbook.createCellStyle();
        headStyle.setFont(headFont);
//        headStyle.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
        //新版本FillPatternType
        headStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headStyle.setAlignment(HorizontalAlignment.CENTER);
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        //数字字体样式
        HSSFFont dataFont = workbook.createFont();
        dataFont.setFontName("宋体");
        dataFont.setFontHeightInPoints((short) 16);

        //数据样式
        HSSFCellStyle dataStyle = workbook.createCellStyle();
        dataStyle.setFont(dataFont);
        dataStyle.setAlignment(HorizontalAlignment.LEFT);
        dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);

       //===========表头操作=====================
        for (int i = 0; i < HEAD_NAME.length; i++) {
        	//设置宽度
            sheet.setColumnWidth(i,4000);
            if (i == 0) {
                //创建一行表头
                headRow = sheet.createRow(i);
                headRow.setHeight((short) 800);
            }
            //创建单元格数据
            HSSFCell cell = headRow.createCell(i);
            //给单元格赋值
            cell.setCellValue(HEAD_NAME[i]);
        }
    }

    public String export(List appointment,ObjectMapper objectMapper) throws IOException {

        FILE_OUTPUT_STREAM = new FileOutputStream(fileCheck(FILE_URL));

        //===========数据操作=====================
        for (int i = 0; i < appointment.size(); i++) {
            HSSFRow row = sheet.createRow(i+1);
            //为每个数据创建一个单元格
            Appointment value = objectMapper.convertValue(appointment.get(i), Appointment.class);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(dataStyle);
            cell.setCellValue(value.getAId());

            HSSFCell cell2 = row.createCell(1);
            cell2.setCellStyle(dataStyle);
            cell2.setCellValue(value.getName());

            HSSFCell cell3 = row.createCell(2);
            cell3.setCellStyle(dataStyle);
            cell3.setCellValue(value.getCompany());

            HSSFCell cell4 = row.createCell(3);
            cell4.setCellStyle(dataStyle);
            cell4.setCellValue(value.getPost());

            HSSFCell cell5 = row.createCell(4);
            cell5.setCellStyle(dataStyle);
            cell5.setCellValue(value.getPhone());

            HSSFCell cell6 = row.createCell(5);
            cell6.setCellStyle(dataStyle);
            cell6.setCellValue(value.getEmail());

            HSSFCell cell7 = row.createCell(6);
            cell7.setCellStyle(dataStyle);
            cell7.setCellValue(value.getMessage());
        }
        workbook.write(FILE_OUTPUT_STREAM);
        FILE_OUTPUT_STREAM.flush();
        FILE_OUTPUT_STREAM.close();
        workbook.close();
        logger.debug("文件流读写关闭成功....");
        return fileName;
    }

    public static File fileCheck(File file) throws IOException {
        if (!file.getPath().endsWith(".xls")) {
            if (!file.exists()) {
                file.mkdirs();
            }
            fileName = "cbim_" + System.currentTimeMillis() + ".xls";
            file = new File(file.getPath(), fileName);
            file.createNewFile();
        } else if (!file.exists()) {
            //是文件且路径不对时直接返回异常信息
            throw new IllegalStateException("路径异常");
        }
        return file;
    }

}

6. 导出到本地配置

可以通过response流将文件返回给客户端,也可以用nginx搭建文件服务器。
文件写在服务器上通过nginx 搭建文件服务器来实现下载到本地

6.1nginx配置

在nginx.conf下配置(找到自己对应的nginx配置文件)

location /myfiles {
    alias /export/share/test/;   # 文件存放目录,注意要以 '/' 结尾;
    autoindex on;               # 自动列出目录下的文件;
    autoindex_exact_size off;   # 文件大小按 GM 的格式显示,而不是 Bytes}

配置成功如下: 点击文件即可下载,或者通过http请求在myfiles后加上该文件名
在这里插入图片描述

6.2前端代码

发送ajax请求controller得到的文件路径,再进行http请求

success:function (res) {
    if (res.code == 200) {
        //下载到本地
        window.location.href(res.msg);
    }
    alert(res.desc);
    layer.close(layer.index);
}

最终结果演示:
在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现JavaExcel批量导入导出所选表格数据,您可以使用以下步骤: 1. 首先,您需要使用Java中的Apache POI库来处理Excel文件。您可以在Maven项目中添加以下依赖项,以便在项目中使用POI: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 2. 接下来,您需要编写代码来读取和写入Excel文件。以下是读取Excel文件的示例代码: ``` FileInputStream fis = new FileInputStream(new File("path/to/excel/file.xlsx")); Workbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); // 处理单元格数据 } } workbook.close(); fis.close(); ``` 3. 对于写入Excel文件,您可以使用以下示例代码: ``` FileOutputStream fos = new FileOutputStream(new File("path/to/excel/file.xlsx")); Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello World!"); workbook.write(fos); workbook.close(); fos.close(); ``` 4. 最后,您需要添加逻辑来选择要读取或写入的Excel表格数据。您可以使用Java中的JFileChooser类来打开文件选择器窗口,以便用户选择要处理的Excel文件。然后,您可以使用POI库中的方法来处理所选表格数据。 希望这些步骤可以帮助您实现JavaExcel批量导入导出所选表格数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值