Class JxlsWorkbook

  • All Implemented Interfaces:
    JxlsConstants

    public class JxlsWorkbook
    extends java.lang.Object
    implements JxlsConstants
    JxlsWorkbook: a class to wrap and simplify the use of Apache POI Workbook in the context of OpenXava JxlsWorkbook's can be created: - empty JxlsWorkbook wb = new JxlsWorkbook("Test"); - from a TableModel JxlsWorkbook wb = new JxlsWorkbook(tableModel, "Test"); - from an xls file JxlsWorkbook wb = new JxlsWorkbook(xlsFile); Usage: JxlsWorkbook wb = new JxlsWorkbook("Test"); JxlsSheet sheet = wb.addSheet("Test"); sheet.setValue(3, 4, "Pi", wb.addStyle(TEXT).setAlign(CENTER).setBold()); sheet.setValue(4, 4, 3.141592654, wb.addStyle(FLOAT).setAllBorders(THIN_BORDER)); sheet.setFormula(4, 5, "=2*$R4$C4", wb.addStyle("##0.0000")); sheet.setFormula(4, 6, "=2*R4C4", wb.addStyle("##0.000")); wb.write(new FileOutputStream("c:/Test.xls")); Use of POI more advanced functionalities JxlsWorkbook wb = new JxlsWorkbook("Test"); JxlsSheet sheet = wb.addSheet("Test"); sheet.setValue(3, 4, "Pi", wb.addStyle(TEXT).setAlign(CENTER).setBold()); Workbook poiWorkbook = wb.createPOIWorkbook(); // do an advanced function poiWorkbook.write(new FileOutputStream("c:/Test.xls"));
    Author:
    Laurent Wibaux
    • Field Detail

      • styles

        protected java.util.Map<java.lang.String,​JxlsStyle> styles
      • fonts

        protected java.util.Map<java.lang.String,​org.apache.poi.ss.usermodel.Font> fonts
      • sheets

        protected java.util.Vector<JxlsSheet> sheets
      • sheetNames

        protected java.util.Map<java.lang.String,​JxlsSheet> sheetNames
      • poiWorkbook

        protected org.apache.poi.ss.usermodel.Workbook poiWorkbook
    • Constructor Detail

      • JxlsWorkbook

        public JxlsWorkbook​(java.lang.String name)
        Constructs an empty JxlsWorkbook
        Parameters:
        name - : the name of the JxlsWorkbook
      • JxlsWorkbook

        public JxlsWorkbook​(javax.swing.table.TableModel table,
                            java.lang.String name)
        Constructs a JxlsWorkbook containing the data of the table
        Parameters:
        table - : a Swing table model
        name - : the name of the Workbook
      • JxlsWorkbook

        public JxlsWorkbook​(java.io.File xlsFile)
        Constructs a JxlsWorkbook and fills it with the raw data of the input file styles are not copied
        Parameters:
        xlsFile - : an xls file
    • Method Detail

      • getFontName

        public java.lang.String getFontName()
      • setFontName

        public void setFontName​(java.lang.String fontName)
        Sets the default font to use when generating the xls
        Parameters:
        fontName - : the name of the font
      • getFontSize

        public short getFontSize()
      • setFontSize

        public void setFontSize​(short fontSize)
        Sets the default font size to use when generating the xls
        Parameters:
        fontSize - : the point size of the font
      • getFloatFormat

        public java.lang.String getFloatFormat()
      • setFloatFormat

        public void setFloatFormat​(java.lang.String floatFormat)
        Sets the default format to use for numbers when generating the xls This should be in the form "###.0"
        Parameters:
        floatFormat - : the format
      • getIntegerFormat

        public java.lang.String getIntegerFormat()
      • setIntegerFormat

        public void setIntegerFormat​(java.lang.String integerFormat)
        Sets the default format to use for integers when generating the xls This should be in the form "### ###"
        Parameters:
        integerFormat - : the format
      • getDateFormat

        public java.lang.String getDateFormat()
      • setDateFormat

        public void setDateFormat​(java.lang.String dateFormat)
        Sets the default format to use for integers when generating the xls This should be in the form "dd/MM/yy"
        Parameters:
        dateFormat - : the format
      • getDefaultStyle

        public JxlsStyle getDefaultStyle()
      • getDefaultDateStyle

        public JxlsStyle getDefaultDateStyle()
      • getDefaultFloatStyle

        public JxlsStyle getDefaultFloatStyle()
      • addStyle

        public JxlsStyle addStyle​(int type)
      • addStyle

        public JxlsStyle addStyle​(java.lang.String format)
      • addStyle

        public JxlsStyle addStyle​(java.lang.String name,
                                  int type)
        Creates a JxlsStyle which can be referenced when using sheet.setValue(column, row, value, style)
        Parameters:
        name - the name to use when recalling the style
        type - the type of value managed by the style (one of: TEXT, INTEGER, FLOAT, DATE)
        Returns:
        the JxlsStyle
      • addStyle

        public JxlsStyle addStyle​(java.lang.String name,
                                  java.lang.String format)
        Creates a JxlsStyle which can be referenced when using sheet.setValue(column, row, value, style)
        Parameters:
        name - the name to use when recalling the style
        format - the format the style should use ("#.0%")
        Returns:
        the JxlsStyle
      • addClonedStyle

        public JxlsStyle addClonedStyle​(java.lang.String name,
                                        JxlsStyle style)
        Creates a JxlsStyle based upon another existing JxlsStyle which can be referenced when using sheet.setValue(column, row, value, style)
        Parameters:
        name - the name to use when recalling the style
        style - the style to clone
        Returns:
        the cloned JxlsStyle
      • getStyle

        public JxlsStyle getStyle​(java.lang.String name)
      • addSheet

        public JxlsSheet addSheet​(java.lang.String name)
        Creates a JxlsSheet in which cells can be added
        Parameters:
        name - the name which will appear on the tab
        Returns:
        the JxlsSheet
      • addSheet

        public JxlsSheet addSheet​(java.lang.String name,
                                  int index)
        Creates a JxlsSheet in which cells can be added
        Parameters:
        name - the name which will appear on the tab
        index - the index at which the sheet should appear in the tab list
        Returns:
        the JxlsSheet
      • deleteSheet

        public void deleteSheet​(int index)
        Deletes a JxlsSheet
        Parameters:
        index - the index of the tab to delete
      • getSheet

        public JxlsSheet getSheet​(int index)
        Gets a sheet through its index
        Parameters:
        index - the index of the sheet
        Returns:
        the JxlsSheet
      • getSheet

        public JxlsSheet getSheet​(java.lang.String name)
        Gets a sheet through its name
        Parameters:
        name - the name of the sheet
        Returns:
        the JxlsSheet
      • getSheets

        public java.util.Vector<JxlsSheet> getSheets()
        Gets the list of sheets
        Returns:
        a Vector
      • getSheetsMap

        public java.util.Map<java.lang.String,​JxlsSheet> getSheetsMap()
        Gets a map containing the sheets mapped by name
        Returns:
        a Map
      • deletePOIWorkbook

        public void deletePOIWorkbook()
        Resets the Apache POI Workbook from the JxlsWorkbok to regenerate the Workbook on next createPOIWorkbook
      • createPOIWorkbook

        public org.apache.poi.ss.usermodel.Workbook createPOIWorkbook()
        Creates an apache POI Workbook from the JxlsWorkbok
        Returns:
        a Workbook
      • write

        public void write​(java.io.OutputStream os)
                   throws java.lang.Exception
        Writes the xls to an OutputStream
        Parameters:
        os - the OutputStream to write to
        Throws:
        java.lang.Exception - if the OutputStream can not be opened
      • write

        public void write​(javax.servlet.http.HttpServletResponse response)
                   throws java.lang.Exception
        Writes the xls to an HttpServletResponse, setting the correct mime type and headers
        Parameters:
        response - the HttpServletResponse to write to
        Throws:
        java.lang.Exception - if the HttpServletResponse can not be written
      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception