【问题标题】:Generate proper report when data is not in database [Telerik]当数据不在数据库中时生成正确的报告 [Telerik]
【发布时间】:2015-04-21 07:57:35
【问题描述】:

我正在生成 Telerik 报告 "按教育程度、领域和性别划分的学生人数"

这里是我用来创建此报告的 SQL 查询

SELECT
    [tbl_hec_ISCED].[ISCED_ID], 
    [tbl_hec_ISCED].[ISCED_Level], 
    [tbl_hec_Programme].[ISCED_ID] AS 'tbl_hec_ProgrammeISCED_ID', 
    [tbl_hec_Programme].[Programme_ID], 
    [tbl_hec_Programme].[Specialisation_ID_Number], 
    [tbl_hec_specialisation].[Rank_ID_Number], 
    [tbl_hec_specialisation].[Rank_Title], 
    [tbl_HEI_student].[Programme_ID] AS 'tbl_HEI_studentProgramme_ID', 
    [tbl_HEI_student].[Gender]
FROM ((([tbl_HEI_student]
 FULL OUTER JOIN [tbl_hec_Programme]
 ON [tbl_HEI_student].[Programme_ID] = [tbl_hec_Programme].[Programme_ID])
 FULL OUTER JOIN [tbl_hec_specialisation]
 ON [tbl_hec_Programme].[Specialisation_ID_Number] = [tbl_hec_specialisation].[Rank_ID_Number])
 FULL OUTER JOIN [tbl_hec_ISCED]
 ON [tbl_hec_Programme].[ISCED_ID] = [tbl_hec_ISCED].[ISCED_ID])
WHERE ([tbl_HEI_student].[Gender]='Male' or [tbl_HEI_student].[Gender]='Female') and ([tbl_hec_ISCED].[ISCED_Level]='5' or [tbl_hec_ISCED].[ISCED_Level]='6'or [tbl_hec_ISCED].[ISCED_Level]='7'or [tbl_hec_ISCED].[ISCED_Level]='8')

我收到空报告,因为某些值不在数据库中。我附上它的图片,

这里是视图

我想在数据库中没有数据时生成报告。如下所示,这意味着空行的零值。

这里是预期的报告输出

我该如何克服这个挑战

【问题讨论】:

  • 对于这种报告,我认为使用带有占位符的模板文档,然后使用代码中的计算值设置它们会更容易。这适用于您吗?如果需要,我可以提供一些示例代码。
  • 是的,请您在这里提及它作为答案,我会尝试整合,
  • 你是怎么做到的?已经一年了。如果我的回答对您有帮助,请将其标记为已接受的答案。或者至少放置 cmets 或对其进行投票,以便它可以帮助其他有类似问题的人。

标签: sql telerik telerik-grid telerik-mvc telerik-reporting


【解决方案1】:

我的建议是使用已设置所有报表布局以及将被计算值替换的值占位符(字段)的模板来使用文档合并功能。

我在我的项目中使用 Aspose.Words 库,但我确信还有其他库,甚至可能还有 Telerik Reporting。这是一个非常简单的功能,因此任何可以执行复杂操作的报告工具也必须能够执行此操作。

这是 Aspose 的一些示例代码。其他库会有不同的实现。

using Aspose.Words;

void GenerateDocument(string templateFilePath, Dictionary<string, object> fieldNamesAndValues) 
{
    // This is our document object
    Document output = null;

    // Obtain the template file
    if (File.Exists(templateFilePath))
    {
        // If the template file is successfully located, use this template
        output = new Document(templateFilePath);
    }

    // Merge the provided values into the appropriate fields of the template
    output.MailMerge.Execute(fieldNamesAndValues.Keys.ToArray(), fieldNamesAndValues.Values.ToArray());

    // Save the document into a stream as PDF
    MemoryStream stream = new MemoryStream();
    doc.Save(stream, SaveFormat.Pdf);

    // You can then do whatever with the stream: 
    // save it or push it to the browser for download
}

以您的预期结果为例,假设这些是第一行的占位符(字段)的名称:

MALE_EDUCATION_ISCED5, MALE_EDUCATION_ISCED6, MALE_EDUCATION_ISCED7

然后您可以像这样生成报告:

Dictionary<string, object> fieldsAndValues = new Dictionary<string, object>();

fieldsAndValues.Add("MALE_EDUCATION_ISCED5", calculatedValue1);
fieldsAndValues.Add("MALE_EDUCATION_ISCED6", calculatedValue2);
fieldsAndValues.Add("MALE_EDUCATION_ISCED7", calculatedValue3);
// and so on for other fields

GenerateDocument("~/Templates/Report.docx", fieldsAndValues);

有关如何在 Microsoft Word 中添加字段的更多信息: https://support.office.com/en-us/article/7e9ea3b4-83ec-4203-9e66-4efc027f2cf3

有关 Aspose MailMerge 的更多信息: http://www.aspose.com/docs/display/wordsnet/How+to++Execute+Simple+Mail+Merge

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多