Package org.eclipse.birt.report.engine.emitter.excel

Source Code of org.eclipse.birt.report.engine.emitter.excel.ExcelUtil

package org.eclipse.birt.report.engine.emitter.excel;

import java.text.NumberFormat;
import java.sql.Time;
import java.util.Date;


import org.eclipse.birt.core.format.DateFormatter;
import org.eclipse.birt.core.format.StringFormatter;
import org.eclipse.birt.core.format.NumberFormatter;


import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.SimpleDateFormat;
import it.birt.report.engine.emitter.xls.JEmitterConfig.XLSRenderOption;
import it.birt.report.engine.emitter.xls.StringUtil;

import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ExcelUtil {

    public static Logger logger = Logger.getLogger(ExcelUtil.class.getName());

    public static String ridQuote(String val) {
        if (val.charAt(0) == '"' && val.charAt(val.length() - 1) == '"') {
            return val.substring(1, val.length() - 1);
        }

        return val;
    }

    public static String formatDate(Object data) {

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd'T'HH:mm:ss");
        return dateFormat.format((Date) data);

    }

    public static String formatNumber(Object data) {

        DecimalFormat numberFormat = new DecimalFormat("0.00E00");
        return numberFormat.format((Number) data);

    }

    public static String getPattern(Object data, String val) {
        if (val != null && data instanceof Date) {
            if (val.indexOf("kk:mm") >= 0) {
                return "Short Time";
            } else if (val.startsWith("ahh")) {
                return "Long Time";
            } else if (!val.startsWith("ahh") && val.indexOf("ahh") >= 0) {
                return "General Date";
            }
            return new DateFormatter(val).getPattern();
        } else if (val == null && data instanceof Time) {
            return "Long Time";
        } else if (val == null && data instanceof java.sql.Date) {
            return "yyyy-M-d";
        } else if (val == null && data instanceof java.util.Date) {
            return "yyyy-M-d HH:ss:mm AM/PM";
        } else if (val != null && data instanceof Number) {
            if (val.indexOf("E") >= 0) {
                return "Scientific";
            }
            return new NumberFormatter(val).getPattern();

        } else if (val != null && data instanceof String) {
            return new StringFormatter(val).getPattern();
        }

        return null;
    }

    public static int convertToPt(String size) {
        try {
            int s = Integer.valueOf(size.substring(0, size.length() - 2)).intValue();
            if (size.endsWith("in")) {
                return s * 72;
            } else if (size.endsWith("cm")) {
                return (int) (s / 2.54 * 72);
            } else if (size.endsWith("mm")) {
                return (int) (s * 10 / 2.54 * 72);
            } else if (size.endsWith("pc")) {
                return s;
            } else {
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String getColumnOfExp(String exp) {
        return exp.substring(exp.indexOf("dataSetRow["), exp.lastIndexOf("]") + 1);
    }

    public static boolean isNumber(String val) {
        return isNumber(val, null);
    }

    public static boolean isNumber(String val, Locale locale) {
        NumberFormat nf = locale == null ? NumberFormat.getInstance() : NumberFormat.getInstance(locale);
        try {
            nf.parse(val);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static double getNumber(String val) {
        return getNumber(val, null);
    }

    public static double getNumber(String val, Locale locale) {
        NumberFormat nf = locale == null ? NumberFormat.getInstance() : NumberFormat.getInstance(locale);
        try {
            Number num = nf.parse(val);
            return num.doubleValue();
        } catch (Exception e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
            return 0;
        }
    }

    public static String getType(Object val) {
        if (val instanceof Number) {
            return Data.NUMBER;
        } else if (val instanceof Date) {
            return Data.DATE;
        } else {
            return Data.STRING;
        }
    }

    public static double milliInchesToMillimeters(double milliInches) {
        return milliInches / (2540 * XLSRenderOption.ADJUST_FACTOR);
    }
    /**
     * Rimuove caratteri non ammessi dal nome di un foglio.
     * Caratteri non ammessi : [ ] * ? / \
     * @param sheetName
     * @return
     */
    public static String escapeSheetName(String sheetName)
    {
        sheetName = StringUtil.stZL(sheetName);
        sheetName = sheetName.replaceAll("\\[", "_");
        sheetName = sheetName.replaceAll("\\]", "_");
        sheetName = sheetName.replaceAll("\\*", "_");
        sheetName = sheetName.replaceAll("\\?", "_");
        sheetName = sheetName.replaceAll("\\/", "_");
        sheetName = sheetName.replaceAll("\\\\", "_");
        return sheetName;
    }
}

TOP

Related Classes of org.eclipse.birt.report.engine.emitter.excel.ExcelUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.