如何使用 Infragistics WPF 读取 excel

How to read excel using Infragistics WPF

我有excel,其中Sheet 1 我有2个表,我想通过infragistics WPF读取数据并绑定到UI下面是[=中的数据结构16=],我是 WPF 的新手,谁能帮忙

读取 Excel 文件的方法之一是使用允许可视化和编辑电子表格数据的 XamSpreadsheet 控件。

有代码示例。

using Infragistics.Documents.Excel;
using System;
using System.IO;
using System.Windows;
using System.Windows.Resources;

namespace ReadExcelFile
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadFile();        
        }

        private void LoadFile()
        {
            // For this example the excel document is located in resources
            var fileName = "/ReadExcelFile;component/Resources/Book1.xlsx";
            Stream stream = GetFileAsStream(fileName);            
            if (Workbook.Load(stream) is Workbook wb)
            {
                this.xamSpreadsheet1.Workbook = wb;
                this.Title = "Book1.xlsx";
            }
        }

        public static Stream GetFileAsStream(string fullName)
        {
            StreamResourceInfo sri = Application.GetResourceStream(new Uri(fullName, UriKind.Relative));
            return sri.Stream;
        }
    }
}

XAML:

<Window x:Class="ReadExcelFile.MainWindow"
        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"
        xmlns:ig="http://schemas.infragistics.com/xaml"        
        mc:Ignorable="d"
        Height="450" Width="800">
    
    <Grid>
        <ig:XamSpreadsheet x:Name="xamSpreadsheet1" />
    </Grid>
</Window>

该应用程序将如屏幕截图所示:

有必要添加对以下程序集的引用:

  • InfragisticsWPF4.Controls.Grids.XamSpreadsheet
  • InfragisticsWPF4.Documents.Excel

有关更多信息,请参阅:XamSpreadsheet