I have excel where in Sheet 1 i have 2 tables ,i want to read data by infragistics WPF and bind in UI below is the structure of data in excel , i am new to WPF can anyone help
1 Answer
One of the methods to read the Excel file is using the XamSpreadsheet
control that allows visualizing and editing of spreadsheet data.
There is the code example.
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;
}
}
}
The 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>
The application will looks like on the screenshot:
It is necessary add references to the following assemblies:
- InfragisticsWPF4.Controls.Grids.XamSpreadsheet
- InfragisticsWPF4.Documents.Excel
For additional information see: XamSpreadsheet