Example usage for org.apache.poi.ss.examples.html ToHtml printStyles

List of usage examples for org.apache.poi.ss.examples.html ToHtml printStyles

Introduction

In this page you can find the example usage for org.apache.poi.ss.examples.html ToHtml printStyles.

Prototype

public void printStyles() 

Source Link

Usage

From source file:de.teststory.jspwiki.worksheetplugin.WorksheetPlugin.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/* w ww.j  ava  2s.co  m*/
public String execute(WikiContext context, Map params) throws PluginException {
    log.info("WorksheetPlugin executed");
    try {
        // Analyze Parameters
        String src = getCleanParameter(params, PARAM_SRC);
        if (src == null) {
            throw new PluginException("Parameter '" + PARAM_SRC + "' is required for Workbook plugin");
        }
        int worksheetId = -1;
        String s = getCleanParameter(params, PARAM_WORKSHEET_ID);
        if (s != null) {
            try {
                worksheetId = Integer.valueOf(s).intValue();
            } catch (NumberFormatException e) {
                throw new PluginException("Parameter '" + PARAM_WORKSHEET_ID + "' must be numeric: " + s);
            }
        }
        String worksheetName = getCleanParameter(params, PARAM_WORKSHEET_NAME); // may be null

        // Load the workbook
        WikiEngine engine = context.getEngine();
        AttachmentManager mgr = engine.getAttachmentManager();
        Attachment att = mgr.getAttachmentInfo(context, src);
        if (att == null) {
            throw new PluginException("Attachment '" + src + "' not found.");
        }
        InputStream inStream = mgr.getAttachmentStream(context, att);
        Workbook wb = WorkbookFactory.create(inStream);
        if (wb == null) {
            throw new PluginException("Could not load Workbook '" + src + "'");
        }

        // find worksheet
        Sheet sheet = findSheet(wb, worksheetId, worksheetName);
        if (sheet == null) {
            throw new PluginException(
                    "Could not find Worksheet. Index=" + worksheetId + ", name='" + worksheetName + "'");
        }

        StringBuilder sb = new StringBuilder();
        ToHtml toHtml = ToHtml.create(wb, sb);
        sb.append("<style type=\"text/css\">");
        toHtml.printStyles();
        sb.append("</style>");
        toHtml.printSheet(sheet);
        return sb.toString();

    } catch (ProviderException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage(), e);
    } catch (EncryptedDocumentException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage(), e);
    } catch (InvalidFormatException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage(), e);
    }
}