C#添加多个Sheet表

using System;
using System.Reflection;
using Microsoft.Office.Interop.Excel;


namespace Customize.Common
{
    ///
    /// Class1 的摘要说明
    ///
    public class ExcelHelper
    {
        public ExcelHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

    ///
        /// 添加多个Excel的Sheet
        /// 使用此方法应该注意:在web.config文件的中加入
        /// 添加dll的引用 和 using System.Reflection; 的引用
        ///
        ///
        public static string MakeMoreSheet()
        {
            string results = string.Empty;
            //const int nCells = 5;
            Application app = new Application();
            if (app == null)
            {
                results = "FileNotExists";
            }

            app.Visible = true;
            //Getting the workbooks collection
            Workbooks workbooks = app.Workbooks;
           

            //Adding a new workbook The following line is the temporary workaround for the LCID problem
            _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);//添加一个Sheet表
            //Getting the worksheets collection 得到sheet的集合
            Sheets sheets = workbook.Worksheets;
            _Worksheet worksheet1 = (_Worksheet)sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);//添加第二个sheet

            //_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
            // This paragraph puts the value 5 to the cell G1
            //Range range1 = worksheet.get_Range("G1", Missing.Value);           
            //range1.Value2 = nCells;

            //_Worksheet worksheet2 = (_Worksheet)sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); //
            //_Workbook workbook1 = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            //Sheets sheets2 = workbook.Worksheets;

            //_Worksheet worksheet2 = (_Worksheet)sheets.get_Item(2);
            // This paragraph puts the value 5 to the cell G1
            //Range range2 = worksheet2.get_Range("G2", Missing.Value);
            //range2.Value2 = nCells;
            return results;
        }

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-664578/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-664578/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值