【问题标题】:iteration through datagrid rows for export to excel (again)遍历数据网格行以导出到 excel(再次)
【发布时间】:2016-04-06 08:15:31
【问题描述】:

我正在尝试将 dataGrid 行导出到 Excel 工作表。 因为我从 WinForms(dataGridView) 切换到 WPF (dataGrid),到目前为止我基本上对WPF一无所知 我需要你的帮助。

也许有人可以告诉我如何改变我的循环 或者我必须做些什么来填充行 Excel 工作表的单元格。

我已经阅读了所有关于 SO 的文章,但涵盖了这个问题 似乎没有找到适合我的问题的主题。

这是我为填充列名所做的,其中 完美运行:

for (int i = 1; i < dataGrid.Columns.Count + 1; i++)
{
    Excel.Range BackgroundColor;
    BackgroundColor = xlWorkSheet.get_Range("a9", "j9");
    BackgroundColor.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.RoyalBlue);

    AxlEx.Cells[9, i] = dataGrid.Columns[i - 1].Header;
}

当涉及到用行填充单元格时,我尝试了无数次来让它工作

for (int i = 0; i < dataGrid.Items.Count; i++)
{
    DataRowView aux = (DataRowView)dataGrid.Items[i];

    for (int j = 0; j < aux.Row.ItemArray.Length; j++)
    {
        //Console.WriteLine(string.Format("{0}-{1}", j, aux.Row.ItemArray[j]));
        AxlEx.Cells[i + 10, j + 1] = aux.Row.ItemArray[j];
    }
}

因类型不匹配而引发 System.InvalidCast 异常 这很明显......但我不知道如何转换,这里也是配件 关于 SO 的主题没有我可以理解的示例来更改我的代码。

在我有这个之前:

for (int i = 0; i < dataGrid.Items.Count; i++)
{
    for (int j = 0; j < dataGrid.Columns.Count; j++)
    {
        AxlEx.Cells[i + 10, j + 1] = dataRow.Row.ItemArray[j].ToString();
    }
}

如果我参考的话,它适用于 1 行

DataRowView dataRow = (DataRowView)dataGrid.SelectedItem;

我怎样才能让它工作?

【问题讨论】:

  • 你的ItemsSource 是什么DataGridObservableCollection&lt;T&gt;DataTable?
  • Hello StepUp 它是一个数据表

标签: c# wpf excel datagrid


【解决方案1】:

我不知道是否需要调试你的代码。但是,我想展示我的工作代码以将数据从DataGrid 导出到MS Excel

最好将此工作从 UI Thread 转移到 ThreadPool

 using Excel = Microsoft.Office.Interop.Excel;//add this library

 Task.Run(() => {                    
                // load excel, and create a new workbook
                Excel.Application excelApp = new Excel.Application();
                excelApp.Workbooks.Add();

                // single worksheet
                Excel._Worksheet workSheet = excelApp.ActiveSheet;

                // column headings
                for (int i = 0; i < YourDataTable.Columns.Count; i++)
                {
                    workSheet.Cells[1, (i + 1)] = YourDataTable.Columns[i].ColumnName;
                }

                // rows
                for (int i = 0; i < YourDataTable.Rows.Count; i++)
                {
                    // to do: format datetime values before printing
                    for (int j = 0; j < YourDataTable.Columns.Count; j++)
                    {
                        workSheet.Cells[(i + 2), (j + 1)] = YourDataTable.Rows[i][j];
                    }
                }
                excelApp.Visible = true;
            });

【讨论】:

    【解决方案2】:

    我发现了问题......

    for (int i = 0; i < dataGrid.Items.Count-1; i++)
                {
                    DataRowView aux = (DataRowView)dataGrid.Items[i];
    
                    for (int j = 0; j < aux.Row.ItemArray.Length; j++)
                    {
                        //Console.WriteLine(string.Format("{0}-{1}", j, aux.Row.ItemArray[j]));
                        AxlEx.Cells[i + 10, j + 1] = aux.Row.ItemArray[j];
                    }
                }
    

    我不得不减去 (dataGrid.Items.Count-1),因为 dataGrid 中有一个额外的空白行,这似乎导致了问题。 可能是由于 NULL 字段返回值??? 数据网格

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多