2016-11-10 75 views
0

我有這個Excel,目前從我的C#應用​​程序到其細胞內容寫入內容的文件的文件:追加到Excel中已創建

private void button8_Click(object sender, EventArgs e) 
    { 
     Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 

     if (xlApp == null) 
     { 
      MessageBox.Show("Excel is not properly installed!!"); 
      return; 
     } 


     Excel.Workbook xlWorkBook; 
     Excel.Worksheet xlWorkSheet; 
     object misValue = System.Reflection.Missing.Value; 

     xlWorkBook = xlApp.Workbooks.Add(misValue); 
     xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

     xlWorkSheet.Cells[1, 1] = comboBox2.Text; 
     xlWorkSheet.Cells[1, 2] = textBox5.Text; 
     xlWorkSheet.Cells[1, 3] = textBox2.Text; 
     xlWorkSheet.Cells[1, 4] = comboBox3.Text; 
     xlWorkSheet.Cells[1, 5] = textBox3.Text; 
     xlWorkSheet.Cells[1, 6] = comboBox1.Text; 



     xlWorkBook.SaveAs(@"cross_check.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 

     Marshal.ReleaseComObject(xlWorkSheet); 
     Marshal.ReleaseComObject(xlWorkBook); 
     Marshal.ReleaseComObject(xlApp); 

     MessageBox.Show("Excel file created succcessfully"); 
    } 

} 

如何追加到同一個文件已經被創造出來的?爲了進一步擴展,目前我必須指定這些值必須添加到的單元格。我怎麼喜歡以某種方式增加,就像無論用戶點擊添加到文件按鈕的次數,它應該增加以前的模式。 例如。我有:

 xlWorkSheet.Cells[1, 1] = comboBox2.Text; 
     xlWorkSheet.Cells[1, 2] = textBox5.Text; 
     xlWorkSheet.Cells[1, 3] = textBox2.Text; 
     xlWorkSheet.Cells[1, 4] = comboBox3.Text; 
     xlWorkSheet.Cells[1, 5] = textBox3.Text; 
     xlWorkSheet.Cells[1, 6] = comboBox1.Text; 

在點擊按鈕時,我會怎麼做,現在遵循此模式:

 xlWorkSheet.Cells[2, 1] = comboBox2.Text; 
     xlWorkSheet.Cells[2, 2] = textBox5.Text; 
     xlWorkSheet.Cells[2, 3] = textBox2.Text; 
     xlWorkSheet.Cells[2, 4] = comboBox3.Text; 
     xlWorkSheet.Cells[2, 5] = textBox3.Text; 
     xlWorkSheet.Cells[2, 6] = comboBox1.Text; 

回答

1

