Java HSSFRichTextString样式在打印预览中消失
我在使用ApachePOI生成xls的打印预览中遇到问题。我使用HSSFRichTextString对单元格内容应用样式。对于文本,我使用Arial字体;对于单元格内容中的数字,我使用Terminal字体。 例如,“SS 123 RR”SS/RR将采用Arial字体,123将采用Terminal字体。 下面是同样的代码片段Java HSSFRichTextString样式在打印预览中消失,java,excel,apache-poi,Java,Excel,Apache Poi,我在使用ApachePOI生成xls的打印预览中遇到问题。我使用HSSFRichTextString对单元格内容应用样式。对于文本,我使用Arial字体;对于单元格内容中的数字,我使用Terminal字体。 例如,“SS 123 RR”SS/RR将采用Arial字体,123将采用Terminal字体。 下面是同样的代码片段 package com.adp.wfn.payroll.exportToExcel; import java.io.File; import java.io.FileInp
package com.adp.wfn.payroll.exportToExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ReadAndWriteRichText {
public static void main(String[] args) {
try {
InputStream inp = new FileInputStream("/home/devel/Desktop/hi/CheckRegister_Template.xls");
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
Font font1 = workbook.getFontAt(cell.getCellStyle().getFontIndex());
System.out.println(font1);
Font font2 = workbook.createFont();
font2.setFontName("Terminal");
font2.setFontHeightInPoints(font1.getFontHeightInPoints());
System.out.println(font2);
String originalStr="ss 123 ss";
HSSFRichTextString richString = new HSSFRichTextString( "SS 123 SS 44" );
cell.setCellValue( formatNumbersOfCellText(originalStr,font2,font1) );
FileOutputStream out = new FileOutputStream(
new File("/home/devel/Desktop/createworkbookRaj.xlsx"));
workbook.write(out);
out.close();
System.out.println("createworkbook.xlsx written successfully");
} catch (Exception ex) {
}
}
private static HSSFRichTextString formatNumbersOfCellText(String outputString,Font numberFont, Font strFont){
HSSFRichTextString formattedString = null;
try{//"ss 123 nn 67"
if(null != outputString && !outputString.trim().equalsIgnoreCase("")){
int lstIndexCalculated=0;
int startIndex ;
int endIndex;
String[] splittedArr = outputString.split("\\s");
if(null != splittedArr && splittedArr.length > 0){
formattedString = new HSSFRichTextString(outputString);
for (int i = 0; i < splittedArr.length; i++) {
if(lstIndexCalculated == 0){
startIndex = outputString.indexOf(splittedArr[i]);
}else{
startIndex = outputString.indexOf(splittedArr[i],lstIndexCalculated);
if(lstIndexCalculated < startIndex){
formattedString.applyFont(lstIndexCalculated,startIndex ,numberFont);
}
}
endIndex = startIndex + (splittedArr[i].length());
lstIndexCalculated = endIndex;
if(isNumericField(splittedArr[i])){
formattedString.applyFont(startIndex,endIndex ,numberFont);
}else{
formattedString.applyFont(startIndex,endIndex ,strFont);
}
startIndex = 0;
endIndex = 0;
}
if(lstIndexCalculated != 0){
return formattedString;
}
}
}
}catch(Exception e){
return null;
}
return null;
}
private static boolean isNumericField(String inputStr){
if(null != inputStr && !inputStr.trim().equalsIgnoreCase("")){
try{
inputStr = inputStr.replaceAll("[(,)]", "");
Double value = Double.valueOf(inputStr);
return true;
}catch(NumberFormatException numFormatExp){
return false;
}catch (Exception e) {
return false;
}
}
return false;
}
}
package com.adp.wfn.payroll.exportToExcel;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.InputStream;
导入java.util.array;
导入org.apache.poi.hssf.usermodel.HSSFRichTextString;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Font;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
公共类ReadAndWriteRichText{
公共静态void main(字符串[]args){
试一试{
InputStream inp=新文件InputStream(“/home/devel/Desktop/hi/CheckRegister_Template.xls”);
工作簿=新的HSSF工作簿();
工作表=工作簿.createSheet();
Row Row=sheet.createRow(1);
Cell Cell=row.createCell(1);
Font font1=workbook.getFontAt(cell.getCellStyle().getFontIndex());
系统输出打印项次(font1);
Font font2=工作簿.createFont();
font2.setFontName(“终端”);
font2.setFontHeightInPoints(font1.getFontHeightInPoints());
系统输出打印项次(font2);
字符串originalStr=“ss 123 ss”;
HSSFRichTextString richString=新的HSSFRichTextString(“SS 123 SS 44”);
cell.setCellValue(formatNumbersOfCellText(originalStr,font2,font1));
FileOutputStream out=新的FileOutputStream(
新文件(“/home/devel/Desktop/createworkbookRaj.xlsx”);
练习册。写(出);
out.close();
System.out.println(“createworkbook.xlsx编写成功”);
}捕获(例外情况除外){
}
}
私有静态HSSFRichTextString格式CellText的编号(字符串输出字符串、字体编号字体、字体样式字体){
HSSFRichTextString formattedString=null;
试试{/“ss 123 nn 67”
if(null!=outputString&!outputString.trim().equalsIgnoreCase(“”){
int lstinexcalculated=0;
国际标准指数;
内部索引;
String[]splittedArr=outputString.split(\\s”);
if(null!=splittedArr&&splittedArr.length>0){
formattedString=新的HSSFRichTextString(outputString);
for(int i=0;i
现在,当我打开生成的xls时,我看到了应用于内容的格式。
请参阅相同的。
但现在当我打印预览时,我发现字体有问题。应用于数字的终端字体在打印预览中不再可见。请参阅“打印预览”视图。
请注意两幅图像中数字“1”之间的差异。
有什么想法/建议我如何克服这个问题?
提前感谢。这与
ApachePOI
无关。即使您不是使用ApachePOI创建Excel工作表,而是使用Excel本身,您也会在打印预览中看到相同的内容。打印预览不是为了精确地显示每个字符,而是为了显示整个页面设置。没有Axel,使用纯Excel和手动格式化值,效果很好。虽然打印显示页面设置&不是精确的样式,但仍然是打印方式的复制品。我从excel中打印出来,仍然可以看到它的问题。没有样式。