下面我将为您提供关于“SSM框架使用poi导入导出Excel的详细方法”的完整攻略:
一、依赖导入
首先,在Maven中添加对poi、poi-ooxml和poi-ooxml-schemas等依赖的导入。具体代码如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
二、读取Excel内容
假设有一个学生信息Excel表,我们需要读取这个Excel表格中的内容,并将数据导入到数据库中。首先定义一个学生信息模型:
public class Student {
private String name;
private String sex;
private int age;
// 省略get和set方法
}
然后,定义一个工具类ExcelUtil,用于读取Excel文件,并将文件中的数据转换为Java对象:
public class ExcelUtil {
public static List<Student> readExcel(String filePath) throws IOException {
FileInputStream fis = null;
Workbook workbook = null;
try {
fis = new FileInputStream(filePath);
workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
List<Student> studentList = new ArrayList<>();
for (Row row : sheet) {
if (row.getRowNum() == 0) {
continue;
}
Student student = new Student();
student.setName(row.getCell(0).getStringCellValue());
student.setSex(row.getCell(1).getStringCellValue());
student.setAge((int) row.getCell(2).getNumericCellValue());
studentList.add(student);
}
return studentList;
} finally {
if (workbook != null) {
workbook.close();
}
if (fis != null) {
fis.close();
}
}
}
}
调用ExcelUtil.readExcel(filePath)方法即可读取Excel文件并返回学生信息列表。
三、导出Excel
下面以学生信息为例,演示如何将学生信息导出为Excel文件。同样的,先定义一个工具类ExcelUtil用于处理Excel文件。在ExcelUtil中,我们实现了将学生信息导出为Excel表格的方法writeExcel()。
public class ExcelUtil {
public static void writeExcel(List<Student> studentList, String filePath) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row rowHeader = sheet.createRow(0);
rowHeader.createCell(0).setCellValue("姓名");
rowHeader.createCell(1).setCellValue("性别");
rowHeader.createCell(2).setCellValue("年龄");
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
Row row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(student.getName());
row.createCell(1).setCellValue(student.getSex());
row.createCell(2).setCellValue(student.getAge());
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
workbook.write(fos);
} finally {
if (workbook != null) {
workbook.close();
}
if (fos != null) {
fos.close();
}
}
}
}
调用ExcelUtil.writeExcel(studentList, filePath)方法即可将studentList中的信息导出为Excel文件,并保存到filePath中。
四、示例
示例1:将Excel表格中的学生信息导入到数据库中
假设我们已经通过ExcelUtil.readExcel(filePath)方法读取了学生信息Excel表格,并将其保存为List
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public void importStudent(List<Student> studentList) {
studentMapper.batchInsert(studentList);
}
}
示例2:将学生信息导出为Excel文件
@Controller
public class ExportController {
@Autowired
private StudentService studentService;
@RequestMapping("/export")
public void export(HttpServletResponse response) throws IOException {
List<Student> studentList = studentService.getStudentList();
ExcelUtil.writeExcel(studentList, "student.xlsx");
File file = new File("student.xlsx");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(file);
os = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} finally {
if (fis != null) {
fis.close();
}
if (os != null) {
os.close();
}
}
}
}
在这个示例中,我们首先通过studentService.getStudentList()方法获取学生信息列表,然后调用ExcelUtil.writeExcel(studentList, "student.xlsx")方法将列表导出为Excel文件,并将文件输出到response的输出流中,这样就实现了导出Excel文件的功能。
希望以上内容能对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SSM框架使用poi导入导出Excel的详细方法 - Python技术站