Search in sources :

Example 1 with WritableCell

use of jxl.write.WritableCell in project dwoss by gg-net.

the class JExcelLucidCalcWriter method write.

@Override
public File write(CCalcDocument document) {
    if (document == null)
        return null;
    validate(document);
    try {
        Map<CFormat, WritableCellFormat> formatCache = new HashMap<>();
        File file = document.getFile();
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(Locale.GERMANY);
        WritableWorkbook workbook = Workbook.createWorkbook(file, ws);
        FormatUtil util = new FormatUtil();
        for (int sheetIndex = 0; sheetIndex < document.getSheets().size(); sheetIndex++) {
            CSheet csheet = document.getSheets().get(sheetIndex);
            WritableSheet jsheet = workbook.createSheet(csheet.getName(), sheetIndex);
            jsheet.getSettings().setShowGridLines(csheet.isShowGridLines());
            for (CColumnView cColumn : csheet.getColumnViews()) {
                jsheet.setColumnView(cColumn.getColumnIndex(), cColumn.getSize());
            }
            for (CRowView cRowView : csheet.getRowViews()) {
                jsheet.setRowView(cRowView.getRowIndex(), cRowView.getSize());
            }
            for (CCell cCell : csheet.getCells()) {
                WritableCellFormat jformat;
                CFormat cFormat = cCell.getFormat();
                if (cFormat == null) {
                    jformat = WritableWorkbook.NORMAL_STYLE;
                } else {
                    cFormat = cFormat.fillNull(DEFAULT_FORMAT);
                    if (formatCache.containsKey(cFormat)) {
                        jformat = formatCache.get(cFormat);
                    } else {
                        WritableFont jfont = new WritableFont(WritableFont.createFont(cFormat.getName()));
                        jfont.setPointSize(cFormat.getSize());
                        jfont.setColour(util.discover(cFormat.getForeground()));
                        switch(cFormat.getStyle()) {
                            case BOLD:
                                jfont.setBoldStyle(jxl.write.WritableFont.BOLD);
                                break;
                            case ITALIC:
                                jfont.setItalic(true);
                                break;
                            case BOLD_ITALIC:
                                jfont.setBoldStyle(jxl.write.WritableFont.BOLD);
                                jfont.setItalic(true);
                                break;
                        }
                        jformat = new WritableCellFormat(jfont, util.discover(cFormat.getRepresentation()));
                        jformat.setBackground(util.discover(cFormat.getBackground()));
                        jformat.setAlignment(util.discover(cFormat.getHorizontalAlignment()));
                        jformat.setVerticalAlignment(util.discover(cFormat.getVerticalAlignment()));
                        if (cFormat.getBorder() != null) {
                            jformat.setBorder(Border.ALL, util.discover(cFormat.getBorder().getLineStyle()), util.discover(cFormat.getBorder().getColor()));
                        }
                        if (cFormat.isWrap() != null)
                            jformat.setWrap(cFormat.isWrap());
                        formatCache.put(cFormat, jformat);
                    }
                }
                Object elem = cCell.getValue();
                WritableCell jcell = null;
                if (elem == null) {
                    jcell = new Blank(cCell.getColumnIndex(), cCell.getRowIndex(), jformat);
                } else if (elem instanceof Number) {
                    double value = Double.MIN_VALUE;
                    if (elem instanceof Integer) {
                        value = ((Integer) elem).doubleValue();
                    } else if (elem instanceof Long) {
                        value = ((Long) elem).doubleValue();
                    } else if (elem instanceof Float) {
                        value = ((Float) elem).doubleValue();
                    } else if (elem instanceof Double) {
                        value = (Double) elem;
                    }
                    // TODO The rest of the 8 datatypes
                    jcell = new jxl.write.Number(cCell.getColumnIndex(), cCell.getRowIndex(), value, jformat);
                } else if (elem instanceof Boolean) {
                    jcell = new jxl.write.Boolean(cCell.getColumnIndex(), cCell.getRowIndex(), (Boolean) elem, jformat);
                } else if (elem instanceof Date) {
                    jcell = new jxl.write.DateTime(cCell.getColumnIndex(), cCell.getRowIndex(), (Date) elem, jformat);
                } else if (elem instanceof IFormula) {
                    IFormula f = ((IFormula) elem);
                    try {
                        FormulaParser parser = new FormulaParser(f.toRawFormula(), null, null, ws);
                        parser.parse();
                        jcell = new jxl.write.Formula(cCell.getColumnIndex(), cCell.getRowIndex(), f.toRawFormula(), jformat);
                    } catch (Exception e) {
                        jcell = new jxl.write.Label(cCell.getColumnIndex(), cCell.getRowIndex(), "Error in Formula: " + f.toRawFormula() + " | " + e, jformat);
                    }
                } else {
                    jcell = new jxl.write.Label(cCell.getColumnIndex(), cCell.getRowIndex(), elem.toString(), jformat);
                }
                jsheet.addCell(jcell);
            }
        }
        workbook.write();
        workbook.close();
        return file;
    } catch (WriteException | IOException ex) {
        throw new RuntimeException("Exception during document creation", ex);
    }
}
Also used : HashMap(java.util.HashMap) WritableCell(jxl.write.WritableCell) CSheet(eu.ggnet.lucidcalc.CSheet) FormulaParser(jxl.biff.formula.FormulaParser) CColumnView(eu.ggnet.lucidcalc.CColumnView) IFormula(eu.ggnet.lucidcalc.IFormula) CRowView(eu.ggnet.lucidcalc.CRowView) CCell(eu.ggnet.lucidcalc.CCell) CFormat(eu.ggnet.lucidcalc.CFormat) Blank(jxl.write.Blank) WriteException(jxl.write.WriteException) WritableFont(jxl.write.WritableFont) WorkbookSettings(jxl.WorkbookSettings) WritableSheet(jxl.write.WritableSheet) IOException(java.io.IOException) WritableCellFormat(jxl.write.WritableCellFormat) Date(java.util.Date) WriteException(jxl.write.WriteException) IOException(java.io.IOException) ConstraintViolationException(javax.validation.ConstraintViolationException) WritableWorkbook(jxl.write.WritableWorkbook) File(java.io.File)

Aggregations

CCell (eu.ggnet.lucidcalc.CCell)1 CColumnView (eu.ggnet.lucidcalc.CColumnView)1 CFormat (eu.ggnet.lucidcalc.CFormat)1 CRowView (eu.ggnet.lucidcalc.CRowView)1 CSheet (eu.ggnet.lucidcalc.CSheet)1 IFormula (eu.ggnet.lucidcalc.IFormula)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 WorkbookSettings (jxl.WorkbookSettings)1 FormulaParser (jxl.biff.formula.FormulaParser)1 Blank (jxl.write.Blank)1 WritableCell (jxl.write.WritableCell)1 WritableCellFormat (jxl.write.WritableCellFormat)1 WritableFont (jxl.write.WritableFont)1 WritableSheet (jxl.write.WritableSheet)1 WritableWorkbook (jxl.write.WritableWorkbook)1 WriteException (jxl.write.WriteException)1