我想你通過 的Microsoft.Office.Interop.Excel參考使用Excel對象。然後,你必須修改你的代碼 如下

 private void button8_Click(object sender, EventArgs e) 
     { 
     Microsoft.Office.Interop.Excel.Application xlApp; //Declare the 
       //Excel object 
      try 
      { 
      xlApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); 
      } 
      catch (Exception ee) 
      { 
       xlApp = new Microsoft.Office.Interop.Excel.Application(); 


       if (xlApp == null) 
       { 
       MessageBox.Show("Excel is not properly installed!!"); 
       return; 
       } 


      } 

     if (xlApp == null) 
     { 
      MessageBox.Show("Excel is not properly installed!!"); 
      return; 
     } 



     object misValue = System.Reflection.Missing.Value; 


     Microsoft.Office.Interop.Excel.Workbook xlWorkBook=xlApp.Workbooks.Add(misValue); 

    try 
    { 

     xlWorkBook = xlApp.Workbooks.Open(@"cross_check.xls");//, 


    } 
    catch (Exception ex) 
    { 

    ;//  
    } 
     Microsoft.Office.Interop.Excel.Range range; 

     Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = 
     (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
     int rownum = 1; 
     int MAX_ROWS=30000; //You may define your own limit 
     bool written = true; 
     range = xlWorkSheet.UsedRange; 
     while ((written) && (rownum<MAX_ROWS)) 
     { 
     var test = (range.Cells[rownum, 1] as 
     Microsoft.Office.Interop.Excel.Range).Value2; 
     if (test != null) 
      { 
      rownum++; 
      } 
     else 
      { 
      written = false; 
      } 
     } 
     if (written == false) 
     { 
     xlWorkSheet.Cells[rownum, 1] = comboBox2.Text; 
     xlWorkSheet.Cells[rownum, 2] = textBox5.Text; 
     xlWorkSheet.Cells[rownum, 3] = textBox2.Text; 
     xlWorkSheet.Cells[rownum, 4] = comboBox3.Text; 
     xlWorkSheet.Cells[rownum, 5] = textBox3.Text; 
     xlWorkSheet.Cells[rownum, 6] = comboBox1.Text; 
     } 
     xlApp.DisplayAlerts = false; //Disables the prompts 
     xlWorkBook.SaveAs(@"cross_check.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue); 
     xlApp.DisplayAlerts = true; // 

     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 

     Marshal.ReleaseComObject(xlWorkSheet); 
     Marshal.ReleaseComObject(xlWorkBook); 
     Marshal.ReleaseComObject(xlApp); 

     MessageBox.Show("Excel file created/updated succcessfully"); 

    } 

在代碼爲Excel對象進行檢查的第一步,如果它已經 運行,執行。如果是這樣,我們不創建一個新的Excel對象,但我們使用系統中正在運行的一個。然後該工作簿已正確創建或更新。保存時,必須保存爲

 Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared 

爲了能夠重新打開並更新它。

要加速數據輸入修改後的代碼,您可能會使用引號。

您應該添加一個附加按鈕,例如button9並在click事件中使用帶引號的代碼,並對輸入數據的button8進行必要的修改。 此外,你必須給變量xlApp,xlWorkBook,xlWorkSheet等申報 是全球性的和公衆在下面的代碼,

............ 
    public bool startd = false; 
    public int rownum; 
    public int MAX_ROWS = 30000;//You may define your own limit here 
    public bool isFirstTime = true; 
    public string oldBtnFileText; 
    public string oldBtnDataText; 
    public Microsoft.Office.Interop.Excel.Application xlApp; 
    public Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
    public Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
    public Microsoft.Office.Interop.Excel.Range range; 

    public object misValue; 


    private void button8_Click(object sender, EventArgs e) 
    { 
     if (startd == false) 
     { 

      return; 
     } 
     if (isFirstTime == true) 
     { 
      bool written = true; 
      int rnum = 1; 
      if (xlWorkSheet!=null) 
      { 
       range=xlWorkSheet.UsedRange; 
      while ((written) && (rnum < MAX_ROWS)) 
      { 
       var test = (range.Cells[rnum, 1] as Microsoft.Office.Interop.Excel.Range).Value2; 
       if (test != null) 
       { 
        rnum++; 
       } 
       else 
       { 
        written = false; 
       } 
      } 
      if (written == false) 
      { 
       rownum = rnum; 
       isFirstTime = false; 
      } 
      else 
      { 
       MessageBox.Show("The current WorkSheet is Full"); 
       return; 
      } 
     } 
     } 
     if (xlWorkSheet!=null) 
     { 
      xlWorkSheet.Cells[rownum, 1] = comboBox2.Text; 
      xlWorkSheet.Cells[rownum, 2] = textBox5.Text; 
      xlWorkSheet.Cells[rownum, 3] = textBox2.Text; 
      xlWorkSheet.Cells[rownum, 4] = comboBox3.Text; 
      xlWorkSheet.Cells[rownum, 5] = textBox3.Text; 
      xlWorkSheet.Cells[rownum, 6] = comboBox1.Text; 
      rownum++; 
     } 

    } 
    private void button9_Click(object sender, EventArgs e) 
    { 
     if (startd == false) 
     { 
      try 
      { 
       xlApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); 
      } 
      catch (Exception ee) 
      { 
       xlApp = new Microsoft.Office.Interop.Excel.Application(); 
       if (xlApp == null) 
       { 
        MessageBox.Show("Excel is not properly installed!!"); 
        return; 
       } 
      } 
      if (xlApp == null) 
      { 
       MessageBox.Show("Excel is not properly installed!!"); 
       return; 
      } 
      misValue = System.Reflection.Missing.Value; 
      xlWorkBook = xlApp.Workbooks.Add(misValue); 
      try 
      { 
       xlWorkBook = xlApp.Workbooks.Open(@"cross_check.xls");//, 
      } 
      catch (Exception ex) 
      { 
       ;//  
      } 
      xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
      oldBtnFileText = button9.Text.ToString(); 
      button9.Text = "File Ready to accept data"; 
      oldBtnDataText = button1.Text.ToString(); 
      button8.Text = "Enter Data"; 
      startd = true; 
     } 
     else 
     { 
      xlApp.DisplayAlerts = false; 
      xlWorkBook.SaveAs(@"cross_check.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue); 
      xlApp.DisplayAlerts = true; 
      xlWorkBook.Close(true, misValue, misValue); 
      xlApp.Quit(); 

      Marshal.ReleaseComObject(xlWorkSheet); 
      Marshal.ReleaseComObject(xlWorkBook); 
      Marshal.ReleaseComObject(xlApp); 

      MessageBox.Show("Excel file created/updated succcessfully"); 

      startd = false; 
      button9.Text = oldBtnFileText; //Restore the initial captions 
      button8.Text = oldBtnDataText;//... 
     } 

    } 
// 

希望這些可能是有用的。

+0

謝謝,我會嘗試這個,並給你我的更新 – Jevon

+0

我這樣做,但它會提示我,並詢問是否要覆蓋當前文件。 – Jevon

+0

您可以檢查更新的代碼。它應該可以正常工作。 – SteveTheGrk