C# 频繁向Excel表格中追加数据
现用C#读取一硬件中的数据,然后写入到Excel表格中,数据量很小,但要求频繁追加到Excel表格中(第一次新建一表格,后面全部将数据追加到Excel表格中).时间越快越好 小于0.2S.
现写一程序,测试了下,发现达不到要求,偶尔还会报错,提示无法访问文件,也就是上一周期的Excel表格没有关闭,下一周期又来打开.
代码如下:求各位指点,谢谢
private void AutoWriteToExcel()//向Excel表格中写入数据
{
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)Excel.Workbooks;
int k = 0;
IntPtr intptr = new IntPtr(Excel.Hwnd);
if (IsFirstLoad==true) //第二次写入时直接打开第一新建的表格
{
Microsoft.Office.Interop.Excel.Workbook book = books.Open(FileName, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
Microsoft.Office.Interop.Excel.Range range = null;
int Row = sheet1.UsedRange.Count/3;
range = sheet1.UsedRange;
//range = sheet1.get_Range;
string[,] objVal = new string[4, 4];
for (int i = 0; i < this.listView1.Columns.Count; i++)
{
objVal[0, i] = this.listView1.Columns[i].Text;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
objVal[i+1, j] = this.listView1.Items[i].SubItems[j].Text;
}
}
range = sheet1.get_Range("A"+(Row+1).ToString(), "C"+(Row+4).ToString());
range.NumberFormatLocal = "@";
range.Value2 = objVal;
//range.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb(); //设置单元格的背景色
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
Excel.DisplayAlerts = false;
Excel.AlertBeforeOverwriting = true;
//System.Windows.Forms.Application.DoEvents();
try
{
sheet1.SaveAs(FileName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
book.Close(false, miss, miss);
books.Close();
Excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel);
KillSpecialExcel(Excel);
//GetWindowThreadProcessId(intptr, out k);
//System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
//p.Kill();
GC.Collect();
}
else //第一次写入,新建一表格
{
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
Microsoft.Office.Interop.Excel.Range range = null;
Microsoft.Office.Interop.Excel.Range range2 = null;
string[,] objVal = new string[4,4];
for (int i = 0; i < this.listView1.Columns.Count; i++)
{
objVal[0,i] = this.listView1.Columns[i].Text;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
objVal[i+1,j] = this.listView1.Items[i].SubItems[j].Text;
}
}
range = sheet1.get_Range("A1", "C4");
range.NumberFormatLocal = "@";
range.Value2 = objVal;
//range.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb(); //设置单元格的背景色
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
Excel.DisplayAlerts = false;
Excel.AlertBeforeOverwriting = true;
//System.Windows.Forms.Application.DoEvents();
try
{
sheet1.SaveAs(FileName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
book.Close(false, miss, miss);
books.Close();
Excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel);
GetWindowThreadProcessId(intptr, out k);
//System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
//p.Kill();
KillSpecialExcel(Excel); //杀死进程
GC.Collect();
IsFirstLoad = true;
}
}
public void KillSpecialExcel(Microsoft.Office.Interop.Excel.ApplicationClass Excel)//杀死线程
{
IntPtr intptr = new IntPtr(Excel.Hwnd);
try
{
if (Excel != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(intptr, out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
bok.WorkSheets["test1"].Range["W29].Resize(200,35)。Value2 = arr;
wb.Application.EnableEvents = false;
wb.Application.Interactive = false;
wb.Application.ScreenUpdating = false;
wb.Application.EnableEvents = true;
wb.Application.Interactive = true;
wb.Application.ScreenUpdating = true;