gpt4 book ai didi

java - 为什么 XSSFRichTextString.applyFont() 不能像 Java 文档中编写的那样工作?

转载 作者:行者123 更新时间:2023-12-02 09:26:56 26 4
gpt4 key购买 nike

我正在尝试在字符串的某些部分应用粗体字体并将其放入单元格中。

XSSFFont font = workbook.createFont();
font.setBold(true);
XSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
XSSFCell c = nextRow.createCell(4);
c.setCellStyle(style);
XSSFRichTextString string = new XSSFRichTextString(report.getSurroundText());
string.applyFont( startIndex, getEndOfWord(startIndex, report.getFoundWord()), font);
c.setCellValue(string);

此代码作为我的代码的一部分,生成 .xlsx文件,它确实生成了一个未损坏的文件,但应为粗体的文本不正确。相反,它会突出显示从文本开头到我在 applyFont() 中设置为结束索引的索引。方法。基本上出于某种原因startIndex被忽略。

在调试期间,startIndex返回值getEndOfWord()是正确的。

编辑:

try(FileOutputStream fileOut = new FileOutputStream(new File(directory.getAbsoluteFile() + File.separator + 
FilenameUtils.getBaseName(csvFile.getAbsolutePath()) + ".xlsx"));) {
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet("Highlights");
XSSFRow headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue(firstLine);

XSSFRow titleRow = sheet.createRow(1);
titleRow.createCell(0).setCellValue(SCANID);
titleRow.createCell(1).setCellValue(DOCID);
titleRow.createCell(2).setCellValue(FOUNDWORD);
titleRow.createCell(3).setCellValue(OFFSET);
titleRow.createCell(4).setCellValue(SURROUNDTEXT);

XSSFFont font = workbook.createFont();
font.setBold(true);
XSSFFont deFont = workbook.createFont();
font.setBold(false);

int row = 2;
for (MuiDetailReport report : lst) {
XSSFRow nextRow = sheet.createRow(row);
nextRow.createCell(0).setCellValue(report.getScanId());
nextRow.createCell(1).setCellValue(report.getDocId());
nextRow.createCell(2).setCellValue(report.getFoundWord());
if (report.getOffset() != 0) nextRow.createCell(3).setCellValue(report.getOffset());
else nextRow.createCell(3).setCellValue("");
if (!report.getFoundWord().isBlank() && !report.getSurroundText().isBlank()) {
int startIndex = getStartOfWord(report.getFoundWord(), report.getSurroundText());
if (startIndex == -1) nextRow.createCell(4).setCellValue("");
else {
XSSFCell c = nextRow.createCell(4);
XSSFRichTextString string = new XSSFRichTextString(report.getSurroundText());
string.applyFont(startIndex, getEndOfWord(startIndex, report.getFoundWord()), font);
c.setCellValue(string);
}
} else nextRow.createCell(4).setCellValue("");
row++;
}
workbook.write(fileOut);
}
fileOut.flush();
}

这是我创建 .xlsx 的方法文件。方法参数:String firstLine, List<MuiDetailReport> lst, File csvFile 。全部大写字符的变量为 static final String

我的结果是“HellomynameisThad”而不是“HellomynameisThad”

最佳答案

让我们来一次真正的Minimal, Reproducible Example .

下面的结果应该是文本

你好名字isThad

在单元格A1中。

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelBoldWord {

public static void main(String[] args) throws Exception {

Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();

String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";

CreationHelper creationHelper = workbook.getCreationHelper();

Font font = workbook.createFont(); // default font
Font fontBold = workbook.createFont();
fontBold.setBold(true);

String text = "HellomynameisThad";
String word = "name";

RichTextString richTextString = creationHelper.createRichTextString(text);
int startIndex = text.indexOf(word);
int endIndex = startIndex + word.length();
richTextString.applyFont(startIndex, endIndex, fontBold);

Sheet sheet = workbook.createSheet();
sheet.createRow(0).createCell(0).setCellValue(richTextString);

FileOutputStream out = new FileOutputStream(fileName);
workbook.write(out);
out.close();
workbook.close();
}
}

没有?

关于java - 为什么 XSSFRichTextString.applyFont() 不能像 Java 文档中编写的那样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com