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; }
}