[alibaba/easyexcel]如何设置单元格为文本格式
回答
cell.setCellType(CellType.STRING); CellStyle cellStyle = cell.getCellStyle(); cellStyle.setDataFormat(49);
这两种设置都不生效
你加个numberformat注解 或者入参就是文本就可以
numberformat注解 或者入参就是文本, 能保证输出正确。但是导出单元格格式还是“General”,当再次编辑的时候最靠前的“0”会丢失,所以想知道怎么在导出的时候这个单元格设置成“Text”?
cell.setCellType(CellType.STRING); CellStyle cellStyle = cell.getCellStyle(); cellStyle.setDataFormat(49);
这两种设置都不生效
numberformat注解 或者入参就是文本, 能保证输出正确。但是导出单元格格式还是“General”,当再次编辑的时候最靠前的“0”会丢失,所以想知道怎么在导出的时候这个单元格设置成“Text”?
cell.setCellType(CellType.STRING); CellStyle cellStyle = cell.getCellStyle(); cellStyle.setDataFormat(49);
这两种设置都不生效
需要针对这个单元格单独设置样式 //设置文本样式 final CellStyle textStyle = excelWriter.writeContext().writeWorkbookHolder().getWorkbook().createCellStyle(); DataFormat format = excelWriter.writeContext().writeWorkbookHolder().getWorkbook().createDataFormat(); excelWriter.textStyle.setDataFormat(format.getFormat("@"));
registerWriteHandler(new CellWriteHandler() {
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
cell.setCellStyle(textStyle);
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List
numberformat注解 或者入参就是文本, 能保证输出正确。但是导出单元格格式还是“General”,当再次编辑的时候最靠前的“0”会丢失,所以想知道怎么在导出的时候这个单元格设置成“Text”? cell.setCellType(CellType.STRING); CellStyle cellStyle = cell.getCellStyle(); cellStyle.setDataFormat(49); 这两种设置都不生效
需要针对这个单元格单独设置样式 //设置文本样式 final CellStyle textStyle = excelWriter.writeContext().writeWorkbookHolder().getWorkbook().createCellStyle(); DataFormat format = excelWriter.writeContext().writeWorkbookHolder().getWorkbook().createDataFormat(); excelWriter.textStyle.setDataFormat(format.getFormat("@"));
registerWriteHandler(new CellWriteHandler() { @override public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) { } @override public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { cell.setCellStyle(textStyle); } @override public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { } })
excelWriter 这个是哪里来的
我也遇到一样的问题,数字在前面加0,加不上,动态导出的,也没办法加注解
@figodeng 楼主有解决吗?我也遇到了一样的问题