[bug-65943] support rich text in SXSSFWorkbook (only when shared string table is enabled)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898801 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-03-09 20:06:59 +00:00
parent 81b557030c
commit 552471e578
3 changed files with 51 additions and 3 deletions

View File

@ -55,13 +55,11 @@ public class SXSSFCreationHelper implements CreationHelper {
@Override
public XSSFRichTextString createRichTextString(String text) {
LOG.atInfo().log("SXSSF doesn't support Rich Text Strings, any formatting information will be lost");
return new XSSFRichTextString(text);
}
@Override
public SXSSFFormulaEvaluator createFormulaEvaluator() {
return new SXSSFFormulaEvaluator(wb);
}

View File

@ -38,6 +38,7 @@ 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.FormulaError;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.CodepointsUtil;
import org.apache.poi.util.Removal;
@ -322,7 +323,7 @@ public class SheetDataWriter implements Closeable {
}
case STRING: {
if (_sharedStringSource != null) {
XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
RichTextString rt = cell.getRichStringCellValue();
int sRef = _sharedStringSource.addSharedStringItem(rt);
writeAttribute("t", STCellType.S.toString());

View File

@ -51,6 +51,10 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
@ -154,6 +158,51 @@ public final class TestSXSSFWorkbook extends BaseTestXWorkbook {
}
}
@Test
void useSharedStringsTableWithRichText() throws Exception {
try (SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true)) {
SharedStringsTable sss = wb.getSharedStringSource();
assertNotNull(sss);
XSSFFont redFont = new XSSFFont();
redFont.setColor(new XSSFColor(new java.awt.Color(241,76,93), new DefaultIndexedColorMap()));
Row row = wb.createSheet("S1").createRow(0);
row.createCell(0).setCellValue("A");
row.createCell(1).setCellValue("B");
XSSFRichTextString rts = new XSSFRichTextString("A");
rts.applyFont(redFont);
row.createCell(2).setCellValue(rts);
try (XSSFWorkbook xssfWorkbook = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb)) {
sss = wb.getSharedStringSource();
assertEquals(3, sss.getUniqueCount());
assertTrue(wb.dispose());
Sheet sheet1 = xssfWorkbook.getSheetAt(0);
assertEquals("S1", sheet1.getSheetName());
assertEquals(1, sheet1.getPhysicalNumberOfRows());
row = sheet1.getRow(0);
assertNotNull(row);
Cell cell = row.getCell(0);
assertNotNull(cell);
assertEquals("A", cell.getStringCellValue());
cell = row.getCell(1);
assertNotNull(cell);
assertEquals("B", cell.getStringCellValue());
cell = row.getCell(2);
assertNotNull(cell);
assertEquals("A", cell.getStringCellValue());
XSSFRichTextString outputRichTextString = (XSSFRichTextString) cell.getRichStringCellValue();
XSSFFont outputFont = outputRichTextString.getFontAtIndex(0);
assertEquals(redFont, outputFont);
}
}
}
@Test
void addToExistingWorkbook() throws IOException {
try (XSSFWorkbook xssfWb1 = new XSSFWorkbook()) {