제출시 Excel 파일을 만들거나 (아직 만들지 않은 경우) Excel 파일에 추가하는 Windows 응용 프로그램이 있습니다. 파일은 매번 생성되지만 파일을 복사 할 것인지 묻는 메시지가 나타납니다. 파일을 다시 쓰지 않고 파일에 추가하고 싶습니다. 누군가 내가 뭘 잘못하고 있다고 말할 수 있습니까?Excel에 데이터 저장
다음은 내 제출 기능에 내 코드입니다 :
string filePath = "C:\\Temp\\test.xlsx";
Excel.Application oApp;
Excel.Worksheet oSheet;
Excel.Workbook oBook;
oApp = new Excel.Application();
oBook = oApp.Workbooks.Add();
oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
if (!File.Exists(filePath))
{
using (File.CreateText(filePath))
{
oSheet.Cells[1, 2] = "First Name"; oSheet.Cells[1, 3] = "Last Name"; oSheet.Cells[1, 4] = "Street Address"; oSheet.Cells[1, 5] = "City"; oSheet.Cells[1, 6] = "State";
oSheet.Cells[1, 7] = "Zip Code"; oSheet.Cells[1, 8] = "Phone"; oSheet.Cells[1, 9] = "Email"; oSheet.Cells[1, 10] = "DOB"; oSheet.Cells[1, 11] = "Gender"; oSheet.Cells[1, 12] = "High School";
oSheet.Cells[1, 13] = "Graduation Year"; oSheet.Cells[1, 14] = "Planned Term"; oSheet.Cells[1, 15] = "Intended Major";
oSheet.Cells[2, 2] = txtFirstName.Text; oSheet.Cells[2, 3] = txtLastName.Text; oSheet.Cells[2, 4] = txtAddress.Text; oSheet.Cells[2, 5] = txtCity.Text; oSheet.Cells[2, 6] = txtState.Text;
oSheet.Cells[2, 7] = txtZipCode.Text; oSheet.Cells[2, 8] = txtPhone.Text; oSheet.Cells[2, 9] = txtEmail.Text; oSheet.Cells[2, 10] = dtpDOB.Value; oSheet.Cells[2, 11] = genderSet.ToString();
oSheet.Cells[2, 12] = txtHighSchool.Text; oSheet.Cells[2, 13] = txtGraduationYear.Text; oSheet.Cells[2, 14] = txtTermofEnrollment.Text; oSheet.Cells[2, 15] = outText.ToString();
}
}
else
{
using (File.AppendText(filePath))
{
oSheet.Cells[2, 2] = txtFirstName.Text; oSheet.Cells[2, 3] = txtLastName.Text; oSheet.Cells[2, 4] = txtAddress.Text; oSheet.Cells[2, 5] = txtCity.Text; oSheet.Cells[2, 6] = txtState.Text;
oSheet.Cells[2, 7] = txtZipCode.Text; oSheet.Cells[2, 8] = txtPhone.Text; oSheet.Cells[2, 9] = txtEmail.Text; oSheet.Cells[2, 10] = dtpDOB.Value; oSheet.Cells[2, 11] = genderSet.ToString();
oSheet.Cells[2, 12] = txtHighSchool.Text; oSheet.Cells[2, 13] = txtGraduationYear.Text; oSheet.Cells[2, 14] = txtTermofEnrollment.Text; oSheet.Cells[2, 15] = outText.ToString();
}
}
'SaveAs' 메소드는 ** 다른 파일 **에서 통합 문서의 변경 사항을 저장합니다. – luke2012