■ UltraGrid 클래스에서 엑셀 파일을 로드하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
using System.IO; using System.Data; using Infragistics.Documents.Excel; using Infragistics.Win.UltraWinGrid; private UltraGrid ultraGrid; ... string filePath = Application.StartupPath + @"\EMPLOYEE.xls"; if(File.Exists(filePath)) { Workbook workBook = Workbook.Load(filePath); Worksheet workSheet = workBook.Worksheets[0]; DataTable employeeTable = new DataTable("Employee"); int rowCount = 0; int columnCount = 0; foreach(WorksheetRow worksheetRow in workSheet.Rows) { if(rowCount == 0) { foreach(WorksheetCell worksheetCell in worksheetRow.Cells) { string cellValue = worksheetCell.Value.ToString().Trim(); if(cellValue != string.Empty) { DataColumn dataColumn = employeeTable.Columns.Add(); dataColumn.ColumnName = cellValue; dataColumn.DataType = workSheet.Rows[rowCount + 1].Cells[columnCount].Value.GetType(); } else { break; } columnCount++; } } else { columnCount = 0; DataRow employeeRow = employeeTable.NewRow(); foreach(WorksheetCell worksheetCell in worksheetRow.Cells) { object cellValue = workSheet.Rows[rowCount].Cells[columnCount].Value; if(cellValue != null) { employeeRow[columnCount] = cellValue; } else { break; } columnCount++; } employeeTable.Rows.Add(employeeRow); } rowCount++; } employeeTable.AcceptChanges(); this.ultraGrid.DataSource = employeeTable; } |
※ 첨부 엑셀 파일은 실행 프로그램과 동일한 폴더 내에 있다고 가정한다.