C#操作EXCEL进行复制单元格

发布时间:2019-06-21 23:18:29.453编辑:佚名阅读(357)

private void test(string filePath)
        {
           try
            {
                Microsoft.Office.Interop.Excel.Application ThisApplication = new Microsoft.Office.Interop.Excel.Application();
                var ThisWorkbook = ThisApplication.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                ThisApplication.DisplayAlerts = false;
                var xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkbook.Worksheets["sheet1"];
                Microsoft.Office.Interop.Excel.Range range = xlSheet.get_Range("P1", Type.Missing);                
              
                Microsoft.Office.Interop.Excel.Range range1 = xlSheet.get_Range("C6", Type.Missing);
                range.Copy(range1);
                ThisWorkbook.Save();//.SaveAs(filePath, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                ThisWorkbook.Close();
                if (ThisWorkbook != null)
                {
                    ThisWorkbook.Close(true, Type.Missing, Type.Missing);
                    ThisWorkbook = null;
                }
                if (ThisApplication != null)
                {
                    ThisApplication.Quit();
                    KillSpecialExcel(ThisApplication);
                    ThisApplication = null;
                }
            }
            catch(Exception ex)
            {
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
}


  关键字:C#EXCEL复制单元格


鼓掌

0

正能量

0

0

呵呵

0


评论区