easyexcel设置单元格格式为⽂本
导出时注册registerWriteHandler(new RowWriteHandlerImpl())
/**
* @author jamin
* @date 2020/7/29 15:18
bigdecimal转换为integer*/
public class RowWriteHandlerImpl implements CellWriteHandler {
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer inte ger1, Boolean aBoolean){
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBool ean){
//设置单元格格式为⽂本
Workbook workbook = Sheet().getWorkbook();
CellStyle cellStyle = ateCellStyle();
DataFormat dataFormat = ateDataFormat();
cellStyle.Format("@"));
cell.setCellStyle(cellStyle);
}
//加@Override会报错
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Int eger integer, Boolean aBoolean){
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer int eger, Boolean aBoolean){
}
}
测试代码,针对评论中空格,开始为0的问题
easyexcel版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
import l.EasyExcel;
import l.annotation.ExcelProperty;
import l.metadata.CellData;
import l.metadata.Head;
import l.write.handler.CellWriteHandler;
import adata.holder.WriteSheetHolder;
import adata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jamin
* @date 2021/3/3 11:21
*/
public class Test {
@org.junit.Test
public void test() {
List<Person> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Person person = new Person();
person.w().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); person.setId("0"+i);
person.setName(" ");
list.add(person);
}
EasyExcel.write("d:\\1.xls").sheet(0).registerWriteHandler(new RowWriteHandlerImpl()).doWrite(list); }
class Person {
/**
* names
*/
@ExcelProperty(index = 2)
private String name;
/**
* id
*/
@ExcelProperty(index = 0)
private String id;
/**
* Dates
*/
@ExcelProperty(index = 1)
private String date;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
/
**
* @author jamin
* @date 2020/7/29 15:18
*/
class RowWriteHandlerImpl implements CellWriteHandler {
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integ er1, Boolean aBoolean) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoole an) {
//设置单元格格式为⽂本
Workbook workbook = Sheet().getWorkbook();
CellStyle cellStyle = ateCellStyle();
DataFormat dataFormat = ateDataFormat();
cellStyle.Format("@"));
cell.setCellStyle(cellStyle);
}
//加@Override会报错
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Inte ger integer, Boolean aBoolean) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer inte ger, Boolean aBoolean) {
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论