需要有关Excel问题的帮助(VSTO) [英] Need Help with Excel problem (VSTO)

查看:114
本文介绍了需要有关Excel问题的帮助(VSTO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码行...

The follow line of code...

wbkSource.Sheets(strFromSht).Copy(wbk.Sheets(1));


出现错误


getting error

Error	2	Non-invocable member 'Microsoft.Office.Interop.Excel._Workbook.Sheets' cannot be used like a method.	 

有人可以帮我吗.我的目的是将工作表复制到另一个
工作簿

Can someone please help me. My intent is to copy a worksheet to another
workbook

private String strWorkBookLocation = "C:\\PivotFiles\\source.xls";
       private String strWbkLocation      = "C:\\PivotFiles\\temp.xls";
       private String cstrPivotTableSheet = "Pivot Table";
       private String strFromSht          = "Source";

               Excel.Application app;
               Excel.Workbook wbk;
               Excel.Worksheet sht;
               Excel.Worksheet sh;
               Excel.Workbook wbkSource;
               DataSet dSet = new DataSet();
               DataGrid dg = new DataGrid();
               int y = 2;
               int x = 0;

               try
               {


                   FetchMFRData(ref dSet, "12/24/2010", "12/24/2010");
                   dg.DataSource = dSet;
                   dg.DataBind();
                   FileStream fs = new FileStream(strWorkBookLocation, FileMode.Create);
                   StreamWriter tw = new StreamWriter(fs);
                   HtmlTextWriter writer = new HtmlTextWriter(tw);
                   dg.RenderControl(writer);
                   writer.Flush();
                   writer.Close();
                   tw.Close();
                   fs.Close();

                   app = new Excel.Application();
                   app.DisplayAlerts = false;

                   wbkSource = app.Workbooks.Open(strWorkBookLocation,
                               Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               Type.Missing, Type.Missing);
                   wbk       = app.Workbooks.Open(strWbkLocation,
                               Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                               Type.Missing, Type.Missing);

                   // This loop is used for deleting all sheets in the workbook
                   // except the Pivot table sheet.  Generally the only reason
                   // another sheet may be present is if there was an error
                   // during another run and the source sheet didn't get deleted.

                   foreach (Excel.Worksheet sheet in wbk.Sheets)
                   {
                       if (sheet.Name.Equals(cstrPivotTableSheet) == false)
                           sheet.Delete();
                   }

                  wbkSource.Sheets(strFromSht).Copy(wbk.Sheets(1));


               }
               catch (Exception ex)
               {
               }
           }
       }

推荐答案

如果您尝试怎么办:
What happens if you try:
Excel.Worksheet sh_temp = wbkSource.Worksheets(strFromSht);
sh_temp.Copy(wbk.Sheets(1));


这篇关于需要有关Excel问题的帮助(VSTO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