'Method to output as XLS
@ToDo: Finish & use HTML2XLS method in XLS codec to be DRY & reusable'

@staticmethod
def _xls(data):

    try:
        import xlwt
    except ImportError:
        if (current.auth.permission.format in S3Request.INTERACTIVE_FORMATS):
            current.session.error = S3XLS.ERROR.XLWT_ERROR
            redirect(URL(extension=''))
        else:
            error = S3XLS.ERROR.XLWT_ERROR
            current.log.error(error)
            return error
    (indicators, orgs, consolidated) = data
    title = 'BOCA'
    book = xlwt.Workbook(encoding='utf-8')
    sheet_name = title
    if (len(sheet_name) > 31):
        sheet_name = sheet_name[:31]
    sheet1 = book.add_sheet(sheet_name)
    styleHeader = xlwt.XFStyle()
    styleHeader.font.bold = True
    styleHeader.pattern.pattern = styleHeader.pattern.SOLID_PATTERN
    styleHeader.pattern.pattern_fore_colour = 44
    sheet1.write_merge(0, 1, 0, 0, 'TOPICS', styleHeader)
    sheet1.write_merge(0, 0, 1, 6, 'Consolidated    Ratings', styleHeader)
    sheet1.row(1).write(1, 'A', styleHeader)
    sheet1.row(1).write(2, 'B', styleHeader)
    sheet1.row(1).write(3, 'C', styleHeader)
    sheet1.row(1).write(4, 'D', styleHeader)
    sheet1.row(1).write(5, 'E', styleHeader)
    sheet1.row(1).write(6, 'F', styleHeader)
    orepresent = org_OrganisationRepresent(parent=False, acronym=False)
    col = 7
    for o in orgs:
        sheet1.row(1).write(col, orepresent(o), styleHeader)
        col += 1
    styleSection = xlwt.XFStyle()
    styleSection.font.bold = True
    styleSection.pattern.pattern = styleSection.pattern.SOLID_PATTERN
    styleSection.pattern.pattern_fore_colour = 47
    row = 3
    section = None
    max_width = 0
    for i in indicators:
        if (i.section != section):
            section = i.section
            sheet1.row(row).write(0, section, styleSection)
            if (len(section) > max_width):
                max_width = len(section)
            row += 1
        sheet1.row(row).write(0, ('%s.    %s' % (i.number, i.name)))
        indicator_id = i.id
        values = consolidated[indicator_id]
        col = 1
        for v in ('A', 'B', 'C', 'D', 'E', 'F'):
            sheet1.row(row).write(col, values[v])
            col += 1
        for o in orgs:
            rating = orgs[o].get(indicator_id, '')
            sheet1.row(row).write(col, rating)
            col += 1
        row += 1
    COL_WIDTH_MULTIPLIER = 310
    width = max((max_width * COL_WIDTH_MULTIPLIER), 2000)
    width = min(width, 65535)
    sheet1.col(0).width = width
    try:
        from cStringIO import StringIO
    except:
        from StringIO import StringIO
    output = StringIO()
    book.save(output)
    from gluon.contenttype import contenttype
    filename = ('%s.xls' % title)
    disposition = ('attachment;    filename="%s"' % filename)
    response = current.response
    response.headers['Content-Type'] = contenttype('.xls')
    response.headers['Content-disposition'] = disposition
    output.seek(0)
    return output.read()
