日期:2014-05-17  浏览次数:20883 次

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&n