如何解决如何使用Infragistics WPF阅读Excel
我在表1中有excel,我有2个表,我想通过Infragistics WPF读取数据,并在UI中绑定以下内容,这是excel中的数据结构,我是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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。