数据无法写入excel文件

时间:2013-08-23 14:14:17

标签: c# excel excel-interop

我编写了代码来读取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; }
}

3 个答案:

答案 0 :(得分: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; }
}

答案 1 :(得分: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

请注意,我在上面的示例中一直非常小心not to use two dots

答案 2 :(得分:0)

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

这会使:

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

完成后:

Marshal.ReleaseComObject(_range);