ich Dir das Excel-Objekt durch die Microsoft.Office.Interop.Excel Referenz verwenden annehmen . Dann müssen Sie Ihren Code ändern, wie folgt
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");
}
Bei den ersten Schritten des Codes eine Prüfung auf das Excel-Objekt, wenn es ohnehin schon ist läuft, durchgeführt wird. Wenn dies der Fall ist, erstellen wir kein neues Excel-Objekt, sondern das, das im System ausgeführt wird. Dann wird die Arbeitsmappe ordnungsgemäß erstellt oder aktualisiert. Beim Speichern muss er mit
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared
gespeichert werden, damit er wieder geöffnet und aktualisiert werden kann.
Um die Eingabe von Daten zu beschleunigen, wird ein modifizierter Code verwendet, den Sie verwenden können.
Sie sollten einen zusätzlichen Knopf hinzufügen, z. button9 und verwenden Sie den zitierten Code im Click-Ereignis mit den erforderlichen Änderungen für die Schaltfläche8, in die Sie Daten eingeben. Außerdem müssen Sie die Variablen xlApp, xlWorkBook, xlWorkSheet etc sein global und PUBLIC wie im folgenden Code deklarieren,
............
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;//...
}
}
//
Hoffe, dass diese nützlich sein kann.
Vielen Dank, ich werde dies versuchen und Ihnen meine Updates geben – Jevon
Ich mache das aber es fragt mich und fragt, ob ich die aktuelle Datei überschreiben möchte. – Jevon
Sie können den aktualisierten Code überprüfen. Es sollte OK funktionieren. – SteveTheGrk