8

私はデータテーブルを持っていて、それをExcelファイルにエクスポートしたいと思っています.

4

4 に答える 4

10

見やすくするためだけに

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());
}
于 2012-06-23T08:50:34.103 に答える
4

データテーブルから .csv (カンマ区切り値ファイル) を保存できます。このファイルは、Excel で開くことができます。

さらに、WPF であろうと Winforms であろうと、変換コードは C# などの言語で記述されており、WPF または Winforms に固有のものではないため、変換は両方で同じです。

于 2012-06-23T08:44:53.447 に答える
2

私にとってはうまくいきますありがとうございます... 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
于 2012-08-21T10:13:49.517 に答える
1

一方通行

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

于 2013-01-18T07:14:08.107 に答える