本文說明如何使用MyReport來實現Flex DataGrid組件的自動化打印預覽和打印功能。
實現代碼
<?xmlversion="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%"height="100%" fontSize="24" horizontalAlign="center" paddingBottom="40"
paddingLeft="40" paddingRight="40" paddingTop="40" creationComplete="Init()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import myreport.ReportEngine;
import myreport.ReportViewer;
import myreport.data.report.CaptionCellSetting;
import myreport.data.report.CaptionRowSetting;
import myreport.data.report.ReportSettings;
import myreport.data.report.TableCellSetting;
import myreport.data.report.TableColumnSetting;
import myreport.data.report.TableRowSetting;
private function Init():void
{
//初始化時設置DataGrid的數據源
_Grid.dataProvider = GetTableData();
}
private function GetTableData():ArrayCollection
{
var list:ArrayCollection = new ArrayCollection();
for (var i:int =0; i < 25; i++)
{
list.addItem({ID: i, 名稱: "商品信息XXX 規格XXX 型號XXX", 數量: i+1, 金額: (i+1)*10, 日期:newDate()});
}
return list;
}
private function PreviewReport():void
{
//預覽
var style:ReportSettings= DataGridToMyReport(_Grid, "示例:DataGridToMyReport");
myreport.ReportViewer.Instance.Show(new XML(style.ToXML()), style.TableData, style.ParameterData);
}
private function PrintReport():void
{
//直接打印
var style:ReportSettings= DataGridToMyReport(_Grid, "示例:DataGridToMyReport");
myreport.ReportEngine.PrintAsync(new XML(style.ToXML()), style.TableData, style.ParameterData);
}
/**
*
* 封裝的轉換方法,實現DataGid轉成報表樣式
*
* @param grid: 表格控件(傳入前確保表格控件已經設置數據源)
* @param title:報表標題
*/
private function DataGridToMyReport(grid:DataGrid,title:String):ReportSettings
{
var style:ReportSettings = new ReportSettings();
//數據源
style.TableData = grid.dataProvideras ArrayCollection;
var params:Dictionary = newDictionary();
params.Title = title;
style.ParameterData = params;
//報表樣式
style.TableHeaderRepeat = true;//表格頭重複
style.TableFooterRepeat = true;//表格尾重複
style.AutoWidth = true;//報表寬度自動遞增
style.PageByColumn = true;//分欄打印
style.SetUnit("px");
//標題
var captionRow:CaptionRowSetting = new CaptionRowSetting();
var caption:CaptionCellSetting = new CaptionCellSetting();
caption.Width = style.ClientWidth;
caption.Style.FontBold = true;
caption.Style.FontSize = 16;
caption.Style.TextAlign = "center";
caption.Value = "=@Title";
captionRow.CaptionCellSettings.push(caption);
style.PageHeaderSettings.push(captionRow);
//表格
var headerRow:TableRowSetting = new TableRowSetting();
var contentRow:TableRowSetting = new TableRowSetting();
var gridColumns:Array = grid.columns;
for each(var gridCol:DataGridColumnin gridColumns)
{
if(!gridCol.visible)
continue;
//添加列
var column:TableColumnSetting = new TableColumnSetting();
column.Width = gridCol.width;
style.TableColumnSettings.push(column);
//添加表格頭單元格
var headerCell:TableCellSetting = new TableCellSetting();
headerCell.Style.FontBold = true;
headerCell.Style.TextAlign = "center";
headerCell.Value = gridCol.headerText;
headerRow.TableCellSettings.push(headerCell);
//添加表格主體單元格
var contentCell:TableCellSetting = new TableCellSetting();
contentCell.Value = "=#" + gridCol.dataField;
contentRow.TableCellSettings.push(contentCell);
}
style.TableHeaderSettings.push(headerRow);
style.TableDetailSettings.push(contentRow);
return style;
}
]]>
</mx:Script>
<mx:Label text="演示如何用程序動態生成報表樣式,實現DataGrid to MyReport。" width="100%" textAlign="center"/>
<mx:Button label="打印預覽"click="PreviewReport()"/>
<mx:Button label="直接打印"click="PrintReport()"/>
<mx:DataGridid="_Grid" width="600"height="100%" horizontalScrollPolicy="on">
<mx:columns>
<mx:DataGridColumn dataField="ID" headerText="ID"width="56"/>
<mx:DataGridColumn dataField="名稱" headerText="名稱"width="200"/>
<mx:DataGridColumn dataField="數量" headerText="數量"width="100"/>
<mx:DataGridColumn dataField="金額" headerText="金額"width="100"/>
<mx:DataGridColumn dataField="日期" headerText="日期"width="200"/>
</mx:columns>
</mx:DataGrid>
</mx:VBox>
效果圖
MyReport介紹
相關文章
備註
*技術交流與合作:QQ: 791663094;Email:[email protected]