`

C#操作Excel

    博客分类:
  • C#
阅读更多

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.IO;
using System.Collections;
using System.Data;
 
namespace Core.Helper
{
 
    /// <summary>
    /// excel操作类
    /// 开发环境:2007
    /// </summary>
    public class ExcelHelper : IDisposable
    {
 
        #region 构造函数
        /// <summary>
        /// 构造函数,将一个已有Excel工作簿作为模板,并指定输出路径
        /// </summary>
        /// <param name="templetFilePath">Excel模板文件路径</param>
        /// <param name="outputFilePath">输出Excel文件路径</param>
        public ExcelHelper(string templetFilePath, string outputFilePath)
        {
            if (templetFilePath == null)
                throw new Exception("Excel模板文件路径不能为空!");
 
            if (outputFilePath == null)
                throw new Exception("输出Excel文件路径不能为空!");
 
            if (!File.Exists(templetFilePath))
 
 
                throw new Exception("指定路径的Excel模板文件不存在!");
 
            this.templetFile = templetFilePath;
            this.outputFile = outputFilePath;
 
 
            excelApp = new Excel.ApplicationClass();
            excelApp.Visible = false;
 
            excelApp.DisplayAlerts = false;
            excelApp.AlertBeforeOverwriting = false;
 
            //打开模板文件,得到WorkBook对象
            workBook = excelApp.Workbooks.Open(templetFile, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, Type.Missing, Type.Missing);
 
            //得到WorkSheet对象
            workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
 
        }
 
        DateTime beforeTime;
        DateTime afterTime;
        /// <summary>
        /// 构造函数,打开一个已有的工作簿
        /// </summary>
        /// <param name="fileName">Excel文件名</param>
        public ExcelHelper(string fileName)
        {
            if (!File.Exists(fileName))
                throw new Exception("指定路径的Excel文件不存在!");
 
 
            //创建一个Application对象并使其可见
            beforeTime = DateTime.Now;
            excelApp = new Excel.ApplicationClass();
 
            excelApp.Visible = false;
 
            excelApp.DisplayAlerts = false;
            excelApp.AlertBeforeOverwriting = false;
 
            //打开一个WorkBook
            workBook = excelApp.Workbooks.Open(fileName,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
 
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 
            afterTime = DateTime.Now;
            //得到WorkSheet对象
            workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
        }
 
        /// <summary>
        /// 构造函数,新建一个工作簿
        /// </summary>
        public ExcelHelper()
        {
            beforeTime = DateTime.Now;
            excelApp = new Excel.ApplicationClass();
            afterTime = DateTime.Now;
            excelApp.Visible = false;
            //设置禁止弹出保存和覆盖的询问提示框
 
            excelApp.DisplayAlerts = false;
            excelApp.AlertBeforeOverwriting = false;
 
            //新建一个WorkBook
            workBook = excelApp.Workbooks.Add(Type.Missing);
 
            //得到WorkSheet对象
            workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
 
        }
        #endregion
 
        #region 成员变量
        private string templetFile = null;
        private string outputFile = null;
        private object missing = System.Reflection.Missing.Value;
        Excel.Application excelApp;
 
        Excel.Workbook workBook;
        Excel.Worksheet workSheet;
        Excel.Range range;
        Excel.Range range1;
        Excel.Range range2;
        Excel.TextBox textBox;
        private int sheetCount = 1;   //WorkSheet数量
        private string sheetPrefixName = "页";
        #endregion
 
        #region 公共属性
        /// <summary>
        /// WorkSheet前缀名,比如:前缀名为“页”,那么WorkSheet名称依次为“页-1,页-2...”
        /// </summary>
 
 
        public string SheetPrefixName
        {
            set { this.sheetPrefixName = value; }
        }
 
        /// <summary>
        /// WorkSheet数量
        /// </summary>
        public int WorkSheetCount
        {
            get { return workBook.Sheets.Count; }
        }
 
        /// <summary>
        /// Excel模板文件路径
        /// </summary>
        public string TempletFilePath
        {
            set { this.templetFile = value; }
        }
 
        /// <summary>
        /// 输出Excel文件路径
        /// </summary>
        public string OutputFilePath
        {
            set { this.outputFile = value; }
        }
        #endregion
 
        #region 公共方法
 
        #region Data Export Methods
 
        /// <summary>
        /// 将DataTable数据写入Excel文件(自动分页)
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="rows">每个WorkSheet写入多少行数据</param>
        /// <param name="top">表格数据起始行索引</param>
        /// <param name="left">表格数据起始列索引</param>
        public void DataTableToExcel(DataTable dt, int rows, int top, int left)
        {
            int rowCount = dt.Rows.Count; //DataTable行数
            int colCount = dt.Columns.Count; //DataTable列数
 
            sheetCount = this.GetSheetCount(rowCount, rows); //WorkSheet个数
            //   StringBuilder sb;
 
            //复制sheetCount-1个WorkSheet对象
            for (int i = 1; i < sheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Copy(missing, workBook.Worksheets[i]);
            }
 
            for (int i = 1; i <= sheetCount; i++)
            {
                int startRow = (i - 1) * rows; //记录起始行索引
                int endRow = i * rows;   //记录结束行索引
 
                //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
                if (i == sheetCount)
                    endRow = rowCount;
 
                //获取要写入数据的WorkSheet对象,并重命名
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Name = sheetPrefixName + "-" + i.ToString();
 
                //将dt中的数据写入WorkSheet
                //    for(int j=0;j<endRow-startRow;j++)
                //    {
                //     for(int k=0;k<colCount;k++)
                //     {
                //      workSheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString();
                //     }
                //    }
 
                //利用二维数组批量写入
                int row = endRow - startRow;
                object[,] ss = new object[row, colCount];
 
                for (int j = 0; j < row; j++)
                {
                    for (int k = 0; k < colCount; k++)
                    {
 
                        ss[j, k] = dt.Rows[startRow + j][k].ToString();
                    }
                }
 
                range = (Excel.Range)workSheet.Cells[top, left];
                range = range.get_Resize(row, colCount);
                range.Value2 = ss;
 
                #region 利用Windwo粘贴板批量拷贝数据(在Web下面行不通)
                /*sb = new StringBuilder();
    for(int j=0;j<endRow-startRow;j++)
    {
     for(int k=0;k<colCount;k++)
     {
      sb.Append( dt.Rows[startRow + j][k].ToString() );
      sb.Append("\t");
     }
 
     sb.Append("\n");
    }
 
    System.Windows.Forms.Clipboard.SetDataObject(sb.ToString());
 
    range = (Excel.Range)workSheet.Cells[top,left];
    workSheet.Paste(range,false);*/
                #endregion
 
            }
        }
 
 
        /// <summary>
        /// 将DataTable数据写入Excel文件(不分页)
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="top">表格数据起始行索引</param>
        /// <param name="left">表格数据起始列索引</param>
        public void DataTableToExcel(DataTable dt, int top, int left)
        {
            int rowCount = dt.Rows.Count; //DataTable行数
            int colCount = dt.Columns.Count; //DataTable列数
 
            //利用二维数组批量写入
            object[,] arr = new object[rowCount, colCount];
 
            for (int j = 0; j < rowCount; j++)
            {
                for (int k = 0; k < colCount; k++)
                {
                    arr[j, k] = dt.Rows[j][k].ToString();
                }
            }
 
            range = (Excel.Range)workSheet.Cells[top, left];
            range = range.get_Resize(rowCount, colCount);
            range.Value2 = arr;
        }
 
        public void DataTableToExcel(DataTable dt, int top, int left, Excel.Worksheet objSheet)
        {
            int rowCount = dt.Rows.Count; //DataTable行数
            int colCount = dt.Columns.Count; //DataTable列数
 
            //利用二维数组批量写入
            object[,] arr = new object[rowCount, colCount];
 
            for (int j = 0; j < rowCount; j++)
            {
                for (int k = 0; k < colCount; k++)
                {
                    arr[j, k] = dt.Rows[j][k].ToString();
                }
            }
 
            range = (Excel.Range)objSheet.Cells[top, left];
            range = range.get_Resize(rowCount, colCount);
            range.Value2 = arr;
        }
 
        /// <summary>
        /// 将DataTable数据写入Excel文件(自动分页,并指定要合并的列索引)
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="rows">每个WorkSheet写入多少行数据</param>
        /// <param name="top">表格数据起始行索引</param>
        /// <param name="left">表格数据起始列索引</param>
        /// <param name="mergeColumnIndex">DataTable中要合并相同行的列索引,从0开始</param>
        public void DataTableToExcel(DataTable dt, int rows, int top, int left, int mergeColumnIndex)
        {
            int rowCount = dt.Rows.Count; //源DataTable行数
            int colCount = dt.Columns.Count; //源DataTable列数
            sheetCount = this.GetSheetCount(rowCount, rows); //WorkSheet个数
            //   StringBuilder sb;
 
            //复制sheetCount-1个WorkSheet对象
            for (int i = 1; i < sheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Copy(missing, workBook.Worksheets[i]);
            }
 
            for (int i = 1; i <= sheetCount; i++)
            {
                int startRow = (i - 1) * rows; //记录起始行索引
                int endRow = i * rows;   //记录结束行索引
 
                //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
                if (i == sheetCount)
                    endRow = rowCount;
 
                //获取要写入数据的WorkSheet对象,并重命名
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Name = sheetPrefixName + "-" + i.ToString();
 
                //将dt中的数据写入WorkSheet
                //    for(int j=0;j<endRow-startRow;j++)
                //    {
                //     for(int k=0;k<colCount;k++)
                //     {
                //      workSheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString();
 
                //     }
                //    }
 
                //利用二维数组批量写入
                int row = endRow - startRow;
                object[,] ss = new object[row, colCount];
 
                for (int j = 0; j < row; j++)
                {
                    for (int k = 0; k < colCount; k++)
                    {
                        ss[j, k] = dt.Rows[startRow + j][k].ToString();
                    }
                }
 
                range = (Excel.Range)workSheet.Cells[top, left];
                range = range.get_Resize(row, colCount);
                range.Value2 = ss;
 
                //合并相同行
                this.MergeRows(workSheet, left + mergeColumnIndex, top, rows);
 
            }
        }
 
 
        /// <summary>
        /// 将二维数组数据写入Excel文件(自动分页)
        /// </summary>
        /// <param name="arr">二维数组</param>
        /// <param name="rows">每个WorkSheet写入多少行数据</param>
        /// <param name="top">行索引</param>
        /// <param name="left">列索引</param>
        public void ArrayToExcel(object[,] arr, int rows, int top, int left)
        {
            int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
 
 
            int colCount = arr.GetLength(1); //二维数据列数(二维长度)
            sheetCount = this.GetSheetCount(rowCount, rows); //WorkSheet个数
 
            //复制sheetCount-1个WorkSheet对象
            for (int i = 1; i < sheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Copy(missing, workBook.Worksheets[i]);
            }
 
            //将二维数组数据写入Excel
 
 
            for (int i = sheetCount; i >= 1; i--)
            {
                int startRow = (i - 1) * rows; //记录起始行索引
                int endRow = i * rows;   //记录结束行索引
 
                //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
                if (i == sheetCount)
                    endRow = rowCount;
 
                //获取要写入数据的WorkSheet对象,并重命名
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Name = sheetPrefixName + "-" + i.ToString();
 
                //将二维数组中的数据写入WorkSheet
                //    for(int j=0;j<endRow-startRow;j++)
                //    {
                //     for(int k=0;k<colCount;k++)
                //     {
                //      workSheet.Cells[top + j,left + k] = arr[startRow + j,k];
 
                //     }
                //    }
 
                //利用二维数组批量写入
                int row = endRow - startRow;
                object[,] ss = new object[row, colCount];
 
                for (int j = 0; j < row; j++)
                {
                    for (int k = 0; k < colCount; k++)
                    {
 
 
                        ss[j, k] = arr[startRow + j, k];
                    }
                }
 
                range = (Excel.Range)workSheet.Cells[top, left];
                range = range.get_Resize(row, colCount);
                range.Value2 = ss;
            }
 
        }//end ArrayToExcel
 
 
        /// <summary>
        /// 将二维数组数据写入Excel文件(不分页)
        /// </summary>
        /// <param name="arr">二维数组</param>
        /// <param name="top">行索引</param>
        /// <param name="left">列索引</param>
        public void ArrayToExcel(object[,] arr, int top, int left)
        {
            int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
            int colCount = arr.GetLength(1); //二维数据列数(二维长度)
 
            range = (Excel.Range)workSheet.Cells[top, left];
            range = range.get_Resize(rowCount, colCount);
            range.FormulaArray = arr;
 
        }//end ArrayToExcel
 
        /// <summary>
        /// 将二维数组数据写入Excel文件(不分页)
        /// </summary>
        /// <param name="arr">二维数组</param>
        /// <param name="top">行索引</param>
        /// <param name="left">列索引</param>
        /// <param name="isFormula">填充的数据是否需要计算</param>
        public void ArrayToExcel(object[,] arr, int top, int left, bool isFormula)
        {
            int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
            int colCount = arr.GetLength(1); //二维数据列数(二维长度)
 
            range = (Excel.Range)workSheet.Cells[top, left];
            range = range.get_Resize(rowCount, colCount);
 
            //注意:使用range.FormulaArray写合并的单元格会出问题
            if (isFormula)
                range.FormulaArray = arr;
            else
                range.Value2 = arr;
 
 
 
        }//end ArrayToExcel
 
        /// <summary>
        /// 将二维数组数据写入Excel文件(不分页),合并指定列的相同行
        /// </summary>
        /// <param name="arr">二维数组</param>
        /// <param name="top">行索引</param>
        /// <param name="left">列索引</param>
        /// <param name="isFormula">填充的数据是否需要计算</param>
        /// <param name="mergeColumnIndex">需要合并行的列索引</param>
        public void ArrayToExcel(object[,] arr, int top, int left, bool isFormula, int mergeColumnIndex)
        {
            int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
            int colCount = arr.GetLength(1); //二维数据列数(二维长度)
 
            range = (Excel.Range)workSheet.Cells[top, left];
            range = range.get_Resize(rowCount, colCount);
 
            //注意:使用range.FormulaArray写合并的单元格会出问题
            if (isFormula)
                range.FormulaArray = arr;
            else
                range.Value2 = arr;
 
            this.MergeRows(workSheet, mergeColumnIndex, top, rowCount);
 
        }//end ArrayToExcel
 
        /// <summary>
        /// 将二维数组数据写入Excel文件(不分页)
        /// </summary>
        /// <param name="sheetIndex">工作表索引</param>
        /// <param name="arr">二维数组</param>
        /// <param name="top">行索引</param>
        /// <param name="left">列索引</param>
        public void ArrayToExcel(int sheetIndex, object[,] arr, int top, int left)
        {
 
 
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            // 改变当前工作表
            this.workSheet = (Excel.Worksheet)this.workBook.Sheets.get_Item(sheetIndex);
 
            int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
            int colCount = arr.GetLength(1); //二维数据列数(二维长度)
 
 
            range = (Excel.Range)workSheet.Cells[top, left];
            range = range.get_Resize(rowCount, colCount);
 
            range.Value2 = arr;
 
        }//end ArrayToExcel
 
        /// <summary>
        /// 将二维数组数据写入Excel文件(自动分页,并指定要合并的列索引)
        /// </summary>
        /// <param name="arr">二维数组</param>
        /// <param name="rows">每个WorkSheet写入多少行数据</param>
        /// <param name="top">行索引</param>
        /// <param name="left">列索引</param>
        /// <param name="mergeColumnIndex">数组的二维索引,相当于DataTable的列索引,索引从0开始</param>
        public void ArrayToExcel(object[,] arr, int rows, int top, int left, int mergeColumnIndex)
        {
            int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
            int colCount = arr.GetLength(1); //二维数据列数(二维长度)
            sheetCount = this.GetSheetCount(rowCount, rows); //WorkSheet个数
 
            //复制sheetCount-1个WorkSheet对象
            for (int i = 1; i < sheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Copy(missing, workBook.Worksheets[i]);
            }
 
            //将二维数组数据写入Excel
            for (int i = sheetCount; i >= 1; i--)
            {
                int startRow = (i - 1) * rows; //记录起始行索引
                int endRow = i * rows;   //记录结束行索引
 
                //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
                if (i == sheetCount)
                    endRow = rowCount;
 
                //获取要写入数据的WorkSheet对象,并重命名
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                workSheet.Name = sheetPrefixName + "-" + i.ToString();
 
                //将二维数组中的数据写入WorkSheet
                for (int j = 0; j < endRow - startRow; j++)
                {
 
                    for (int k = 0; k < colCount; k++)
                    {
                        workSheet.Cells[top + j, left + k] = arr[startRow + j, k];
                    }
                }
 
                //利用二维数组批量写入
                int row = endRow - startRow;
                object[,] ss = new object[row, colCount];
 
                for (int j = 0; j < row; j++)
                {
                    for (int k = 0; k < colCount; k++)
                    {
                        ss[j, k] = arr[startRow + j, k];
                    }
                }
 
                range = (Excel.Range)workSheet.Cells[top, left];
 
 
                range = range.get_Resize(row, colCount);
                range.Value2 = ss;
 
                //合并相同行
                this.MergeRows(workSheet, left + mergeColumnIndex, top, rows);
            }
 
        }//end ArrayToExcel
        #endregion
 
        #region WorkSheet Methods
 
        /// <summary>
        /// 改变当前工作表
        /// </summary>
        /// <param name="sheetIndex">工作表索引</param>
        public void ChangeCurrentWorkSheet(int sheetIndex)
        {
            //若指定工作表索引超出范围,则不改变当前工作表
            if (sheetIndex < 1)
                return;
 
            if (sheetIndex > this.WorkSheetCount)
                return;
 
            this.workSheet = (Excel.Worksheet)this.workBook.Sheets.get_Item(sheetIndex);
        }
        /// <summary>
 
        /// 隐藏指定名称的工作表
        /// </summary>
        /// <param name="sheetName">工作表名称</param>
        public void HiddenWorkSheet(string sheetName)
        {
            try
            {
                Excel.Worksheet sheet = null;
 
                for (int i = 1; i <= this.WorkSheetCount; i++)
                {
                    workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
 
 
 
                    if (workSheet.Name == sheetName)
                        sheet = workSheet;
                }
 
                if (sheet != null)
                    sheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
                else
                {
                    this.KillExcelProcess(false);
 
 
                    throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 隐藏指定索引的工作表
 
        /// </summary>
        /// <param name="sheetIndex"></param>
        public void HiddenWorkSheet(int sheetIndex)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                Excel.Worksheet sheet = null;
                sheet = (Excel.Worksheet)workBook.Sheets.get_Item(sheetIndex);
 
                sheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
 
        /// <summary>
        /// 在指定名称的工作表后面拷贝指定个数的该工作表的副本,并重命名
        /// </summary>
        /// <param name="sheetName">工作表名称</param>
        /// <param name="sheetCount">工作表个数</param>
        public void CopyWorkSheets(string sheetName, int sheetCount)
        {
            try
            {
                Excel.Worksheet sheet = null;
                int sheetIndex = 0;
 
 
 
 
                for (int i = 1; i <= this.WorkSheetCount; i++)
                {
                    workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
 
                    if (workSheet.Name == sheetName)
                    {
                        sheet = workSheet;
                        sheetIndex = workSheet.Index;
 
 
                    }
                }
 
                if (sheet != null)
                {
                    for (int i = sheetCount; i >= 1; i--)
                    {
                        sheet.Copy(this.missing, sheet);
                    }
 
                    //重命名
                    for (int i = sheetIndex; i <= sheetIndex + sheetCount; i++)
                    {
                        workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
                        workSheet.Name = sheetName + "-" + Convert.ToString(i - sheetIndex + 1);
                    }
                }
                else
                {
                    this.KillExcelProcess(false);
                    throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 将一个工作表拷贝到另一个工作表后面,并重命名
        /// </summary>
        /// <param name="srcSheetIndex">拷贝源工作表索引</param>
        /// <param name="aimSheetIndex">参照位置工作表索引,新工作表拷贝在该工作表后面</param>
        /// <param name="newSheetName"></param>
        public void CopyWorkSheet(int srcSheetIndex, int aimSheetIndex, string newSheetName)
        {
            if (srcSheetIndex > this.WorkSheetCount || aimSheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                Excel.Worksheet srcSheet = (Excel.Worksheet)workBook.Sheets.get_Item(srcSheetIndex);
                Excel.Worksheet aimSheet = (Excel.Worksheet)workBook.Sheets.get_Item(aimSheetIndex);
 
                srcSheet.Copy(this.missing, aimSheet);
 
 
 
                //重命名
                workSheet = (Excel.Worksheet)aimSheet.Next; //获取新拷贝的工作表
                workSheet.Name = newSheetName;
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
 
        /// <summary>
 
        /// 根据名称删除工作表
        /// </summary>
        /// <param name="sheetName"></param>
        public void DeleteWorkSheet(string sheetName)
        {
            try
            {
                Excel.Worksheet sheet = null;
 
                //找到名称位sheetName的工作表
                for (int i = 1; i <= this.WorkSheetCount; i++)
                {
                    workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
 
                    if (workSheet.Name == sheetName)
                    {
                        sheet = workSheet;
                    }
                }
 
                if (sheet != null)
                {
 
 
                    sheet.Delete();
                }
                else
                {
                    this.KillExcelProcess(false);
                    throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 根据索引删除工作表
        /// </summary>
        /// <param name="sheetIndex"></param>
        public void DeleteWorkSheet(int sheetIndex)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                Excel.Worksheet sheet = null;
                sheet = (Excel.Worksheet)workBook.Sheets.get_Item(sheetIndex);
 
                sheet.Delete();
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        #endregion
 
        #region TextBox Methods
        /// <summary>
 
        /// 向指定文本框写入数据,对每个WorkSheet操作
        /// </summary>
        /// <param name="textboxName">文本框名称</param>
        /// <param name="text">要写入的文本</param>
        public void SetTextBox(string textboxName, string text)
        {
            for (int i = 1; i <= this.WorkSheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
 
 
                try
                {
                    textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
                    textBox.Text = text;
                }
                catch
                {
                    this.KillExcelProcess(false);
                    throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!");
                }
            }
        }
 
        /// <summary>
        /// 向指定文本框写入数据,对指定WorkSheet操作
        /// </summary>
        /// <param name="sheetIndex">工作表索引</param>
        /// <param name="textboxName">文本框名称</param>
        /// <param name="text">要写入的文本</param>
        public void SetTextBox(int sheetIndex, string textboxName, string text)
        {
            workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
 
 
            try
            {
                textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
                textBox.Text = text;
            }
            catch
            {
                this.KillExcelProcess(false);
                throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!");
            }
        }
 
        /// <summary>
        /// 向文本框写入数据,对每个WorkSheet操作
        /// </summary>
        /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
        public void SetTextBoxes(Hashtable ht)
        {
            if (ht.Count == 0) return;
 
            for (int i = 1; i <= this.WorkSheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
 
                foreach (DictionaryEntry dic in ht)
                {
                    try
                    {
                        textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
                        textBox.Text = dic.Value.ToString();
                    }
                    catch
                    {
                        this.KillExcelProcess(false);
                        throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!");
                    }
                }
            }
        }
 
        /// <summary>
        /// 向文本框写入数据,对指定WorkSheet操作
        /// </summary>
        /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
        public void SetTextBoxes(int sheetIndex, Hashtable ht)
        {
            if (ht.Count == 0) return;
 
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
 
            foreach (DictionaryEntry dic in ht)
            {
                try
                {
                    textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
                    textBox.Text = dic.Value.ToString();
                }
                catch
                {
                    this.KillExcelProcess(false);
                    throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!");
                }
            }
        }
        #endregion
 
        #region Cell Methods
        /// <summary>
        /// 向单元格写入数据,对当前WorkSheet操作
        /// </summary>
        /// <param name="rowIndex">行索引</param>
        /// <param name="columnIndex">列索引</param>
 
 
        /// <param name="text">要写入的文本值</param>
        public void SetCells(int rowIndex, int columnIndex, string text)
        {
            try
            {
                workSheet.Cells[rowIndex, columnIndex] = text;
            }
            catch
            {
                this.KillExcelProcess(false);
                throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
            }
        }
 
        /// <summary>
        /// 向单元格写入数据,对指定WorkSheet操作
        /// </summary>
        /// <param name="sheetIndex">工作表索引</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="columnIndex">列索引</param>
        /// <param name="text">要写入的文本值</param>
        public void SetCells(int sheetIndex, int rowIndex, int columnIndex, string text)
        {
            try
            {
                this.ChangeCurrentWorkSheet(sheetIndex); //改变当前工作表为指定工作表
                workSheet.Cells[rowIndex, columnIndex] = text;
            }
            catch
            {
                this.KillExcelProcess(false);
                throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
            }
        }
 
        /// <summary>
        /// 向单元格写入数据,对每个WorkSheet操作
        /// </summary>
        /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
        public void SetCells(Hashtable ht)
        {
            int rowIndex;
            int columnIndex;
            string position;
 
            if (ht.Count == 0) return;
 
            for (int i = 1; i <= this.WorkSheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
 
                foreach (DictionaryEntry dic in ht)
                {
                    try
                    {
                        position = dic.Key.ToString();
                        rowIndex = Convert.ToInt32(position.Split(',')[0]);
 
 
 
                        columnIndex = Convert.ToInt32(position.Split(',')[1]);
 
                        workSheet.Cells[rowIndex, columnIndex] = dic.Value;
                    }
                    catch
                    {
                        this.KillExcelProcess(false);
                        throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
 
                    }
                }
            }
        }
 
        /// <summary>
        /// 向单元格写入数据,对指定WorkSheet操作
        /// </summary>
        /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
        public void SetCells(int sheetIndex, Hashtable ht)
        {
            int rowIndex;
            int columnIndex;
            string position;
 
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            if (ht.Count == 0) return;
 
            workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
 
            foreach (DictionaryEntry dic in ht)
            {
                try
                {
                    position = dic.Key.ToString();
                    rowIndex = Convert.ToInt32(position.Split(',')[0]);
                    columnIndex = Convert.ToInt32(position.Split(',')[1]);
 
                    workSheet.Cells[rowIndex, columnIndex] = dic.Value;
                }
 
                catch
                {
                    this.KillExcelProcess(false);
                    throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
                }
            }
        }
 
        /// <summary>
        /// 设置单元格为可计算的
        /// </summary>
        /// <remarks>
 
 
        /// 如果Excel的单元格格式设置为数字,日期或者其他类型时,需要设置这些单元格的FormulaR1C1属性,
        /// 否则写到这些单元格的数据将不会按照预先设定的格式显示
        /// </remarks>
        /// <param name="arr">保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
        public void SetCells(int sheetIndex, string[] arr)
        {
            int rowIndex;
            int columnIndex;
            string position;
 
            if (sheetIndex > this.WorkSheetCount)
            {
 
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            if (arr.Length == 0) return;
 
            workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
 
            for (int i = 0; i < arr.Length; i++)
            {
                try
                {
                    position = arr[i];
 
                    rowIndex = Convert.ToInt32(position.Split(',')[0]);
                    columnIndex = Convert.ToInt32(position.Split(',')[1]);
 
                    Excel.Range cell = (Excel.Range)workSheet.Cells[rowIndex, columnIndex];
                    cell.FormulaR1C1 = cell.Text;
                }
                catch
                {
                    this.KillExcelProcess(false);
                    throw new Exception(string.Format("计算单元格{0}出错!", arr[i]));
                }
            }
        }
 
        /// <summary>
        /// 向单元格写入数据,对指定WorkSheet操作
        /// </summary>
        /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
        public void SetCells(string sheetName, Hashtable ht)
        {
            int rowIndex;
 
 
            int columnIndex;
            string position;
            Excel.Worksheet sheet = null;
            int sheetIndex = 0;
 
            if (ht.Count == 0) return;
 
            try
            {
                for (int i = 1; i <= this.WorkSheetCount; i++)
                {
                    workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
 
                    if (workSheet.Name == sheetName)
                    {
                        sheet = workSheet;
                        sheetIndex = workSheet.Index;
                    }
                }
 
                if (sheet != null)
                {
                    foreach (DictionaryEntry dic in ht)
                    {
                        try
                        {
                            position = dic.Key.ToString();
                            rowIndex = Convert.ToInt32(position.Split(',')[0]);
                            columnIndex = Convert.ToInt32(position.Split(',')[1]);
 
 
 
                            sheet.Cells[rowIndex, columnIndex] = dic.Value;
                        }
                        catch
                        {
                            this.KillExcelProcess(false);
                            throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
                        }
                    }
                }
                else
                {
                    this.KillExcelProcess(false);
                    throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
                }
            }
 
 
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
 
        /// <summary>
        /// 合并单元格,并赋值,对每个WorkSheet操作
        /// </summary>
        /// <param name="beginRowIndex">开始行索引</param>
        /// <param name="beginColumnIndex">开始列索引</param>
        /// <param name="endRowIndex">结束行索引</param>
        /// <param name="endColumnIndex">结束列索引</param>
        /// <param name="text">合并后Range的值</param>
        public void MergeCells(int beginRowIndex, int beginColumnIndex, int endRowIndex, int endColumnIndex, string text)
        {
            for (int i = 1; i <= this.WorkSheetCount; i++)
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
                range = workSheet.get_Range(workSheet.Cells[beginRowIndex, beginColumnIndex], workSheet.Cells[endRowIndex, endColumnIndex]);
 
 
                range.ClearContents(); //先把Range内容清除,合并才不会出错
                range.MergeCells = true;
                range.Value2 = text;
                range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
            }
        }
 
        /// <summary>
        /// 合并单元格,并赋值,对指定WorkSheet操作
        /// </summary>
        /// <param name="sheetIndex">WorkSheet索引</param>
        /// <param name="beginRowIndex">开始行索引</param>
        /// <param name="beginColumnIndex">开始列索引</param>
        /// <param name="endRowIndex">结束行索引</param>
        /// <param name="endColumnIndex">结束列索引</param>
        /// <param name="text">合并后Range的值</param>
        public void MergeCells(int sheetIndex, int beginRowIndex, int beginColumnIndex, int endRowIndex, int endColumnIndex, string text)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
            range = workSheet.get_Range(workSheet.Cells[beginRowIndex, beginColumnIndex], workSheet.Cells[endRowIndex, endColumnIndex]);
 
            range.ClearContents(); //先把Range内容清除,合并才不会出错
            range.MergeCells = true;
 
            range.Value2 = text;
            range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
        }
        #endregion
 
        #region Row Methods
        /// <summary>
        /// 将指定索引列的数据相同的行合并,对每个WorkSheet操作
        /// </summary>
        /// <param name="columnIndex">列索引</param>
        /// <param name="beginRowIndex">开始行索引</param>
        /// <param name="endRowIndex">结束行索引</param>
 
        public void MergeRows(int columnIndex, int beginRowIndex, int endRowIndex)
        {
            if (endRowIndex - beginRowIndex < 1)
                return;
 
            for (int i = 1; i <= this.WorkSheetCount; i++)
            {
                int beginIndex = beginRowIndex;
                int count = 0;
                string text1;
                string text2;
                workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
 
                for (int j = beginRowIndex; j <= endRowIndex; j++)
                {
                    range = (Excel.Range)workSheet.Cells[j, columnIndex];
                    text1 = range.Text.ToString();
 
                    range = (Excel.Range)workSheet.Cells[j + 1, columnIndex];
                    text2 = range.Text.ToString();
 
 
                    if (text1 == text2)
                    {
                        ++count;
                    }
                    else
                    {
                        if (count > 0)
                        {
                            this.MergeCells(workSheet, beginIndex, columnIndex, beginIndex + count, columnIndex, text1);
                        }
 
                        beginIndex = j + 1; //设置开始合并行索引
                        count = 0; //计数器清0
                    }
 
                }
 
            }
        }
 
 
        /// <summary>
        /// 将指定索引列的数据相同的行合并,对指定WorkSheet操作
        /// </summary>
        /// <param name="sheetIndex">WorkSheet索引</param>
        /// <param name="columnIndex">列索引</param>
        /// <param name="beginRowIndex">开始行索引</param>
        /// <param name="endRowIndex">结束行索引</param>
        public void MergeRows(int sheetIndex, int columnIndex, int beginRowIndex, int endRowIndex)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            if (endRowIndex - beginRowIndex < 1)
                return;
 
            int beginIndex = beginRowIndex;
            int count = 0;
            string text1;
            string text2;
            workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
 
            for (int j = beginRowIndex; j <= endRowIndex; j++)
            {
                range = (Excel.Range)workSheet.Cells[j, columnIndex];
                text1 = range.Text.ToString();
 
                range = (Excel.Range)workSheet.Cells[j + 1, columnIndex];
                text2 = range.Text.ToString();
 
                if (text1 == text2)
                {
                    ++count;
                }
                else
                {
                    if (count > 0)
                    {
                        this.MergeCells(workSheet, beginIndex, columnIndex, beginIndex + count, columnIndex, text1);
                    }
 
 
 
                    beginIndex = j + 1; //设置开始合并行索引
                    count = 0; //计数器清0
                }
 
            }
 
        }
 
 
        /// <summary>
        /// 插行(在指定行上面插入指定数量行)
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="count"></param>
        public void InsertRows(int rowIndex, int count)
        {
            try
            {
                for (int n = 1; n <= this.WorkSheetCount; n++)
                {
                    workSheet = (Excel.Worksheet)workBook.Worksheets[n];
                    range = (Excel.Range)workSheet.Rows[rowIndex, this.missing];
 
                    for (int i = 0; i < count; i++)
                    {
                        range.Insert(Excel.XlDirection.xlDown, missing);
                    }
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
 
 
        /// 插行(在指定WorkSheet指定行上面插入指定数量行)
        /// </summary>
        /// <param name="sheetIndex"></param>
        /// <param name="rowIndex"></param>
        /// <param name="count"></param>
        public void InsertRows(int sheetIndex, int rowIndex, int count)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
 
            }
 
            try
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
                range = (Excel.Range)workSheet.Rows[rowIndex, this.missing];
 
                for (int i = 0; i < count; i++)
                {
                    range.Insert(Excel.XlDirection.xlDown, missing);
                }
 
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 复制行(在指定行下面复制指定数量行)
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="count"></param>
 
        public void CopyRows(int rowIndex, int count)
        {
            try
            {
                for (int n = 1; n <= this.WorkSheetCount; n++)
                {
                    workSheet = (Excel.Worksheet)workBook.Worksheets[n];
                    range1 = (Excel.Range)workSheet.Rows[rowIndex, this.missing];
 
                    for (int i = 1; i <= count; i++)
                    {
                        range2 = (Excel.Range)workSheet.Rows[rowIndex + i, this.missing];
                        range1.Copy(range2);
                    }
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
 
 
                throw e;
            }
        }
 
        /// <summary>
        /// 复制行(在指定WorkSheet指定行下面复制指定数量行)
        /// </summary>
        /// <param name="sheetIndex"></param>
        /// <param name="rowIndex"></param>
        /// <param name="count"></param>
        public void CopyRows(int sheetIndex, int rowIndex, int count)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
                range1 = (Excel.Range)workSheet.Rows[rowIndex, this.missing];
 
                for (int i = 1; i <= count; i++)
                {
                    range2 = (Excel.Range)workSheet.Rows[rowIndex + i, this.missing];
                    range1.Copy(range2);
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="count"></param>
        public void DeleteRows(int rowIndex, int count)
        {
            try
            {
                for (int n = 1; n <= this.WorkSheetCount; n++)
                {
                    workSheet = (Excel.Worksheet)workBook.Worksheets[n];
                    range = (Excel.Range)workSheet.Rows[rowIndex, this.missing];
 
                    for (int i = 0; i < count; i++)
                    {
                        range.Delete(Excel.XlDirection.xlDown);
                    }
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="sheetIndex"></param>
        /// <param name="rowIndex"></param>
        /// <param name="count"></param>
        public void DeleteRows(int sheetIndex, int rowIndex, int count)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
                range = (Excel.Range)workSheet.Rows[rowIndex, this.missing];
 
 
 
                for (int i = 0; i < count; i++)
                {
                    range.Delete(Excel.XlDirection.xlDown);
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
 
 
        }
 
        #endregion
 
        #region Column Methods
 
        /// <summary>
        /// 插列(在指定列右边插入指定数量列)
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="count"></param>
        public void InsertColumns(int columnIndex, int count)
        {
            try
            {
                for (int n = 1; n <= this.WorkSheetCount; n++)
                {
                    workSheet = (Excel.Worksheet)workBook.Worksheets[n];
                    range = (Excel.Range)workSheet.Columns[this.missing, columnIndex];
 
                    for (int i = 0; i < count; i++)
                    {
                        range.Insert(Excel.XlDirection.xlDown, missing);
                    }
 
 
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 插列(在指定WorkSheet指定列右边插入指定数量列)
        /// </summary>
        /// <param name="sheetIndex"></param>
        /// <param name="columnIndex"></param>
        /// <param name="count"></param>
        public void InsertColumns(int sheetIndex, int columnIndex, int count)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
                range = (Excel.Range)workSheet.Columns[this.missing, columnIndex];
 
                for (int i = 0; i < count; i++)
                {
                    range.Insert(Excel.XlDirection.xlDown, missing);
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 复制列(在指定列右边复制指定数量列)
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="count"></param>
        public void CopyColumns(int columnIndex, int count)
        {
            try
            {
                for (int n = 1; n <= this.WorkSheetCount; n++)
                {
                    workSheet = (Excel.Worksheet)workBook.Worksheets[n];
                    //     range1 = (Excel.Range)workSheet.Columns[columnIndex,this.missing];
                    range1 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex) + "1", this.IntToLetter(columnIndex) + "10000");
 
                    for (int i = 1; i <= count; i++)
                    {
                        //      range2 = (Excel.Range)workSheet.Columns[this.missing,columnIndex + i];
                        range2 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex + i) + "1", this.IntToLetter(columnIndex + i) + "10000");
                        range1.Copy(range2);
                    }
                }
 
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 复制列(在指定WorkSheet指定列右边复制指定数量列)
        /// </summary>
        /// <param name="sheetIndex"></param>
        /// <param name="columnIndex"></param>
        /// <param name="count"></param>
        public void CopyColumns(int sheetIndex, int columnIndex, int count)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
                //    range1 = (Excel.Range)workSheet.Columns[Type.Missing,columnIndex];
                range1 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex) + "1", this.IntToLetter(columnIndex) + "10000");
 
                for (int i = 1; i <= count; i++)
                {
                    //     range2 = (Excel.Range)workSheet.Columns[Type.Missing,columnIndex + i];
                    range2 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex + i) + "1", this.IntToLetter(columnIndex + i) + "10000");
                    range1.Copy(range2);
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
        /// <summary>
        /// 删除列
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="count"></param>
        public void DeleteColumns(int columnIndex, int count)
        {
            try
            {
                for (int n = 1; n <= this.WorkSheetCount; n++)
                {
                    workSheet = (Excel.Worksheet)workBook.Worksheets[n];
                    range = (Excel.Range)workSheet.Columns[this.missing, columnIndex];
 
                    for (int i = 0; i < count; i++)
                    {
                        range.Delete(Excel.XlDirection.xlDown);
                    }
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
 
                throw e;
            }
        }
 
        /// <summary>
        /// 删除列
        /// </summary>
        /// <param name="sheetIndex"></param>
        /// <param name="columnIndex"></param>
        /// <param name="count"></param>
        public void DeleteColumns(int sheetIndex, int columnIndex, int count)
        {
            if (sheetIndex > this.WorkSheetCount)
            {
                this.KillExcelProcess(false);
                throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
            }
 
            try
            {
                workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
                range = (Excel.Range)workSheet.Columns[this.missing, columnIndex];
 
                for (int i = 0; i < count; i++)
                {
                    range.Delete(Excel.XlDirection.xlDown);
                }
            }
            catch (Exception e)
            {
                this.KillExcelProcess(false);
                throw e;
            }
        }
 
 
        #region 通过列数获取excel列名
 
        public String GetColumnName(int num)
        {
            String[] COL_NAME =   
        {   
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",   
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",   
                "U", "V", "W", "X", "Y", "Z" 
        };
            int a = 0, b = num;
            String col = "";
            if (b < 26)
            {
                col = COL_NAME[b];
            }
            else
            {
                while (b >= 26)
                {
                    a = b % 26;
                    b = b / 26;
                    col = COL_NAME[a] + col;
                }
                col = COL_NAME[b - 1] + col;
            }
            return col;
        }
 
        #endregion
 
        #endregion
 
        #region Range Methods
 
        /// <summary>
        /// 将指定范围区域拷贝到目标区域
        /// </summary>
        /// <param name="sheetIndex">WorkSheet索引</param>
        /// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param>
        /// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param>
        /// <param name="targetCell">目标区域的

  


  
分享到:
评论
1 楼 clshuai 2012-04-08  
博主,你好,看了你的这篇文章,让我很敬佩。可谓内容之全,涵盖范围之广啊。

之后我一直在研读你的代码,可是让我遗憾的是,博主没有把代码写全,后面好像还少了很多的内容,希望博主能补全,或者提供代码下载,小弟们将不胜感激。

或者麻烦博主给我email一份:hellocaols@163.com, 将非常的感谢!

相关推荐

    C# 操作Excel写数据生成图表

    C#的,winform的,这是我自己写的一个c#操作excel的例子,其中包括数据操作,字体,格式,单元格等操作,还有一个生成柱状图的方法,所有的方法都在ExcelHelper.cs类里,点击Form1窗体里的按钮可以看到各种操作的...

    C#操作Excel的几种方法

    ### C#操作Excel的几种方法 #### 概述 在.NET框架中,通过C#语言进行Excel文件的操作是一项常见的需求,比如数据导入导出、报表生成等场景。本文将详细介绍几种利用C#来操作Excel的方法,并重点分析一种基于`...

    C# 操作Excel案例

    本案例聚焦于"C#操作Excel",这涉及到如何使用C#与Microsoft Office的Excel应用程序进行交互,以实现数据读取、写入、修改等操作。在实际工作中,这种技能对于数据处理、报表生成和自动化任务至关重要。 C#操作...

    C# 操作excel单元格合并,格式,冻结

    ### C# 操作Excel基础 在C#中处理Excel文档通常涉及到几个关键类:`Workbook`, `Worksheet`, 和 `Application`。这些类提供了操作Excel文档所需的大部分功能。 #### 创建Excel Application实例 ```csharp private ...

    C#操作EXCEL文件

    本文将深入探讨C#操作Excel文件的相关知识点。 首先,C#本身并不直接支持Excel操作,而是需要借助第三方库或者.NET Framework中的组件来实现。最常见的库之一是Microsoft.Office.Interop.Excel,它是.NET Framework...

    C#操作EXCEL类

    在C#中,操作Excel是一项常见的任务,尤其在数据分析和报表生成方面。在这个场景中,开发者面临的问题是如何根据动态的数据生成美观且...通过不断学习和实践,可以提高C#操作Excel的技能,创建更加复杂和美观的报表。

    C#操作excel

    ### C#操作Excel知识点详解 #### 一、概述 在.NET框架中,无论是Web应用程序还是桌面应用程序,特别是在涉及数据库操作的管理系统中,经常会遇到需要读取Excel数据或将数据导出到Excel的情况。本文将详细介绍几种...

    C#操作EXCEL EXCEL类库 Excel模板处理 将DataTable数据写入Excel文件 C#数据写入EXCEL

    总结一下,C#操作Excel可以选择多种途径,包括Interop库和第三方库如EPPlus。在处理大数据时,推荐使用后者,因为它更高效且不依赖于Office的本地安装。此外,通过模板处理和样式管理,可以轻松创建专业且格式化的...

    C#操作Excel源代码

    标题 "C#操作Excel源代码" 提供了一个关键信息,即使用C#编程语言来处理Microsoft Excel文件。在C#中,我们可以利用.NET框架提供的Microsoft.Office.Interop.Excel命名空间,这使得我们能够通过COM组件与Excel应用...

    C#操作Excel类示例

    总的来说,C#操作Excel的代码示例可以帮助开发者在不依赖完整Office环境的情况下,高效地进行数据处理。无论是读取现有数据还是生成新的Excel报告,这些类和方法都能提供必要的功能。通过学习和应用这些示例,开发者...

    C#操作Excel.pdf

    ### C#操作Excel知识点详解 #### 一、引言与背景 Excel作为微软办公套件中的重要组成部分,凭借其强大的功能和友好的用户界面而深受广大用户的喜爱。特别是在数据分析、财务管理等领域,Excel更是不可或缺的工具之...

    一个简单的C#操作Excel的例子

    首先,要进行C#操作Excel,我们需要引用一个库来处理Excel文件。在.NET框架中,常常使用`Microsoft.Office.Interop.Excel`库,它是Office Interop组件的一部分,允许与Office应用程序进行交互。不过,这个库需要在...

    C# 操作Excel大全

    C# 操作Excel大全 c//删除行 //删除一列数据 //设置背景色 //设置Format属性 等

    C#操作excel的方法汇总

    C#操作Excel的方法汇总 C#操作Excel的方法汇总是指使用C#语言对Excel进行读取、写入、编辑和导出等操作的方法汇总。这些方法可以帮助开发者快速、高效地完成Excel相关的任务。 一、使用OleDbConnection读取Excel...

Global site tag (gtag.js) - Google Analytics