下面是“Java web将数据导出为Excel格式文件代码片段”的完整攻略:
1.准备工作
在进行Java web数据导出Excel操作前,首先需要依赖以下jar包:
- poi-ooxml.jar
- poi.jar
- xmlbeans.jar
- poi-ooxml-schemas.jar
- commons-collections4-4.4.jar
以上jar包可在以下maven仓库中获取:
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
2.核心代码示例
实现Java web中将数据导出为Excel格式文件的代码如下:
@Controller
public class ExportExcelController {
@GetMapping("/export")
public void export(HttpServletResponse response) throws IOException {
// 1.定义表头
String[] titles = {"姓名", "性别", "年龄"};
// 2.获取数据
List<User> users = new ArrayList<>();
users.add(new User("张三", "男", 20));
users.add(new User("李四", "女", 22));
users.add(new User("王五", "男", 25));
// 3.定义文件名
String fileName = "用户信息表" + System.currentTimeMillis() + ".xlsx";
// 4.创建工作簿对象
XSSFWorkbook workbook = new XSSFWorkbook();
// 5.创建工作表对象
XSSFSheet sheet = workbook.createSheet("用户信息表");
// 6.创建行
XSSFRow row = sheet.createRow(0);
// 7.创建单元格,并填充表头数据
for (int i = 0; i < titles.length; i++) {
XSSFCell cell = row.createCell(i);
cell.setCellValue(titles[i]);
}
// 8.循环写入数据
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
XSSFRow rowData = sheet.createRow(i + 1);
rowData.createCell(0).setCellValue(user.getName());
rowData.createCell(1).setCellValue(user.getSex());
rowData.createCell(2).setCellValue(user.getAge());
}
// 9.设置文件头
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 10.设置响应ContentType和字符编码
response.setContentType("application/octet-stream;charset=utf-8");
// 11.获取输出流
OutputStream os = response.getOutputStream();
// 12.输出文件
workbook.write(os);
os.flush();
os.close();
}
}
上述代码实现了将一个用户列表导出为Excel文件的功能。其中,变量名和函数名均添加了注释,方便阅读和理解。同时也支持局部语言化处理。
3.示例说明
下面将分别介绍两个导出不同数据的示例:
简单字符串数据
@RestController
@RequestMapping("/excel")
public class ExportExcelController {
@GetMapping("/simple")
public void exportSimple(HttpServletResponse response) throws IOException {
List<String> dataList = Arrays.asList("Java", "Python", "Go", "C++", "Scala", "Swift");
// 2.定义文件名
String fileName = "simpleExcel" + System.currentTimeMillis() + ".xlsx";
// 3.创建工作簿对象
XSSFWorkbook workbook = new XSSFWorkbook();
// 4.创建工作表对象
XSSFSheet sheet = workbook.createSheet("simpleExcel");
// 5.创建行
XSSFRow row = sheet.createRow(0);
// 6.创建单元格,并填充表头数据
XSSFCell cell = row.createCell(0);
cell.setCellValue("编程语言");
// 7.循环写入数据
for (int i = 0; i < dataList.size(); i++) {
XSSFRow rowData = sheet.createRow(i + 1);
rowData.createCell(0).setCellValue(dataList.get(i));
}
// 8.设置文件头
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 9.设置响应ContentType和字符编码
response.setContentType("application/octet-stream;charset=utf-8");
// 10.获取输出流
OutputStream os = response.getOutputStream();
// 11.输出文件
workbook.write(os);
os.flush();
os.close();
}
}
上述代码中数据为简单的字符串数据,直接循环暴力输出即可。
复杂对象数据
@RestController
@RequestMapping("/excel")
public class ExportExcelController {
@GetMapping("/complex")
public void exportComplex(HttpServletResponse response) throws IOException {
// 定义表头
String[] headers = {"序号", "姓名", "年龄", "性别", "教育背景"};
// 获取数据
List<Person> list = new ArrayList<>();
list.add(new Person(1, "张三", 23, "男", "硕士"));
list.add(new Person(2, "李四", 25, "女", "博士"));
list.add(new Person(3, "王五", 27, "男", "本科"));
list.add(new Person(4, "赵六", 20, "女", "专科"));
list.add(new Person(5, "钱七", 31, "男", "其他"));
// 定义文件名
String fileName = "personInfo" + System.currentTimeMillis() + ".xlsx";
// 创建工作簿对象
XSSFWorkbook workbook = new XSSFWorkbook();
// 创建工作表对象
XSSFSheet sheet = workbook.createSheet("personInfo");
// 创建表头行
XSSFRow headerRow = sheet.createRow(0);
// 循环创建表头列
for (int i = 0; i < headers.length; i++) {
XSSFCell headerCell = headerRow.createCell(i);
headerCell.setCellValue(headers[i]);
}
// 循环写入数据
for (int i = 0; i < list.size(); i++) {
XSSFRow row = sheet.createRow(i + 1);
Person person = list.get(i);
row.createCell(0).setCellValue(person.getId());
row.createCell(1).setCellValue(person.getName());
row.createCell(2).setCellValue(person.getAge());
row.createCell(3).setCellValue(person.getGender());
row.createCell(4).setCellValue(person.getEducation());
}
// 设置文件头
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 设置响应ContentType和字符编码
response.setContentType("application/octet-stream;charset=utf-8");
// 获取输出流
OutputStream os = response.getOutputStream();
// 输出文件
workbook.write(os);
os.flush();
os.close();
}
}
上述代码中数据为复杂类型的对象数据,需要根据对象中的属性进行单元格赋值。此时我们需要循环外层对象和内层对象来完成Excel的表格填写。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java web将数据导出为Excel格式文件代码片段 - Python技术站