设置文本单元格的内容格式

This commit is contained in:
ThomasChant 2020-08-23 20:58:39 +08:00
parent 9f21f8816a
commit 97ac94b140
1 changed files with 16 additions and 19 deletions

View File

@ -19,23 +19,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
@ -462,12 +446,21 @@ public class ExcelUtil<T>
*/
public Cell createCell(Excel attr, Row row, int column)
{
// 创建列
// 创建列
Cell cell = row.createCell(column);
// 写入列信息
cell.setCellValue(attr.name());
setDataValidation(attr, row, column);
cell.setCellStyle(styles.get("header"));
// 对于文本列设置内容格式为文本格式
if (ColumnType.STRING == attr.cellType())
{
CellStyle dataColStyle = wb.createCellStyle();
DataFormat wbDataFormat = wb.createDataFormat();
dataColStyle.setDataFormat(wbDataFormat.getFormat("@"));
sheet.setDefaultColumnStyle(column, dataColStyle);
}
return cell;
}
@ -482,6 +475,9 @@ public class ExcelUtil<T>
{
if (ColumnType.STRING == attr.cellType())
{
CellStyle cellStyle = cell.getCellStyle();
DataFormat wbDataFormat = wb.createDataFormat();
cellStyle.setDataFormat(wbDataFormat.getFormat("@"));
cell.setCellType(CellType.STRING);
cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
}
@ -492,6 +488,7 @@ public class ExcelUtil<T>
}
}
/**
* 创建表格样式
*/