C#新增多個Sheet表

iDotNetSpace發表於2010-06-07

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章