poi基本使用
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
本地生成Excel
数据类
package com.ytkj.mozq_excel_server.poi;
import lombok.Data;
import java.util.List;
/**
* @date: 2019/10/30 15:00
*/
@Data
public class ExcelData {
private String fileName = "测试表格";
private String sheetName = "表格名称";
private String[] columns;
private List<String[]> data;
}
创建代码
package com.ytkj.mozq_excel_server.poi;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.format.CellFormatType;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @date: 2019/10/30 14:57
*/
public class Test01 {
public static void main(String[] args) throws IOException {
//行和列的起始索引都是0。
/*=========== 创建数据对象 ==========*/
ExcelData excelData = new ExcelData();
excelData.setColumns(new String[]{
"车牌号",
"车辆载重",
"车辆类型"
});
List<String[]> data = new ArrayList<>();
data.add(new String[]{
"陕A0001",
"5.5",
"半挂"
});
excelData.setData(data);
/*=========== 创建工作簿,写入数据 ==========*/
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("测试表");
//创建标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < excelData.getColumns().length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelData.getColumns()[i]);
}
//创建数据行
for (int i = 0; i < excelData.getData().size(); i++) {
HSSFRow aRow = sheet.createRow(i + 1);
String[] rowData = excelData.getData().get(i);
for (int j = 0; j < rowData.length; j++) {
HSSFCell cell = aRow.createCell(j);
cell.setCellValue(rowData[j]);
}
}
/*=========== 将工作簿写入输出流 ==========*/
wb.write(new FileOutputStream("E:\\mozq\\00store\\demo1.xlsx"));
}
/*
Exception in thread "main" java.io.FileNotFoundException: E:\mozq\00store\x1.xlsx (另一个程序正在使用此文件,进程无法访问。)
*/
}
下载Excel
package com.ytkj.mozq_excel_server.controller.car.test;
import com.ytkj.mozq_excel_server.poi.ExcelData;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @date: 2019/10/30 15:44
*/
@RestController
@RequestMapping("/car")
public class POIController {
@RequestMapping("/exportCarExcel")
public void exportCarExcel(HttpServletResponse response){
/*=========== 创建数据对象 ==========*/
ExcelData excelData = getCarExcelData();
/*=========== 创建工作簿,写入数据 ==========*/
HSSFWorkbook wb = getWorkbook(excelData);
//将数据写入响应
try {
wb.write(response.getOutputStream());
// response.setHeader("Content-Disposition", "attachment;filename=" + "测试表下载.xlsx");
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;filename=" + new String("测试表下载.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
} catch (IOException e) {
e.printStackTrace();
}
}
private HSSFWorkbook getWorkbook(ExcelData excelData){
/*=========== 创建工作簿,写入数据 ==========*/
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("测试表");
//创建标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < excelData.getColumns().length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelData.getColumns()[i]);
}
//创建数据行
for (int i = 0; i < excelData.getData().size(); i++) {
HSSFRow aRow = sheet.createRow(i + 1);
String[] rowData = excelData.getData().get(i);
for (int j = 0; j < rowData.length; j++) {
HSSFCell cell = aRow.createCell(j);
cell.setCellValue(rowData[j]);
}
}
return wb;
}
private ExcelData getCarExcelData(){
ExcelData excelData = new ExcelData();
excelData.setColumns(new String[]{
"车牌号",
"车辆载重",
"车辆类型"
});
List<String[]> data = new ArrayList<>();
data.add(new String[]{
"陕A0001",
"5.5",
"半挂"
});
excelData.setData(data);
return excelData;
}
}
Bugs
java.lang.IllegalArgumentException: The workbook already contains a sheet of this name
原因:
重复创建名称相同的Sheet
HSSFWorkbook wb = getWorkbook(excelData);//这里已经创建了Sheet表。
HSSFSheet sheet = wb.createSheet("测试表");//这里又创建同名称的Sheet表。
# 下载文件名称乱码。
_____.xlsx
错误代码:
response.setHeader("Content-Disposition", "attachment;filename=" + "测试表下载.xlsx");
正确代码:
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;filename=" + new String("测试表下载.xlsx".getBytes("UTF-8"), "ISO-8859-1"));