2015-01-05 50 views
0

我有一個Windows應用程序,提交後創建一個Excel文件(如果尚未創建)或附加到Excel文件。該文件每次都在創建,但我也收到一條消息,詢問是否要複製該文件。我想附加到該文件不重寫該文件。有人能告訴我我做錯了什麼嗎?保存數據到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(); 

    } 
} 
+0

'SaveAs'方法將保存對工作簿的更改在一個**不同的文件**。 – luke2012

回答

0

File.CreateText()方法打開一個文件中寫入UTF-8編碼的文本和File.AppendText()方法創建StreamWriter爲UTF-8編碼的文件追加到現有文件中。 Excel文件與文本文件不同。您可能需要像使用Excel Interop一樣使用Excel Interop,或者使用諸如Open XML之類的東西。

的代碼修改後是:

string filePath = "C:\\Temp\\test.xlsx"; 

Excel.Application oApp; 
Excel.Worksheet oSheet; 
Excel.Workbook oBook; 

oApp = new Excel.Application(); 

if (!File.Exists(filePath)) 
{ 
    oBook = oApp.Workbooks.Add(); 
    oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1); 

    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(); 
    oBook.SaveAs(filePath); 
} 
else 
{ 
    oBook = oApp.Workbooks.Open(@"C:\Test\YourWorkbook.xlsx"); 

    oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1); 
    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(); 
    oBook.Save(); 
} 

oBook.Close(); 
oApp.Quit(); 
oSheet = null; 
oBook = null; 
oApp = null;  
+0

謝謝。我做了上面的修改,文件沒有被重新創建,但新的數據沒有被追加。它正在寫入當前在excel文件中的任何數據。我錯過了一步嗎? – user1863593

+0

我想出了我失蹤的一步。我需要增加rowCount並修改單元格: – user1863593

0

您正在創建新的Excel工作表每次調用該函數的時間。如果你想把數據追加到現有的excel文件中,你必須先打開它,追加你需要的數據,然後調用save函數。

您可以打開現有的這樣

var xlApp = new Excel.Application(); 
xlApp.Workbooks.Open(path); 

現在你可以修改它的數據,當您完成後,保存並關閉。

0

我想出步驟我失蹤。我需要增加rowCount並修改單元格。它看起來按需要工作:

else 
      { 
       oBook = oApp.Workbooks.Open(@"C:\\Temp\\test.xlsx"); 
       oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1); 

       Microsoft.Office.Interop.Excel.Range range = oSheet.UsedRange; 

       int rowCount = range.Rows.Count + 1; 



       oSheet.Cells[rowCount, 2] = txtFirstName.Text; 
       oSheet.Cells[rowCount, 3] = txtLastName.Text; 
       oSheet.Cells[rowCount, 4] = txtAddress.Text; 
       oSheet.Cells[rowCount, 5] = txtCity.Text; 
       oSheet.Cells[rowCount, 6] = txtState.Text; 
       oSheet.Cells[rowCount, 7] = txtZipCode.Text; 
       oSheet.Cells[rowCount, 8] = txtPhone.Text; 
       oSheet.Cells[rowCount, 9] = txtEmail.Text; 
       oSheet.Cells[rowCount, 10] = dtpDOB.Value; 
       oSheet.Cells[rowCount, 11] = genderSet.ToString(); 
       oSheet.Cells[rowCount, 12] = txtHighSchool.Text; 
       oSheet.Cells[rowCount, 13] = txtGraduationYear.Text; 
       oSheet.Cells[rowCount, 14] = txtTermofEnrollment.Text; 
       oSheet.Cells[rowCount, 15] = outText.ToString(); 
       oBook.Save(); 
      }