본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - 엑셀없이 엑셀파일 빠르게 읽기 ( XLS, XLSX, ExcelDataReader, EXCEL )

728x90
반응형

WPF & C# - 엑셀없이 엑셀파일 빠르게 읽기 ( XLS, XLSX, ExcelDataReader, EXCEL )

TestEXCEL.zip
1.07MB

 

MainWindows.xaml

1
2
3
4
5
6
7
<Grid>
<StackPanel>
    <Button x:Name="btn" Height="30" Click="btn_Click">GetFile</Button>
    <Button x:Name="btnOPEN" Height="30" Click="btnOPEN_Click">OPEN</Button>
    <DataGrid x:Name="dtGrd"/>
</StackPanel>
</Grid>
cs

 

MainWindows.xaml.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public MainWindow()
{
    InitializeComponent();
}
 
string file = "";   // 파일 Path
private void btn_Click(object sender, RoutedEventArgs e)
{
    // 파일 열기
    OpenFileDialog ofdlg = new OpenFileDialog();
    {
        // 필터설정
        ofdlg.Filter = "EXCEL File |*.xls|" + "EXCEL File |*.xlsx";
        ofdlg.CheckFileExists = true;   // 파일 존재여부확인
        ofdlg.CheckPathExists = true;   // 폴더 존재여부확인
 
        // 파일 열기 (값의 유무 확인)
        if (ofdlg.ShowDialog().GetValueOrDefault())
        {
            file = ofdlg.FileName;
            btn.Content = file;
        }
    }
}
 
private void btnOPEN_Click(object sender, RoutedEventArgs e)
{
    using (var stream = File.Open(file, FileMode.Open, FileAccess.Read))
    {
        using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
            var result = reader.AsDataSet(new ExcelDataSetConfiguration() //데이터셋 변환하기
            {
                ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
                {
                    //Column 자동생성을 무시하고 첫번째 행을 열로 자동 지정.
                    UseHeaderRow = true,
                }
            });
            dtGrd.ItemsSource = result.Tables[0].DefaultView; //엑셀파일의 첫번째 Table을 가져온다
        }
    }
}
cs

 

C# 환경에서 엑셀파일을 열때 엑셀프로그램 없이 사용가능한방법으로 아래 dll 등을 참조해서 사용한다.

보통 Microsoft.Office.Interop.Excel 을 가장 많이 사용하는데 속도가 심각하게 느리다.
ExcelDataReader 을 사용해보니 아주 준수한 속도가 나오고,
Microsoft.ACE.OLEDB.12.0 가 속도가 가장 빠르다. <==  해당파일의 경우 오피스가 깔려 있거나, Microsoft Access Database Engine 2010 재배포 가능 패키지 를 설치 해 주어야 한다.

 

Microsoft.Office.Interop.Excel

https://hostramus.tistory.com/91

https://blog.naver.com/dmaker123/222280650655


NPOI
https://jujun.tistory.com/597


Aspose.Cells for .NET ( 라이선스는 free 지만 제한이 있음 )
https://blog.aspose.com/ko/cells/create-excel-xls-xlsx-programmatically-in-csharp-net/


Microsoft.ACE.OLEDB.12.0 참조 후 아래 파일을 설치하고 사용가능
https://www.microsoft.com/ko-kr/download/details.aspx?id=13255

참고 https://www.csharpstudy.com/Practical/Prac-excel.aspx

ExcelDataReader

https://github.com/ExcelDataReader/ExcelDataReader

 

 

 

출처 및 참고

[C#] 대량의 엑셀 파일 빠르게 읽어오기 - 파일의 IT 블로그 (tistory.com)

 

[C#] 대량의 엑셀 파일 빠르게 읽어오기

인터넷에 C#으로 엑셀 파일을 읽어와 DataGridView에 뿌리는 소스는 많으나 대부분 Com 오브젝트나 OpenXML을 이용한 방법들이고 방법도 이중 for문을 이용하는게 많아서 datagridview에 하나하나 뿌리면

pgh268400.tistory.com

 

728x90
반응형