代碼如下:
DataBand1.Sort = new string[2] { "ASC", "Name" };
使用StiComponent.GetActualSize方法,該方法返回組件的尺寸。但是,請注意,如果你想要該方法返回CanGrow屬性的正確數據,CanShrink和AutoWidth屬性必須設置為true。
代碼如下:
C#
StiComponent component = report.GetComponentByName("ComponentName");
VB
Dim Component As StiComponent = Report.GetComponentByName("ComponentName");
使用StiRichText.RtfText屬性。
使用以下代碼:
component.Bookmark = "{Categories.CategoryName}";
Text屬性中的文本表達式在報表渲染時被計算、被保存,將計算出一個結果值后會放置在TextValue中。換句話說就是你可以指定表達式。示例如下:
text1.Text = "Phone: {Customers.Phone}";
在TextValue中,你只可以指定字符串:
text1.TextValue = "123";
如果Text和TextValue一塊被指定,那么TextValue輸出將會被使用。
在該字段你需要指定數據源和數據列。示例如下:
Customers.Photo
注意不能放括號。你也可以通過鏈接進行調用。如下:
Products.Customers.Photo
使用BeforePrintEvent事件,在事件中指定以下代碼:
C#
Image1.Image = Image.FromFile(MyDataSource.ImagePath);
VB
Image1.Image = Image.FromFile(MyDataSource.ImagePath)
你還可以使用ImageData屬性:
C#
{Image.FromFile(MyDataSource.ImagePath)}
VB
{Image.FromFile(MyDataSource.ImagePath)}
ComboBox comboBox = ReportComboBox.Control;
ReportComboBox.Items.Add("123");
使用StiReport類的Script屬性。該屬性包含報表的代碼文本。
使用StiReport類的ScriptNew方法。代碼如下:
C#
StiReport report = new StiReport(); report.ScriptNew();
VB
Dim Report As StiReport = New StiReport() Report.ScriptNew()
使用StiReport類的ScriptUpdate方法來更新報表的代碼。方法如下:
C#
StiReport report = new StiReport(); report.ScriptUpdate();
VB
Dim Report As StiReport = New StiReport() Report.ScriptUpdate()
需注意的是,在報表編譯前,報表代碼會自動更新。因此沒必要調ScriptUpdate方法。
確保屬性窗口可見并且選中(按F4鍵一次或兩次或通過菜單“View” -> “Properties”)。在屬性窗口中選擇組合框中最上面的項目(例如:“MyReport:Report”),將會出現“Referenced Assemblies“屬性,點擊[…]按鈕編輯其值。添加你的程序集,點擊[OK]按鈕。現在你就可以使用報表代碼中自定義程序集中的功能了。
步驟如下:
C#
StiReport report = new StiReport(); report.Load("report.xml"); //Compile the report first report.Compile(); //Initialize the field in a report report.CompiledReport["Parameters"] = OurParameters; report.Render();
VB
Dim Report As StiReport = New StiReport() Report.Load("report.xml") 'Compile the report first Report.Compile() 'Initialize the field in a report Report.CompiledReport("Parameters") = OurParameters Report.Render()
在上面的步驟都完成后你就可以從腳本中訪問你的應用程序字段了。示例:
{Parameters.SomeParameter}
在列名稱前添加一個@符號,示例:@class
使用下面的代碼加載Bitmap格式的圖像:
Image1.Image = Stimulsoft.Base.Drawing.StiImageFromURL.LoadBitmap("http://www.domain.com/bitmap.gif");
使用下面的代碼加載Metafile格式的圖像:
Image1.Image = Stimulsoft.Base.Drawing.StiImageFromURL.LoadMetafile("http://www.domain.com/bitmap.emf");
你還可以使用圖像組件的ImageURL屬性。
你可以從ReportFile屬性獲取以前的文件路徑。示例如下:
string path = report.ReportFile;
使用一種可以在預覽報表時通過點擊事件實現在預覽窗口添加一些X標記的功能。
使用CheckStyle = Cross用于X標記,然后編寫ClickEvent事件處理器:
C#
Stimulsoft.Report.Components.StiCheckBox check = sender as Stimulsoft.Report.Components.StiCheckBox; if (check.CheckedValue == null || ((bool)check.CheckedValue) == false)check.CheckedValue = true; else check.CheckedValue = false; Invalidate();
VB
Dim box1 As Stimulsoft.Report.Components.StiCheckBox = CType(sender, Stimulsoft.Report.Components.StiCheckBox) If ((box1.CheckedValue Is Nothing) OrElse Not CType(box1.CheckedValue, Boolean)) Then box1.CheckedValue = True Else box1.CheckedValue = False End If MyBase.Invalidate
你只需要分配系統變量PageNumber的值即可,例:
PageNumber = 1;
同樣你還可以使用ResetPageNumber屬性。
你可以使用BeforePrintEvent,代碼如下:
C#
if (PageNumber == 1)HeaderBand1.Enabled = false; else HeaderBand1.Enabled = true;
VB
If (MyBase.PageNumber = 1) Then Me.HeaderBand1.Enabled = False Else Me.HeaderBand1.Enabled = True End If
ProcessingDuplicates屬性有以下值:
report.Compile(); report.CompiledReport.GetComponents()["ComponentName"].BeforePrint += new EventHandler(Component_BeforePrint); report.Show();VB
Report.Compile() AddHandler Report.CompiledReport.GetComponents.Item("ComponentName").BeforePrint, New EventHandler(AddressOf Me.Component_BeforePrint) Report.Show()該代碼的處理器處理編譯過的報表的事件。
component.BeforePrintEvent.Script = "MessageBox.Show(\"test\")";VB
Component.BeforePrintEvent.Script = "MessageBox.Show(""test"")"你可以指定事件的腳本,在報表編譯后腳本就會生成一個事件處理器。
報表的頁面存儲在報表的Pages集中。示例如下:
C#
//Returns the first page of a report StiPage page = report.Pages[0];
VB
'Returns the first page of a report Dim Page As StiPage = Report.Pages(0)
你需要將數據帶的高度減小為0。
通常你需要在表達式的基礎上對數據進行排序。計算的結果取決于數據源中的一些字段。例如你已經有一些通過字符串表示的數字,如果不將這些字符串數據源轉換為數字格式就進行排序,那么數據源中的數字順序將不被編譯成數字的自然順序。數字有不同的精度并且被排序為字符串:
1000 20 300
如果更改列的字符串類型為數值型,那么就會被正確排序。但是,如何在不改變數據源類型的情況下進行排序呢?為此,你可以使用Expression作為排序的條件。
{int.Parse(MyDataSource.MyColumn)}
因為不能指定表達式作為排序的條件,那么你需要使用下面的技巧。
將你的排序修改為分組。指定表達式(見下文)作為分組的條件用于分類,將GroupHeaderBnd的高度設置為0。
以上所有步驟都完成后,字符串就會被正確的排序啦。
20 300 1000
注意:在表達式中你可以使用表中的任何行進行排序。
讓我們看看下面的任務。例如,我們需要通過3個表渲染報表:Categories、Products、Order_Details。
我們必須在報告中獲取下一個表—通過Products字符串,按Categories列。Order_Details中的行/列交叉點將要被輸出。 Order_Details利用關系與Categories和Products表關聯。
放置DataBand到頁面上,然后是Products數據源。將CrossTab放置到DataBand并顯示Categories的數據源。在CrossBand放置StiText并顯示Order_Details的字段。滾動Order_Details到需要給StiText組件的OnBeforePrint添加處理程序的行。
Order_Details.First(); while (!Order_Details.IsEof) { if (Order_Details.OrderID == Categories.CategoryID && Order_Details.OrderID == Products.ProductID) break; Order_Details.Next(); }
為什么Order_Details不能通過與Products和Categories已存在的相關性自動滾動到該行?因為數據源被滾動時與Databand相關。因為很多的相關性因此不能在Delphi中滾動。
這種情況下最好的方法是—一個大SQL查詢和一個交叉表。或者是放置在Databand中的一個稍小一點的查詢。
有時你需要格式化RichTextBox對象中的RTF文本。你可以使用的GetValue事件處理程序。對于此操作,你需要創建StiRichText組件。具體代碼如下:
C#
// Create auxiliary StiRichTextBox object StiRichTextBox rich = new StiRichTextBox(); // rtf-text unpacking rich.Rtf = StiRichText.UnpackRtf(System.Xml.XmlConvert.DecodeName(e.Value)); //Select rtf range //Select area of a text for using formatting to it rich.SelectionStart = 0; rich.SelectionLength = 9; //Using lowercase to selected text rich.SelectionCharOffset = -5; // Selected text with center alignment rich.SelectionAlignment = StiRtfSelectionAlign.Center; // Set the color of the selected text rich.SelectionColor = Color.Red; // Set the font for selected text rich.SelectionFont = new Font("Arial", 20, FontStyle.Bold); rich.SelectionStart = 9; rich.SelectionLength = 3; rich.SelectionFont = new Font("Arial", 25); rich.SelectionColor = Color.Blue; rich.SelectionStart = 12; rich.SelectionLength = 11; rich.SelectionFont = new Font("Arial", 20, FontStyle.Bold); //Set selected text in uppercase rich.SelectionCharOffset = 10; // Rtf text packing e.Value = System.Xml.XmlConvert.EncodeName(StiRichText.PackRtf(rich.Rtf)); // Release of resources of additional component rich.Dispose();
VB
Dim box1 As New StiRichTextBox box1.Rtf = StiRichText.UnpackRtf(System.Xml.XmlConvert.DecodeName(e.Value)) box1.SelectionStart = 0 box1.SelectionLength = 9 box1.SelectionCharOffset = -5 box1.SelectionAlignment = StiRtfSelectionAlign.Center box1.SelectionColor = Color.Red box1.SelectionFont = New Font("Arial", 20, FontStyle.Bold) box1.SelectionStart = 9 box1.SelectionLength = 3 box1.SelectionFont = New Font("Arial", 25) box1.SelectionColor = Color.Blue box1.SelectionStart = 12 box1.SelectionLength = 11 box1.SelectionFont = New Font("Arial", 20, FontStyle.Bold) box1.SelectionCharOffset = 10 e.Value = System.Xml.XmlConvert.EncodeName(StiRichText.PackRtf(box1.Rtf)) box1.Dispose
下圖顯示在StiRichText組件創建之前,GetValue處理器設置之后:
RichText組件的屬性如下表:
屬性 | 功能 |
SelectionAlignment | 控制RTF的文本對齊。 |
SelectionHangingIndent | 設置SelectionHangingIndent屬性來表示文本段落中的第一行的左邊緣和在同一段落后續行的左邊緣之間的像素間距的整數。 |
SelectionIndent | 整數值。設置在控制的左邊緣與文本的左邊緣之間的像素間距。設置SelectionHangingIndent屬性來表示文本段落中的第一行的左邊緣和在 同一段落后續行的左邊緣之間的像素間距的整數。SelectionHangingIndent屬性的值僅適用于第一行下面環繞的段落行。 |
SelectionRightIndent | 將SelectionRightIndent屬性設置為整數代表在控制的右邊緣與文本的右邊緣之間的像素間距。 |
在RichText的GetValueEvent事件寫入以下代碼:
C#
System.IO.StreamReader reader = new System.IO.StreamReader("d:\\script.rtf"); string str = reader.ReadToEnd(); reader.Close(); e.Value = System.Xml.XmlConvert.EncodeName(StiRichText.PackRtf(str));
VB
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader("d:\\script.rtf") Dim Str As String = Reader.ReadToEnd() Reader.Close() e.Value = System.Xml.XmlConvert.EncodeName(StiRichText.PackRtf(Str))
Stimulsoft DLL不支持模糊或嵌入。
一個多頁的報告可能會很長,通常需要一個目錄表,讓讀者很容易尋找信息。不幸的是目錄表的頁碼是未知的,直到報表已經被渲染,因此這是一個相當復雜的任務。但是,使用變量和Stimulsoft Reports的頁面BeginRender、EndRender事件是完全可能實現這樣的自動目錄表的:
使用這種方法目錄對應的頁碼將根據報表的頁碼自動計算——如果一個頁面擴展到多頁,那么頁碼會自動調整以顯示每個章節的正確起始頁。一旦你創建你的目錄頁,并添加必要的文字組件,那么你的報表生成該功能的步驟如下:
創建一個Flag變量
第一步是在報表字典中創建一個名為'flag'的布爾變量:
該標志將被用于跟蹤一個頁面錨是否應該被添加到當前頁面。
注意: 該變量名是不是強制性的,你可以根據自己的需求進行修改。
設置標簽
設置用于輸出頁碼的每個文本組件的Tag屬性(位于對象檢查Interaction的下方)為顯示在目錄表的名稱。在上面的例子中第一頁包含“DirectorsOfficersAndAdvisers',因此這將是其標簽值。重復此過程,每增加一頁,被列入目錄表中。
設置文本表達式
為了在目錄表中獲得相應的頁碼,我們將使用GetAnchorPageNumber方法的文本表達式。將每一個輸出頁碼的文本組件設置表達式:
{GetAnchorPageNumber(sender.TagValue)}
與設置標記時不同,你可以為每個文本組件使用完全相同的表達式。
給BeginRender事件添加代碼
通過點擊對象檢查中事件旁邊的省略號為每個頁面打開用于BeginRender的事件編輯器,然后添加如下代碼:
為報告中的每個頁面添加相同的代碼,只是AddAnchor調用應進行修改,以便在每種情況下將相關頁面的名稱顯示為參數。
給EndRender事件添加代碼
通過點擊對象檢查中事件旁邊的省略號為每個頁面打開用于EndRender的事件編輯器,然后添加如下代碼:
flag = true;
同樣你可以為每一個頁面使用相同的表達式。現在你就可以打印或預覽報表,并且目錄表的頁碼將會被正確的計算。
打破零回復...
登錄 慧都網發表評論