To play the demo, you can design a simple UI Winforms Application that allows you to select an excel file, then you can read it and fill data from the excel file into your DataGridView
We will use EPPlus library to read an excel file. You can download it from the Nuget Package Manager in Visual Studio
The EPPlus is an open source library that helps you read an excel file
You can create a class with properties corresponding the column fields in your excel file
For example i've exported the customer table from the Northwind database to the excel file
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
}
To fill data into your DataGridView, you can add a bindingsource to your DataGridView, then set a datasource to the bindingsource
private void btnReadExcel_Click(object sender, EventArgs e)
{
List list = new List();
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel file|*.xlsx" })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo fileInfo = new FileInfo(ofd.FileName);
ExcelPackage package = new ExcelPackage(fileInfo);
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
// get number of rows and columns in the sheet
int rows = worksheet.Dimension.Rows;
int columns = worksheet.Dimension.Columns;
// loop through the worksheet rows and columns
for (int i = 2; i <= rows; i++)
{
Customer obj = new Customer();
for (int j = 1; j <= columns; j++)
{
switch (j)
{
case 1:
obj.CustomerID = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 2:
obj.CompanyName = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 3:
obj.ContactName = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 4:
obj.ContactTitle = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 5:
obj.Address = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 6:
obj.City = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 7:
obj.Region = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 8:
obj.PostalCode = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 9:
obj.Country = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 10:
obj.Phone = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
case 11:
obj.Fax = worksheet.Cells[i, j].Value == null ? string.Empty : worksheet.Cells[i, j].Value.ToString();
break;
}
}
list.Add(obj);
}
customerBindingSource.DataSource = list;
}
}
}
0 Comments