缺少表格总计行中的值和格式

时间:2014-06-20 08:42:58

标签: c# export-to-excel epplus


我正在通过EPPLUS和C#创建一个excel报告。到目前为止,几乎所有工作都有效,但我在excel(2010)中打开报告时遇到了两个问题。

问题1: 在文档启用编辑之前,表的totalsrow不显示值 问题2: totalsrow未格式化,并且不会将货币符号添加到值中,直到在Excel中更改或重新选择总行功能。

我不知道这些问题是否可以在代码中解决。但也许有人可以在我的代码中找到错误或遇到相同的问题并找到解决方案或解决方法。

    private void ExcelReport()
    {
        DataTable ObjekteList = GetObjektExcelList();
        ObjekteList.TableName = "Objekte";
        ExcelPackage pck = new ExcelPackage();
        var ws = pck.Workbook.Worksheets.Add("objektstatistik" + DateTime.Today.ToShortDateString());
        var range = ws.Cells["A1"].LoadFromDataTable(ObjekteList, true, OfficeOpenXml.Table.TableStyles.Medium2);
        var numStyle = pck.Workbook.Styles.CreateNamedStyle("TableCurrency");
        numStyle.Style.Numberformat.Format = @"#,##0.00 €";
        var tbl = ws.Tables[0];
        tbl.ShowTotal = true;
        tbl.Columns[0].TotalsRowLabel = "Total";
        tbl.Columns[11].TotalsRowFunction = RowFunctions.Sum;
        tbl.Columns[11].DataCellStyleName = "TableCurrency";
        for (int i = 2; i <= range.Rows; i++)
        {
            ws.Row(i).CustomHeight = true;
            ws.Row(i).Height = 20;
            switch (ws.Cells[i, range.Columns].Value.ToString())
            {
                case "1":
                    ws.Cells[i, 1, i, range.Columns].Style.Fill.PatternType = ExcelFillStyle.Solid;
                    ws.Cells[i, 1, i, range.Columns].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Bottom.Color.SetColor(Color.FromArgb(160, 165, 255));
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Right.Style = ExcelBorderStyle.Thin;
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Right.Color.SetColor(Color.FromArgb(160, 165, 255));
                    break;
                default:
                    ws.Cells[i, 1, i, range.Columns].Style.Fill.PatternType = ExcelFillStyle.Solid;
                    ws.Cells[i, 1, i, range.Columns].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 255));
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Bottom.Color.SetColor(Color.FromArgb(160, 165, 255));
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Right.Style = ExcelBorderStyle.Thin;
                    ws.Cells[i, 1, i, range.Columns].Style.Border.Right.Color.SetColor(Color.FromArgb(160, 165, 255));
                    break;
            }
        }
        ws.Cells[1, 1, 1, range.Columns].Style.Border.BorderAround(ExcelBorderStyle.Medium, Color.FromArgb(160, 165, 255));
        ws.Row(1).CustomHeight = true;
        ws.Row(1).Height = 30;
        ws.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        ws.Row(1).Style.VerticalAlignment = ExcelVerticalAlignment.Center;
        ws.Cells[range.Rows + 1, 12, range.Rows + 1, 16].Style.Fill.PatternType = ExcelFillStyle.Solid;
        ws.Cells[range.Rows + 1, 20].Style.Fill.PatternType = ExcelFillStyle.Solid;
        ws.Cells[range.Rows + 1, 12, range.Rows + 1, 16].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 255));
        ws.Cells[range.Rows + 1, 20].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 255));
        ws.Cells[range.Rows + 1, 1, range.Rows + 1, range.Columns].Style.Border.BorderAround(ExcelBorderStyle.Medium, Color.FromArgb(184, 204, 255));
        ws.Cells[range.Rows + 1, 1, range.Rows + 1, range.Columns].Style.Border.Top.Style = ExcelBorderStyle.Double;
        ws.Cells[range.Rows + 1, 1, range.Rows + 1, range.Columns].Style.Border.Top.Color.SetColor(Color.FromArgb(160, 165, 255));
        ws.Cells[1, 1, range.Rows + 1, range.Columns].Style.Border.BorderAround(ExcelBorderStyle.Medium, Color.FromArgb(184, 204, 255));
        ws.View.ShowGridLines = true;
        ws.Calculate();
        ws.Cells[ws.Dimension.Address].AutoFitColumns();
        ws.View.FreezePanes(1, 5);
        ws.Column(6).Hidden = true;
        ws.Column(15).Hidden = true;
        ws.Workbook.CalcMode = ExcelCalcMode.Automatic;
        pck.SaveAs(Response.OutputStream);
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=objektstatistik" + DateTime.Today.ToShortDateString() + ".xlsx");
        Response.End();
    }

0 个答案:

没有答案