[TIL] C# WPF : October 19, 2020
C# WPF
File Load
Not enough data
lastFile_Rename
Functionは前回作成が完了し、WPF Windowsで名前を変更した対応するExcelファイルをロードして出力する機能を実現する必要があります.
資料がないわけではないように見えますが、簡単そうに見えるコースはすべて有料コースで、無料コースは雑然とした資料しか存在しません.
一つ一つ応用してみて、エラーを迎えてみましょう.
Try 1 : WPF Excel Datagrid import
これは、Visual Stduio 2015で提供されるWPF DataGrid Tool上でLoad/外付けデータを保存する方法です.EPPlus
パッケージ内のExcelPackage
を使用します.<!-- Window_taxdata_01.xaml -->
<DataGrid x:Name="dataGrid_scrap_01"
HorizontalAlignment="Left"
Margin="70,255,350,0"
VerticalAlignment="Top"
Height="auto"
Width="auto"
CanUserAddRows="False"
CanUserDeleteRows="False"
AutoGenerateColumns="False"
ItemsSource="{Binding ObserList}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="White"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
<Setter Property="IsEditing" Value="True"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Month" Width="1*" Binding="{Binding DataMonth}" IsReadOnly="True"/>
<DataGridTextColumn Header="Division" Width="1.5*" Binding="{Binding DataDivision}" IsReadOnly="True"/>
<DataGridTextColumn Header="고지금액" Width="3*" Binding="{Binding DataCharge1}" IsReadOnly="True"/>
<DataGridTextColumn Header="수납금액" Width="3*" Binding="{Binding DataCharge2}" IsReadOnly="True"/>
<DataGridTextColumn Header="미납금액" Width="3*" Binding="{Binding DataCharge3}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
Nuget Package Manager
でEPPlus
をインストールし、*.cs
コードに以下の内容を追加します.using OfficeOpenXml;
// ...
public void excelFile_Load(string rename_file)
{
var fi = new FileInfo(rename_file);
using (var package = new ExcelPackage(fi))
{
// ...
}
中はちょっと難しいですが、1秒で成功できるような気がします.
Try 2 : Microsoft.Office.Interop.Excel
生成されたDataGrid
を保持し、Microsoft.Office.Interop.Excel
パッケージLoad EXCELデータを介して2次元配列に変数を入れ、DataGrid
に移行する.
NuGet Package Manager
からMicrosoft.Office.Interop.Excel
パッケージをインストールします.using Microsoft.Office.interop.Excel;
// ...
public void excelFile_Load(string rename_file)
{
Application application = new Application();
Workbook workbook = application.Workbooks.Open(Filename: rename_file);
Reference
この問題について([TIL] C# WPF : October 19, 2020), 我々は、より多くの情報をここで見つけました
https://velog.io/@re_brother/TIL-C-WPF-October-19-2020
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
<!-- Window_taxdata_01.xaml -->
<DataGrid x:Name="dataGrid_scrap_01"
HorizontalAlignment="Left"
Margin="70,255,350,0"
VerticalAlignment="Top"
Height="auto"
Width="auto"
CanUserAddRows="False"
CanUserDeleteRows="False"
AutoGenerateColumns="False"
ItemsSource="{Binding ObserList}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="White"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
<Setter Property="IsEditing" Value="True"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Month" Width="1*" Binding="{Binding DataMonth}" IsReadOnly="True"/>
<DataGridTextColumn Header="Division" Width="1.5*" Binding="{Binding DataDivision}" IsReadOnly="True"/>
<DataGridTextColumn Header="고지금액" Width="3*" Binding="{Binding DataCharge1}" IsReadOnly="True"/>
<DataGridTextColumn Header="수납금액" Width="3*" Binding="{Binding DataCharge2}" IsReadOnly="True"/>
<DataGridTextColumn Header="미납금액" Width="3*" Binding="{Binding DataCharge3}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
using OfficeOpenXml;
// ...
public void excelFile_Load(string rename_file)
{
var fi = new FileInfo(rename_file);
using (var package = new ExcelPackage(fi))
{
// ...
}
using Microsoft.Office.interop.Excel;
// ...
public void excelFile_Load(string rename_file)
{
Application application = new Application();
Workbook workbook = application.Workbooks.Open(Filename: rename_file);
Reference
この問題について([TIL] C# WPF : October 19, 2020), 我々は、より多くの情報をここで見つけました https://velog.io/@re_brother/TIL-C-WPF-October-19-2020テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol