javaexcel导出方法如何写
时间: 2024-01-26 11:03:19 浏览: 28
Java中实现Excel导出主要是通过POI类库来实现的,具体步骤如下:
1. 创建工作簿和工作表
```java
Workbook workbook = new HSSFWorkbook(); // 创建工作簿
Sheet sheet = workbook.createSheet("Sheet1"); // 创建工作表
```
2. 创建表头,设置样式
```java
Row row = sheet.createRow(0); // 创建表头行
Cell cell = row.createCell(0); // 创建表头单元格
cell.setCellValue("姓名"); // 设置单元格值
CellStyle style = workbook.createCellStyle(); // 创建单元格样式
Font font = workbook.createFont(); // 创建字体
font.setBold(true); // 设置字体加粗
style.setFont(font); // 将字体应用到样式
cell.setCellStyle(style); // 将样式应用到单元格
```
3. 填充数据
```java
List<Student> students = getStudents(); // 获取学生列表
for (int i = 0; i < students.size(); i++) {
Row row = sheet.createRow(i + 1); // 创建数据行
Student student = students.get(i);
Cell cell1 = row.createCell(0); // 创建单元格并填充数据
cell1.setCellValue(student.getName());
Cell cell2 = row.createCell(1);
cell2.setCellValue(student.getAge());
Cell cell3 = row.createCell(2);
cell3.setCellValue(student.getGender());
}
```
4. 导出Excel文件
```java
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=students.xls");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
```
完整的代码如下:
```java
public void exportExcel(HttpServletResponse response) throws IOException {
Workbook workbook = new HSSFWorkbook(); // 创建工作簿
Sheet sheet = workbook.createSheet("Sheet1"); // 创建工作表
// 创建表头,设置样式
Row row = sheet.createRow(0); // 创建表头行
Cell cell = row.createCell(0); // 创建表头单元格
cell.setCellValue("姓名"); // 设置单元格值
CellStyle style = workbook.createCellStyle(); // 创建单元格样式
Font font = workbook.createFont(); // 创建字体
font.setBold(true); // 设置字体加粗
style.setFont(font); // 将字体应用到样式
cell.setCellStyle(style); // 将样式应用到单元格
cell = row.createCell(1); // 创建表头单元格并设置样式
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("性别");
cell.setCellStyle(style);
// 填充数据
List<Student> students = getStudents(); // 获取学生列表
for (int i = 0; i < students.size(); i++) {
row = sheet.createRow(i + 1); // 创建数据行
Student student = students.get(i);
Cell cell1 = row.createCell(0); // 创建单元格并填充数据
cell1.setCellValue(student.getName());
Cell cell2 = row.createCell(1);
cell2.setCellValue(student.getAge());
Cell cell3 = row.createCell(2);
cell3.setCellValue(student.getGender());
}
// 导出Excel文件
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=students.xls");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)