首页 > 其他分享 >wpf利用ReoGrid控件进行Excel表格展示及导出

wpf利用ReoGrid控件进行Excel表格展示及导出

时间:2022-10-30 18:00:51浏览次数:91  
标签:控件 sheet Windows Excel System dialog ReoGrid using

Nuget包安装

Nuget包管理器直接搜索ReoGrid进行安装,如图

 

 

 

前端

根据官方文档(https://reogrid.net/document/installation/)在前端引入命名空间:

xmlns:rg="clr-namespace:unvell.ReoGrid;assembly=unvell.ReoGrid"
View Code

然后添加该控件:

1 <Grid>
2     <rg:ReoGridControl x:Name="grid" />
3 </Grid>
View Code

前端代码:

 1 <Window
 2     x:Class="ReoGridExample.MainWindow"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6     xmlns:local="clr-namespace:ReoGridExample"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     xmlns:reogrid="clr-namespace:unvell.ReoGrid;assembly=unvell.ReoGrid"
 9     Title="ReoGridExample"
10     Width="800"
11     Height="450"
12     mc:Ignorable="d">
13     <Grid>
14         <Grid.RowDefinitions>
15             <RowDefinition Height="*" />
16             <RowDefinition Height="auto" />
17         </Grid.RowDefinitions>
18 
19         <reogrid:ReoGridControl x:Name="ReoGrid" />
20         <StackPanel
21             Grid.Row="1"
22             Margin="5"
23             HorizontalAlignment="Right"
24             Orientation="Horizontal">
25             <Button
26                 Margin="0,0,10,0"
27                 Click="ExportFile"
28                 Content="导出Excel" />
29             <Button Click="OpenFile" Content="打开Excel" />
30         </StackPanel>
31 
32     </Grid>
33 </Window>
View Code

后端

 1 using System.Windows.Forms;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows;
 8 using System.Windows.Controls;
 9 using System.Windows.Data;
10 using System.Windows.Documents;
11 using System.Windows.Input;
12 using System.Windows.Media;
13 using System.Windows.Media.Imaging;
14 using System.Windows.Navigation;
15 using System.Windows.Shapes;
16 using unvell.ReoGrid;
17 
18 namespace ReoGridExample
19 {
20     /// <summary>
21     /// MainWindow.xaml 的交互逻辑
22     /// </summary>
23     public partial class MainWindow : Window
24     {
25         private ReoGridControl _ReoGrid;
26 
27         public MainWindow()
28         {
29             InitializeComponent();
30             _ReoGrid = ReoGrid;
31 
32             //也可以通过代码直接进行表格赋值
33             Worksheet sheet = _ReoGrid.CurrentWorksheet;
34             //设置格式
35             sheet.Cells[0, 0].Style.HAlign = ReoGridHorAlign.Center;//水平居中
36             sheet.Cells[0, 0].Style.VAlign = ReoGridVerAlign.Middle;//垂直居中
37             sheet.Cells[0, 0].Style.FontName = "宋体";
38             sheet.Cells[0, 0].Style.FontSize = 8;
39 
40             sheet[0, 0] = "例子";
41         }
42 
43         //打开excel文件
44         private void OpenFile(object sender, RoutedEventArgs e)
45         {
46             OpenFileDialog dialog = new OpenFileDialog();
47             dialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
48             dialog.Multiselect = false;
49             if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
50             {
51                 _ReoGrid.Load(dialog.FileName);
52             }
53         }
54 
55         //导出
56         private void ExportFile(object sender, RoutedEventArgs e)
57         {
58             SaveFileDialog dialog = new SaveFileDialog();
59             dialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
60             dialog.Title = "导出";
61             if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
62             {
63                 _ReoGrid.Save(dialog.FileName);
64             }
65         }
66 
67     }
68 }
View Code

效果

 

标签:控件,sheet,Windows,Excel,System,dialog,ReoGrid,using
From: https://www.cnblogs.com/night-owl/p/16841811.html

相关文章