如何使用c#和rdl对象模型将新元素插入到ssrsrdl对象中

本文关键字:插入 元素 ssrsrdl 对象 何使用 rdl 对象模型 新元素 | 更新日期: 2023-09-27 18:21:35

我正在使用从rdl模式生成的对象模型以编程方式创建rdl。我首先将一个现有的rdl模板加载到文件流中,该模板包含页眉、页脚和正文以及数据源和数据集部分。

string docpath = "RDLTemplates/TemplateLetter.rdl";
Report myReport = null;
using (FileStream fs = new FileStream(docpath, FileMode.Open))
{
    XmlSerializer serializer = new XmlSerializer(typeof(Report));
    myReport = (Report)serializer.Deserialize(fs);
}
return myReport;

然后,我将其传递给InsertTablix()方法,在该方法中,我在流中导航以定位主体部分

 public Report InsertTablix(Report report, List<string> fieldnames)
        {
            int sectionindex = report.ItemsElementName.ToList().IndexOf(ItemsChoiceType118.ReportSections);
            if (sectionindex != -1)
            {
                ReportSectionsType sections = (ReportSectionsType)report.Items[sectionindex];
                if (sections.ReportSection.Length > 0)
                {  
                    int bodyIndex = sections.ReportSection[0].ItemsElementName.ToList().IndexOf(ItemsChoiceType117.Body);
                    if (bodyIndex != -1)
                    {
                        BodyType body = (BodyType)sections.ReportSection[0].Items[bodyIndex];
                        int tablixindex = body.Items.ToList().IndexOf(ItemsChoiceType80.Tablix);
                        if (tablixindex == -1)
                        {
                           //Here is where I would like to insert the tablix if it doesn't exist  

                        }
                    }
                }
            }
            return report;
        } 

我遇到的问题是如何使用对象模型创建/插入一个tablix。我意识到我可以使用XML来创建报告,我已经尝试过这种方法,但我想尝试使用ObjectModel来帮助遵守和验证报告模式。

有人能提供一些关于如何使用这种方法将元素(tablix、textbox等)添加到主体中的见解吗。

-cheeers

如何使用c#和rdl对象模型将新元素插入到ssrsrdl对象中

我正在使用RDLObjectModel来做同样的事情,并花了一点时间来弄清楚。这是我的测试代码(不是很漂亮,在VB.NET中),它能正确插入tablix。也许你会发现这很有用。

'create tablix - manually
Dim tablixRFoo As New RdlObjectModel.Tablix With {.Top = New RdlObjectModel.ReportSize("0.56875in"), .Width = New RdlObjectModel.ReportSize("0.5in")}
tablixRFoo.Name = "tablix1"
tablixRFoo.DataSetName = dsRFoo.Name
'add tablix columns
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})

Dim tColHeirarcy = New RdlObjectModel.TablixHierarchy
Dim tRowHeirarcy = New RdlObjectModel.TablixHierarchy

    Dim fields() = {"field1", "field2", "field3", "field4", "field5"}
    'add tablix header row
    Dim tRowHeader = New RdlObjectModel.TablixRow With {.Height = New RdlObjectModel.ReportSize(0.2)}
    For i As Integer = 0 To fields.Length - 1
        Dim tCell = New RdlObjectModel.TablixCell
        Dim tCellContents = New RdlObjectModel.CellContents
        Dim tTextBox As New RdlObjectModel.Textbox
        tTextBox.Name = tablixRFoo.Name & "Header" & "txt" & fields(i).ToString.Replace(" ", "")
        tTextBox.DefaultName = tTextBox.Name
        tTextBox.Paragraphs(0).TextRuns(0).Value = fields(i).ToString
        tTextBox.Paragraphs(0).TextRuns(0).Style.FontSize = New RdlObjectModel.ReportSize("8pt")
        tTextBox.Paragraphs(0).TextRuns(0).Style.TextAlign = RdlObjectModel.TextAlignments.Left
        tTextBox.ZIndex = i
        tCellContents.ReportItem = tTextBox
        tCell.CellContents = tCellContents
        tRowHeader.TablixCells.Add(tCell)
        tColHeirarcy.TablixMembers.Add(New RdlObjectModel.TablixMember)
    Next
    tablixRFoo.TablixBody.TablixRows.Add(tRowHeader)
    'add tablix data rows
    Dim tRow = New RdlObjectModel.TablixRow With {.Height = New RdlObjectModel.ReportSize(0.2)}

    For i As Integer = 0 To fields.Length - 1
        Dim tCell = New RdlObjectModel.TablixCell
        Dim tCellContents = New RdlObjectModel.CellContents
        Dim tTextBox As New RdlObjectModel.Textbox
        tTextBox.Name = tablixRFoo.Name & "txt" & fields(i).ToString.Replace(" ", "")
        tTextBox.DefaultName = tTextBox.Name
        tTextBox.Paragraphs(0).TextRuns(0).Value = "=Fields!" & fields(i).ToString & ".Value"
        tTextBox.Paragraphs(0).TextRuns(0).Style.FontSize = New RdlObjectModel.ReportSize("8pt")
        tTextBox.Paragraphs(0).TextRuns(0).Style.TextAlign = RdlObjectModel.TextAlignments.Left
        tTextBox.ZIndex = i

        tCellContents.ReportItem = tTextBox
        tCell.CellContents = tCellContents
        tRow.TablixCells.Add(tCell)
    Next
    tablixRFoo.TablixBody.TablixRows.Add(tRow)
    Dim rMem = New RdlObjectModel.TablixMember With {.KeepTogether = True}
    Dim rMem2 = New RdlObjectModel.TablixMember With {.Group = New RdlObjectModel.Group With {.Name = "Details"}}
    tRowHeirarcy.TablixMembers.Add(rMem)
    tRowHeirarcy.TablixMembers.Add(rMem2)
    tablixRFoo.Style = New RdlObjectModel.Style With {.Border = New RdlObjectModel.Border}
    'add tablix to report
    tablixRFoo.TablixColumnHierarchy = tColHeirarcy
    tablixRFoo.TablixRowHierarchy = tRowHeirarcy
    rdlRpt.Body.ReportItems.Add(tablixRFoo)