0

オブジェクトのリストがあり、それらをデータ テーブルに変換しましたが、それらを Excel にエクスポートできません。

以下はサンプルコードです

class Program
{
    static void Main(string[] args)
    {
        Student s1 = new Student("Student-A",100);
        Student s2 = new Student("Student-B", 90);
        Student s3 = new Student("Student-C", 80);

        List<Student> studentList = new List<Student>() { s1,s2,s3};

        ListToDataTable converter = new ListToDataTable();
        DataTable dt = converter.ToDataTable(studentList);

        Console.WriteLine();
    }
}

以下は、2 つのプロパティを持つ学生クラスです。

    class Student
{
    public string Name { get; set; }
    public int? Score { get; set; }

    public Student(string name,int? score)
    {
        this.Name = name;
        this.Score = score;
    }
}

以下は、オブジェクトのリストをデータテーブルに変換するために使用されるクラスです

public class ListToDataTable
{
    public DataTable ToDataTable<T>(List<T> items)
    {
        DataTable dataTable = new DataTable(typeof(T).Name);            
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {                
            dataTable.Columns.Add(prop.Name);
        }
        foreach (T item in items)
        {
            var values = new object[Props.Length];
            for (int i = 0; i < Props.Length; i++)
            {                    
                values[i] = Props[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }

        return dataTable;
    }
}
4

3 に答える 3

0

これは相互運用機能としてタグ付けされているため、そのルートに進みました (csv ファイルを作成する必要はなく、Excel に直接エクスポートするだけです)。

このソリューションは最も美しいものではありませんが、機能します。また、studentList を直接 Excel にエクスポートできるように、いくつか変更しました (最初に dataTable に変換する必要はありません)。

まず、ソリューションに「Microsoft.Office.Interop.Excel」への参照を追加する必要があります。これを行うには、ソリューション エクスプローラーで [参照] を右クリックし、[参照の追加] をクリックしてから [.NET] タブをクリックし、下にスクロールして見つけます。

それが完了したら、次のようにコードを更新します。

using System;
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;

static void Main()
    {
        var s1 = new Student("Student-A", 100);
        var s2 = new Student("Student-B", 90);
        var s3 = new Student("Student-C", 80);

        var studentList = new List<Student> { s1, s2, s3 };

        // Create an excel sheet
        var xlApp = new Excel.Application { Visible = true };           // Create instance of Excel and make it visible.
        xlApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet);             // Create a workbook (WB)
        var xlWS = (Excel.Worksheet)xlApp.ActiveSheet;                  // Reference the active worksheet (WS)
        xlWS.Name = "Exported Student";                                 // Name the worksheet

        // Add Header fields to Excel [row, column]
        var r = 1;
        xlWS.Cells[r, 1] = "Name";
        xlWS.Cells[r, 2] = "Score";

        // Copy data from StudentList to Excel
        foreach (Student student in studentList)
        {
            r++;
            xlWS.Cells[r, 1] = student.Name;
            xlWS.Cells[r, 2] = student.Score;
        }

    }

これにより、studentList が Excel シートに自動的にエクスポートされます。ListToDataTable クラスは必要ありませんでした。

于 2013-06-11T16:01:07.757 に答える