C# 使用ClosedXML将Excel文件转换为DataGridview

C# 使用ClosedXML将Excel文件转换为DataGridview,c#,datagridview,closedxml,C#,Datagridview,Closedxml,在我的项目中,我必须导入一个excel文件。类型是.xlsx。该文件中有一个包含4列的表。我必须向这个excel文件中添加额外的列。我的问题是,在我的解决方案中,DataGridview有两个headertext。每次我再次阅读文件时,我都会得到一行新的标题文本。我认为问题出在foreach(row.Cells()中的IXLCell单元格)中,但我没有发现错误所在 try { OpenFileDialog OpenDataList = new OpenFileDialog();// E

在我的项目中,我必须导入一个excel文件。类型是.xlsx。该文件中有一个包含4列的表。我必须向这个excel文件中添加额外的列。我的问题是,在我的解决方案中,DataGridview有两个headertext。每次我再次阅读文件时,我都会得到一行新的标题文本。我认为问题出在foreach(row.Cells()中的IXLCell单元格)中,但我没有发现错误所在

try
{

    OpenFileDialog OpenDataList = new OpenFileDialog();// Erzeugen einen OpenFileDialog-Objektes
    OpenDataList.Title = "Datenpunktliste öffnen"; // Initialisiert den Namen des Fensters
    OpenDataList.InitialDirectory = @"C:\Users\AdministratorOSMO\Desktop\Testordner"; // Legt den Default Dateipfad fest
    OpenDataList.Filter = "Excel Files|*.xls;*.xlsx"; // Filter für die zur Auswahl stehenden Dateitypen
    OpenDataList.FilterIndex = 2;// Legt den Standardfilter beim Öffnen fest

    if (OpenDataList.ShowDialog() == DialogResult.OK)//Abfrage ob der ShowDialog mit OK bestätigt wurde
    {
        string PathName = OpenDataList.FileName;// die Variable pathName wird mit dem Inhalt von Filename initialisiert
        using (XLWorkbook workBook = new XLWorkbook(PathName))//Arbeitmappe wird erstellt
        {

            IXLWorksheet workSheet = workBook.Worksheet(1);//Tabelle 1 der Excel-Datei wird in workSheet geschrieben
            DataTable dt = new DataTable();// neuer DataTable dt wird erzeugt
            bool firstRow = true;// Boolsche Variable für Erste Reihe ?
            foreach (IXLRow row in workSheet.Rows())
            {
                while (firstRow)

                {
                    foreach (IXLCell cell in row.Cells())
                    {
                    dt.Columns.Add(cell.Value.ToString());
                    }

                    dt.Columns.Add(new DataColumn("OK", typeof(bool)));
                    dt.Columns.Add(new DataColumn("Datum", typeof(string)));
                    firstRow = false;
                }

                dt.Rows.Add();

                int i = 0;
                foreach (IXLCell cell in row.Cells())
                {
                    dt.Rows[dt.Rows.Count-1][i] = cell.Value.ToString();
                    i++;
                }

                Dgv_Data_List.DataSource = dt;
                Dgv_Data_List.ReadOnly = true;

            }

        }                                       
    }
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex.Message);
}

从第一行设置列标题后,还可以处理与普通数据行相同的行。您可以使用
continue
转到下一行来停止该操作。(下面的最后两行也可以超出foreach循环。)


更新了我的答案。将
while
更改为
if
,您不需要
while
循环(
continue
转到
while
循环,而不是
foreach
循环,这是我第一个版本的问题。)顺便说一句:您不应该将对我答案的答复作为对问题的单独回答发布。
foreach (IXLRow row in workSheet.Rows())
{
    if (firstRow)       
    {
        foreach (IXLCell cell in row.Cells())
        {
            dt.Columns.Add(cell.Value.ToString());
        }

        dt.Columns.Add(new DataColumn("OK", typeof(bool)));
        dt.Columns.Add(new DataColumn("Datum", typeof(string)));
        firstRow = false;
        continue;  // go to next row
    }

    dt.Rows.Add();

    int i = 0;
    foreach (IXLCell cell in row.Cells())
    {
        dt.Rows[dt.Rows.Count-1][i] = cell.Value.ToString();
        i++;
    }
}

Dgv_Data_List.DataSource = dt;
Dgv_Data_List.ReadOnly = true;