0

Excelファイルを読み取るコードを書きました。連絡先の値を追加すると、これらの値を Excel ファイルに書き込む必要がありますが、これらの値を Excel ファイルのセルに書き込むことができないという問題があります。空欄!!!!なんで?コードの何が問題になっていますか?

 static void Main(string[] args)
    {

        var contacts = new List<Contact>();

        contacts.Add(new Contact{Firstname = "name 1", Lastname = "lastname 1", Email = "email 1", PhoneNumber = "phone 1"});
        contacts.Add(new Contact { Firstname = "name 2", Lastname = "lastname 2", Email = "email 2", PhoneNumber = "phone 2" });

        Application app = new Application();
       // excelapp.Visible = true;


       // _Workbook workbook = (_Workbook)(excelapp.Workbooks.Add(Type.Missing));
        Workbook workbook = app.Workbooks.Open(@"N:\files\transform_results.xlsx");
        _Worksheet worksheet = workbook.Sheets["Sheet1"];
        Range xlRange = worksheet.UsedRange;
        worksheet = (_Worksheet)workbook.ActiveSheet;


        worksheet.Cells[1, 1] = "First Name";
        worksheet.Cells[1, 2] = "Last Name";
        worksheet.Cells[1, 3] = "Email";


        int row = 4;

        foreach (var contact in contacts)
        {


            worksheet.Cells[1, 1] = contact.Firstname;
            worksheet.Cells[1, 2] = contact.Lastname;
            worksheet.Cells[1, 3] = contact.Email;
            worksheet.Cells[1, 4] = contact.PhoneNumber;
        }

        app.UserControl = true;
    }

public class Contact
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
}
4

3 に答える 3

3

これをテストしましたが、動作します。

static void Main(string[] args)
{
    var contacts = new List<Contact>();

    contacts.Add(new Contact { Firstname = "name 1", Lastname = "lastname 1", Email = "email 1", PhoneNumber = "phone 1" });
    contacts.Add(new Contact { Firstname = "name 2", Lastname = "lastname 2", Email = "email 2", PhoneNumber = "phone 2" });

    Excel.Application app = new Excel.Application();
    app.Visible = true;
    var workbook = app.Workbooks.Open(@"C:\testit.xlsx");
    _Worksheet worksheet = workbook.Sheets["Sheet1"];
    Range xlRange = worksheet.UsedRange;
    worksheet = (_Worksheet)workbook.ActiveSheet;

    worksheet.Cells[1, 1] = "First Name";
    worksheet.Cells[1, 2] = "Last Name";
    worksheet.Cells[1, 3] = "Email";

    int row = 4;

    foreach (var contact in contacts)
    {
        row++;

        worksheet.Cells[row, 1] = contact.Firstname;
        worksheet.Cells[row, 2] = contact.Lastname;
        worksheet.Cells[row, 3] = contact.Email;
        worksheet.Cells[row, 4] = contact.PhoneNumber;
    }

    app.UserControl = true;
    app.Quit();

}

public class Contact
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
}
于 2013-08-23T14:27:37.473 に答える
2

このコードスニペット...

worksheet.Cells[1, 1]

... Range オブジェクトを返します。Value2セルの内容を変更するには、このオブジェクトのプロパティを設定する必要があります。

var cell = worksheet.Cells[1, 1];
cell.Value2 = contact.Firstname;

参照: Microsoft.Office.Interop.Excel._Worksheet.Cells
および: Microsoft.Office.Interop.Excel.Range.Value2

上記の例では、2 つのドットを使用しないように細心の注意を払っていることに注意してください。

于 2013-08-23T14:22:53.960 に答える