这是生成exl文件的代码。每次我将记录追加到exl文件时。我再次设置字体的setter。对于exl文件中的第一次输入,样式正在应用。但是当我将下一个条目添加到exl文件时,此时间样式不适用。
public class ExcelOutputWriter implements OutputWriter {
private List<String> headerList;
private List<List<String>> rowsList;
private List<BGVInformationVO> allBGVInformationVOList = new ArrayList<>();
private XSSFWorkbook workbook;
private XSSFSheet sheet;
private XSSFCellStyle headerCellStyle;
private XSSFCellStyle fontGreenStyle;
private XSSFCellStyle fontRedStyle;
private XSSFCellStyle fontOrangeStyle;
private XSSFCellStyle fontYellowStyle;
private XSSFCellStyle dateCellStyle;
private XSSFCellStyle dtlCellStyle;
private int rowCount;
Logger logger = BGVerificationLogger.getLogger(ExcelOutputWriter.class);
@Override
public void setHeaderList(List<String> headerList) {
this.headerList = headerList;
}
public List<List<String>> getRowsList() {
return rowsList;
}
public void setRowsList(List<List<String>> rowsList) {
this.rowsList = rowsList;
}
public XSSFWorkbook getWorkbook() {
return workbook;
}
public void setWorkbook(XSSFWorkbook workbook) {
this.workbook = workbook;
}
public XSSFSheet getSheet() {
return sheet;
}
public void setSheet(XSSFSheet sheet) {
this.sheet = sheet;
}
public XSSFCellStyle getHeaderCellStyle() {
return headerCellStyle;
}
public void setHeaderCellStyle(XSSFCellStyle headerCellStyle) {
this.headerCellStyle = headerCellStyle;
}
public XSSFCellStyle getFontGreenStyle() {
return fontGreenStyle;
}
public void setFontGreenStyle(XSSFCellStyle fontGreenStyle) {
this.fontGreenStyle = fontGreenStyle;
}
public XSSFCellStyle getFontRedStyle() {
return fontRedStyle;
}
public void setFontRedStyle(XSSFCellStyle fontRedStyle) {
this.fontRedStyle = fontRedStyle;
}
public XSSFCellStyle getFontYellowStyle() {
return fontYellowStyle;
}
public void setFontYellowStyle(XSSFCellStyle fontYellowStyle) {
this.fontYellowStyle = fontYellowStyle;
}
public XSSFCellStyle getFontOrangeStyle() {
return fontOrangeStyle;
}
public void setFontOrangeStyle(XSSFCellStyle fontOrangeStyle) {
this.fontOrangeStyle = fontOrangeStyle;
}
public XSSFCellStyle getDateCellStyle() {
return dateCellStyle;
}
public void setDateCellStyle(XSSFCellStyle dateCellStyle) {
this.dateCellStyle = dateCellStyle;
}
public XSSFCellStyle getDtlCellStyle() {
return dtlCellStyle;
}
public void setDtlCellStyle(XSSFCellStyle dtlCellStyle) {
this.dtlCellStyle = dtlCellStyle;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
@Override
public void addBGVInformationVO(BGVInformationVO bgvInformationVO) {
allBGVInformationVOList.add(bgvInformationVO);
}
@Override
public void writeToOutputFile(String fullFileName) throws IOException {
logger.info("Start writing to Output Excel file: " + fullFileName);
File outputExcelFile = new File(fullFileName);
if (outputExcelFile.exists()) {
writeToExistingWorkBook(fullFileName);
writeDetailRecord();
} else {
createNewExcelWorkBook();
writeHeaderRecord();
writeDetailRecord();
}
try (FileOutputStream outputStream = new FileOutputStream(fullFileName))
{
workbook.write(outputStream);
outputStream.close();
}
logger.info("Output Excel writing Complete");
}
private void createNewExcelWorkBook() throws IOException {
createWorkBook();
createSheet(BGVerificationConstant.BGV_SHEET_NAME);
createHeaderCellStyle();
createGreenFontStyle();
createRedFontStyle();
createYellowFontStyle();
createOrangeFontStyle();
createDateCellStyle();
createDtlCellStyle();
}
private void writeToExistingWorkBook(String fullFileName) throws IOException
{
createWorkBook(new FileInputStream(fullFileName));
getSheet(BGVerificationConstant.BGV_SHEET_NAME);
//createHeaderCellStyle();
createGreenFontStyle();
createRedFontStyle();
createYellowFontStyle();
createOrangeFontStyle();
createDateCellStyle();
createDtlCellStyle();
setRowCount(this.sheet.getLastRowNum());
}
private void writeHeaderRecord() throws IOException {
int rowNumCounter = -1;
int columnCount = -1;
XSSFRow row = getSheet().createRow(++rowNumCounter);
XSSFCell cell = row.createCell(++columnCount);
cell.setCellStyle(headerCellStyle);
cell.setCellValue("Extracted BGV Report ");
getSheet().addMergedRegion(new CellRangeAddress(0, 0, 0, 16));
columnCount = -1;
row = getSheet().createRow(++rowNumCounter);
for (String headerName : headerList) {
cell = row.createCell(++columnCount);
cell.setCellStyle(headerCellStyle);
cell.setCellValue(headerName);
}
setRowCount(rowNumCounter);
}
private void writeDetailRecord() throws IOException {
try {
for (BGVInformationVO bgvInformationVO : allBGVInformationVOList) {
XSSFRow row = getSheet().createRow(++rowCount);
row.setHeight((short) 900);
int columnCount = -1;
XSSFCell cell = row.createCell(++columnCount);
cell.setCellStyle(dtlCellStyle);
cell.setCellValue(bgvInformationVO.getVendor());
cell = row.createCell(++columnCount);
cell.setCellStyle(dtlCellStyle);
cell.setCellValue(bgvInformationVO.getCandidateName());
cell = row.createCell(++columnCount);
cell.setCellStyle(dtlCellStyle);
cell.setCellValue(bgvInformationVO.getDesignation());
cell = row.createCell(++columnCount);
cell.setCellStyle(dateCellStyle);
cell.setCellValue(bgvInformationVO.getDateOfInitiation());
cell = row.createCell(++columnCount);
cell.setCellStyle(dateCellStyle);
cell.setCellValue(bgvInformationVO.getDateOfReport());
cell = row.createCell(++columnCount);
cell.setCellStyle(dateCellStyle);
cell.setCellValue(bgvInformationVO.getDateOfBirth());
cell = row.createCell(++columnCount);
cell.setCellStyle(dtlCellStyle);
cell.setCellValue(bgvInformationVO.getCaseRefNumber());
cell = row.createCell(++columnCount);
if ("green".equalsIgnoreCase(bgvInformationVO.getColorCode())) {
cell.setCellValue(bgvInformationVO.getColorCode());
cell.setCellStyle(fontGreenStyle);
} else if
("orange".equalsIgnoreCase(bgvInformationVO.getColorCode())) {
cell.setCellValue(bgvInformationVO.getColorCode());
cell.setCellStyle(fontOrangeStyle);
}
else if
("yellow".equalsIgnoreCase(bgvInformationVO.getColorCode())) {
cell.setCellValue(bgvInformationVO.getColorCode());
cell.setCellStyle(fontYellowStyle);
}
else {
cell.setCellValue(bgvInformationVO.getColorCode());
cell.setCellStyle(fontRedStyle);
}
cell = row.createCell(++columnCount);
if
(StringUtils.containsIgnoreCase(bgvInformationVO.getEducation(),
BGVerificationConstant.NOTCLEAR)
||
StringUtils.containsIgnoreCase(bgvInformationVO.getEducation(),
BGVerificationConstant.UNABLETOCOMPLETE)) {
cell.setCellValue(bgvInformationVO.getEducation());
cell.setCellStyle(fontRedStyle);
} else {
cell.setCellValue(bgvInformationVO.getEducation());
cell.setCellStyle(fontGreenStyle);
}
cell = row.createCell(++columnCount);
if
(StringUtils.containsIgnoreCase(bgvInformationVO.getAddress(),
BGVerificationConstant.NOTCLEAR)
||
StringUtils.containsIgnoreCase(bgvInformationVO.getAddress(),
BGVerificationConstant.UNABLETOCOMPLETE)) {
cell.setCellValue(bgvInformationVO.getAddress());
cell.setCellStyle(fontRedStyle);
} else {
cell.setCellValue(bgvInformationVO.getAddress());
cell.setCellStyle(fontGreenStyle);
}
cell = row.createCell(++columnCount);
if
(StringUtils.containsIgnoreCase(bgvInformationVO.getPoliceCase(),
BGVerificationConstant.NOTCLEAR)
||
StringUtils.containsIgnoreCase(bgvInformationVO.getPoliceCase(),
BGVerificationConstant.UNABLETOCOMPLETE)) {
cell.setCellValue(bgvInformationVO.getPoliceCase());
cell.setCellStyle(fontRedStyle);
} else {
cell.setCellValue(bgvInformationVO.getPoliceCase());
cell.setCellStyle(fontGreenStyle);
}
cell = row.createCell(++columnCount);
if
(StringUtils.containsIgnoreCase(bgvInformationVO.getEmployment(),
BGVerificationConstant.NOTCLEAR)
||
StringUtils.containsIgnoreCase(bgvInformationVO.getEmployment(),
BGVerificationConstant.UNABLETOCOMPLETE)) {
cell.setCellValue(bgvInformationVO.getEmployment());
cell.setCellStyle(fontRedStyle);
} else {
cell.setCellValue(bgvInformationVO.getEmployment());
cell.setCellStyle(fontGreenStyle);
}
cell = row.createCell(++columnCount);
if
(StringUtils.containsIgnoreCase(bgvInformationVO.getReference(),
BGVerificationConstant.NOTCLEAR)
||
StringUtils.containsIgnoreCase(bgvInformationVO.getReference(),
BGVerificationConstant.UNABLETOCOMPLETE)) {
cell.setCellValue(bgvInformationVO.getReference());
cell.setCellStyle(fontRedStyle);
} else {
cell.setCellValue(bgvInformationVO.getReference());
cell.setCellStyle(fontGreenStyle);
}
cell = row.createCell(++columnCount);
if
(StringUtils.containsIgnoreCase(bgvInformationVO.getCriminalDatabse(),
BGVerificationConstant.NOTCLEAR)
||
StringUtils.containsIgnoreCase(bgvInformationVO.getCriminalDatabse(),
BGVerificationConstant.UNABLETOCOMPLETE)) {
cell.setCellValue(bgvInformationVO.getCriminalDatabse());
cell.setCellStyle(fontRedStyle);
} else {
cell.setCellValue(bgvInformationVO.getCriminalDatabse());
cell.setCellStyle(fontGreenStyle);
}
cell = row.createCell(++columnCount);
cell.setCellStyle(dtlCellStyle);
cell.setCellValue(bgvInformationVO.getNoOfEmploymentVerified());
cell = row.createCell(++columnCount);
cell.setCellStyle(dtlCellStyle);
cell.setCellValue(bgvInformationVO.getNoOfAdrressesVerified());
cell = row.createCell(++columnCount);
cell.setCellStyle(dtlCellStyle);
cell.setCellValue("--");
for (int colNum = 0; colNum < columnCount; colNum++)
sheet.autoSizeColumn(colNum);
}
} catch (Exception e) {
logger.error("exception generated in ExcelOutputWriter class " + e);
}
}
private void createDtlCellStyle() {
XSSFCellStyle detailCellType = workbook.createCellStyle();
detailCellType.setBorderTop((short) 1); // double lines border
detailCellType.setBorderLeft((short) 1);
detailCellType.setBorderRight((short) 1);
detailCellType.setBorderBottom((short) 1); // single line border
detailCellType.setAlignment(CellStyle.ALIGN_CENTER);
detailCellType.setWrapText(true);
XSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
font.setBold(false);
font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
detailCellType.setFont(font);
setDtlCellStyle(detailCellType);
}
private void createDateCellStyle() {
XSSFCellStyle cellStyleDate = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();
cellStyleDate.setBorderTop((short) 1); // double lines border
cellStyleDate.setBorderLeft((short) 1);
cellStyleDate.setBorderRight((short) 1);
cellStyleDate.setBorderBottom((short) 1); // single line border
cellStyleDate.setAlignment(CellStyle.ALIGN_CENTER);
cellStyleDate.setDataFormat(createHelper.createDataFormat().getFormat("dd/MM/yyyy"));
XSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
font.setBold(false);
font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
cellStyleDate.setFont(font);
setDateCellStyle(cellStyleDate);
}
// creating font color style
private void createRedFontStyle() {
XSSFFont fontRed = workbook.createFont();
fontRed.setFontName(HSSFFont.FONT_ARIAL);
fontRed.setColor(HSSFColor.RED.index);
fontRed.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
XSSFCellStyle redFontStyle = workbook.createCellStyle();
redFontStyle.setBorderTop((short) 1); // double lines border
redFontStyle.setBorderLeft((short) 1);
redFontStyle.setBorderRight((short) 1);
redFontStyle.setBorderBottom((short) 1); // single line border
redFontStyle.setAlignment(CellStyle.ALIGN_CENTER);
//redFontStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
//redFontStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
redFontStyle.setFont(fontRed);
redFontStyle.setWrapText(true);
setFontRedStyle(redFontStyle);
}
private void createGreenFontStyle() {
XSSFFont fontGreen = workbook.createFont();
fontGreen.setFontName(HSSFFont.FONT_ARIAL);
fontGreen.setColor(HSSFColor.GREEN.index);
fontGreen.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
XSSFCellStyle greenFontStyle = workbook.createCellStyle();
greenFontStyle.setBorderTop((short) 1); // double lines border
greenFontStyle.setBorderLeft((short) 1);
greenFontStyle.setBorderRight((short) 1);
greenFontStyle.setBorderBottom((short) 1); // single line border
greenFontStyle.setAlignment(CellStyle.ALIGN_CENTER);
greenFontStyle.setFont(fontGreen);
//greenFontStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
//greenFontStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
greenFontStyle.setWrapText(true);
setFontGreenStyle(greenFontStyle);
}
private void createYellowFontStyle() {
XSSFFont fontYellow = workbook.createFont();
fontYellow.setFontName(HSSFFont.FONT_ARIAL);
fontYellow.setColor(HSSFColor.YELLOW.index);
fontYellow.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
XSSFCellStyle yellowFontStyle = workbook.createCellStyle();
yellowFontStyle.setBorderTop((short) 1); // double lines border
yellowFontStyle.setBorderLeft((short) 1);
yellowFontStyle.setBorderRight((short) 1);
yellowFontStyle.setBorderBottom((short) 1); // single line border
yellowFontStyle.setFont(fontYellow);
setFontYellowStyle(yellowFontStyle);
}
private void createOrangeFontStyle() {
XSSFFont fontOrange = workbook.createFont();
fontOrange.setFontName(HSSFFont.FONT_ARIAL);
fontOrange.setColor(HSSFColor.ORANGE.index);
fontOrange.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
XSSFCellStyle orangeFontStyle = workbook.createCellStyle();
orangeFontStyle.setBorderTop((short) 1); // double lines border
orangeFontStyle.setBorderLeft((short) 1);
orangeFontStyle.setBorderRight((short) 1);
orangeFontStyle.setBorderBottom((short) 1); // single line border
orangeFontStyle.setAlignment(CellStyle.ALIGN_CENTER);
orangeFontStyle.setFont(fontOrange);
setFontOrangeStyle(orangeFontStyle);
}
private void createHeaderCellStyle() {
XSSFCellStyle cellStyleHeaderRow = workbook.createCellStyle();
cellStyleHeaderRow.setBorderTop((short) 1); // double lines border
cellStyleHeaderRow.setBorderLeft((short) 1);
cellStyleHeaderRow.setBorderRight((short) 1);
cellStyleHeaderRow.setBorderBottom((short) 1); // single line border
cellStyleHeaderRow.setAlignment(CellStyle.ALIGN_CENTER);
cellStyleHeaderRow.setFillBackgroundColor(HSSFColor.BRIGHT_GREEN.index);
cellStyleHeaderRow.setWrapText(true);
XSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
cellStyleHeaderRow.setFont(font);
setHeaderCellStyle(cellStyleHeaderRow);
}
private void createSheet(String sheetName) {
setSheet(workbook.createSheet(sheetName));
}
private void getSheet(String sheetName) {
setSheet(workbook.getSheet(sheetName));
}
private void createWorkBook() {
setWorkbook(new XSSFWorkbook());
}
private void createWorkBook(InputStream inp) {
try {
setWorkbook(new XSSFWorkbook(inp));
} catch (IOException e) {
logger.error("Error while creating the workbook from the file,
details: " + e);
}
}
private void createWorkBook(String filePath) {
try {
OPCPackage pkg = OPCPackage.open(filePath);
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(pkg);
setWorkbook(wb);
} catch (Exception e) {
logger.error("Error while creating the workbook from the file,
details: " + e);
}
}
}
输出就像这样,文本颜色仅应用于第一行
答案 0 :(得分:0)
对于这个问题,有很多关于apache poi及其对象的事情。 我无法尝试您的代码,因为它正在使用您自定义的代码和对象。
无论如何,让我们谈谈apache poi对象库:
在我的下面的示例中,我使用 XSSFWorkbook 和 XSSFSheet 而不是HSSFWorkbook和HSSFSheet,因为这样会限制您最多4000个单元格字体,并建议您例外但实际上,当您减少要设置的单元格数量时,它将仅适用于少数单元格,通常小于<40。
使用 XSS 对象,您可以将字体应用于所有单元格。 当您处理大型文件时,您可以获得Java堆空间异常 有用的增加它。
代码下方和结果截图:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Main {
public Main() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws IOException {
FileInputStream is = null;
XSSFWorkbook wb = null;
FileOutputStream out = null;
try {
is = new FileInputStream(new File("pathtofile\\myfile.xlsx"));
wb = new XSSFWorkbook(is);
String str = "1111";
XSSFSheet sheet = wb.getSheet("Sheet1");
Cell cell =null;
Font font =null;
CellStyle style = null;
for(int i=0;i<1000;i++){
Row row = sheet.createRow(i);
for(int j=0;j<10;j++){
cell = row.createCell(j);
font = wb.createFont();
if(j % 2 ==0)
font.setColor(HSSFColor.RED.index);
else
font.setColor(HSSFColor.GREEN.index);
style = wb.createCellStyle();
style.setFont(font);
cell.setCellStyle(style);
cell.setCellValue(str);
}
}
out = new FileOutputStream(new File("pathtofile\\myfile.xlsx"));
wb.write(out);
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(wb != null)
wb.close();
if(is !=null)
is.close();
if(out != null)
out.close();
}
}
}
您可以注意到我创建了一个 .xlsx 文件,该文件适用于 XSS 对象,而不是 .XLS 文件,请阅读{{ 3}}