使用POI导出Excel

封装一个poi导出excel的标准程序,excel样式没做过多处理,直接贴代码

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.List;


import javax.servlet.http.HttpServletResponse;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


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


/**
 * @Author luohongchao
 * @Date 下午3:29:49
 *
 */
public class ExcelUtil {
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 功能描述:excel生成工具
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static <T> void toExcelOutStream(OutputStream out,String codedFileName,List<ExcelVo> header,List<T> datas){
<span style="white-space:pre">		</span>try  
<span style="white-space:pre">	</span>     {  
<span style="white-space:pre">			</span> String oldCodedFileName = codedFileName;
<span style="white-space:pre">	</span>    <span style="white-space:pre">	</span> // 产生工作簿对象  
<span style="white-space:pre">			</span> SXSSFWorkbook workbook = new SXSSFWorkbook(100); 
<span style="white-space:pre">	</span>         //产生工作表对象 
<span style="white-space:pre">	</span>         int sheetNum=1;
<span style="white-space:pre">	</span>         Sheet sheet = workbook.createSheet(codedFileName +"-"+sheetNum++);
<span style="white-space:pre">	</span>         // 进行转码,使其支持中文文件名  
<span style="white-space:pre">	</span>         codedFileName = java.net.URLEncoder.encode(codedFileName+DateUtil.getDateTimeStamp("_"), "UTF-8");
<span style="white-space:pre">	</span>         generateExcelHeader(sheet, header, workbook);
<span style="white-space:pre">	</span>         //插入数据
<span style="white-space:pre">	</span>         Field field;
<span style="white-space:pre">	</span>         int sheetLength=0;
<span style="white-space:pre">	</span>         int rowNum = 1;
<span style="white-space:pre">	</span>         for (int i = 1; i <= datas.size(); i++){
<span style="white-space:pre">	</span>               T obj = datas.get(i-1);
<span style="white-space:pre">	</span>               if(obj != null){
<span style="white-space:pre">	</span>            <span style="white-space:pre">	</span>   //加入超过单个Sheet的最大长度,则新城一个新的sheet页
<span style="white-space:pre">	</span>            <span style="white-space:pre">	</span>   sheetLength++;
<span style="white-space:pre">	</span>            <span style="white-space:pre">	</span>   if(sheetLength>=65535){
<span style="white-space:pre">	</span>            <span style="white-space:pre">		</span>   sheetLength = 0;
<span style="white-space:pre">	</span>            <span style="white-space:pre">		</span>   rowNum = 1;
<span style="white-space:pre">	</span>            <span style="white-space:pre">		</span>   sheet = workbook.createSheet(oldCodedFileName+"-" + sheetNum++);
<span style="white-space:pre">	</span>            <span style="white-space:pre">		</span>   generateExcelHeader(sheet, header, workbook);
<span style="white-space:pre">	</span>            <span style="white-space:pre">	</span>   }
<span style="white-space:pre">	</span>            <span style="white-space:pre">	</span>   Row row = sheet.createRow(rowNum++);//创建一行  
<span style="white-space:pre">		</span>               for(int j=0;j<header.size();j++){
<span style="white-space:pre">		</span>            <span style="white-space:pre">	</span>   Cell cell = row.createCell(j);//创建一列  
<span style="white-space:pre">		</span>            <span style="white-space:pre">	</span>   cell.setCellType(header.get(j).getRowType());
<span style="white-space:pre">		</span>            <span style="white-space:pre">	</span>   field = obj.getClass().getDeclaredField(header.get(j).getRowNameEg());
<span style="white-space:pre">		</span>            <span style="white-space:pre">	</span>   field.setAccessible(true);
<span style="white-space:pre">		</span>            <span style="white-space:pre">	</span>   Object value = field.get(obj);
<span style="white-space:pre">		</span>            <span style="white-space:pre">	</span>   setCellValue(cell, value, header.get(j).getRowType());
<span style="white-space:pre">		</span>               }
<span style="white-space:pre">	</span>               }
<span style="white-space:pre">	</span>         }
<span style="white-space:pre">	</span>        workbook.write(out);
     <span style="white-space:pre">	</span>   <span style="white-space:pre">	</span>out.flush();
<span style="white-space:pre">	</span>     }
<span style="white-space:pre">	</span>     catch (Exception e)  
<span style="white-space:pre">	</span>     {e.printStackTrace();}  
<span style="white-space:pre">	</span>     finally  
<span style="white-space:pre">	</span>     {  
<span style="white-space:pre">	</span>         try  
<span style="white-space:pre">	</span>         {  
<span style="white-space:pre">	</span>        <span style="white-space:pre">	</span> if(out != null){
<span style="white-space:pre">	</span>        <span style="white-space:pre">		</span> out.close();  
<span style="white-space:pre">	</span>        <span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span>         }  
<span style="white-space:pre">	</span>         catch (IOException e)  
<span style="white-space:pre">	</span>         {e.printStackTrace();}  
<span style="white-space:pre">	</span>     } 
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>private static void generateExcelHeader(Sheet sheet,List<ExcelVo> header,SXSSFWorkbook workbook){
<span style="white-space:pre">		</span> //单元格样式
        CellStyle headerStyle = workbook.createCellStyle();
        //字体样式
        Font font= workbook.createFont();
        font.setFontHeightInPoints((short)12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerStyle.setFont(font);
        //生成头行
        if(header !=null && header.size()>0){
       <span style="white-space:pre">	</span> Row row = sheet.createRow(0);//创建一行  
       <span style="white-space:pre">	</span> for(int i=0;i<header.size();i++){
            <span style="white-space:pre">	</span>Cell cell = row.createCell(i);//创建一列  
            <span style="white-space:pre">	</span>cell.setCellType(header.get(i).getRowType());
            <span style="white-space:pre">	</span>cell.setCellValue(header.get(i).getRowNameCn());
            <span style="white-space:pre">	</span>cell.setCellStyle(headerStyle);
            <span style="white-space:pre">	</span>sheet.setColumnWidth(i, header.get(i).getCellWith());
       <span style="white-space:pre">	</span> }
        }
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * @param response 响应流
<span style="white-space:pre">	</span> * @param codedFileName 文件名称,默认生成时间戳格式
<span style="white-space:pre">	</span> * @param header 头行数据
<span style="white-space:pre">	</span> * @param datas 表格数据
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static <T> void  toExcelOutStream(HttpServletResponse response,String codedFileName,List<ExcelVo> header,List<T> datas){
<span style="white-space:pre">	</span>     // 生成提示信息,  
<span style="white-space:pre">	</span>     response.setContentType("application/vnd.ms-excel");
<span style="white-space:pre">	</span>     response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xlsx");
<span style="white-space:pre">	</span>     try {
<span style="white-space:pre">			</span>toExcelOutStream(response.getOutputStream(), codedFileName, header, datas);
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * @param cell 单元格
<span style="white-space:pre">	</span> * @param value 值
<span style="white-space:pre">	</span> * @param cellType 单元格类型
<span style="white-space:pre">	</span> * 设置单元格的值
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private static void setCellValue(Cell cell,Object value,int cellType){
<span style="white-space:pre">		</span>if(cellType==HSSFCell.CELL_TYPE_STRING){
<span style="white-space:pre">			</span>cell.setCellValue(String.valueOf(value));
<span style="white-space:pre">		</span>}else if(cellType==HSSFCell.CELL_TYPE_NUMERIC){
<span style="white-space:pre">			</span>cell.setCellValue((double)value);
<span style="white-space:pre">		</span>}else if(cellType==HSSFCell.CELL_TYPE_BOOLEAN){
<span style="white-space:pre">			</span>cell.setCellValue((boolean)value);
<span style="white-space:pre">		</span>}else if(cellType==HSSFCell.CELL_TYPE_BLANK){
<span style="white-space:pre">			</span>cell.setCellValue("");
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * @Author luohongchao
<span style="white-space:pre">	</span> * @Date 下午3:30:28
<span style="white-space:pre">	</span> * 生成excel所需的头信息
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>@Data
<span style="white-space:pre">	</span>@AllArgsConstructor
<span style="white-space:pre">	</span>@NoArgsConstructor
<span style="white-space:pre">	</span>public static class ExcelVo{
<span style="white-space:pre">		</span>public ExcelVo(String rowNameCn,int rowType,String rowNameEg){
<span style="white-space:pre">			</span>this.rowNameCn = rowNameCn;
<span style="white-space:pre">			</span>this.rowNameEg = rowNameEg;
<span style="white-space:pre">			</span>this.rowType = rowType;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>/**
<span style="white-space:pre">		</span> * 字段中文描述
<span style="white-space:pre">		</span> */
<span style="white-space:pre">		</span>private String rowNameCn;
<span style="white-space:pre">		</span>/**
<span style="white-space:pre">		</span> * 字段类型
<span style="white-space:pre">		</span> * */
<span style="white-space:pre">		</span>private int rowType;
<span style="white-space:pre">		</span>/**
<span style="white-space:pre">		</span> * 字段名
<span style="white-space:pre">		</span> * */
<span style="white-space:pre">		</span>private String rowNameEg;
<span style="white-space:pre">		</span>/**
<span style="white-space:pre">		</span> * 列宽
<span style="white-space:pre">		</span> * */
<span style="white-space:pre">		</span>private int cellWith = 25 * 256;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot使用POI导出Excel可以分为以下几个步骤: 1. 添加POI依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建Excel文件 使用POI创建Excel文件,可以参考以下代码: ``` Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); Row row = sheet.createRow(); Cell cell = row.createCell(); cell.setCellValue("Hello World"); ``` 3. 导出Excel文件 使用Java IO流将Excel文件导出,可以参考以下代码: ``` response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=test.xlsx"); workbook.write(response.getOutputStream()); ``` 其中,response是HttpServletResponse对象,用于设置响应头信息。 完整的代码示例可以参考以下链接: https://www.cnblogs.com/zhengbin/p/11210297.html ### 回答2: Springboot是一个非常流行的框架,它提供了很多便利的功能来帮助开发人员快速开发应用程序。其中一个功能是使用POI库来导出Excel文件。POI是一个Java库,提供了读写Microsoft Office格式文件的功能,包括Excel和Word。在本文中,我们将学习如何使用Springboot和POI库来导出Excel文件。 首先,我们需要在pom.xml文件中添加POI和其他必要的依赖项。以下是一个简单的pom.xml文件中的依赖项列表: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.0.0</version> </dependency> </dependencies> ``` 接下来,我们需要创建一个Controller类来处理Excel文件的导出请求。以下是一个示例Controller类: ``` @RestController public class ExcelController { @GetMapping("/export-excel") public void exportExcel(HttpServletResponse response) throws IOException { response.setContentType("application/vnd.ms-excel"); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"example.xlsx\""); Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Example Sheet"); Row header = sheet.createRow(0); header.createCell(0).setCellValue("Column 1"); header.createCell(1).setCellValue("Column 2"); Row data = sheet.createRow(1); data.createCell(0).setCellValue("Data 1"); data.createCell(1).setCellValue("Data 2"); workbook.write(response.getOutputStream()); } } ``` 上面的代码中,我们创建了一个GET请求处理程序来导出Excel文件。首先,我们设置响应内容类型为“application/vnd.ms-excel”,该类型用于Excel文件。然后,我们设置响应头,指定文件的名称和类型,以便浏览器可以显示一个下载对话框。 接下来,我们创建一个Workbook对象和一个Sheet对象。在示例中,我们使用XSSFWorkbook和Sheet.createSheet方法创建一个名为“Example Sheet”的工作表。然后,我们创建一个标题行并设置两个列:“Column 1”和“Column 2”文件。接下来,我们创建一个数据行,并使用setCellValue方法在两个单元格中设置值,“Data 1”和“Data 2”。最后,我们将Workbook对象写入response的输出流,从而将Excel文件发送给用户。 总的来说,使用Springboot和POI导出Excel文件非常简单。我们只需要创建一个Controller类来处理请求和Workbook对象并将其写入响应输出流即可。此外,我们可以更改工作表的样式和内容,以便输出所需的格式。 ### 回答3: SpringBoot作为一款非常流行的Java Web框架,提供了非常多的便捷工具和模块,其中就包括了POI库,让我们可以非常方便地进行Excel文件的导入和导出。下面我将介绍如何使用SpringBoot和POI库进行Excel文件的导出。 首先,在pom.xml文件中,我们需要导入POI库的依赖,如下所示: ``` <!-- poi 依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <!-- poi-ooxml 依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 这样我们就可以使用POI库提供的工具类进行Excel文件的读写操作了。 接下来,我们需要编写一个Controller来处理Excel文件的导出请求。在Controller中,我们需要使用POI库创建一个Workbook对象,然后在Workbook中添加数据,最后通过Response将Excel文件输出给客户端。下面是一个示例代码。 ``` @RestController public class ExcelController { @GetMapping("/export") public void exportExcel(HttpServletResponse response) { try { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建表格 Sheet sheet = workbook.createSheet("测试表"); // 添加表头 Row header = sheet.createRow(0); header.createCell(0).setCellValue("姓名"); header.createCell(1).setCellValue("年龄"); // 添加数据 Row row = sheet.createRow(1); row.createCell(0).setCellValue("张三"); row.createCell(1).setCellValue(20); // 输出Excel文件 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=test.xlsx"); workbook.write(response.getOutputStream()); workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们创建了一个名为"测试表"的表格,在表格中添加了表头和数据。最后,我们将Excel文件输出给客户端,通过设置response的ContentType和Content-Disposition实现自动下载。 总之,SpringBoot和POI库提供了非常便捷的Excel文件导出功能,我们可以在Controller中轻松地创建Workbook对象、添加数据并输出给客户端。这样就可以很方便地实现数据报表的导出功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值