复合表头报表除了使用报表工具来实现外,在Silverlight里面还可以使用DataGrid和RadGridView来实现,这里介绍一下RadGridView如何实现复合表头的报表。
首先来看代码实例:
1、XAML代码
<UserControl x:Class="MyCharts.CircuitReportsNew"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
d:DesignHeight="543" d:DesignWidth="1100" Width="Auto" Height="Auto" >
<Grid x:Name="LayoutRoot" Background="#FFE1E1E1">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Name="gdSearch1">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="23"></ColumnDefinition>
<ColumnDefinition Width="35"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="70"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<sdk:Label Grid.Column="1" Height="23" HorizontalAlignment="Right" Content="年份: " Name="lblYear" VerticalAlignment="Center" Width="60" />
<telerik:RadDateTimePicker Grid.Column="2" HorizontalContentAlignment="Left" Name="rdtp_DataTime" Height="23" DateSelectionMode="Year" InputMode="DatePicker" Visibility="Visible"/>
<sdk:Label Grid.Column="3" Name="lblMonth" Content="月份:" HorizontalAlignment="Right"></sdk:Label>
<ComboBox Name="cbb_Month" Grid.Column="4" Height="23" Width="80">
<ComboBoxItem Content="1-3月" Tag="1-3月" IsSelected="True" ></ComboBoxItem>
<ComboBoxItem Content="4-6月" Tag="4-6月"></ComboBoxItem>
<ComboBoxItem Content="7-9月" Tag="7-9月"></ComboBoxItem>
<ComboBoxItem Content="10-12月" Tag="10-12月"></ComboBoxItem>
</ComboBox>
<Button Grid.Column="5" Content="确定" Height="23" Name="bt_Confirm" Width="75" Click="bt_Confirm_Click" />
<Button Grid.Column="6" Content="打印" Height="23" Name="bt_Print" Width="75" Click="bt_Print_Click" />
<Button Grid.Column="7" Content="导出" Height="23" Name="bt_Excel" Width="75" Click="bt_Excel_Click" />
</Grid>
<Grid Name="gdCircuitReport" Grid.Row="1" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Name="txtBTitle" FontSize="17" FontWeight="Black" Grid.Row="0" Height="23" HorizontalAlignment="Center" Text="用户季报报表" VerticalAlignment="Center" />
<telerik:RadGridView x:Name="rgv_list" Grid.Row="1" AutoExpandGroups="True"
ShowColumnFooters="False" ShowGroupFooters="True"
AutoGenerateColumns="False" ShowGroupPanel="False"
IsReadOnly="True" CanUserFreezeColumns="False" RowIndicatorVisibility="Collapsed"
CanUserResizeColumns="True" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Width="*">
<telerik:GridViewDataColumn.Header>
<Grid Name="gdGrid4">
<Grid.RowDefinitions >
<RowDefinition Height="20*" />
<RowDefinition Height="1" />
<RowDefinition Height="20*" />
<RowDefinition Height="1" />
<RowDefinition Height="20*" />
<RowDefinition Height="1" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="1" />
</Grid.ColumnDefinitions>
<ContentPresenter Content="2013电量报表" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="18" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Height="1" Visibility="Visible" Grid.Row="1" Grid.ColumnSpan="18" />
<ContentPresenter Content="用户单位:阿迪锅" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="4" Grid.Column="0" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="1" />
<ContentPresenter Content="能源类型:用电" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="4" Grid.Column="6" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="3" />
<ContentPresenter Content="编号:43" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="4" Grid.Column="13" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="5" />
<ContentPresenter Content="编号" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="0" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="1" />
<ContentPresenter Content="名称" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="2" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="3" />
<ContentPresenter Content="名称" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="4" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="5" />
<ContentPresenter Content="名称" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="6" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="7" />
<ContentPresenter Content="名称" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="8" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="9" />
<ContentPresenter Content="名称" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="10" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="11" />
<ContentPresenter Content="名称" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="12" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="13" />
<ContentPresenter Content="用电量" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="14" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="15" />
<ContentPresenter Content="金额" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="16" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="4" Grid.Column="17" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Height="1" Visibility="Visible" Grid.Row="3" Grid.ColumnSpan="18" />
</Grid>
</telerik:GridViewDataColumn.Header>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal" >
<TextBlock Width="49" Text="{Binding L,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding S,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding M,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding XL,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding XXL,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding XL3,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding XL,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding Liu,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
<TextBlock Width="49" Text="{Binding Yang,Mode=TwoWay}" TextAlignment="Right" />
<Rectangle Fill="#FF0B1D1D" VerticalAlignment="Stretch" Width="1" Height="23" Visibility="Visible" />
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</Grid>
</UserControl>
PS:为了兼容分辨率,这里采用了DataGrid的百分比布局。
如果重复的引用Telerik的命名空间则会引发错误。
错误内容:
错误 1 分析规则 Element ::= . EmptyElement | ( StartElement ElementBody ). 中的意外 PROPERTYELEMENT。 E:\Test\SLReportTest\SLReportTest\MainPage.xaml SLReportTest
这里是“元素冲突导致的”,要把控件元素里的命名空间删除掉,因为空间内部属性设置冲突。
2、后台代码实现打印和导出
private void bt_Confirm_Click(object sender, RoutedEventArgs e)
{
}
/// <summary>
/// 打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bt_Print_Click(object sender, RoutedEventArgs e)
{
PrintDocument prtDoc = new PrintDocument();
prtDoc.PrintPage += new EventHandler<PrintPageEventArgs>(prtDoc_PrintPage);
string title = txtBTitle.Text;
prtDoc.Print(title); // " Printing Test " 是文档名 在打印机的任务列表里可以看到
}
void prtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.PageVisual = gdCircuitReport; //打印所有内容
}
private void bt_Excel_Click(object sender, RoutedEventArgs e)
{
string extensioin = "xls";
ExportFormat format = ExportFormat.Html;
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = extensioin;
dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extensioin, "Excel");
dialog.DefaultFileName = txtBTitle.Text;
dialog.FilterIndex = 1;
if (dialog.ShowDialog() == true)
{
try
{
using (Stream str = dialog.OpenFile())
{
GridViewExportOptions exportextension = new GridViewExportOptions();
exportextension.Format = format;
exportextension.Encoding = System.Text.Encoding.GetEncoding("utf-8");//放置乱码的出现
exportextension.ShowColumnFooters = true;
exportextension.ShowColumnHeaders = true;
exportextension.ShowGroupFooters = true;
this.rgv_list.Export(str, exportextension);
}
}
catch (Exception)
{
}
}
//rgv_list.ToCsv();//Csv文件
//string extension = "csv";
//SaveFileDialog dialog = new SaveFileDialog()
//{
// DefaultExt = extension,
// Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "CSV"),
// FilterIndex = 1
//};
//if (dialog.ShowDialog() == true)
//{
// using (Stream stream = dialog.OpenFile())
// {
// rgv_list.Export(stream,
// new GridViewExportOptions()
// {
// Format = ExportFormat.Csv,
// ShowColumnHeaders = true,
// ShowColumnFooters = true,
// ShowGroupFooters = false,
// });
// }
//}
}
前台代码定义了报表的绑定格式,这里的后台代码实现了报表的打印和导出。