Java实现Excel的下载和导入
Excel是广泛使用的电子表格软件,常用于数据存储和分析。在Java中,我们可以使用Apache POI库来处理Excel文件。本文将介绍如何使用Java实现Excel的下载和导入功能,并提供相应的代码示例。
1. Excel下载
要实现Excel的下载,我们首先需要创建一个Excel文件,并将其保存到指定的路径。然后,我们需要将Excel文件作为响应返回给前端。
创建Excel文件
使用Apache POI库创建Excel文件非常简单。下面的代码示例演示了如何创建一个包含标题和数据的Excel文件:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelDownloadExample {
public static void main(String[] args) {
// 创建工作簿
Workbook workbook = new XSSFWorkbook();
// 创建工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 创建标题行
Row headerRow = sheet.createRow(0);
Cell headerCell1 = headerRow.createCell(0);
headerCell1.setCellValue("Name");
Cell headerCell2 = headerRow.createCell(1);
headerCell2.setCellValue("Age");
// 创建数据行
Row dataRow = sheet.createRow(1);
Cell dataCell1 = dataRow.createCell(0);
dataCell1.setCellValue("John");
Cell dataCell2 = dataRow.createCell(1);
dataCell2.setCellValue(25);
// 将Excel文件保存到指定路径
try (FileOutputStream outputStream = new FileOutputStream("path/to/excel.xlsx")) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在上述代码中,我们使用XSSFWorkbook
类创建了一个工作簿,然后使用createSheet
方法创建了一个名为"Sheet1"的工作表。接下来,我们创建了标题行和数据行,并为它们设置相应的值。最后,我们使用FileOutputStream
将Excel文件保存到指定路径。
下载Excel文件
要将Excel文件作为响应返回给前端,我们需要使用Spring MVC或其他框架。下面的代码示例演示了如何使用Spring MVC实现Excel文件的下载:
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.File;
@Controller
@RequestMapping("/excel")
public class ExcelDownloadController {
@GetMapping("/download")
public ResponseEntity<FileSystemResource> downloadExcel() {
String filePath = "path/to/excel.xlsx";
File file = new File(filePath);
FileSystemResource resource = new FileSystemResource(file);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "excel.xlsx");
return ResponseEntity.ok()
.headers(headers)
.body(resource);
}
}
在上述代码中,我们使用@Controller
和@RequestMapping
注解创建了一个控制器,并定义了一个用于下载Excel文件的GET请求处理方法。该方法将Excel文件作为FileSystemResource
对象返回,并设置了相应的HTTP头信息,包括内容类型和附件名称。
2. Excel导入
要实现Excel的导入,我们首先需要读取Excel文件中的数据。然后,我们可以将读取到的数据用于后续的业务逻辑处理。
读取Excel文件
使用Apache POI库读取Excel文件也非常简单。下面的代码示例演示了如何读取Excel文件中的数据:
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ExcelImportExample {
public static void main(String[] args) {
String filePath = "path/to/excel.xlsx";
try (Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(filePath)))) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
Cell cell1 = row.getCell(0);
String name = cell1.getStringCellValue();
Cell cell2 = row.getCell(1);
int age = (int) cell2.getNumericCellValue();
System.out.println("Name: " + name + ", Age: " + age);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
在上述代码中,我们使用WorkbookFactory
类的create
方法打开Excel文件,并获取第一个工作表。然后,我们遍历工作