EasyPoi介紹:
EasyPoi是一個(gè)功能強(qiáng)大且易于使用的Java Excel操作框架,其主要特點(diǎn)如下:
-
簡(jiǎn)單易用:EasyPoi提供簡(jiǎn)潔而直觀的API,使Java開發(fā)人員能夠輕松地進(jìn)行Excel導(dǎo)入導(dǎo)出操作,無(wú)需繁瑣的代碼和復(fù)雜的配置。
-
支持多種數(shù)據(jù)源:EasyPoi支持從數(shù)據(jù)庫(kù)、List集合、Map等各種數(shù)據(jù)源快速生成Excel文件,并且可以將Excel文件中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)或其他數(shù)據(jù)源中。
-
強(qiáng)大的導(dǎo)入導(dǎo)出功能:EasyPoi提供了豐富的導(dǎo)入導(dǎo)出功能,包括導(dǎo)出Excel文件、設(shè)置表頭樣式、數(shù)據(jù)格式化、合并單元格、設(shè)置列寬、設(shè)置公式等。同時(shí),還支持導(dǎo)入Excel文件并自動(dòng)映射到Java對(duì)象中,大大簡(jiǎn)化了數(shù)據(jù)導(dǎo)入的過(guò)程。
-
支持多種Excel格式:EasyPoi支持導(dǎo)入導(dǎo)出多種常見的Excel格式,包括xls、xlsx等,同時(shí)還支持導(dǎo)出csv、pdf等其他格式,滿足不同場(chǎng)景下的需求。
-
高性能:EasyPoi通過(guò)優(yōu)化底層算法和IO處理,提供了出色的性能表現(xiàn),在海量數(shù)據(jù)的導(dǎo)入導(dǎo)出過(guò)程中能夠保持較高的效率。
-
可擴(kuò)展性強(qiáng):EasyPoi支持用戶自定義樣式和格式,可以根據(jù)具體需求進(jìn)行擴(kuò)展和定制,滿足各種復(fù)雜的導(dǎo)入導(dǎo)出場(chǎng)景。
-
運(yùn)行穩(wěn)定可靠:EasyPoi已在許多項(xiàng)目中得到廣泛應(yīng)用并驗(yàn)證了其穩(wěn)定性和可靠性,可以放心使用。
使用:
導(dǎo)出excel
1.導(dǎo)包
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
2.pojo加注解
pojo字段上的注解name屬性對(duì)應(yīng)excel表頭文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-521679.html
public class Student implements Serializable {
@Excel(name = "學(xué)生姓名")
private String name;
@Excel(name = "入學(xué)時(shí)間",exportFormat = "yyyy-MM-dd HH:mm")
private Date createTime;
}
3.controller
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@SneakyThrows //拋出異常,不建議使用
@GetMapping
public void exportData(HttpServletResponse response){
//1.查詢數(shù)據(jù)
List<Student> datas = studentMapper.selectList();
//2.封裝成表格
//參數(shù)1:表格標(biāo)題,參數(shù)2:sheet名稱
ExportParams exportParams = new ExportParams("學(xué)生信息", "1班學(xué)生信息");
//參數(shù)1:表格參數(shù) 參數(shù)2:實(shí)體類 參數(shù)3:數(shù)據(jù)
Workbook sheets = ExcelExportUtil.exportExcel(exportParams, Student.class, datas);
//3.返回表格
//設(shè)置表格文件名字
String fileName = "一班學(xué)生數(shù)據(jù)";
fileName = URLEncoder.encode(fileName,"UTF8");
//設(shè)置返回?cái)?shù)據(jù)類型
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
//將表格輸出
sheets.write(response.getOutputStream());
}
}
注意事項(xiàng):前端的請(qǐng)求方式不能是Ajax異步請(qǐng)求,只能使用get方式文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-521679.html
導(dǎo)入excel
1.導(dǎo)包(同上)
2.pojo加注解(同上)
3.controller
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@SneakyThrows
@PostMapping
public void importData(MultipartFile file){
//設(shè)置導(dǎo)入?yún)?shù)
ImportParams importParams = new ImportParams();
importParams.setTitleRows(1); //標(biāo)題占1行,默認(rèn)0
importParams.setHeadRows(1); //表頭占1行,默認(rèn)1
//excel轉(zhuǎn)POJO
List<Student> studentList = ExcelImportUtil.importExcel(file.getInputStream(),
Student.class, importParams);
//添加到數(shù)據(jù)庫(kù)
Iterator<Student> iterator = studentList.iterator();
while(iterator.hasNext()){
Student studnet = iterator.next();
studentMapper.insert(studnet);
}
System.out.println(studentList);
}
}
到了這里,關(guān)于EasyPio導(dǎo)入導(dǎo)出excel表格的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!