Java使用Apache POI
Apache POI 是创建和维护操作各种符合Office Open XML(OOXML )标准和微软 的OLE 2复合文档 格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel 文件.而且,还可以使用Java读取和创建MS Word 和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)
记录时间:
鹤酒的空间
准备工作
加载pom.xml依赖,这里的业务是要使用excel
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi</ artifactId>
< version> 5.1.0</ version>
</ dependency>
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi-ooxml</ artifactId>
< version> 5.1.0</ version>
</ dependency>
原有代码备份
package com. hejiu45. hejiu45_api_test. controller ;
import com. alibaba. excel. EasyExcel ;
import com. alibaba. excel. ExcelWriter ;
import com. alibaba. excel. support. ExcelTypeEnum ;
import com. alibaba. fastjson2. JSON ;
import com. alibaba. fastjson2. JSONObject ;
import com. hejiu45. hejiu45_api_test. controller. utils. R ;
import com. hejiu45. hejiu45_api_test. ec. kzec. KzEntity ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. data. mongodb. core. MongoTemplate ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. OutputStream ;
import java. util. Collection ;
import java. util. Collections ;
import java. util. List ;
import java. util. function. Supplier ;
@RestController
@Slf4j
@RequestMapping ( "/test" )
public class TestMongoCRUDController {
@Autowired
MongoTemplate mongoTemplate;
private KzEntity kzEntity;
@GetMapping
public void getAllData ( ) throws FileNotFoundException {
List < JSONObject > jsondata = mongoTemplate. findAll ( JSONObject . class , "test" ) ;
List < KzEntity > kzEntities = JSON . parseArray ( JSON . toJSONString ( jsondata) , KzEntity . class ) ;
Object parse = JSON . parse ( JSON . toJSONString ( jsondata) ) ;
KzEntity kzEntity = JSONObject . parseObject ( JSONObject . toJSONString ( jsondata) , KzEntity . class ) ;
String excelFilePath = "E:\\银狐\\大屏综合管理平台\\test.xlsx" ;
EasyExcel . write ( new FileOutputStream ( excelFilePath) ) . sheet ( "sheet1" ) . doWrite ( ( Collection < ? > ) parse) ;
for ( JSONObject jsonObject: jsondata) {
kzEntity = JSON . toJavaObject ( jsonObject, KzEntity . class ) ;
System . out. println ( kzEntity. toString ( ) ) ;
}
EasyExcel . write ( new FileOutputStream ( excelFilePath) ) . sheet ( "sheet1" ) . doWrite ( ( Collection < ? > ) kzEntity) ;
System . out. println ( kzEntities. toString ( ) ) ;
}
}
基本固定逻辑
以下代码是写的操作
package com. hejiu45. hejiu45_api_test. controller ;
import com. alibaba. excel. EasyExcel ;
import com. alibaba. excel. ExcelWriter ;
import com. alibaba. excel. support. ExcelTypeEnum ;
import com. alibaba. fastjson2. JSON ;
import com. alibaba. fastjson2. JSONObject ;
import com. hejiu45. hejiu45_api_test. controller. utils. R ;
import com. hejiu45. hejiu45_api_test. ec. kzec. KzEntity ;
import lombok. extern. slf4j. Slf4j ;
import org. apache. poi. hssf. usermodel. HSSFWorkbook ;
import org. apache. poi. ss. usermodel. Cell ;
import org. apache. poi. ss. usermodel. Row ;
import org. apache. poi. ss. usermodel. Sheet ;
import org. apache. poi. ss. usermodel. Workbook ;
import org. apache. poi. xssf. usermodel. XSSFWorkbook ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. data. mongodb. core. MongoTemplate ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. io. OutputStream ;
import java. util. Collection ;
import java. util. Collections ;
import java. util. List ;
import java. util. function. Supplier ;
@RestController
@Slf4j
@RequestMapping ( "/test" )
public class TestMongoCRUDController {
Workbook workbo = new HSSFWorkbook ( ) ;
Workbook workboa = new XSSFWorkbook ( ) ;
Sheet sheet1 = workbo. createSheet ( "sheet1" ) ;
Sheet sheet2 = workboa. createSheet ( "sheet1" ) ;
@GetMapping ( "03" )
public void getAllData ( ) throws IOException {
Row row = sheet1. createRow ( 0 ) ;
Cell cell = row. createCell ( 0 ) ;
cell. setCellValue ( "疆内" ) ;
FileOutputStream fileOutputStream = new FileOutputStream ( "E:\\银狐\\大屏综合管理平台\\test.xls" ) ;
workbo. write ( fileOutputStream) ;
fileOutputStream. close ( ) ;
workbo. close ( ) ;
System . out. println ( "文件生成成功!" ) ;
}
@GetMapping ( "07" )
public void getAllDataB ( ) throws IOException {
Row row = sheet2. createRow ( 0 ) ;
Cell cell = row. createCell ( 0 ) ;
cell. setCellValue ( "疆内" ) ;
FileOutputStream fileOutputStream = new FileOutputStream ( "E:\\银狐\\大屏综合管理平台\\test.xlsx" ) ;
workboa. write ( fileOutputStream) ;
fileOutputStream. close ( ) ;
workboa. close ( ) ;
System . out. println ( "文件生成成功!" ) ;
}
}
以下代码是读的操作:
下期在见
鹤酒前端会持续更新,精彩内容。