Beispiel #1
0
    def _get_header_styles(self):
        s1 = XFStyle()
        al = Alignment()
        al.horz = Alignment.HORZ_CENTER
        s1.alignment = al

        s2 = XFStyle()
        borders = Borders()
        borders.bottom = 2
        s2.borders = borders

        return s1, s2
Beispiel #2
0
border.top = border.THIN
border.bottom = border.THIN

font = Font()
font.bold = True

alignment = Alignment()
alignment.horz = alignment.HORZ_CENTER

pattern = Pattern()
pattern.pattern = pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 0x35

title_style.borders = border
title_style.font = font
title_style.alignment = alignment
title_style.pattern = pattern

for i in range(len(title)):
    sheet.write(4, i + 4, title[i], title_style)

content = [[1, 'SMITH', '测试', '蔡昶', '2020-10-10'],
           [2, '凤姐', '开发', '蔡昶', '2020-10-11']]

content_style = XFStyle()
border = Borders()
border.left = border.THIN
border.right = border.THIN
border.top = border.THIN
border.bottom = border.THIN
Beispiel #3
0
border.top = border.THIN
border.bottom = border.THIN

font = Font()
font.blod = True

alignment = Alignment()
alignment.horz = alignment.HORZ_CENTER

pattern = Pattern()
pattern.pattern = pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 0x35

title_style.borders = border
title_style.font = font
title_style.alignment = alignment
title_style.pattern = pattern

for i in range(len(title)):
    sheet.write(4, 4 + i, title[i], title_style)

content = [[7369, 'SMITH', 'CLERK', 7902, '12/17/1980'],
           [7499, 'ALLEN', 'SALESMAN', 7698, '2/20/1981'],
           [7521, 'WARD', 'SALESMAN', 7698, '2/22/1981'],
           [7566, 'JONES', 'MANAGER', 7839, '4/2/1981'],
           [7654, 'MARTIN', 'SALESMAN', 7698, '9/28/1981']]
content_style = XFStyle()
# border=Borders()
# border.left=border.THIN
# border.right=border.THIN
# border.top=border.THIN
Beispiel #4
0
def template_download(request):
    """
    导出模板数据到excel
    """
    pk = request.GET.get('pk', '')
    business_name = request.GET.get('business_name', '')
    template_name = request.GET.get('template_name', '')
    flag = request.GET.get('flag', '')
    res_json = {'result': True, 'isNotPass': True, 'data': u""}
    if pk or flag == Flag.EDIT or flag == Flag.TASK or flag == 'False':
        business = BUSINESS_MODEL['template']
        cc_name = business_name
    elif flag == Flag.BUSINESS:
        business = BUSINESS_MODEL['instance']
        cc_name = business_name.split('_')[0]
    else:
        res_json['data'] = u'参数错误'
        return render_mako_context(request, '/manager/403.html', res_json)

    # 获取对应的模版并校验权限
    try:
        if pk:
            template = business.objects.get(id=pk)
            Application.objects.get(cc_name=template.business_name,
                                    operator__username=request.user.username)
        else:
            template = business.objects.get(business_name=business_name,
                                            template_name=template_name)
            Application.objects.get(cc_name=cc_name,
                                    operator__username=request.user.username)
    except ObjectDoesNotExist:
        res_json['data'] = u'您无操作权限,请联系系统管理员'
        return render_mako_context(request, '/manager/403.html', res_json)

    # 表格初始化
    response = HttpResponse(content_type="application/vnd.ms-excel")
    response['Content-Disposition'] = 'attachment; filename="%s_%s.xls"' % (
        template.business_name.encode('utf-8'),
        template.business_type.encode('utf-8'))
    wbk = xlwt.Workbook(encoding='utf-8')
    sheet = wbk.add_sheet('sheet1')
    sheet.col(2).width = 20000
    sheet.col(3).width = 20000

    # 枚举参数初始化
    if flag == Flag.EDIT or flag == Flag.TASK or flag == 'False':
        head_list = HeadList.TEMPLATE
        excel_list = ExcelList.TEMPLATE
        business_step = BUSINESS_STEP_MODEL["template"]
        business_detail = BUSINESS_DETAIL_MODEL['template']
        q = Q()
    else:
        q = ~Q(stat=1)
        head_list = HeadList.INSTANCE
        excel_list = ExcelList.INSTANCE
        business_step = BUSINESS_STEP_MODEL['instance']
        business_detail = BUSINESS_DETAIL_MODEL['instance']
    try:
        for i, data in enumerate(head_list):
            sheet.write(0, i, data)
        row = 1
        algn_center_hv = Alignment()
        algn_center_hv.horz = Alignment.HORZ_CENTER
        algn_center_hv.vert = Alignment.VERT_CENTER
        style_cdata = XFStyle()
        style_cdata.alignment = algn_center_hv

        # 获取步骤详情并填充表格
        steps = business_step.objects.filter(business=template).order_by('id')
        for step in steps:
            details = business_detail.objects.filter(
                q, business_step=step).order_by('step_xh')
            sheet.write_merge(row, row + details.count() - 1, 0, 0, step.name,
                              style_cdata)
            details = business_detail.to_dict(details)
            for detail in details:
                for i, value_key in enumerate(excel_list):
                    sheet.write(row, i + 1, detail[value_key])
                row += 1
        wbk.save(response)
        return response
    except Exception, e:
        logger.error(u'下载模版失败%s' % e)
        res_json['data'] = u'下载模版失败'
        return render_mako_context(request, '/manager/403.html', res_json)