一、Excel導出概述
在開發中,有時候需要將數據導出為Excel格式,在Excel中進行查看、分析、匯總等操作。對於Java Web開發者而言,一款好用的Excel導出插件是必不可少的。在SpringBoot中,我們可以使用EasyExcel、POI和JExcel等插件來實現Excel導出。
二、EasyExcel實現Excel導出
EasyExcel是基於POI封裝的一款Java解析和生成Excel的開源項目,具有高效、易用、功能強大的特點。EasyExcel支持大文件讀寫、百萬數據導出、快速生成Excel樣式等功能。下面介紹一下在SpringBoot中如何實現Excel導出。
1、引入EasyExcel依賴
我們可以通過在pom.xml文件中引入如下EasyExcel的maven依賴來使用EasyExcel插件:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6-beta5</version>
</dependency>
2、定義實體類
假設我們要將一個人的信息導出為Excel表格,我們首先需要定義一個實體類:
public class Person {
private String name;
private Integer age;
private String email;
//省略get、set方法
}
3、創建ExcelWriter對象並寫入數據
接下來是最關鍵的一步,即創建ExcelWriter對象並寫入數據。我們可以通過如下代碼來實現:
@RequestMapping("/export")
public void export(HttpServletResponse response) throws IOException {
List list = new ArrayList();
//省略添加數據操作
String fileName = "person.xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
response.setContentType("multipart/form-data");
EasyExcel.write(response.getOutputStream(), Person.class).sheet("sheet").doWrite(list);
}
4、代碼示例
下面是完整的Excel導出代碼示例,其中還包括了生成Excel樣式的操作:
@RequestMapping("/export")
public void export(HttpServletResponse response) throws IOException {
// 數據準備
List list = new ArrayList();
Person person = new Person();
person.setName("張三");
person.setAge(20);
person.setEmail("zhangsan@demo.com");
list.add(person);
// 生成Excel樣式
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
WriteSheet writeSheet = EasyExcel.writerSheet("sheet").build();
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 14);
headWriteCellStyle.setWriteFont(headWriteFont);
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 12);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 寫入數據
excelWriter.write(list, writeSheet, new TableWriteData(), new CellWriteData(headWriteCellStyle,contentWriteCellStyle));
// 輸出Excel
excelWriter.finish();
response.setContentType("multipart/form-data");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("person.xlsx","UTF-8"));
}
三、POI實現Excel導出
POI(Poor Obfuscation Implementation)是由Apache組織提供的用於操作Microsoft Office格式文件的Java API。下面介紹一下在SpringBoot中如何實現Excel導出。
1、引入POI依賴
我們可以通過在pom.xml文件中引入如下POI的maven依賴來使用POI插件:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
2、創建Excel對象
接下來是最關鍵的一步,即創建Excel對象。我們可以通過如下代碼來實現:
Workbook workbook = new XSSFWorkbook();//或者new HSSFWorkbook()
Sheet sheet = workbook.createSheet("sheet");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("姓名");
row.createCell(1).setCellValue("年齡");
row.createCell(2).setCellValue("郵箱");
3、寫入數據並導出
我們可以通過如下代碼將數據寫入Excel中:
int rowIndex = 1;
for (Person person : list) {
Row nRow = sheet.createRow(rowIndex++);
nRow.createCell(0).setCellValue(person.getName());
nRow.createCell(1).setCellValue(person.getAge());
nRow.createCell(2).setCellValue(person.getEmail());
}
接下來是導出的代碼:
String fileName = "person.xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
workbook.write(response.getOutputStream());
workbook.close();
4、代碼示例
下面是完整的Excel導出代碼示例:
@RequestMapping("/export")
public void export(HttpServletResponse response) throws IOException {
List list = new ArrayList();
Person person = new Person();
person.setName("張三");
person.setAge(20);
person.setEmail("zhangsan@demo.com");
list.add(person);
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("姓名");
row.createCell(1).setCellValue("年齡");
row.createCell(2).setCellValue("郵箱");
int rowIndex = 1;
for (Person person : list) {
Row nRow = sheet.createRow(rowIndex++);
nRow.createCell(0).setCellValue(person.getName());
nRow.createCell(1).setCellValue(person.getAge());
nRow.createCell(2).setCellValue(person.getEmail());
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("person.xlsx","UTF-8"));
workbook.write(response.getOutputStream());
workbook.close();
}
四、JExcel實現Excel導出
JExcel是一款功能強大的Excel處理庫,支持Excel 97/2000/XP/2003格式,使用JExcel可以實現單元格樣式設置、單元格合併、單元格格式轉換以及數據寫入等功能。下面介紹一下在SpringBoot中如何實現Excel導出。
1、引入JExcel依賴
我們可以通過在pom.xml文件中引入如下JExcel的maven依賴來使用JExcel插件:
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
2、創建Excel對象
接下來是最關鍵的一步,即創建Excel對象。我們可以通過如下代碼來實現:
WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
Label label1 = new Label(0, 0, "姓名");
Label label2 = new Label(1, 0, "年齡");
Label label3 = new Label(2, 0, "郵箱");
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
3、寫入數據並導出
我們可以通過如下代碼將數據寫入Excel中:
int rowIndex = 1;
for (Person person : list) {
Label labelN1 = new Label(0, rowIndex, person.getName());
Label labelN2 = new Label(1, rowIndex, person.getAge().toString());
Label labelN3 = new Label(2, rowIndex, person.getEmail());
sheet.addCell(labelN1);
sheet.addCell(labelN2);
sheet.addCell(labelN3);
rowIndex++;
}
接下來是導出的代碼:
workbook.write();
workbook.close();
response.flushBuffer();
4、代碼示例
下面是完整的Excel導出代碼示例:
@RequestMapping("/export")
public void export(HttpServletResponse response) throws Exception {
List list = new ArrayList();
Person person = new Person();
person.setName("張三");
person.setAge(20);
person.setEmail("zhangsan@demo.com");
list.add(person);
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("person.xls","UTF-8"));
WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
Label label1 = new Label(0, 0, "姓名");
Label label2 = new Label(1, 0, "年齡");
Label label3 = new Label(2, 0, "郵箱");
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
int rowIndex = 1;
for (Person person : list) {
Label labelN1 = new Label(0, rowIndex, person.getName());
Label labelN2 = new Label(1, rowIndex, person.getAge().toString());
Label labelN3 = new Label(2, rowIndex, person.getEmail());
sheet.addCell(labelN1);
sheet.addCell(labelN2);
sheet.addCell(labelN3);
rowIndex++;
}
workbook.write();
workbook.close();
response.flushBuffer();
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/196886.html
微信掃一掃
支付寶掃一掃