Search in sources :

Example 1 with ExampleConversion

use of org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion in project poi by apache.

the class NumberRenderingSpreadsheetGenerator method main.

public static void main(String[] args) {
    writeJavaDoc();
    HSSFWorkbook wb = new HSSFWorkbook();
    SheetWriter sw = new SheetWriter(wb);
    ExampleConversion[] exampleValues = NumberToTextConversionExamples.getExampleConversions();
    for (ExampleConversion example : exampleValues) {
        sw.addTestRow(example.getRawDoubleBits(), example.getExcelRendering());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        wb.write(baos);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    byte[] fileContent = baos.toByteArray();
    replaceNaNs(fileContent, sw.getReplacementNaNs());
    File outputFile = new File("ExcelNumberRendering.xls");
    try {
        FileOutputStream os = new FileOutputStream(outputFile);
        os.write(fileContent);
        os.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");
}
Also used : ExampleConversion(org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 2 with ExampleConversion

use of org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion in project poi by apache.

the class NumberRenderingSpreadsheetGenerator method writeJavaDoc.

public static void writeJavaDoc() {
    ExampleConversion[] exampleConversions = NumberToTextConversionExamples.getExampleConversions();
    for (ExampleConversion ec : exampleConversions) {
        String line = " * <tr><td>" + formatLongAsHex(ec.getRawDoubleBits()) + "</td><td>" + Double.toString(ec.getDoubleValue()) + "</td><td>" + ec.getExcelRendering() + "</td></tr>";
        System.out.println(line);
    }
}
Also used : ExampleConversion(org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 3 with ExampleConversion

use of org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion in project poi by apache.

the class TestNumberToTextConverter method testAll.

/**
	 * Confirms that <tt>ExcelNumberToTextConverter.toText(d)</tt> produces the right results.
	 * As part of preparing this test class, the <tt>ExampleConversion</tt> instances should be set
	 * up to contain the rendering as produced by Excel.
	 */
public void testAll() {
    int failureCount = 0;
    ExampleConversion[] examples = NumberToTextConversionExamples.getExampleConversions();
    for (int i = 0; i < examples.length; i++) {
        ExampleConversion example = examples[i];
        try {
            if (example.isNaN()) {
                confirmNaN(example.getRawDoubleBits(), example.getExcelRendering());
                continue;
            }
            String actual = NumberToTextConverter.toText(example.getDoubleValue());
            if (!example.getExcelRendering().equals(actual)) {
                failureCount++;
                String msg = "Error rendering for examples[" + i + "] " + formatExample(example) + " " + " bad-result='" + actual + "' " + new ComparisonFailure(null, example.getExcelRendering(), actual).getMessage();
                System.err.println(msg);
                continue;
            }
        } catch (RuntimeException e) {
            failureCount++;
            System.err.println("Error in excel rendering for examples[" + i + "] " + formatExample(example) + "':" + e.getMessage());
            e.printStackTrace();
        }
    }
    if (failureCount > 0) {
        throw new AssertionFailedError(failureCount + " error(s) in excel number to text conversion (see std-err)");
    }
}
Also used : ExampleConversion(org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion) ComparisonFailure(junit.framework.ComparisonFailure) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

ExampleConversion (org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 ComparisonFailure (junit.framework.ComparisonFailure)1 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1