使用微软自带的Microsoft.Office.Interop.Excel保存数据到excel,能达到目的,但是很慢,要600到700多毫秒

  1. NUGET添加Microsoft.Office.Interop.Excel动态库
  2. 引用的命名空间
    using System.Reflection;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.IO;
    using System.Runtime.InteropServices;

     

  3. 核心代码(没有文件,就创建一个文件,有的话直接在最后追加)
     Excel.Application excelApp = new Excel.Application();
                excelApp.AlertBeforeOverwriting = false;
                Excel.Workbook excelDoc = null;
    
                object Nothing = Missing.Value;
                Excel.Worksheet ws = null;
    
                string FileName = "";
    
                try
                {
    
                    string strFilePath = @"D:\111111";
    
                    if (index == 1)
                    {
                        strFilePath += @"\1\";
                    }
                    else
                    {
                        strFilePath += @"\2\";
                    }
                   // strFilePath += @"\";
                    strFilePath += System.DateTime.Now.ToString("yyyy");
                    strFilePath += @"\";
                    strFilePath += System.DateTime.Now.ToString("MM");
    
                    if (System.IO.Directory.Exists(strFilePath) == false)
                    {
                        System.IO.Directory.CreateDirectory(strFilePath);
                    }
    
                    FileName = strFilePath + @"\" + System.DateTime.Now.ToString("dd") + @".xlsx";
    
                    if (System.IO.File.Exists(FileName) == false)
                    {
                        excelDoc = excelApp.Workbooks.Add(Nothing);
                        ws = (Excel.Worksheet)excelDoc.Sheets[1];
                        ws.Cells.Columns.AutoFit();
                        ws.Cells[1, "A"] = "时间";
                        ws.Cells[1, "B"] = "xxxx";
                        ws.Cells[1, "C"] = "yyyy";
    
                        ws.Cells[2, "A"] = System.DateTime.Now.ToString("HH:mm:ss");
                        ws.Cells[2, "B"] = 121111;
                        ws.Cells[2, "C"] = 123123123;
    
                        excelDoc.SaveAs(FileName, Nothing, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);
                    }
                    else
                    {
                        excelDoc = excelApp.Workbooks.Open(FileName,false, false, Excel.XlPlatform.xlWindows, true, false, true, true, Missing.Value);
                        ws = (Excel.Worksheet)excelDoc.Worksheets.get_Item(1);
                        ws.Cells.Columns.AutoFit();
                        Excel.Range range = ws.UsedRange;
                        int usedRowCount = range.Rows.Count;
    
                        ws.Cells[usedRowCount + 1, "A"] = System.DateTime.Now.ToString("HH:mm:ss");
                        ws.Cells[usedRowCount + 1, "B"] = 11111;
                        ws.Cells[usedRowCount + 1, "C"] = 123123123;
    
                        excelDoc.Save();
                    }
                }
                catch (Exception exp)
                {
    
                }
                finally
                {
                    excelDoc.Close(true, Nothing, Nothing);
                    excelApp.Quit();
    
                    IntPtr t = new IntPtr(excelApp.Hwnd);          //杀死进程的好方法,很有效
                    int k = 0;
                    GetWindowThreadProcessId(t, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }

     

  4. 要引用这个函数,杀进程的,不然程序退出了,任务管理器里面还有excel程序未关闭
 [DllImport("User32.dll", CharSet = CharSet.Auto)]
 public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

 

你可能感兴趣的:(.NET)