[英]Setting Some part of cell content to bold/italic using apache-poi 4.0.1 library
我想使用粗體和斜體來設置單元格值的內容。 例如“這是示例 內容” 。
但是,這不適用於XSSFRichTextString。
我正在使用Apache POI庫版本4.0.1。 我嘗試使用XSSFRichTextString使內容加粗和斜體組合。 我通過在cell1Value.append(“ sample”,fontBold)方法中傳遞兩個參數來附加字符串,即String和Font。
XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
XSSFFont fontBold= wb.createFont();
fontBold.setBold(true); //set bold
fontBold.setUnderline(HSSFFont.U_SINGLE);
XSSFFont fontItalic= wb.createFont();
fontItalic.setItalic(true); //set italic
cell1Value.append("sample ",fontBold);
cell1Value.append("content", fontItalic);
System.err.println(cell1Value.getCTRst());
Cell cell1 = row.createCell(0);
cell1.setCellValue(cell1Value);
我期望“樣本”為粗體,“內容”為斜體。 但是,下划線工作正常,並且我的“示例”字詞已正確下划線。 請提出我想念的東西嗎?
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TextBoldItalic {
public static void main(String[] args) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
XSSFFont fontBold = wb.createFont();
fontBold.setBold(true);
XSSFFont fontItalic = wb.createFont();
fontItalic.setItalic(true);
XSSFFont fontBoldItalic = wb.createFont();
fontBoldItalic.setBold(true);
fontBoldItalic.setItalic(true);
XSSFRichTextString cellValue = new XSSFRichTextString();
cellValue.append("This is ", fontBold);
cellValue.append("sample ", fontItalic);
cellValue.append("content", fontBoldItalic);
cell.setCellValue(cellValue);
OutputStream fileOut = new FileOutputStream("TextBoldItalic.xlsx");
wb.write(fileOut);
wb.close();
}
}
該代碼對我有用,並在LibreOffice中為我提供了此結果 。 OpenOffice也可以。 沒有MS Excel在這里進行測試,抱歉。 當然,像Online-Excel-Viewer這樣的工具將無法正確執行。 因此,請嘗試我的代碼並進行報告。
由於代碼看起來合理,只需執行完整的運行即可:
我測試過的以下
有效。
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet("With Rich Text");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
XSSFFont fontPlain = wb.createFont();
XSSFFont fontBoldItalic = wb.createFont();
fontBoldItalic.setBoldItalic(true);
fontBoldItalic.setItalic(true);
XSSFFont fontItalic = wb.createFont();
fontItalic.setItalic(true);
XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
cell1Value.applyFont(fontPlain);
cell1Value.append("sample ", fontBoldItalic);
cell1Value.append("content", fontItalic);
cell.setCellValue(cell1Value);
wb.write(new FileOutputStream(xlsxFile));
} catch (IOException e) {
e.printStackTrace();
}
我的猜測是可變的混淆或微不足道的東西。 也許是字體。
使用WPS Spreadsheets
的問題是它們聲稱與Excel
最兼容,但有時它們完全失敗。 這次,如果它們明確設置為true,則它們會誤解所有布爾字體設置(粗體,斜體,粗體)。
Office Open XML
提供了具有val
屬性的布爾元素。 示例: <b val="true"/>
或<b val="false"/>
或<b val="1"/>
或<b val="0"/>
。 但是對於具有<b/>
設置粗體字體就足夠了。 對於不設置粗體的字體,根本不使用b
元素就足夠了。
Apache poi
始終將<b val="true"/>
為粗體,將<b val="false"/>
設置為非粗體。 但是WPS Spreadsheets
現在似乎誤解了<b val="true"/>
。 它只期望<b/>
。
以下代碼是用於為Excel
創建富文本字符串的最兼容的代碼。 它支持Office Open XML (*.xlsx)
和BIFF (*.xls)
並且僅將<Boolean val="true"/>
糾正為<Boolean/>
。
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
class CreateExcelRichText {
static RichTextString createRichTextString(Workbook workbook, String[] textParts, Font[] fonts) {
CreationHelper creationHelper = workbook.getCreationHelper();
RichTextString richTextString = creationHelper.createRichTextString(String.join("", textParts));
int start = 0;
int end = 0;
for (int tp = 0; tp < textParts.length; tp ++) {
Font font = null;
if (tp < fonts.length) font = fonts[tp];
end += textParts[tp].length();
if (font != null) richTextString.applyFont(start, end, font);
start += textParts[tp].length();
}
if (richTextString instanceof XSSFRichTextString) {
//unset val="true" for boolean objects
XSSFRichTextString xSSFRichTextString = (XSSFRichTextString)richTextString;
String[] boolenanObjectsToUnset = new String[]{"b", "i", "strike"};
for (String boolenanObjectToUnset : boolenanObjectsToUnset) {
XmlObject[] xmlObjects = xSSFRichTextString.getCTRst().selectPath(
"declare namespace main='http://schemas.openxmlformats.org/spreadsheetml/2006/main' " +
".//main:" + boolenanObjectToUnset);
for (XmlObject xmlObject : xmlObjects) {
CTBooleanProperty booleanProperty = (CTBooleanProperty)xmlObject;
if (booleanProperty.getVal()) booleanProperty.unsetVal();
}
}
}
return richTextString;
}
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();
Font font = workbook.createFont();
Font fontBoldItalic = workbook.createFont();
fontBoldItalic.setBold(true);
fontBoldItalic.setItalic(true);
Font fontItalic = workbook.createFont();
fontItalic.setItalic(true);
Font fontStrikeout = workbook.createFont();
fontStrikeout.setStrikeout(true);
String[] textParts = new String[]{"This is ", "Sample ", "content. ", "This is crossed out."};
Font[] fonts = new Font[]{font, fontBoldItalic, fontItalic, fontStrikeout};
RichTextString richTextString = createRichTextString(workbook, textParts, fonts);
Sheet sheet = workbook.createSheet();
sheet.createRow(0).createCell(0).setCellValue(richTextString);
String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
FileOutputStream out = new FileOutputStream(fileName);
workbook.write(out);
out.close();
workbook.close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.