一个Sheet作为程序打印报表的模板,里面有4个Sheet。
真正用到的只有1个Sheet,其它3个Sheet要删除。
怎么用C#代码完成?

解决方案 »

  1.   

    using Excel=Microsoft.Office.Interop.Excel;
    ....... Object refmissing = System.Reflection.Missing.Value;
    Excel._Application exc = new Excel.ApplicationClass();
    exc.Visible = true;
    Excel.Workbooks workbooks = exc.Workbooks;
                workbooks._Open("c:\\test.xls",refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing);
    Excel.Worksheet sheet = (Excel.Worksheet)exc.Worksheets.get_Item(1);
    sheet.Delete();
    exc.Save(refmissing);

    MessageBox.Show("ok");
      

  2.   

    请参见我的blog
    http://linfuguo.cnblogs.com/
    及下面这个文件
    http://www.cnblogs.com/linfuguo/articles/462699.html
      

  3.   

    不行?why不行?报什么错?贴出来看看,应该是可以运行的代码啊
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.Interop.Excel;namespace exceldemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Application app = new Application();
                    app.Visible = true;
                    app.UserControl = true;
                    app.DisplayAlerts = false;
                    Workbooks myworkbooks = app.Workbooks;
                    Workbook myworkbook = myworkbooks.Add(@"C:\Documents and Settings\Administrator\My Documents\我接收到的文件\book.xls");
                    Worksheet mysheet = (Worksheet)myworkbook.Worksheets.get_Item(1);
                    mysheet.Delete();
                    myworkbook.Save();
                    myworkbooks.Application.Save(@"C:\Documents and Settings\Administrator\My Documents\我接收到的文件\book.xls");
                    //string str = ((Range)mysheet.Cells[1, 1]).Text.ToString(); int i = mysheet.Rows.Count;
                    //Console.WriteLine(myworkbook.Worksheets.Count);
                    app.Quit();
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
    测过了,似乎可以....