Spring Boot--POI导出excel文件下载

1. 依赖

 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>3.16</version>
		</dependency>
		 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>3.16</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-scratchpad</artifactId>
		    <version>3.16</version>
		</dependency>
  
		<dependency>
		    <groupId>com.google.code.gson</groupId>
		    <artifactId>gson</artifactId>
		</dependency>

2. 代码

2.1 写入excel

/**
	 * 导出excel
	 * @param list 数据集合
	 * @param column 列名
	 * @param templatePath 模板路径 
	 * @param os  输出流
	 */
	public static <T> void exportExcel(List<T> list, String[] column, String templatePath, OutputStream os) {

		// 获取列名map
		Map<String, String> map = XmlParser.getColumnName(templatePath);

		// 声明一个工作薄
		HSSFWorkbook wb = new HSSFWorkbook();
		// 声明一个单子并命名
		HSSFSheet sheet = wb.createSheet("1");
		// 给单子名称一个长度
		sheet.setDefaultColumnWidth((short) 15);
		// 生成一个样式
		HSSFCellStyle style = wb.createCellStyle();
		// 创建第一行(也可以称为表头)
		HSSFRow row = sheet.createRow(0);
		// 样式字体居中
		style.setAlignment(HorizontalAlignment.CENTER);
		// 给表头第一行一次创建单元格
		if (column == null || column.length == 0)
			return ;
		for (int index 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot 可以使用 Apache POI 库来导出 Excel 文件。以下是一个简单的示例,演示如何使用 Spring Boot 和 Apache POI 库来创建和导出 Excel 文件: 1. 首先在 pom.xml 文件中添加 Apache POI 依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个控制器类,例如 ExcelController,其中包含导出 Excel 文件的方法。示例代码如下: ```java @RestController @RequestMapping("/excel") public class ExcelController { @GetMapping("/export") public void exportExcel(HttpServletResponse response) throws IOException { // 创建一个 Excel 工作簿 Workbook workbook = new XSSFWorkbook(); // 创建一个工作表 Sheet sheet = workbook.createSheet("My Sheet"); // 添加表头 Row header = sheet.createRow(0); header.createCell(0).setCellValue("Name"); header.createCell(1).setCellValue("Age"); // 添加数据 Row row = sheet.createRow(1); row.createCell(0).setCellValue("John"); row.createCell(1).setCellValue(25); // 设置响应头 response.setHeader("Content-Disposition", "attachment; filename=my_excel_file.xlsx"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // 输出 Excel 文件 OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.close(); workbook.close(); } } ``` 3. 在浏览器中访问 /excel/export 路径,即可下载导出Excel 文件。 这只是一个简单的示例,实际应用中可能需要更复杂的 Excel 文件格式和数据处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值