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

请注意,在上面的示例中,我非常小心不要使用两个点

于 2013-08-23T14:22:53.960 回答
0

我确实想补充一点,您需要小心使用这种代码。使用互操作性时,您需要做更多的内存管理。尝试始终在使用它们之前声明您的对象,然后清除它们。尽量不要在您的互操作代码中包含超过 2 个点 (.)(例如 object1.object2.value)

这将使:

Microsoft.Office.Interop.Excel.Range _range;
_range = worksheet.get_Range("A1", "A1");
_range.Value2 = contact.Firstname;

完成后:

Marshal.ReleaseComObject(_range);
于 2013-08-23T14:29:11.250 回答