zoukankan      html  css  js  c++  java
  • silverlight 将chart图倒入到excel

    mainpage.xaml

    1  <Grid x:Name="LayoutRoot" Background="White">
    2             <StackPanel Orientation="Vertical" >
    3             <Button Content="Export to Excel" Height="23" HorizontalAlignment="Left" x:Name="button1" VerticalAlignment="Top" Width="150" Click="button1_Click" />
    4             <toolkit:Chart HorizontalAlignment="Left" Name="chart1" Title="Line Chart" VerticalAlignment="Top" Height="300" Width="640" >
    5             </toolkit:Chart>
    6         </StackPanel>
    7     </Grid>
    View Code

    后台

     1  public MainPage()
     2         {
     3             InitializeComponent();
     4             this.Loaded += new RoutedEventHandler(UserControl_Loaded); ;
     5         }
     6         private void UserControl_Loaded(object sender, RoutedEventArgs e)
     7         {
     8             ObservableCollection<EmployeePoints> empPoints = new ObservableCollection<EmployeePoints>()
     9             { 
    10                 new EmployeePoints() { Name = "John", Points = 10 }, 
    11                 new EmployeePoints() { Name = "Sam", Points = 40 },
    12                 new EmployeePoints() { Name = "Mary", Points = 80 }, 
    13                 new EmployeePoints() { Name = "Pierce", Points = 60 }, 
    14                 new EmployeePoints() { Name = "Paul", Points = 70 }, 
    15             };
    16             LineSeries columnSeries = new LineSeries();//实例化一个Series:
    17 
    18             //注意ColumnSeries所在命名空间   
    19 
    20             //为:using System.Windows.Controls.DataVisualization.Charting;
    21 
    22 
    23             columnSeries.ItemsSource = empPoints;  //设置数据源
    24             columnSeries.DependentValueBinding = new Binding("Points");       //后台进行数据绑定         
    25             columnSeries.IndependentValueBinding = new Binding("Name");
    26             //columnSeries.Title = "";
    27 
    28             chart1.Series.Add(columnSeries);//为Chart绑定“显示类型”
    29 
    30 
    31 
    32         }
    33 
    34         private void button1_Click(object sender, RoutedEventArgs e)
    35         {
    36             WriteableBitmap bitmap = new WriteableBitmap(640, 300);
    37             bitmap.Render(chart1, null);
    38             bitmap.Invalidate();
    39 
    40 
    41             // open file dailog for selecting export file
    42             SaveFileDialog sDialog = new SaveFileDialog();
    43             sDialog.Filter = "Excel Files(*.xls)|*.xls";
    44 
    45             if (sDialog.ShowDialog() == true)
    46             {
    47                 // create a workbook object
    48                 Workbook workbook = new Workbook();
    49                 //Create a worksheet object 
    50                 Worksheet worksheet1 = new Worksheet("SheetWithImage");
    51 
    52                 // creat a spreadsheet picture object
    53                 Lite.ExcelLibrary.SpreadSheet.Picture pic = new Lite.ExcelLibrary.SpreadSheet.Picture();
    54                 //set its image property from silverlight image control
    55                 // image formates 0xF01E=png,0xF01D=jpeg
    56                 //ImageTranslator.TranslateImageToBytes translate an image control to byte array
    57                 // that will be used by excel picture object to plot picture in excel file.
    58                 System.Windows.Controls.Image image = new System.Windows.Controls.Image();
    59                 image.Source = bitmap;
    60                 image.Name = "imgExport";
    61                 image.Width = 640;
    62                 image.Height = 300;
    63                 image.Stretch = Stretch.Fill;
    64                 pic.Image = new Lite.ExcelLibrary.SpreadSheet.Image(ImageTranslator.TranslateImageToBytes(image), 0xF01E);
    65 
    66                 //set picture size
    67                 pic.TopLeftCorner = new CellAnchor(1, 1, 10, 10);
    68                 pic.BottomRightCorner = new CellAnchor(8, 5, 10, 10);
    69                 // add picture to spreadsheet
    70                 worksheet1.AddPicture(pic);
    71                 /// add worksheet to workbook
    72                 workbook.Worksheets.Add(worksheet1);
    73                 // get the stream of selected file
    74                 Stream sFile = sDialog.OpenFile();
    75                 // save excel file 
    76                 workbook.Save(sFile);
    77             }
    78         }
    79 
    80 
    81         public class EmployeePoints
    82         {
    83             public string Name { get; set; }
    84             public int Points { get; set; }
    85         }
    86     }
    View Code


    用到了工具       Lite Excel Binaries

    下载地址:http://excellite.codeplex.com/

    The source from http://sreeharip.wordpress.com/2011/12/04/exporting-a-silverlight-chart-to-excel-in-browser/

       
  • 相关阅读:
    GitLab 介绍
    git 标签
    git 分支
    git 仓库 撤销提交 git reset and 查看本地历史操作 git reflog
    git 仓库 回退功能 git checkout
    python 并发编程 多进程 练习题
    git 命令 查看历史提交 git log
    git 命令 git diff 查看 Git 区域文件的具体改动
    POJ 2608
    POJ 2610
  • 原文地址:https://www.cnblogs.com/akingyao/p/3077261.html
Copyright © 2011-2022 走看看