C
C#2y ago
Tanatozzz

✅ data from excel to wpf app

I want to make an application to display the data I need in graphs, etc. The only problem is that the original data is provided in .xlsx and .xls format It would be much more convenient for me to work directly with the database, but the condition is to work with files of this type. They have tables with data. How can i read the file when loading such a file and save the data in a convenient form in the application, in a list, for example For further work, I mean to create graphs, and view data on something by clicking on a conditional Example1 (Where, how much, when, etc.) Personally, I have thoughts to do it all through the DataGrid, but I almost never worked with it, and it's hard to imagine how to provide data for it in a more convenient form. There is also an idea just through the ListView, but for this I need to transfer all the data from the Excel file to the list, in this version I can more or less imagine how I can implement this, but the only problem is reading and writing data to the list
13 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tanatozzz
TanatozzzOP2y ago
i wanna do this on wpf
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tanatozzz
TanatozzzOP2y ago
This is a training project in general, and is specifically needed as a desktop application. Before that, I did not work directly with Excel data, and therefore I do not know what to do now
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tanatozzz
TanatozzzOP2y ago
50\50 This is necessary for some work purposes, and I want to do it as a desktop application separately, it must be integrated into another application that is already working, and therefore I need my own solution
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tanatozzz
TanatozzzOP2y ago
The only thing I can say is that the PowerBI option will not work, and in terms of the fact that I want to do it in WPF as a practice for myself, and how to use it later in my work, this is a very narrow application, and I need do it exactly on WPF
Alexicon
Alexicon2y ago
I cant speak for how to read a excel file directly, however if your plan is to use a DataGrid element then the DataTable type might be the easiest way to display the data. For example you can bind to a DataTable directly like this:
<Window
x:Class="CSharp.Interactive.WPF.Simple.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"
mc:Ignorable="d"
Title="MainWindow"
Width="800"
Height="450"
x:Name="xMainWindow">

<Grid>
<DataGrid ItemsSource="{Binding Path=MyDataTable, ElementName=xMainWindow}"/>
</Grid>
</Window>
<Window
x:Class="CSharp.Interactive.WPF.Simple.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"
mc:Ignorable="d"
Title="MainWindow"
Width="800"
Height="450"
x:Name="xMainWindow">

<Grid>
<DataGrid ItemsSource="{Binding Path=MyDataTable, ElementName=xMainWindow}"/>
</Grid>
</Window>
And then in the main window code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
MyDataTable = new DataTable();

//...

InitializeComponent();
}

public DataTable MyDataTable { get; }

//...
}
public partial class MainWindow : Window
{
public MainWindow()
{
MyDataTable = new DataTable();

//...

InitializeComponent();
}

public DataTable MyDataTable { get; }

//...
}
Alexicon
Alexicon2y ago
Here is the full code behind I used for this example incase it also helps (keep in mind if you plan to use a view model its basically the same thing):
public partial class MainWindow : Window
{
public MainWindow()
{
MyDataTable = new DataTable();

string csv =
"""
name,price,color
car,20000,red
house,300000,brown
lamp,25,white
""";

CreateDataTableFromCsv(csv);

InitializeComponent();
}

public DataTable MyDataTable { get; }

public void CreateDataTableFromCsv(string csv)
{
bool isFirst = true;
foreach (string row in csv.Split(Environment.NewLine))
{
DataRow dataRow = MyDataTable.NewRow();

string[] columns = row.Split(',');
for (int columnIndex = 0; columnIndex < columns.Length; columnIndex++)
{
string column = columns[columnIndex];

//we assume that the first row are the column headers
if (isFirst)
{
MyDataTable.Columns.Add(column);
}
else
{
dataRow[columnIndex] = column;

}
}

if (!isFirst)
{
MyDataTable.Rows.Add(dataRow);
}

isFirst = false;
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
MyDataTable = new DataTable();

string csv =
"""
name,price,color
car,20000,red
house,300000,brown
lamp,25,white
""";

CreateDataTableFromCsv(csv);

InitializeComponent();
}

public DataTable MyDataTable { get; }

public void CreateDataTableFromCsv(string csv)
{
bool isFirst = true;
foreach (string row in csv.Split(Environment.NewLine))
{
DataRow dataRow = MyDataTable.NewRow();

string[] columns = row.Split(',');
for (int columnIndex = 0; columnIndex < columns.Length; columnIndex++)
{
string column = columns[columnIndex];

//we assume that the first row are the column headers
if (isFirst)
{
MyDataTable.Columns.Add(column);
}
else
{
dataRow[columnIndex] = column;

}
}

if (!isFirst)
{
MyDataTable.Rows.Add(dataRow);
}

isFirst = false;
}
}
}
Tanatozzz
TanatozzzOP2y ago
thank you, i gonna try this
7ecvz4i5j54g0srf5d7n
For reading xlsx files there is a library called ClosedXml, based on OpenXml but a bit easier to use. But if you need to read xls files also it won't work.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server