HSSFRichTextString样式在打印预览中消失

时间:2017-01-10 09:20:47

标签: java excel apache-poi

我在使用apache POI生成的xls时遇到打印预览问题。我已经使用HSSFRichTextString将样式应用于单元格内容。对于文本我正在应用Arial字体&对于单元格内容中的数字我正在使用终端字体。 E.g" SS 123 RR" SS / RR将在Arial,123将在终端。 以下是相同的代码段。

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;

    }

}

现在当我打开生成的xls时,我看到应用于内容的格式。 请参考xls view获取相同内容。 但现在当我打印预览时,我看到了字体的问题。应用于数字的终端字体在打印预览中不再可见。有关打印预览视图,请参阅enter image description here。 请注意数字&#39; 1&#39;之间的差异在两个图像中。 任何想法/建议我如何克服这个? 在此先感谢。

0 个答案:

没有答案