JAVA POI导出的两种方式

这篇博客介绍了使用JAVA POI库导出数据的两种方法。第一种方法涉及将List<Map>数据导出,包括引入jar包、创建ExportExcelUtils工具类和编写controller部分。第二种方法是导出List<Obj>,包括添加jar包、创建实体类和工具类,然后在controller中实现。通过浏览器访问指定地址即可完成导出操作。
摘要由CSDN通过智能技术生成

方式一:简单的List《Map》导出
1.加入jar包

<!-- 导出excel配置 start -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.17</version>
		</dependency>
		
		<dependency>
			<groupId>net.sourceforge.jexcelapi</groupId>
			<artifactId>jxl</artifactId>
			<version>2.6.12</version>
		</dependency>
		<!-- 导出excel配置 end -->

2.加入工具类 ExportExcelUtils

package cn.lzj.ladmin.common.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * @ClassName: ExportExcelUtils
 * @Description: 导出Excel文件
 */
public class ExportExcelUtils {
	/**
	 * @Description: 创建工作簿导出
	 * @param list 导出数据集合
	 * @param keys 表头数组集合
	 * @param columnNames  导出excel里面的列名属性
	 * @param styleMap  样式列表[可以使用默认样式]
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Workbook createWorkbook(List<Map> list, String[] keys,
			String[] columnNames,Map<Short, HorizontalAlignment> styleMap) {
		// 创建excel工作簿
		SXSSFWorkbook wb = new SXSSFWorkbook(1000);
		// 创建第一个sheet(页),并命名
		Sheet sheet = null;
		
		// 创建两种单元格格式
		CellStyle cs = wb.createCellStyle();
		CellStyle cs2 = wb.createCellStyle();

		// 创建两种字体
		Font f = wb.createFont();
		Font f2 = wb.createFont();

		// 创建第一种字体样式(用于列名)
		f.setFontHeightInPoints((short) 10);
		f.setColor(IndexedColors.BLACK.getIndex());
		f.setBold(true);

		// 创建第二种字体样式(用于值)
		f2.setFontHeightInPoints((short) 10);
		f2.setColor(IndexedColors.BLACK.getIndex());

		// 设置第一种单元格的样式(用于列名)
		cs.setFont(f);
		cs.setBorderLeft(BorderStyle.THIN);
		cs.setBorderRight(BorderStyle.THIN);
		cs.setBorderTop(BorderStyle.THIN);
		cs.setBorderBottom(BorderStyle.THIN);
		cs.setAlignment(HorizontalAlignment.CENTER);

		// 设置第二种单元格的样式(用于值)
		cs2.setFont(f2);
		cs2.setBorderLeft(BorderStyle.THIN);
		cs2.setBorderRight(BorderStyle.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值