天工造物局 java导出Excel返给前端

java导出Excel返给前端

教你如何实现Java导出Excel返回给前端

一、流程图

准备数据 创建Excel文档 写入数据 导出Excel 返回给前端

二、整体步骤

1. 准备数据
  • 在后端准备需要导出的数据,可以是从数据库查询获取的数据。
2. 创建Excel文档
  • 使用Apache POI库创建一个新的Excel文档对象。
// 创建一个新的Excel文档
Workbook workbook = new XSSFWorkbook();
  • 1.
  • 2.
3. 写入数据
  • 将准备好的数据写入到Excel文档中。
// 创建一个工作表
Sheet sheet = workbook.createSheet("Sheet1");

// 创建一行
Row row = sheet.createRow(0);

// 创建单元格并写入数据
Cell cell = row.createCell(0);
cell.setCellValue("姓名");

// 继续创建单元格并写入其他数据...
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
4. 导出Excel
  • 将创建好的Excel文档导出为文件流。
// 将Excel文档写入输出流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
  • 1.
  • 2.
  • 3.
5. 返回给前端
  • 将Excel文件流返回给前端,可以通过HTTP响应返回。
// 设置响应头
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=example.xlsx");

// 将Excel文件流写入响应体
response.getOutputStream().write(outputStream.toByteArray());
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

三、完整代码示例

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ExcelExporter {

    public void exportData(HttpServletResponse response) throws IOException {
        // 准备数据
        String[] headers = {"姓名", "年龄", "性别"};
        Object[][] data = {{"张三", 20, "男"}, {"李四", 25, "女"}};

        // 创建一个新的Excel文档
        Workbook workbook = new XSSFWorkbook();

        // 创建一个工作表
        Sheet sheet = workbook.createSheet("Sheet1");

        // 写入表头
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 写入数据
        for (int i = 0; i < data.length; i++) {
            Row row = sheet.createRow(i + 1);
            for (int j = 0; j < data[i].length; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(data[i][j].toString());
            }
        }

        // 将Excel文档写入输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=example.xlsx");

        // 将Excel文件流写入响应体
        response.getOutputStream().write(outputStream.toByteArray());
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.

四、总结

通过上述步骤,你可以成功地实现Java导出Excel并返回给前端。记得在每一步都加上必要的异常处理,确保代码的稳定性和可靠性。希朝这篇文章对你有所帮助,如果有任何疑问,欢迎随时向我提问。祝你编程顺利!

原创作者: u_16175460 转载于: https://blog.51cto.com/u_16175460/11308327
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐

  • 浏览量 858
  • 收藏 0
  • 0

所有评论(0)

查看更多评论 
已为社区贡献1条内容