poi-3.11 SXSSFWorkbook 导出Execl 含Sheet分页

6 篇文章 0 订阅
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.util.HSSFColor;
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.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import com.google.common.collect.Lists;

public class ExeclExportUtils {

	public static void execute(String[] cellTitle, List<String[]> cellData, File filePath) {
		SXSSFWorkbook workBook = null;
		FileOutputStream outStream = null;
		try {
			// 声明一个工作薄
			workBook = new SXSSFWorkbook();
			// 标题样式
			CellStyle cellTitleStyle = workBook.createCellStyle();
			// 设置水平对齐的样式为居中对齐;
			cellTitleStyle.setAlignment(CellStyle.ALIGN_CENTER);
			// 设置垂直对齐的样式为居中对齐;
			cellTitleStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);
			// 设置背景颜色
			cellTitleStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
			// solid 填充 foreground 前景色
			cellTitleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
			// 边框
			cellTitleStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
			cellTitleStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
			cellTitleStyle.setBorderBottom(IndexedColors.BLACK.getIndex());
			// 标题字体样式
			Font cellTitleFont = workBook.createFont();
			cellTitleFont.setColor(HSSFColor.WHITE.index); // 将字体设置为“白色”
			cellTitleFont.setFontHeightInPoints((short) 12); // 将字体大小设置为12px
			cellTitleFont.setFontName("华文细黑"); // 将“华文细黑”字体应用到当前单元格上
			cellTitleStyle.setFont(cellTitleFont);

			// 数据 - 根据数据大小拆分多页
			int sheetNum = 50000;
			List<List<String[]>> listData = Lists.partition(cellData, sheetNum);
			for (int i = 0; i < listData.size(); i++) {
				Sheet sheetDetail = workBook.createSheet();
				workBook.setSheetName(i, ""+i);
				// 创建表格标题行 第一行
				Row titleRow = sheetDetail.createRow(0);
				titleRow.setHeightInPoints(20);
				for (int j = 0; j < cellTitle.length; j++) {
					Cell cell = titleRow.createCell(j);
					cell.setCellStyle(cellTitleStyle);
					cell.setCellValue(cellTitle[j]);
				}
				// 插入导出的数据
				List<String[]> detailDataList = listData.get(i);
				for (int j = 0; j < detailDataList.size(); j++) {
					Row row = sheetDetail.createRow(j + 1);
					for (int k = 0; k < cellTitle.length; k++) {
						String valStr = detailDataList.get(j)[k];
						if (valStr.trim().length() == 0 || "null".equals(valStr)) {
							valStr = "";
						}
						row.createCell(k).setCellValue(valStr);
					}
				}
				// 调整宽度
				for (int j = 0; j < cellTitle.length; j++) {
					sheetDetail.setColumnWidth(j, cellTitle[j].length() * 1000);
				}
				// 固定第一行标题
				sheetDetail.createFreezePane(1, 1, 1, 1);
			}
			// 导出
			System.out.println("生成文件路径:" + filePath.getAbsolutePath());
			// 文件输出流
			outStream = new FileOutputStream(filePath);
			workBook.write(outStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != outStream) {
				try {
					outStream.flush();
					outStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != workBook) {
				try {
					workBook.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>26.0-jre</version>
        </dependency>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
poi-tl是一个基于POI的模板引擎,可以用于动态生成Excel文档。下面是使用poi-tl动态导出Excel的步骤: 1. 导入poi-tl的依赖包。 2. 创建Excel模板文件,可以使用Microsoft Excel软件创建一个模板文件,然后在模板中使用poi-tl的标签进行动态生成。 3. 在Java代码中,使用poi-tl的API读取模板文件,并进行数据填充。 4. 将填充好数据的Excel文件保存到指定路径。 下面是一个使用poi-tl动态导出Excel的示例代码: ```java import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.List; import java.util.Map; public class ExcelExportUtil { public static void main(String[] args) { try { // 读取模板文件 InputStream is = new FileInputStream("template.xlsx"); Workbook workbook = new XSSFWorkbook(is); // 获取模板中的Sheet Sheet sheet = workbook.getSheetAt(0); // 定义数据 List<Map<String, Object>> dataList = getDataList(); // 填充数据 int rowIndex = 1; // 从第二行开始填充数据 for (Map<String, Object> data : dataList) { Row row = sheet.createRow(rowIndex++); int cellIndex = 0; for (String key : data.keySet()) { Cell cell = row.createCell(cellIndex++); cell.setCellValue(data.get(key).toString()); } } // 保存文件 OutputStream os = new FileOutputStream("output.xlsx"); workbook.write(os); os.close(); System.out.println("Excel导出成功!"); } catch (Exception e) { e.printStackTrace(); } } private static List<Map<String, Object>> getDataList() { // 模拟数据 List<Map<String, Object>> dataList = new ArrayList<>(); Map<String, Object> data1 = new HashMap<>(); data1.put("name", "张三"); data1.put("age", 20); data1.put("gender", "男"); dataList.add(data1); Map<String, Object> data2 = new HashMap<>(); data2.put("name", "李四"); data2.put("age", 25); data2.put("gender", "女"); dataList.add(data2); return dataList; } } ``` 请注意,上述代码中的"template.xlsx"是模板文件的路径,"output.xlsx"是导出的Excel文件的路径。你需要根据实际情况修改这两个路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值