개발(IT)/DevExpress(WinForm)

DevExpress Excel 열기

isony 2023. 12. 1. 21:36
반응형

엑셀파일을 Open 선택하여 DataGridView로 읽어오기

 

private void btnOpen_Click(object sender, EventArgs e)
{
    using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
    {
        openFileDialog1.Filter = "Excel File|*.xlsx;*.xls";
        openFileDialog1.Title = "Import Excel";
        openFileDialog1.Multiselect = false;

        if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
        {
            string filePath = openFileDialog1.FileName;
            string fileExt = new FileInfo(filePath).Extension;

            ExcelDataSource source = new ExcelDataSource();
            source.FileName = filePath;

            ExcelWorksheetSettings worksheetSettings = new ExcelWorksheetSettings();
            //worksheetSettings.WorksheetName = "sheet1";
            worksheetSettings.WorksheetIndex = 0;

            ExcelSourceOptions sourceOptions = new ExcelSourceOptions();
            sourceOptions.ImportSettings = worksheetSettings;
            sourceOptions.SkipHiddenRows = false;
            sourceOptions.SkipHiddenColumns = false;
            sourceOptions.UseFirstRowAsHeader = true;

            source.SourceOptions = sourceOptions;
            source.Fill();
            dgExcel.DataSource = source;
        }
    }
}
반응형