c# 将excel的某sheet复制到另一个excel中

时间:2022-06-03 19:57:59
报异常:Exception from HRESULT: 0x800A03EC
在copy那一步
            
object oMissing = System.Reflection.Missing.Value;
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            ApplicationClass objExl_Source = new ApplicationClass();
            objExl_Source.EnableEvents = false;
            objExl_Source.DisplayAlerts = false;
            objExl_Source.Visible = true;
            objExl_Source.ScreenUpdating = true;

            Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            ApplicationClass objExl_Destine = new ApplicationClass();
            objExl_Destine.EnableEvents = false;         
            objExl_Destine.DisplayAlerts = false;       
            objExl_Destine.Visible = true;         
            objExl_Destine.ScreenUpdating = true;

            Workbook objWBook_Destine = objExl_Destine.Workbooks.Open(_sPath_Destine,
                oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,
                oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);

            //Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source,
            //   0,false,5,oMissing,oMissing,false,XlPlatform.xlWindows,
            //   oMissing,true,false,0,true,false,false);

            //Workbook objWBook_Destine = objExl_Destine.Workbooks.Open(_sPath_Destine,
            //   0, false, 5, oMissing, oMissing, false, XlPlatform.xlWindows,
            //   oMissing, true, false, 0, true, false, false);

            Worksheet objWSheet_Source = (Worksheet)objWBook_Source.Worksheets[strSheetName];

            objWSheet_Source.Copy((Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count], oMissing);
            

       

            //objWBook_Source.Save();
            objWBook_Destine.Save();

            objExl_Source.Quit();
            objExl_Destine.Quit();

6 个解决方案

#1


#2


用一个进程打开两个工作book.

 string _sPath_Source = "d:\\a.xls";
            string _sPath_Destine = "d:\\b.xls";
            object oMissing = System.Reflection.Missing.Value;
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            ApplicationClass objExl_Source = new ApplicationClass();
            objExl_Source.EnableEvents = false;
            objExl_Source.DisplayAlerts = false;
            objExl_Source.Visible = false;
          //  objExl_Source.ScreenUpdating = true;

            Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            
            //ApplicationClass objExl_Destine = new ApplicationClass();
            //objExl_Destine.EnableEvents = false;
            //objExl_Destine.DisplayAlerts = false;
            //objExl_Destine.Visible = false;
          //  objExl_Destine.ScreenUpdating = true;

            Workbook objWBook_Destine = objExl_Source.Workbooks.Open(_sPath_Destine,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            //Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source, 
            //  0,false,5,oMissing,oMissing,false,XlPlatform.xlWindows, 
            //  oMissing,true,false,0,true,false,false); 

            //Workbook objWBook_Destine = objExl_Destine.Workbooks.Open(_sPath_Destine, 
            //  0, false, 5, oMissing, oMissing, false, XlPlatform.xlWindows, 
            //  oMissing, true, false, 0, true, false, false); 
            string strSheetName = "Sheet1";
            Worksheet objWSheet_Source = (Worksheet)objWBook_Source.Worksheets[strSheetName];

            objWSheet_Source.Copy((Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count], oMissing);


            

            //objWBook_Source.Save(); 
            objWBook_Destine.Save();
            objWBook_Destine = null;
            objExl_Source.Quit();
           // objExl_Destine.Quit();

#3


帮忙顶上去!

#5


先看看例子 

#6


up有分没....

#1


#2


用一个进程打开两个工作book.

 string _sPath_Source = "d:\\a.xls";
            string _sPath_Destine = "d:\\b.xls";
            object oMissing = System.Reflection.Missing.Value;
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            ApplicationClass objExl_Source = new ApplicationClass();
            objExl_Source.EnableEvents = false;
            objExl_Source.DisplayAlerts = false;
            objExl_Source.Visible = false;
          //  objExl_Source.ScreenUpdating = true;

            Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            
            //ApplicationClass objExl_Destine = new ApplicationClass();
            //objExl_Destine.EnableEvents = false;
            //objExl_Destine.DisplayAlerts = false;
            //objExl_Destine.Visible = false;
          //  objExl_Destine.ScreenUpdating = true;

            Workbook objWBook_Destine = objExl_Source.Workbooks.Open(_sPath_Destine,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            //Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source, 
            //  0,false,5,oMissing,oMissing,false,XlPlatform.xlWindows, 
            //  oMissing,true,false,0,true,false,false); 

            //Workbook objWBook_Destine = objExl_Destine.Workbooks.Open(_sPath_Destine, 
            //  0, false, 5, oMissing, oMissing, false, XlPlatform.xlWindows, 
            //  oMissing, true, false, 0, true, false, false); 
            string strSheetName = "Sheet1";
            Worksheet objWSheet_Source = (Worksheet)objWBook_Source.Worksheets[strSheetName];

            objWSheet_Source.Copy((Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count], oMissing);


            

            //objWBook_Source.Save(); 
            objWBook_Destine.Save();
            objWBook_Destine = null;
            objExl_Source.Quit();
           // objExl_Destine.Quit();

#3


帮忙顶上去!

#4


#5


先看看例子 

#6


up有分没....