我有一个Excel book模板Sheet1,现在要通过程序根据需要把Sheet1复制到Sheet2,sheet3,请教各位用什么方法》》

解决方案 »

  1.   

    Workbooks("Book1").Worksheets("Sheet1").Cells.Copy(null);   
      Workbooks("Book1").Worksheets("Sheet2").Past();   把Book当成数据库,   把Sheet当成数据表.   
      

  2.   

    Workbooks("Book1").Worksheets报错:E:\YSInfo\Web\ExcelInto\ExcelLocate.aspx.cs(177): “Excel._Workbook.Worksheets”表示“属性”,此处应为“方法”
      

  3.   

    可以把excelbook当作一个数据库来进行操作
    String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path";Extended Properties=Excel 8.0;";
    OleDbConnection con = new OleDbConnection(sConnectionString);
    OleDbCommand cmd=new OleDbCommand("SELECT * FROM ["Sheet1"$]",con);
      

  4.   

    不会吧,能不能直接对整个Sheet操作,不然向数据库那样,会不会很麻烦,而且Excel中还存在图片
      

  5.   

    对不起,没看清你的问题
    整体复制的话就这样
    Excel.WorkSheet sheet=excelBook.WorkSheets[0];
    excelBook.WorkSheets.Add(sheet,Type.Missing,Type.Missing,Type.Missing);
      

  6.   

    好像没有用啊,还是只有一个Sheet
      

  7.   

    Application xApp   =   new Application();   
    object oMiss = Missing.Value;
    Workbook xBook   =   xApp.Workbooks.Open(templateFilePath, null,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss);   

    Excel.Worksheet sheet = (Excel.Worksheet)xBook.Worksheets[1];
    xBook.Worksheets.Add(sheet,Type.Missing,Type.Missing,Type.Missing);
    但是book中只有一个Sheet,根本没有复制出来的
      

  8.   

    我试了下
    WorkSheets.Add好象只能添加一个空的sheet,不能进行复制
    用sheet.Copy(sheet,Type.Missing)可以复制
    里面的参数是所复制的sheet的位置
      

  9.   

    sheet有一个clone方法,可以进行复制,然后把复制的这个sheet再加到book中
      

  10.   

    修改后的代码
    Application xApp   =   new Application();   
    object oMiss = Missing.Value;
    Workbook xBook   =   xApp.Workbooks.Open(templateFilePath, null,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss);   

    Excel.Worksheet sheet = (Excel.Worksheet)xBook.Worksheets[1];
    sheet.Copy(sheet,Type.Missing);还是没有复制出来,急啊
      

  11.   

    我将sheet.Copy(sheet,Type.Missing);中的Type.Missing改成"Sheet2"就报错:HRESULT 中的异常:0x800A03EC。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: HRESULT 中的异常:0x800A03EC。源错误: 
    行 177:
    行 178: Excel.Worksheet sheet = (Excel.Worksheet)xBook.Worksheets[1];
    行 179: sheet.Copy(sheet,"Sheet2");
    行 180:
    行 181:
     
      

  12.   

    Excel.Worksheet Shett2= (Excel.Worksheet)objBook.Worksheets.get_Item(2);
                        Shett2.Copy(Type.Missing, Shett2);
    把Sheet1复制到Sheet2,我当时也为这个问题郁闷了很久!
      

  13.   

    song_better() :
     sheet1怎么没在代码中体现啊
      

  14.   

    最新修改:
    Application xApp   =   new Application();   
    object oMiss = Missing.Value;
    Workbook xBook   =   xApp.Workbooks.Open(templateFilePath, null,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss);   

    Excel.Worksheet Shett2= (Excel.Worksheet)xBook.Worksheets.get_Item(2);
    Shett2.Copy(Type.Missing, Shett2);
    xBook = null;
    xApp.Quit();
    xApp = null;还是没有复制成功,各位能给出能用的详细代码吗
      

  15.   

    能复制了,但是有出来一个问题,复制后的book标签是 Sheet1/Sheet2/Sheet1(2)/Sheet3
    能不能直接江Sheet2变成新复制的Sheet
      

  16.   

    Microsoft.Office.Interop.Excel.Application _app = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook _workBook =null;
     
    //头
    _workBook = _app.Workbooks.Open(Server.MapPath("PriceStat_01.xls"), Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value);  
    Microsoft.Office.Interop.Excel.Worksheet _workSheet1 = (Worksheet)_workBook.Worksheets["表尾Excel"];
    Microsoft.Office.Interop.Excel.Worksheet _workSheet2= (Worksheet)_workBook.Worksheets["表头Excel"];

    Range cell1;
    Range cell2;
    Range cell3;
    Range cell4;
        
    cell1 = (Range)_workSheet1.Cells[1, 1];
    cell2 = (Range)_workSheet1.Cells[10,10];
    //Sheet1被拷贝的rang
    Range rangeBegin = _workSheet1.get_Range(cell1, cell2); cell3 = (Range)_workSheet2.Cells[12,1];
    cell4 = (Range)_workSheet2.Cells[22,1]; //Sheet2拷贝到的rang
    Range rangeEnd = _workSheet2.get_Range(cell3, cell4);
    rangeBegin.Copy(rangeEnd);
    //尾部
     
     _workSheet1.Visible = XlSheetVisibility.xlSheetHidden ;
    _workBook.SaveAs(Server.MapPath("导出元件1.xls"), 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    //_workBook2.SaveAs(Server.MapPath("导出元件.xls"), 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    //垃圾回收
    if (_app != null)
    {
        
    if (_app.ActiveWorkbook != null)
    {
    _app.ActiveWorkbook.Close(false, Missing.Value, Missing.Value);
    }
    _app.Quit();
    IntPtr t = new IntPtr(_app.Hwnd);
        
    }
    GC.Collect();