简体   繁体   English

在excel中使用Apache POI更新文本框中的文本,但保留格式

[英]Using Apache POI to update text in a textbox in excel but retain formatting

I am using Excel sheets as a method of creating certificates and simple reports using Apache POI, the data is to be placed in formatted textboxes in the excel sheet. 我使用Excel工作表作为使用Apache POI创建证书和简单报告的方法,数据将放置在Excel工作表的格式化文本框中。 I was hoping to get the TextBox object from the sheet and just change the text but while I can get the RichTextString I cannot find a way to replace the text. 我希望从工作表中获取TextBox对象并仅更改文本,但是虽然我可以获取RichTextString,但找不到替代文本的方法。 My next plan is to get each individual font characteristic, create a new RichTextString with the new text and then set the font characteristics. 我的下一个计划是获取每个单独的字体特征,使用新文本创建一个新的RichTextString,然后设置字体特征。 This is fiddly but not too difficult but I have a tendency to go for the complex solution when a simple one exists. 这很奇怪,但也不太困难,但是当存在一个简单的解决方案时,我倾向于采用复杂的解决方案。

Is there a simple method to just change the text in a textbox and keep the formatting and font characteristcs? 是否有一种简单的方法可以只更改文本框中的文本并保持格式和字体特征?

Maybe this could help you. 也许这可以帮助您。

HSSFFont font = workBook.createFont();
font.setFontHeightInPoints((short) 10);
font.setFontName("Arial");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);


HSSFCellStyle style = workBook.createCellStyle();
style.setFont(font);


HSSFCell myCell = row.createCell(spalte);
myCell.setCellValue(new HSSFRichTextString("Text");
myCell.setCellStyle(style);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM