如何在wpf c#中从数据表导出到excel文件

Posted

技术标签:

【中文标题】如何在wpf c#中从数据表导出到excel文件【英文标题】:How to export from datatable to excel file in wpf c# 【发布时间】:2012-06-25 10:39:50 【问题描述】:

我有一个数据表并希望将其导出到 excel 文件,它是一个 wpf 应用程序,我找到的所有解决方案都是针对 web 应用程序 asp.net 请帮助...

【问题讨论】:

【参考方案1】:

只是为了让所有人都能看到它

Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook wb = null;

object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Worksheet ws = null;
Microsoft.Office.Interop.Excel.Range rng = null;      

try

    excel = new Microsoft.Office.Interop.Excel.Application();
    wb = excel.Workbooks.Add();
    ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;

    for (int Idx = 0; Idx < dt.Columns.Count; Idx++)
    
        ws.Range["A1"].Offset[0, Idx].Value = dt.Columns[Idx].ColumnName;
    

    for (int Idx = 0; Idx < dt.Rows.Count; Idx++)
      // <small>hey! I did not invent this line of code, 
       // I found it somewhere on CodeProject.</small> 
       // <small>It works to add the whole row at once, pretty cool huh?</small>
       ws.Range["A2"].Offset[Idx].Resize[1, dt.Columns.Count].Value = 
       dt.Rows[Idx].ItemArray;
    

    excel.Visible = true;
    wb.Activate();

catch (COMException ex)

    MessageBox.Show("Error accessing Excel: " + ex.ToString());

catch (Exception ex)

    MessageBox.Show("Error: " + ex.ToString());

【讨论】:

【参考方案2】:

您可以从数据表中保存 .csv(逗号分隔值文件)。然后可以在 Excel 中打开此文件。

此外:无论是 WPF 还是 Winforms,两者的转换都是相同的,因为它的转换代码是用您的语言(即 C#)编写的,并不特定于 WPF 或 Winforms。

【讨论】:

【参考方案3】:

对我来说工作得很好,谢谢...像 vb.net 这样的人?

Dim excel As Microsoft.Office.Interop.Excel.Application = Nothing
Dim wb As Microsoft.Office.Interop.Excel.Workbook = Nothing

Dim missing As Object = Type.Missing
Dim ws As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim rng As Microsoft.Office.Interop.Excel.Range = Nothing

Sub ExcelFile(ByVal dt As DataTable)

    Try
        excel = New Microsoft.Office.Interop.Excel.Application()
        wb = excel.Workbooks.Add()
        ws = DirectCast(wb.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)

        For Idx As Integer = 0 To dt.Columns.Count - 1
            ws.Range("A1").Offset(0, Idx).Value = dt.Columns(Idx).ColumnName
        Next

        For Idx As Integer = 0 To dt.Rows.Count - 1
            ' <small>hey! I did not invent this line of code, 
            ' I found it somewhere on CodeProject.</small> 
            ' <small>It works to add the whole row at once, pretty cool huh?</small>
            ' YES IT'S COOL Brother ...
            ws.Range("A2").Offset(Idx).Resize(1, dt.Columns.Count).Value = dt.Rows(Idx).ItemArray
        Next

        excel.Visible = True
        wb.Activate()
    Catch ex As Exception
        MessageBox.Show("Error accessing Excel: " & ex.ToString())

    End Try

End Sub

【讨论】:

【参考方案4】:

一种方式

ArrayList arr = (ArrayList)dataGridView.DataSource;
dt = ArrayListToDataTable(arr);

dataTable2Excel(dt, dataGridView, pFullPath_toExport, nameSheet);

http://www.codeproject.com/Articles/30169/Excel-export-from-DatagridView

http://support.microsoft.com/default.aspx?scid=kb;en-us;317719

【讨论】:

以上是关于如何在wpf c#中从数据表导出到excel文件的主要内容,如果未能解决你的问题,请参考以下文章

C#winform中如何把表导出到EXCEL

c# 只安装WPF环境 导出excel表格 问题

如何在 C# WPF 中从 ListView/XML 中完全删除一个项目?

VB中从SQL Server到Excel的海量数据导出

如何在 C# 中将文件导出到 excel 2007+

在 C# 中从多个数据表创建 csv 文件