Exemplo n.º 1
0
        /// <summary>
        /// シートを作成する
        /// ※カレントシートは変更しない
        /// </summary>
        /// <param name="sheetName"></param>
        public void CreateSheet(string sheetName, SheetPosition position)
        {
            Worksheet lastSheet = null;
            Worksheet newSheet  = null;

            try
            {
                lastSheet     = xlSheets[xlSheets.Count];
                newSheet      = xlSheets.Add(After: lastSheet);
                newSheet.Name = sheetName;
                MoveLastSheet(position);
            }finally
            {
                if (newSheet != null)
                {
                    Marshal.ReleaseComObject(newSheet);
                }
                newSheet = null;

                if (lastSheet != null)
                {
                    Marshal.ReleaseComObject(lastSheet);
                }
                lastSheet = null;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 末尾のシートを指定移動する
        /// </summary>
        /// <param name="sheetName"></param>
        private void MoveLastSheet(SheetPosition position)
        {
            Worksheet moveSheet = null;
            Worksheet insertTo  = null;

            try
            {
                moveSheet = xlSheets[xlSheets.Count];
                switch (position)
                {
                case SheetPosition.First:
                    insertTo = xlSheets[1];
                    moveSheet.Move(Before: insertTo);
                    break;

                case SheetPosition.Last:
                    //insertTo = xlSheets[xlSheets.Count];
                    //moveSheet.Move(After: insertTo);
                    break;

                case SheetPosition.BeforeCurrentSheet:
                    moveSheet.Move(Before: xlSheet);
                    break;

                case SheetPosition.AfterCurrentSheet:
                    moveSheet.Move(After: xlSheet);
                    break;
                }
            }finally
            {
                if (moveSheet != null)
                {
                    Marshal.ReleaseComObject(moveSheet);
                }
                moveSheet = null;

                if (insertTo != null)
                {
                    Marshal.ReleaseComObject(insertTo);
                }
                insertTo = null;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// シートをコピーする
        /// ※カレントシートは変更しない
        /// </summary>
        /// <param name="sheetName"></param>
        public void CopySheet(string toSheetName, string fromSheetName, SheetPosition position)
        {
            Worksheet fromSheet = null;
            Worksheet lastSheet = null;
            Worksheet newSheet  = null;

            try
            {
                fromSheet = FindSheet(fromSheetName);
                if (fromSheet == null)
                {
                    throw new ArgumentException("コピーしようとしているシート[" + fromSheetName + "]が見つかりません。");
                }

                //末尾にコピーしてから移動する(Copyメソッドの戻り値がないため、作成したシートインスタンスを取得できない)
                lastSheet = xlSheets[xlSheets.Count];
                fromSheet.Copy(After: lastSheet);
                newSheet      = xlSheets[xlSheets.Count];
                newSheet.Name = toSheetName;
                MoveLastSheet(position);
            }finally
            {
                if (newSheet != null)
                {
                    Marshal.ReleaseComObject(newSheet);
                }
                newSheet = null;

                if (lastSheet != null)
                {
                    Marshal.ReleaseComObject(lastSheet);
                }
                lastSheet = null;

                if (newSheet != null)
                {
                    Marshal.ReleaseComObject(newSheet);
                }
                newSheet = null;
            }
        }