C# 如何在wpf c中从datatable导出到excel文件#

C# 如何在wpf c中从datatable导出到excel文件#,c#,wpf,excel,datatable,export,C#,Wpf,Excel,Datatable,Export,我有一个数据表,希望将其导出到excel文件,这是一个wpf应用程序,我找到的所有解决方案都是针对web应用程序asp.net的。请帮助…您可以从数据表中保存一个.csv(逗号分隔值文件)。然后可以在Excel中打开此文件 此外:无论是WPF还是Winforms,转换在两者中都是相同的,因为它的转换代码是用您的语言编写的,即C#,并且不是特定于WPF或Winforms的。只是为了更好地显示它 Microsoft.Office.Interop.Excel.Application excel = n

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

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


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

只是为了更好地显示它

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());
}
Microsoft.Office.Interop.Excel.Application Excel=null;
Microsoft.Office.Interop.Excel.Workbook wb=null;
对象缺失=类型。缺失;
Microsoft.Office.Interop.Excel.Worksheet ws=null;
Microsoft.Office.Interop.Excel.Range rng=null;
尝试
{
excel=新的Microsoft.Office.Interop.excel.Application();
wb=excel.Workbooks.Add();
ws=(Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
for(int Idx=0;Idx
对我来说工作很好谢谢。。。有人喜欢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
子Excel文件(ByVal dt作为数据表)

试试看
excel=新的Microsoft.Office.Interop.excel.Application()
wb=excel.Workbooks.Add()
ws=DirectCast(wb.ActiveSheet,Microsoft.Office.Interop.Excel.Worksheet)
对于Idx,整数=0到dt.Columns.Count-1
ws.Range(“A1”).Offset(0,Idx).Value=dt.Columns(Idx).ColumnName
下一个
对于Idx,整数=0到dt.Rows.Count-1
“嘿!这一行代码不是我发明的,
“我在CodeProject的某个地方找到了它。
“一次添加整行很有效,很酷吧?
“是的,很酷,兄弟。。。
ws.Range(“A2”).Offset(Idx).Resize(1,dt.Columns.Count).Value=dt.Rows(Idx).ItemArray
下一个
excel.Visible=True
wb.Activate()
特例
MessageBox.Show(“访问Excel时出错:&ex.ToString())
结束尝试
端接头
单向

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

dataTable2Excel(dt, dataGridView, pFullPath_toExport, nameSheet);

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

dataTable2Excel(dt, dataGridView, pFullPath_toExport, nameSheet);