[TIL] C# WPF : October 19, 2020

RE_BROTHER·2020년 11월 11일
0

TIL

목록 보기
27/41
post-thumbnail

C# WPF

File Load

Not enough data

lastFile_Rename Function은 지난번에 작성을 완료했고, 이름이 변경된 해당 엑셀 파일을 WPF Windows 상에 Load하여 출력하는 기능을 구현해야한다.
나름 자료가 없진 않아보이는데 Simple 해보이는 패키지는 온통 유료 패키지고, 무료 패키지는 여러가지로 지저분하게 엮여있는 자료만 존재한다.
하나 하나 적용해보면서 에러를 맞아보자

Try 1 : WPF Excel Datagrid import

Visual Stduio 2015 에서 기본으로 제공되는 WPF Datagrid Tool에 엑셀 데이터를 Load/Save 하는 방식이다.
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해서 2차원 배열에 변수들을 담아 DataGrid에 옮겨주는 식으로 진행해볼까 한다.

NuGet Package Manager에서 Microsoft.Office.Interop.Excel 패키지를 install 한다.

using Microsoft.Office.interop.Excel;

// ...

public void excelFile_Load(string rename_file)
{
   Application application = new Application();
   Workbook workbook = application.Workbooks.Open(Filename: rename_file);
profile
I hope the All-Rounder Developer & Researcher

0개의 댓글