Browse Source

* 修复填充样式可能丢失的问题

pull/2144/head
Jiaju Zhuang 3 years ago
parent
commit
edc9869b37
  1. 1
      src/main/java/com/alibaba/excel/util/NumberUtils.java
  2. 103
      src/main/java/com/alibaba/excel/util/StyleUtil.java
  3. 12
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  4. 12
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  5. 6
      src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java
  6. 49
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  7. 42
      src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java
  8. 132
      src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java
  9. BIN
      src/test/resources/demo/fill/list.xlsx

1
src/main/java/com/alibaba/excel/util/NumberUtils.java

@ -186,4 +186,5 @@ public class NumberUtils {
decimalFormat.setParseBigDecimal(true);
return decimalFormat.parse(string);
}
}

103
src/main/java/com/alibaba/excel/util/StyleUtil.java

@ -12,6 +12,7 @@ import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
@ -19,6 +20,8 @@ import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
/**
@ -32,11 +35,16 @@ public class StyleUtil {
* Build cell style
*
* @param workbook
* @param originCellStyle
* @param writeCellStyle
* @return
*/
public static CellStyle buildCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
public static CellStyle buildCellStyle(Workbook workbook, CellStyle originCellStyle,
WriteCellStyle writeCellStyle) {
CellStyle cellStyle = workbook.createCellStyle();
if (originCellStyle != null) {
cellStyle.cloneStyleFrom(originCellStyle);
}
if (writeCellStyle == null) {
return cellStyle;
}
@ -121,8 +129,8 @@ public class StyleUtil {
return BuiltinFormats.GENERAL;
}
public static Font buildFont(Workbook workbook, WriteFont writeFont) {
Font font = workbook.createFont();
public static Font buildFont(Workbook workbook, Font originFont, WriteFont writeFont) {
Font font = createFont(workbook, originFont);
if (writeFont == null || font == null) {
return font;
}
@ -156,58 +164,39 @@ public class StyleUtil {
return font;
}
public static WriteCellStyle buildWritCellStyle(CellStyle cellStyle, Font font) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
if (cellStyle == null) {
return writeCellStyle;
}
writeCellStyle.setHidden(cellStyle.getHidden());
writeCellStyle.setLocked(cellStyle.getLocked());
writeCellStyle.setQuotePrefix(cellStyle.getQuotePrefixed());
writeCellStyle.setHorizontalAlignment(cellStyle.getAlignment());
writeCellStyle.setWrapped(cellStyle.getWrapText());
writeCellStyle.setVerticalAlignment(cellStyle.getVerticalAlignment());
writeCellStyle.setRotation(cellStyle.getRotation());
writeCellStyle.setIndent(cellStyle.getIndention());
writeCellStyle.setBorderLeft(cellStyle.getBorderLeft());
writeCellStyle.setBorderRight(cellStyle.getBorderRight());
writeCellStyle.setBorderTop(cellStyle.getBorderTop());
writeCellStyle.setBorderBottom(cellStyle.getBorderBottom());
writeCellStyle.setLeftBorderColor(cellStyle.getLeftBorderColor());
writeCellStyle.setRightBorderColor(cellStyle.getRightBorderColor());
writeCellStyle.setTopBorderColor(cellStyle.getTopBorderColor());
writeCellStyle.setBottomBorderColor(cellStyle.getBottomBorderColor());
writeCellStyle.setFillPatternType(cellStyle.getFillPattern());
//writeCellStyle.setFillBackgroundColor(cellStyle.getFillBackgroundColor());
//writeCellStyle.setFillForegroundColor(cellStyle.getFillForegroundColor());
writeCellStyle.setShrinkToFit(cellStyle.getShrinkToFit());
writeCellStyle.setDataFormatData(buildDataFormat(cellStyle.getDataFormat(), cellStyle.getDataFormatString()));
writeCellStyle.setWriteFont(buildFont(font));
return writeCellStyle;
}
public static DataFormatData buildDataFormat(short dataFormat, String dataFormatString) {
DataFormatData dataFormatData = new DataFormatData();
dataFormatData.setIndex(dataFormat);
dataFormatData.setFormat(dataFormatString);
return dataFormatData;
}
public static WriteFont buildFont(Font font) {
WriteFont writeFont = new WriteFont();
if (font == null) {
return writeFont;
}
writeFont.setFontName(font.getFontName());
writeFont.setFontHeightInPoints(font.getFontHeightInPoints());
writeFont.setItalic(font.getItalic());
writeFont.setStrikeout(font.getStrikeout());
writeFont.setColor(font.getColor());
writeFont.setTypeOffset(font.getTypeOffset());
writeFont.setUnderline(font.getUnderline());
writeFont.setCharset(font.getCharSet());
writeFont.setBold(font.getBold());
return writeFont;
private static Font createFont(Workbook workbook, Font originFont) {
Font font = workbook.createFont();
if (originFont == null) {
return font;
}
if (originFont instanceof XSSFFont) {
XSSFFont xssfFont = (XSSFFont)font;
XSSFFont xssfOriginFont = ((XSSFFont)originFont);
xssfFont.setFontName(xssfOriginFont.getFontName());
xssfFont.setFontHeightInPoints(xssfOriginFont.getFontHeightInPoints());
xssfFont.setItalic(xssfOriginFont.getItalic());
xssfFont.setStrikeout(xssfOriginFont.getStrikeout());
xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null));
xssfFont.setTypeOffset(xssfOriginFont.getTypeOffset());
xssfFont.setUnderline(xssfOriginFont.getUnderline());
xssfFont.setCharSet(xssfOriginFont.getCharSet());
xssfFont.setBold(xssfOriginFont.getBold());
return xssfFont;
} else if (originFont instanceof HSSFFont) {
HSSFFont hssfFont = (HSSFFont)font;
HSSFFont hssfOriginFont = (HSSFFont)originFont;
hssfFont.setFontName(hssfOriginFont.getFontName());
hssfFont.setFontHeightInPoints(hssfOriginFont.getFontHeightInPoints());
hssfFont.setItalic(hssfOriginFont.getItalic());
hssfFont.setStrikeout(hssfOriginFont.getStrikeout());
hssfFont.setColor(hssfOriginFont.getColor());
hssfFont.setTypeOffset(hssfOriginFont.getTypeOffset());
hssfFont.setUnderline(hssfOriginFont.getUnderline());
hssfFont.setCharSet(hssfOriginFont.getCharSet());
hssfFont.setBold(hssfOriginFont.getBold());
return hssfFont;
}
return font;
}
public static RichTextString buildRichTextString(WriteWorkbookHolder writeWorkbookHolder,
@ -222,12 +211,12 @@ public class StyleUtil {
richTextString = new HSSFRichTextString(richTextStringData.getTextString());
}
if (richTextStringData.getWriteFont() != null) {
richTextString.applyFont(writeWorkbookHolder.createFont(richTextStringData.getWriteFont()));
richTextString.applyFont(writeWorkbookHolder.createFont(richTextStringData.getWriteFont(), null, true));
}
if (CollectionUtils.isNotEmpty(richTextStringData.getIntervalFontList())) {
for (IntervalFont intervalFont : richTextStringData.getIntervalFontList()) {
richTextString.applyFont(intervalFont.getStartIndex(), intervalFont.getEndIndex(),
writeWorkbookHolder.createFont(intervalFont.getWriteFont()));
writeWorkbookHolder.createFont(intervalFont.getWriteFont(), null, true));
}
}
return richTextString;

12
src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java

@ -23,12 +23,10 @@ import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.holder.WriteHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
@ -72,9 +70,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
// Fill in formula information
fillFormula(cell, cellData.getFormulaData());
// Fill in style information
fillStyle(cell, cellData.getWriteCellStyle());
// Fill index
cellData.setRowIndex(cell.getRowIndex());
cellData.setColumnIndex(cell.getColumnIndex());
@ -109,13 +104,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
}
private void fillStyle(Cell cell, WriteCellStyle writeCellStyle) {
if (writeCellStyle == null) {
return;
}
CellStyle cellStyle = writeContext.writeWorkbookHolder().createCellStyle(writeCellStyle);
cell.setCellStyle(cellStyle);
}
private void fillFormula(Cell cell, FormulaData formulaData) {
if (formulaData == null) {

12
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -9,7 +9,6 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
@ -178,8 +177,6 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
}
AtomicInteger integer = new AtomicInteger(0);
private void doFill(List<AnalysisCell> analysisCellList, Object oneRowData, FillConfig fillConfig,
Integer relativeRowIndex) {
if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) {
@ -210,14 +207,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (fillConfig.getAutoStyle()) {
Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag))
.map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell))
.ifPresent(cellStyle -> {
cellData.setOriginCellStyle(cellStyle);
//WriteCellStyle writeCellStyle = StyleUtil.buildWritCellStyle(cellStyle,
// writeContext.writeWorkbookHolder().getWorkbook()
// .getFontAt(cellStyle.getFontIndexAsInt()));
//WriteCellStyle.merge(cellData.getWriteCellStyle(), writeCellStyle);
//cellData.setWriteCellStyle(writeCellStyle);
});
.ifPresent(cellData::setOriginCellStyle);
}
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);

6
src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java

@ -11,6 +11,7 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.CellStyle;
/**
* fill cell style.
@ -33,11 +34,12 @@ public class FillStyleCellWriteHandler implements CellWriteHandler {
}
WriteCellData<?> cellData = cellDataList.get(0);
WriteCellStyle writeCellStyle = cellData.getWriteCellStyle();
if (writeCellStyle == null) {
CellStyle originCellStyle = cellData.getOriginCellStyle();
if (writeCellStyle == null && originCellStyle == null) {
return;
}
WriteWorkbookHolder writeWorkbookHolder = context.getWriteWorkbookHolder();
context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle));
context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle, originCellStyle));
}
}

49
src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

@ -24,11 +24,13 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import lombok.Data;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
@ -129,7 +131,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
/**
* Used to cell style.
*/
private Map<WriteCellStyle, CellStyle> cellStyleMap;
private Map<Short, Map<WriteCellStyle, CellStyle>> cellStyleIndexMap;
/**
* Used to font.
*/
@ -206,7 +208,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} else {
this.writeExcelOnException = writeWorkbook.getWriteExcelOnException();
}
this.cellStyleMap = MapUtils.newHashMap();
this.cellStyleIndexMap = MapUtils.newHashMap();
this.fontMap = MapUtils.newHashMap();
this.dataFormatMap = MapUtils.newHashMap();
}
@ -242,16 +244,36 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* create a cell style.
*
* @param writeCellStyle
* @param originCellStyle
* @return
*/
public CellStyle createCellStyle(WriteCellStyle writeCellStyle) {
public CellStyle createCellStyle(WriteCellStyle writeCellStyle, CellStyle originCellStyle) {
if (writeCellStyle == null) {
return originCellStyle;
}
short styleIndex = -1;
Font originFont = null;
boolean useCache = false;
if (originCellStyle != null) {
styleIndex = originCellStyle.getIndex();
if (originCellStyle instanceof XSSFCellStyle) {
originFont = ((XSSFCellStyle)originCellStyle).getFont();
} else if (originCellStyle instanceof HSSFCellStyle) {
originFont = ((HSSFCellStyle)originCellStyle).getFont(workbook);
}
useCache = true;
}
Map<WriteCellStyle, CellStyle> cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex,
key -> MapUtils.newHashMap());
CellStyle cellStyle = cellStyleMap.get(writeCellStyle);
if (cellStyle != null) {
return cellStyle;
}
cellStyle = StyleUtil.buildCellStyle(workbook, writeCellStyle);
cellStyle.setDataFormat(createDataFormat(writeCellStyle.getDataFormatData()));
cellStyle.setFont(createFont(writeCellStyle.getWriteFont()));
cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle);
cellStyle.setDataFormat(createDataFormat(writeCellStyle.getDataFormatData(), useCache));
cellStyle.setFont(createFont(writeCellStyle.getWriteFont(), originFont, useCache));
cellStyleMap.put(writeCellStyle, cellStyle);
return cellStyle;
}
@ -260,14 +282,19 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* create a font.
*
* @param writeFont
* @param originFont
* @param useCache
* @return
*/
public Font createFont(WriteFont writeFont) {
public Font createFont(WriteFont writeFont, Font originFont, boolean useCache) {
if (!useCache) {
return StyleUtil.buildFont(workbook, originFont, writeFont);
}
Font font = fontMap.get(writeFont);
if (font != null) {
return font;
}
font = StyleUtil.buildFont(workbook, writeFont);
font = StyleUtil.buildFont(workbook, originFont, writeFont);
fontMap.put(writeFont, font);
return font;
}
@ -276,9 +303,13 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* create a data format.
*
* @param dataFormatData
* @param useCache
* @return
*/
public Short createDataFormat(DataFormatData dataFormatData) {
public Short createDataFormat(DataFormatData dataFormatData, boolean useCache) {
if (!useCache) {
return StyleUtil.buildDataFormat(workbook, dataFormatData);
}
Short dataFormat = dataFormatMap.get(dataFormatData);
if (dataFormat != null) {
return dataFormat;

42
src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java

@ -69,27 +69,27 @@ public class FillTest {
// 方案1 一下子全部放到内存里面 并填充
String fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
EasyExcel.write(fileName).withTemplate(templateFileName).inMemory(Boolean.TRUE).sheet().doFill(data());
//// 方案2 分多次 填充 会使用文件缓存(省内存) jdk8
//// since: 3.0.0-beta1
//fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
//EasyExcel.write(fileName)
// .withTemplate(templateFileName)
// .sheet()
// .doFill(() -> {
// // 分页查询数据
// return data();
// });
//
//// 方案3 分多次 填充 会使用文件缓存(省内存)
//fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
//ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
//WriteSheet writeSheet = EasyExcel.writerSheet().build();
//excelWriter.fill(data(), writeSheet);
//excelWriter.fill(data(), writeSheet);
//// 千万别忘记关闭流
//excelWriter.finish();
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data());
// 方案2 分多次 填充 会使用文件缓存(省内存) jdk8
// since: 3.0.0-beta1
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(fileName)
.withTemplate(templateFileName)
.sheet()
.doFill(() -> {
// 分页查询数据
return data();
});
// 方案3 分多次 填充 会使用文件缓存(省内存)
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(data(), writeSheet);
excelWriter.fill(data(), writeSheet);
// 千万别忘记关闭流
excelWriter.finish();
}
/**

132
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.temp.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
@ -8,13 +9,22 @@ import java.util.Date;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.util.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -39,25 +49,129 @@ public class PoiTest {
SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum());
SXSSFRow row = xssfSheet.getRow(0);
LOGGER.info("dd{}",row.getCell(0).getColumnIndex());
LOGGER.info("dd{}", row.getCell(0).getColumnIndex());
Date date = row.getCell(1).getDateCellValue();
}
@Test
public void lastRowNumXSSF() throws IOException {
String file = "/Users/zhuangjiaju/test/test3.xlsx";
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file);
String file = "/Users/zhuangjiaju/test/test3 copy 10.xlsx";
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
LOGGER.info("一共:{}个sheet", xssfWorkbook.getNumberOfSheets());
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum());
XSSFRow row = xssfSheet.getRow(0);
LOGGER.info("dd{}",row.getCell(0).getRow().getRowNum());
LOGGER.info("dd{}",xssfSheet.getLastRowNum());
XSSFRow row = xssfSheet.getRow(1);
LOGGER.info("dd{}", row.getCell(0).getRow().getRowNum());
LOGGER.info("dd{}", xssfSheet.getLastRowNum());
Date date = row.getCell(1).getDateCellValue();
LOGGER.info("date{}",date);
XSSFCellStyle cellStyle = row.getCell(0).getCellStyle();
LOGGER.info("size1:{}", cellStyle.getFontIndexAsInt());
XSSFCellStyle cellStyle1 = xssfWorkbook.createCellStyle();
LOGGER.info("size2:{}", cellStyle1.getFontIndexAsInt());
cellStyle1.cloneStyleFrom(cellStyle);
LOGGER.info("size3:{}", cellStyle1.getFontIndexAsInt());
LOGGER.info("bbb:{}", cellStyle1.getFont().getXSSFColor().getIndex());
LOGGER.info("bbb:{}", cellStyle1.getFont().getXSSFColor().getIndexed());
XSSFColor myColor = new XSSFColor(cellStyle1.getFont().getXSSFColor().getRGB(), null);
LOGGER.info("bbb:{}", cellStyle1.getFont().getXSSFColor().getRGB());
LOGGER.info("bbb:{}", cellStyle1.getFont().getXSSFColor().getARGBHex());
LOGGER.info("bbb:{}", cellStyle1.getFont().getBold());
LOGGER.info("bbb:{}", cellStyle1.getFont().getFontName());
XSSFFont xssfFont = xssfWorkbook.createFont();
xssfFont.setColor(myColor);
xssfFont.setFontHeightInPoints((short)50);
xssfFont.setBold(Boolean.TRUE);
cellStyle1.setFont(xssfFont);
cellStyle1.setFillForegroundColor(IndexedColors.PINK.getIndex());
LOGGER.info("aaa:{}", cellStyle1.getFont().getColor());
row.getCell(1).setCellStyle(cellStyle1);
row.getCell(1).setCellValue(3334l);
XSSFCellStyle cellStyle2 = xssfWorkbook.createCellStyle();
cellStyle2.cloneStyleFrom(cellStyle);
cellStyle2.setFillForegroundColor(IndexedColors.BLUE.getIndex());
//cellStyle2.setFont(cellStyle1.getFont());
row.getCell(2).setCellStyle(cellStyle2);
row.getCell(2).setCellValue(3334l);
//LOGGER.info("date1:{}", row.getCell(0).getStringCellValue());
//LOGGER.info("date2:{}", ((XSSFColor) cellStyle.getFillForegroundColorColor()).getIndex());
//LOGGER.info("date2:{}", ((XSSFColor) cellStyle.getFillForegroundColorColor()).isRGB());
//LOGGER.info("date4:{}", ((XSSFColor) cellStyle.getFillForegroundColorColor()).isIndexed());
//LOGGER.info("date3:{}", cellStyle.getFont().getXSSFColor().getRGB());
//LOGGER.info("date4:{}", cellStyle.getFont().getCTFont().getColorArray(0).getRgb());
FileOutputStream fileOutputStream = new FileOutputStream(file);
xssfWorkbook.write(fileOutputStream);
fileOutputStream.flush();
xssfWorkbook.close();
}
@Test
public void lastRowNumXSSFv22() throws IOException {
String file = "/Users/zhuangjiaju/test/test3 copy 2.xls";
HSSFWorkbook xssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
LOGGER.info("一共:{}个sheet", xssfWorkbook.getNumberOfSheets());
HSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum());
HSSFRow row = xssfSheet.getRow(1);
LOGGER.info("dd{}", row.getCell(0).getRow().getRowNum());
LOGGER.info("dd{}", xssfSheet.getLastRowNum());
HSSFCellStyle cellStyle = row.getCell(0).getCellStyle();
LOGGER.info("单元格1的字体:{}", cellStyle.getFontIndexAsInt());
HSSFCellStyle cellStyle1 = xssfWorkbook.createCellStyle();
LOGGER.info("size2:{}", cellStyle1.getFontIndexAsInt());
cellStyle1.cloneStyleFrom(cellStyle);
LOGGER.info("单元格2的字体:{}", cellStyle1.getFontIndexAsInt());
LOGGER.info("bbb:{}", cellStyle1.getFont(xssfWorkbook).getColor());
HSSFFont xssfFont = xssfWorkbook.createFont();
xssfFont.setColor(cellStyle1.getFont(xssfWorkbook).getColor());
xssfFont.setFontHeightInPoints((short)50);
xssfFont.setBold(Boolean.TRUE);
cellStyle1.setFont(xssfFont);
cellStyle1.setFillForegroundColor(IndexedColors.PINK.getIndex());
LOGGER.info("aaa:{}", cellStyle1.getFont(xssfWorkbook).getColor());
row.getCell(1).setCellStyle(cellStyle1);
row.getCell(1).setCellValue(3334l);
HSSFCellStyle cellStyle2 = xssfWorkbook.createCellStyle();
cellStyle2.cloneStyleFrom(cellStyle);
cellStyle2.setFillForegroundColor(IndexedColors.BLUE.getIndex());
//cellStyle2.setFont(cellStyle1.getFont());
row.getCell(2).setCellStyle(cellStyle2);
row.getCell(2).setCellValue(3334l);
//LOGGER.info("date1:{}", row.getCell(0).getStringCellValue());
//LOGGER.info("date2:{}", ((XSSFColor) cellStyle.getFillForegroundColorColor()).getIndex());
//LOGGER.info("date2:{}", ((XSSFColor) cellStyle.getFillForegroundColorColor()).isRGB());
//LOGGER.info("date4:{}", ((XSSFColor) cellStyle.getFillForegroundColorColor()).isIndexed());
//LOGGER.info("date3:{}", cellStyle.getFont().getXSSFColor().getRGB());
//LOGGER.info("date4:{}", cellStyle.getFont().getCTFont().getColorArray(0).getRgb());
FileOutputStream fileOutputStream = new FileOutputStream(file);
xssfWorkbook.write(fileOutputStream);
fileOutputStream.flush();
xssfWorkbook.close();
}
@Test

BIN
src/test/resources/demo/fill/list.xlsx

Binary file not shown.
Loading…
Cancel
Save