C#操作Excel全能帮助类:
1,Excel单元格边框的线条的粗细枚举
2,Excel单元格边框枚举
3,彻底关闭Excel的资源和进程
4,打印预览Excel文件
5,设置工作簿的名称
6,设置指定单元格的内容,可以指定格式
7,设置指定范围的单元格格式


0001using Excel;
0002using System;
0003using System.Reflection;
0004  
0005namespace ExcelClass
0006{
0007        public class ExcelBase
0008    {
0009        #region " Private Variable Definition "
0010  
0011        private Application exlApp;
0012        private _Workbook exlWorkBook;
0013        private _Worksheet exlWorkSheet;
0014        private int sheetNumber = 1;
0015  
0016        #endregion
0017  
0018        #region " Public Property and Constant Definition "
0019  
0020        /// <summary>
0021        /// Excel单元格边框的线条的粗细枚举
0022        /// </summary>
0023        public enum ExcelBorderWeight
0024        {
0025            /// <summary>
0026            /// 极细的线条
0027            /// </summary>
0028            Hairline = Excel.XlBorderWeight.xlHairline,
0029            /// <summary>
0030            /// 中等的线条
0031            /// </summary>
0032            Medium = Excel.XlBorderWeight.xlMedium,
0033            /// <summary>
0034            /// 粗线条
0035            /// </summary>
0036            Thick = Excel.XlBorderWeight.xlThick,
0037            /// <summary>
0038            /// 细线条
0039            /// </summary>
0040            Thin = Excel.XlBorderWeight.xlThin
0041        }
0042  
0043        /// <summary>
0044        /// Excel单元格边框枚举
0045        /// </summary>
0046        public enum ExcelBordersIndex
0047        {
0048            /// <summary>
0049            /// 主对角线从
0050            /// </summary>
0051            DiagonalDown = Excel.XlBordersIndex.xlDiagonalDown,
0052            /// <summary>
0053            /// 辅对角线
0054            /// </summary>
0055            DiagonUp = Excel.XlBordersIndex.xlDiagonalUp,
0056            /// <summary>
0057            ///底边框
0058            /// </summary>
0059            EdgeBottom = Excel.XlBordersIndex.xlEdgeBottom,
0060            /// <summary>
0061            /// 左边框
0062            /// </summary>
0063            EdgeLeft = Excel.XlBordersIndex.xlEdgeLeft,
0064            /// <summary>
0065            /// 右边框
0066            /// </summary>
0067            EdgeRight = Excel.XlBordersIndex.xlEdgeRight,
0068            /// <summary>
0069            /// 顶边框
0070            /// </summary>
0071            EdgeTop = Excel.XlBordersIndex.xlEdgeTop,
0072            /// <summary>
0073            /// 边框内水平横线
0074            /// </summary>
0075            InsideHorizontal = Excel.XlBordersIndex.xlInsideHorizontal,
0076            /// <summary>
0077            /// 边框内垂直竖线
0078            /// </summary>
0079            InsideVertical = Excel.XlBordersIndex.xlInsideVertical
0080        }
0081  
0082        /// <summary>
0083        /// Excel单元格的竖直方法对齐枚举
0084        /// </summary>
0085        public enum ExcelVerticalAlignment
0086        {
0087            /// <summary>
0088            /// 居中
0089            /// </summary>
0090            Center = Excel.Constants.xlCenter,
0091            /// <summary>
0092            /// 靠上
0093            /// </summary>
0094            Top = Excel.Constants.xlTop,
0095            /// <summary>
0096            /// 靠下
0097            /// </summary>
0098            Bottom = Excel.Constants.xlBottom,
0099            /// <summary>
0100            /// 两端对齐
0101            /// </summary>
0102            Justify = Excel.Constants.xlJustify,
0103            /// <summary>
0104            /// 分散对齐
0105            /// </summary>
0106            Distributed = Excel.Constants.xlDistributed
0107  
0108        };
0109  
0110        /// <summary>
0111        /// Excel 水平方向对齐枚举
0112        /// </summary>
0113        public enum ExcelHorizontalAlignment
0114        {
0115            /// <summary>
0116            ///常规
0117            /// </summary>
0118            General = Excel.Constants.xlGeneral,
0119            /// <summary>
0120            /// 靠左
0121            /// </summary>
0122            Left = Excel.Constants.xlLeft,
0123            /// <summary>
0124            /// 居中
0125            /// </summary>
0126            Center = Excel.Constants.xlCenter,
0127            /// <summary>
0128            /// 靠右
0129            /// </summary>
0130            Right = Excel.Constants.xlRight,
0131            /// <summary>
0132            /// 填充
0133            /// </summary>
0134            Fill = Excel.Constants.xlFill,
0135            /// <summary>
0136            /// 两端对齐
0137            /// </summary>
0138            Justify = Excel.Constants.xlJustify,
0139            /// <summary>
0140            /// 跨列居中
0141            /// </summary>
0142            CenterAcrossSelection = Excel.Constants.xlCenterAcrossSelection,
0143            /// <summary>
0144            /// 分散对齐
0145            /// </summary>
0146            Distributed = Excel.Constants.xlDistributed
0147  
0148        }
0149  
0150  
0151        /// <summary>
0152        /// Excel边框线条的枚举
0153        /// </summary>
0154        public enum ExcelStyleLine
0155        {
0156            /// <summary>
0157            /// 没有线条
0158            /// </summary>
0159            StyleNone = Excel.XlLineStyle.xlLineStyleNone,
0160            /// <summary>
0161            /// 连续的细线
0162            /// </summary>
0163            Continious = Excel.XlLineStyle.xlContinuous,
0164            /// <summary>
0165            /// 点状线
0166            /// </summary>
0167            Dot = Excel.XlLineStyle.xlDot,
0168            /// <summary>
0169            /// 双条线
0170            /// </summary>
0171            Double = Excel.XlLineStyle.xlDouble,
0172        }
0173  
0174        /// <summary>
0175        /// 排序的玫举
0176        /// </summary>
0177        public enum ExcelSortOrder
0178        {
0179            /// <summary>
0180            /// 升序
0181            /// </summary>
0182            Ascending = Excel.XlSortOrder.xlAscending,
0183            /// <summary>
0184            /// 降序
0185            /// </summary>
0186            Descending = Excel.XlSortOrder.xlDescending,
0187        }
0188  
0189  
0190  
0191        #endregion
0192  
0193        #region " Construction Method "
0194  
0195        /// <summary>
0196        /// 构造函数
0197        /// </summary>
0198        public ExcelBase()
0199        {
0200            //实例化Excel对象。
0201            exlApp = new Excel.Application();
0202        }
0203  
0204        /// <summary>
0205        /// 构造函数
0206        /// </summary>
0207        /// <param name="ExcelVisible">Excel是否可见</param>
0208        public ExcelBase(bool ExcelVisible)
0209        {
0210            exlApp = new Excel.Application();
0211            exlApp.Visible = ExcelVisible;
0212        }
0213  
0214        #endregion
0215  
0216        #region " Open and dispose method definition "
0217  
0218        /// <summary>
0219        /// 打开一个Excel文件
0220        /// </summary>
0221        public void Open()
0222        {
0223            //Get a new WorkSheet
0224            exlWorkBook = (Workbook)exlApp.Workbooks.Add(Missing.Value);
0225            exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet;
0226        }
0227  
0228        /// <summary>
0229        /// 打开已经存在的Excel文件模版
0230        /// </summary>
0231        /// <param name="XLTPath">已经存在的文件模版的完整路径</param>
0232        public void Open(string XLTPath)
0233        {
0234            if (System.IO.File.Exists(XLTPath))
0235            {
0236                exlWorkBook = (Workbook)exlApp.Workbooks.Add(XLTPath);
0237                exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet;
0238            }
0239            else
0240            {
0241                throw new System.IO.FileNotFoundException(string.Format("{0}不存在,请重新确定文件名", XLTPath));
0242            }
0243        }
0244  
0245        /// <summary>
0246        /// 保存Excel文件
0247        /// </summary>
0248        /// <param name="fileName">保存的文件名</param>
0249        public void SaveAs(string fileName)
0250        {
0251            exlWorkSheet.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value);
0252        }
0253  
0254        /// <summary>
0255        /// 彻底关闭Excel的资源和进程
0256        /// </summary>
0257        public void Dispose()
0258        {
0259            if (exlApp != null)
0260            {
0261                exlApp.Quit();
0262            }
0263            if (exlWorkBook != null)
0264            {
0265                System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWorkBook);
0266                exlWorkBook = null;
0267            }
0268            System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWorkSheet);
0269            exlWorkSheet = null;
0270            System.Runtime.InteropServices.Marshal.ReleaseComObject(exlApp);
0271            exlApp = null;
0272            GC.Collect();
0273        }
0274        #endregion
0275  
0276        #region " Print and PrintPreview method definition "
0277  
0278        /// <summary>
0279        /// 打印Excel文件,可以设置是否是打印前预览打印的Excel文件
0280        /// </summary>
0281        /// <param name="IsPrintPreview">打印前是否预览 , true:打印前预览false:直接打印,不预览 </param>
0282        public void Print(bool IsPrintPreview)
0283        {
0284            bool flag = exlApp.Visible;
0285            if (exlApp.Visible)
0286            {
0287                exlApp.Visible = true;
0288            }
0289            exlWorkSheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, IsPrintPreview, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
0290            exlApp.Visible = flag;
0291        }
0292  
0293        /// <summary>
0294        /// 打印Excel文件,可以设置是否打印预览,以及打印的份数
0295        /// </summary>
0296        /// <param name="IsPrintPreview">打印前是否预览 , true:打印前预览false:直接打印,不预览</param>
0297        /// <param name="iCopy">打印的份数</param>
0298        public void Print(bool IsPrintPreview, int iCopy)
0299        {
0300            if (iCopy < 1)
0301            {
0302                iCopy = 1;
0303            }
0304            exlWorkSheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, IsPrintPreview, iCopy, Missing.Value, Missing.Value, Missing.Value);
0305        }
0306  
0307        /// <summary>
0308        /// 打印预览Excel文件
0309        /// </summary>
0310        public void PrintPreview()
0311        {
0312            exlWorkSheet.PrintPreview(Missing.Value);
0313        }
0314  
0315        #endregion
0316  
0317        #region " Detail control excel method "
0318  
0319        /// <summary>
0320        /// 将Excel隐藏
0321        /// </summary>
0322        public void Hide()
0323        {
0324            exlApp.Visible = false;
0325        }
0326  
0327        /// <summary>
0328        /// 将Excel显示
0329        /// </summary>
0330        public void Show()
0331        {
0332            exlApp.Visible = true;
0333        }
0334  
0335        /// <summary>
0336        /// 设置工作簿的名称
0337        /// </summary>
0338        /// <param name="WorkSheet"></param>
0339        public void SetWorkSheetName(string WorkSheet)
0340        {
0341            exlWorkSheet.Name = WorkSheet;
0342        }
0343  
0344        /// <summary>
0345        ///返回指定单元格的内容
0346        /// </summary>
0347        /// <param name="iRow">定位的行</param>
0348        /// <param name="iCol">定位的列</param>
0349        /// <returns>返回指定单元格的内容</returns>
0350        public string GetCellText(int iRow, int iCol)
0351        {
0352            Range sRange = GetRange(iRow, iCol, iRow, iCol);
0353            string returnText = (string)sRange.Text;
0354            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0355            sRange = null;
0356            return returnText;
0357        }
0358  
0359        /// <summary>
0360        ///返回指定单元格的内容
0361        /// </summary>
0362        /// <param name="iRow">定位的行</param>
0363        /// <param name="iCol">定位的列</param>
0364        /// <returns>返回指定单元格的内容</returns>
0365        public string GetCellText(int startRow, int startCol, int startRow2, int startCol2)
0366        {
0367            Range sRange = GetRange(startRow, startCol, startRow2, startCol2);
0368            string returnText = (string)sRange.Text;
0369            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0370            sRange = null;
0371            return returnText;
0372        }
0373  
0374  
0375        /// <summary>
0376        /// 设置指定范围单元格的内容,通过单元格,比如从"A1" 到 "B3"
0377        /// </summary>
0378        /// <param name="startCell">开始的单元格,比如"A1"</param>
0379        /// <param name="endCell">结束的单元格,比如"B2"</param>机动车统计表.xlt
0380        /// <param name="text">要设置的内容,可以使用Excel的公式</param>
0381        public void SetCellText(string startCell, string endCell, string text)
0382        {
0383            Range sRange = exlWorkSheet.get_Range(startCell, endCell);
0384            //这里没有用value属性,而用Formula属性,因为考虑到可以扩展,可以利用公式
0385            sRange.Cells.Formula = text;
0386            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0387            sRange = null;
0388        }
0389  
0390        /// <summary>
0391        /// 设置指定范围单元格的内容,通过单元格,比如从"A1" 到 "B3"
0392        /// </summary>
0393        /// <param name="startCell">开始的单元格,比如"A1"</param>
0394        /// <param name="endCell">结束的单元格,比如"B2"</param>机动车统计表.xlt
0395        /// <param name="text">要设置的内容,可以使用Excel的公式</param>
0396        public void SetCellText(string startCell, string endCell, int text)
0397        {
0398            Range sRange = exlWorkSheet.get_Range(startCell, endCell);
0399            //这里没有用value属性,而用Formula属性,因为考虑到可以扩展,可以利用公式
0400            sRange.Cells.Formula = text.ToString();
0401            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0402            sRange = null;
0403        }
0404  
0405        /// <summary>
0406        /// 设置指定范围的单元格的内容,通过行列来定位。如第1行第2列内容
0407        /// </summary>
0408        /// <param name="iRow">开始的行</param>
0409        /// <param name="iCol">开始的列</param>
0410        ///<param name="text">要设置的文本,可以使用Excel的公式</param>
0411        public void SetCellText(int iRow, int iCol, string text)
0412        {
0413            Range sRange = this.GetRange(iRow, iCol, iRow, iCol);
0414            sRange.Cells.Formula = text;
0415            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0416            sRange = null;
0417        }
0418  
0419  
0420        /// <summary>
0421        /// 设置指定范围的单元格的内容,通过行列来定位。如第1行第2列内容
0422        /// </summary>
0423        /// <param name="iRow">开始的行</param>
0424        /// <param name="iCol">开始的列</param>
0425        ///<param name="text">要设置的文本,可以使用Excel的公式</param>
0426        public void SetCellTextNOZero(int iRow, int iCol, string text)
0427        {
0428            string txt = "";
0429            try
0430            {
0431                if (System.Convert.ToInt32(text) == 0)
0432                {
0433                    txt = "";
0434                }
0435                else
0436                {
0437                    txt = text;
0438                }
0439            }
0440            catch
0441            {
0442                txt = text;
0443            }
0444  
0445  
0446            Range sRange = this.GetRange(iRow, iCol, iRow, iCol);
0447            sRange.Cells.Formula = txt;
0448            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0449            sRange = null;
0450  
0451        }
0452  
0453        /// <summary>
0454        /// 设置指定范围的单元格的内容,通过行列来定位。如第1行第2列内容
0455        /// </summary>
0456        /// <param name="iRow">开始的行</param>
0457        /// <param name="iCol">开始的列</param>
0458        ///<param name="text">要设置的文本,可以使用Excel的公式</param>
0459        public void SetCellText(int iRow, int iCol, int text)
0460        {
0461            Range sRange = this.GetRange(iRow, iCol, iRow, iCol);
0462            sRange.Cells.Formula = text;
0463            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0464            sRange = null;
0465        }
0466        /// <summary>
0467        /// 设置指定单元格的内容,比如设置"A1"单元格的内容
0468        /// </summary>
0469        /// <param name="cell">指定的单元格</param>
0470        /// <param name="text">要设置的内容,可以使用Excel的公式,如sum(A1:A7)--合计A1到A7数值</param>
0471        public void SetCellText(string cell, string text)
0472        {
0473            Range sRange = GetRange(cell);
0474            sRange.Cells.Formula = text;
0475            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0476            sRange = null;
0477        }
0478  
0479        /// <summary>
0480        /// 设置指定单元格的内容
0481        /// </summary>
0482        /// <param name="cell">指定的单元格</param>
0483        /// <param name="num">要设置的内容</param>
0484        public void SetCellText(string cell, Int32 num)
0485        {
0486            Range sRange = GetRange(cell);
0487            sRange.Cells.Formula = num.ToString();
0488            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0489            sRange = null;
0490        }
0491  
0492        /// <summary>
0493        /// 设置指定单元格的内容,可以指定格式
0494        /// </summary>
0495        /// <param name="cell">要指定的单元格</param>
0496        /// <param name="textValue">要填写的内容</param>
0497        /// <param name="StringFormat">要显示的格式</param>
0498        ///<param name="FontName">设置单元格的字体</param>
0499        /// <param name="FontSize">设置单元格的字体大小</param>
0500        public void setCellTextByFormat(string cell, string textValue, string StringFormat, string FontName, string FontSize)
0501        {
0502            Range sRange = GetRange(cell);
0503            sRange.Select();
0504            if (StringFormat != "")
0505            {
0506                sRange.NumberFormatLocal = StringFormat;
0507            }
0508            if (FontName != "")
0509            {
0510                sRange.Font.Name = FontName;
0511            }
0512            if (FontSize != "")
0513            {
0514                sRange.Font.Size = FontSize;
0515            }
0516            sRange.Cells.Formula = textValue;
0517  
0518  
0519            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0520            sRange = null;
0521        }
0522  
0523        /// <summary>
0524        /// 设置指定单元格的内容,可以指定格式
0525        /// </summary>
0526        /// <param name="cell">要指定的单元格</param>
0527        /// <param name="textValue">要填写的内容</param>
0528        /// <param name="StringFormat">要显示的格式</param>
0529        ///<param name="FontName">设置单元格的字体</param>
0530        /// <param name="FontSize">设置单元格的字体大小</param>
0531        /// <param name="colorIndex">设置单元格的颜色,我查了MSDN但是没有颜色代码的说明,Excel中一共有56种颜色的代码,常用的几个是
0532        /// 1-黑色 2-白色 3-红色 4-草绿色 5-蓝色 6-黄色 7-紫色 ,如果想看仔细的颜色,就依次从 1 循环到 56 把颜色打印出来看看</param>
0533        public void setCellTextByFormat(string cell, string textValue, string StringFormat, string FontName, string FontSize, int colorIndex)
0534        {
0535            Range sRange = GetRange(cell);
0536            sRange.Select();
0537            if (StringFormat != "")
0538            {
0539                sRange.Cells.NumberFormatLocal = StringFormat;
0540            }
0541            if (FontName != "")
0542            {
0543                sRange.Font.Name = FontName;
0544            }
0545            if (FontSize != "")
0546            {
0547                sRange.Font.Size = FontSize;
0548            }
0549            if (colorIndex != 0)
0550            {
0551                sRange.Font.ColorIndex = colorIndex;
0552            }
0553            sRange.Cells.Formula = textValue;
0554  
0555            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0556            sRange = null;
0557        }
0558  
0559        /// <summary>
0560        /// 设置指定单元格的内容,可以指定格式
0561        /// </summary>
0562        /// <param name="cell">要指定的单元格</param>
0563        /// <param name="textValue">要填写的内容</param>
0564        /// <param name="StringFormat">要显示的格式</param>
0565        /// <param name="FontName">设置单元格的字体</param>
0566        /// <param name="FontSize">设置单元格的字体大小</param>
0567        /// <param name="colorIndex">设置单元格的颜色,我查了MSDN但是没有颜色代码的说明,Excel中一共有56种颜色的代码,常用的几个是
0568        /// 1-黑色 2-白色 3-红色 4-草绿色 5-蓝色 6-黄色 7-紫色 ,如果想看仔细的颜色,就依次从 1 循环到 56 把颜色打印出来看看</param>
0569        /// <param name="Bold">设置单元格的字体是否粗体</param>
0570        public void setCellTextByFormat(string cell, string textValue, string StringFormat, string FontName, string FontSize, int colorIndex, bool Bold)
0571        {
0572            Range sRange = GetRange(cell);
0573            sRange.Select();
0574            if (StringFormat != "")
0575            {
0576                sRange.Cells.NumberFormatLocal = StringFormat;
0577            }
0578            if (FontName != "")
0579            {
0580                sRange.Font.Name = FontName;
0581            }
0582            if (FontSize != "")
0583            {
0584                sRange.Font.Size = FontSize;
0585            }
0586            if (colorIndex != 0)
0587            {
0588                sRange.Font.ColorIndex = colorIndex;
0589            }
0590            sRange.Font.Bold = Bold;
0591            sRange.Cells.Formula = textValue;
0592  
0593            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0594            sRange = null;
0595        }
0596  
0597        /// <summary>
0598        /// 设置指定单元格的内容,可以指定格式
0599        /// </summary>
0600        /// <param name="cell">要指定的单元格</param>
0601        /// <param name="textValue">要填写的内容</param>
0602        /// <param name="StringFormat">要显示的格式</param>
0603        /// <param name="FontName">设置单元格的字体</param>
0604        /// <param name="FontSize">设置单元格的字体大小</param>
0605        /// <param name="colorIndex">设置单元格的颜色,我查了MSDN但是没有颜色代码的说明,Excel中一共有56种颜色的代码,常用的几个是
0606        /// 1-黑色 2-白色 3-红色 4-草绿色 5-蓝色 6-黄色 7-紫色 ,如果想看仔细的颜色,就依次从 1 循环到 56 把颜色打印出来看看</param>
0607        /// <param name="Bold">设置单元格的字体是否粗体</param>
0608        /// <param name="BcolorIndex">设置单元格背景颜色</param>
0609        public void setCellTextByFormat(string cell, string textValue, string StringFormat, string FontName, string FontSize, int colorIndex, bool Bold, int BcolorIndex)
0610        {
0611            Range sRange = GetRange(cell);
0612            sRange.Select();
0613            if (StringFormat != "")
0614            {
0615                sRange.Cells.NumberFormatLocal = StringFormat;
0616            }
0617            if (FontName != "")
0618            {
0619                sRange.Font.Name = FontName;
0620            }
0621            if (FontSize != "")
0622            {
0623                sRange.Font.Size = FontSize;
0624            }
0625            if (colorIndex != 0)
0626            {
0627                sRange.Font.ColorIndex = colorIndex;
0628            }
0629            sRange.Font.Bold = Bold;
0630            sRange.Cells.Formula = textValue;
0631            sRange.Interior.ColorIndex = BcolorIndex;
0632  
0633            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0634            sRange = null;
0635        }
0636  
0637  
0638        /// <summary>
0639        /// 设置单元格的内容(指定单元格的格式化字符串)
0640        /// </summary>
0641        /// <param name="cell">指定的单元格</param>
0642        /// <param name="textValue">内容</param>
0643        /// <param name="stringFormat">格式化字符串</param>
0644        public void setCellText(string cell, string textValue, string stringFormat)
0645        {
0646            Range sRange = GetRange(cell);
0647            sRange.Select();
0648            if (stringFormat != "")
0649            {
0650                sRange.Cells.NumberFormatLocal = stringFormat;
0651            }
0652            sRange.Cells.Formula = textValue;
0653            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0654            sRange = null;
0655        }
0656        /// <summary>
0657        /// 得到指定单元格的内容
0658        /// </summary>
0659        /// <param name="cell">指定的单元格比如 A1,A2</param>
0660        /// <returns>返回指定的内容</returns>
0661        public object GetCellText(string cell)
0662        {
0663            object returnValue;
0664            Range sRange = GetRange(cell);
0665            returnValue = sRange.Cells.Text;
0666            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0667            sRange = null;
0668            return returnValue;
0669        }
0670  
0671        /// <summary>
0672        /// 设置指定单元格的内容,比如设置"A1"单元格的内容
0673        /// </summary>
0674        /// <param name="cell">指定的单元格</param>
0675        /// <param name="text">要设置的内容,使用Excel里面的R1C1这样的格式(不知道是不是画蛇添足,因为Excel里的Macro中是这样使用的)</param>
0676        public void SetCellTextR1C1(string cell, string text)
0677        {
0678            Range sRange = GetRange(cell);
0679            sRange.Cells.FormulaR1C1 = text;
0680            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0681            sRange = null;
0682        }
0683  
0684        /// <summary>
0685        ///设置单元格的单元格格式
0686        /// </summary>
0687        /// <param name="cell">要设定的单元格的坐标</param>
0688        /// <param name="formatString">单元格的格式化字符   
0689        /// 常规:"G/通用格式"
0690        /// 数值:"[红色]-0.00"(表示是2位小数,如果是负数的话则用红色表示)
0691        /// 货币:"¥#,##0.000;[红色]¥-#,##0.000"(¥是货币符号,可以用$,也可以不填写,0.000代表三位小数位;[红色]表示如果是负数的话,用红色表示)
0692        /// 日期:@"yyyy"年"m"月"d"日";@"    (表示用年月日了表示)        @"[DBNum1][$-804]yyyy"年"m"月"d"日";@"(表示用汉字表示年月日)
0693        /// 百分比:"0.000%;[红色]-0.000%"(表示小数位为3位,红色表示如果是负数的话则用红色表示)
0694        /// 文本:"@"(表示是文本格式)
0695        /// 特殊:"[DBNum1][$-804]G/通用格式"(能将数字转换成中文小写,如1234转换成一千二百三十四)        "[DBNum2][$-804]G/通用格式"(能将数字转换成中文大写,如1234转换成 壹仟贰佰叁拾肆)
0696        /// 自定义:输入自定义的格式化字符串
0697        /// </param>
0698        public void SetCellFormat(string cell, string formatString)
0699        {
0700            Range sRange = GetRange(cell);
0701            sRange.Select();
0702            sRange.NumberFormatLocal = formatString;
0703            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0704            sRange = null;
0705        }
0706  
0707        /// <summary>
0708        /// 设置指定范围的单元格格式
0709        /// </summary>
0710        /// <param name="startCell">开始的单元格</param>
0711        /// <param name="endCell">结束的单元格</param>
0712        /// <param name="formatString">单元格的格式化字符   
0713        /// 常规:"G/通用格式"
0714        /// 数值:"[红色]-0.00"(表示是2位小数,如果是负数的话则用红色表示)
0715        /// 货币:"¥#,##0.000;[红色]¥-#,##0.000"(¥是货币符号,可以用$,也可以不填写,0.000代表三位小数位;[红色]表示如果是负数的话,用红色表示)
0716        /// 日期:@"yyyy"年"m"月"d"日";@"    (表示用年月日了表示)        @"[DBNum1][$-804]yyyy"年"m"月"d"日";@"(表示用汉字表示年月日)
0717        /// 百分比:"0.000%;[红色]-0.000%"(表示小数位为3位,红色表示如果是负数的话则用红色表示)
0718        /// 文本:"@"(表示是文本格式)
0719        /// 特殊:"[DBNum1][$-804]G/通用格式"(能将数字转换成中文小写,如1234转换成一千二百三十四)        "[DBNum2][$-804]G/通用格式"(能将数字转换成中文大写,如1234转换成 壹仟贰佰叁拾肆)
0720        /// 自定义:输入自定义的格式化字符串</param>
0721        public void SetAreaCellFormat(string startCell, string endCell, string formatString)
0722        {
0723            Range sRange = GetRange(startCell, endCell);
0724            sRange.Select();
0725            sRange.NumberFormatLocal = formatString;
0726            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0727            sRange = null;
0728        }
0729  
0730        /// <summary>
0731        /// 设置某一列,某几列的列宽为自动适应大小,比如要设置第1列为自动适应大小SetColumnAutoFit("A","A")
0732        /// </summary>
0733        /// <param name="startColumn">开始的列</param>
0734        /// <param name="endColumn">结束的列</param>
0735        ///
0736        //TODO:stapangpang  要增加自动适应列大小的方法
0737        public void SetColumnAutoFit(string startColumn, string endColumn)
0738        {
0739            Range sRange = (Range)exlWorkSheet.Columns[String.Format("{0}:{1}", startColumn, endColumn), Missing.Value];
0740            sRange.Select();
0741            sRange.EntireColumn.AutoFit();
0742            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0743            sRange = null;
0744        }
0745        #endregion
0746  
0747        #region " Get excel range method definition "
0748  
0749        /// <summary>
0750        /// 根据行列的定位,返回选定的单元格。因为Range 是通过Cell来定位的,而Cell需要2个参数定位,所以需要四个参数。               
0751        /// </summary>
0752        /// <param name="iStartRow">定位开始Range的Cell的行</param>
0753        /// <param name="iStartCol">定位开始Range的Cell的列</param>
0754        /// <param name="iEndRow">定位结束Range的Cell的行</param>
0755        /// <param name="iEndCol">定位结束Range的Cell的列</param>
0756        /// <returns>返回指定范围的Range</returns>
0757        public Range GetRange(int iStartRow, int iStartCol, int iEndRow, int iEndCol)
0758        {
0759            return exlWorkSheet.get_Range(exlApp.Cells[iStartRow, iStartCol], exlApp.Cells[iEndRow, iEndCol]);
0760        }
0761  
0762        /// <summary>
0763        /// 返回指定的单元格
0764        /// </summary>
0765        /// <param name="cell">指定的单元格</param>
0766        /// <returns>返回指定的单元格</returns>
0767        public Range GetRange(string cell)
0768        {
0769            return exlWorkSheet.get_Range(cell, Missing.Value);
0770        }
0771  
0772        /// <summary>
0773        /// 返回一个单元格的范围
0774        /// </summary>
0775        /// <param name="startCell">开始的单元格坐标</param>
0776        /// <param name="endCell">结束的单元格坐标</param>
0777        /// <returns>返回指定的单元格范围</returns>
0778        public Range GetRange(string startCell, string endCell)
0779        {
0780            return exlWorkSheet.get_Range(startCell, endCell);
0781        }
0782  
0783        /// <summary>
0784        /// 增加一个工作簿
0785        /// </summary>
0786        public void AddWorkSheet()
0787        {
0788            if (this.sheetNumber <= 3)
0789            {
0790                exlApp.ActiveWorkbook.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
0791                exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet;
0792                exlWorkSheet.Select(Missing.Value);
0793            }
0794            else
0795            {
0796                sheetNumber++;
0797                exlApp.ActiveWorkbook.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
0798                exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet;
0799                exlWorkSheet.Select(Missing.Value);
0800            }
0801            //exlWorkBook.ActiveSheet;
0802  
0803        }
0804  
0805  
0806        #endregion
0807  
0808        #region " Excel range style method definition "
0809        /// <summary>
0810        /// 设置单元格的垂直方向对齐方式
0811        /// </summary>
0812        /// <param name="cell">指定的单元格</param>
0813        /// <param name="cellAlignment">垂直方向的对齐方式</param>
0814        public void SetCellVerticalAlignment(string cell, ExcelVerticalAlignment cellAlignment)
0815        {
0816            Range sRange = GetRange(cell);
0817            sRange.Select();
0818            sRange.VerticalAlignment = cellAlignment;
0819            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0820            sRange = null;
0821        }
0822  
0823        /// <summary>
0824        /// 设定指定范围的单元格的垂直对齐方式
0825        /// </summary>
0826        /// <param name="startCell">开始的单元格的坐标</param>
0827        /// <param name="endCell">结束单元格的坐标</param>
0828        /// <param name="cellAlignment">对齐方式</param>
0829        public void SetCellAreaVerticalAlignment(string startCell, string endCell, ExcelVerticalAlignment cellAlignment)
0830        {
0831            Range sRange = GetRange(startCell, endCell);
0832            sRange.Select();
0833            sRange.VerticalAlignment = cellAlignment;
0834            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0835            sRange = null;
0836        }
0837  
0838  
0839        /// <summary>
0840        /// 设置指定范围的单元格的水平方向的对齐方式
0841        /// </summary>
0842        /// <param name="cell">指定的单元格</param>
0843        /// <param name="cellAlignment">水平方向的对齐方式</param>
0844        public void SetCellHorizontalAlignment(string cell, ExcelHorizontalAlignment cellAlignment)
0845        {
0846            Range sRange = GetRange(cell);
0847            sRange.Select();
0848            sRange.HorizontalAlignment = cellAlignment;
0849            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0850            sRange = null;
0851        }
0852  
0853        /// <summary>
0854        /// 设定指定范围的单元格的水平对齐方式
0855        /// </summary>
0856        /// <param name="startCell">开始的单元格的坐标</param>
0857        /// <param name="endCell">结束单元格的坐标</param>
0858        /// <param name="cellAlignment">对齐方式</param>
0859        public void SetCellAreaHorizontalAlignment(string startCell, string endCell, ExcelHorizontalAlignment cellAlignment)
0860        {
0861            Range sRange = GetRange(startCell, endCell);
0862            sRange.Select();
0863            sRange.HorizontalAlignment = cellAlignment;
0864            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0865            sRange = null;
0866        }
0867  
0868        /// <summary>
0869        /// 设置指定单元格的边框,这里只能设置单个单元格的边框
0870        /// </summary>
0871        /// <param name="cell">要设定的单元格</param>
0872        public void SetCellBorder(string cell)
0873        {
0874            Range sRange = GetRange(cell);
0875  
0876  
0877            //上边框
0878            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].LineStyle = ExcelStyleLine.Continious;
0879            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].Weight = ExcelBorderWeight.Thin;
0880            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].ColorIndex = Excel.Constants.xlAutomatic;
0881            //底边框
0882            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].LineStyle = ExcelStyleLine.Continious;
0883            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].Weight = ExcelBorderWeight.Thin;
0884            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].ColorIndex = Excel.Constants.xlAutomatic;
0885            //右边框
0886            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].LineStyle = ExcelStyleLine.Continious;
0887            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].Weight = ExcelBorderWeight.Thin;
0888            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].ColorIndex = Excel.Constants.xlAutomatic;
0889            //左边框
0890            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].LineStyle = ExcelStyleLine.Continious;
0891            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].Weight = ExcelBorderWeight.Thin;
0892            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].ColorIndex = Excel.Constants.xlAutomatic;
0893  
0894            //释放资源
0895            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0896            sRange = null;
0897        }
0898  
0899        /// <summary>
0900        /// 设置指定范围的Excel单元格的边框,包括外边框,内边框
0901        /// </summary>
0902        /// <param name="startCell">开始的单元格坐标</param>
0903        /// <param name="endCell">结束的单元格坐标</param>
0904        public void SetAreaBorder1(string startCell, String endCell)
0905        {
0906            Range sRange = GetRange(startCell, endCell);
0907            //上边框
0908            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].LineStyle = ExcelStyleLine.Continious;
0909            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].Weight = ExcelBorderWeight.Thin;
0910            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].ColorIndex = Excel.Constants.xlAutomatic;
0911            //底边框
0912            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].LineStyle = ExcelStyleLine.Continious;
0913            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].Weight = ExcelBorderWeight.Thin;
0914            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].ColorIndex = Excel.Constants.xlAutomatic;
0915            //右边框
0916            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].LineStyle = ExcelStyleLine.Continious;
0917            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].Weight = ExcelBorderWeight.Thin;
0918            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].ColorIndex = Excel.Constants.xlAutomatic;
0919            //左边框
0920            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].LineStyle = ExcelStyleLine.Continious;
0921            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].Weight = ExcelBorderWeight.Thin;
0922            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].ColorIndex = Excel.Constants.xlAutomatic;
0923  
0924            //范围内竖直竖线
0925            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideVertical].LineStyle = ExcelStyleLine.Continious;
0926            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideVertical].Weight = ExcelBorderWeight.Thin;
0927            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideVertical].ColorIndex = Excel.Constants.xlAutomatic;
0928  
0929  
0930            //释放资源
0931            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0932            sRange = null;
0933        }
0934        /// <summary>
0935        /// 设置指定范围的Excel单元格的边框,包括外边框,内边框
0936        /// </summary>
0937        /// <param name="startCell">开始的单元格坐标</param>
0938        /// <param name="endCell">结束的单元格坐标</param>
0939        public void SetAreaBorder(string startCell, String endCell)
0940        {
0941            Range sRange = GetRange(startCell, endCell);
0942            //上边框
0943            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].LineStyle = ExcelStyleLine.Continious;
0944            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].Weight = ExcelBorderWeight.Thin;
0945            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeTop].ColorIndex = Excel.Constants.xlAutomatic;
0946            //底边框
0947            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].LineStyle = ExcelStyleLine.Continious;
0948            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].Weight = ExcelBorderWeight.Thin;
0949            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeBottom].ColorIndex = Excel.Constants.xlAutomatic;
0950            //右边框
0951            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].LineStyle = ExcelStyleLine.Continious;
0952            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].Weight = ExcelBorderWeight.Thin;
0953            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeRight].ColorIndex = Excel.Constants.xlAutomatic;
0954            //左边框
0955            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].LineStyle = ExcelStyleLine.Continious;
0956            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].Weight = ExcelBorderWeight.Thin;
0957            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.EdgeLeft].ColorIndex = Excel.Constants.xlAutomatic;
0958            //范围内水平横线
0959            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideHorizontal].LineStyle = ExcelStyleLine.Continious;
0960            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideHorizontal].Weight = ExcelBorderWeight.Thin;
0961            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideHorizontal].ColorIndex = Excel.Constants.xlAutomatic;
0962  
0963            //范围内竖直竖线
0964            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideVertical].LineStyle = ExcelStyleLine.Continious;
0965            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideVertical].Weight = ExcelBorderWeight.Thin;
0966            sRange.Borders[(Excel.XlBordersIndex)ExcelBordersIndex.InsideVertical].ColorIndex = Excel.Constants.xlAutomatic;
0967  
0968  
0969            //释放资源
0970            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0971            sRange = null;
0972        }
0973  
0974        /// <summary>
0975        /// 设置单元格的颜色
0976        /// </summary>
0977        /// <param name="cell">定位改单元格</param>
0978        /// <param name="colorIndex">颜色的代码,我查了MSDN但是没有颜色代码的说明,Excel中一共有56种颜色的代码,常用的几个是
0979        /// 1-黑色 2-白色 3-红色 4-草绿色 5-蓝色 6-黄色 7-紫色 ,如果想看仔细的颜色,就依次从 1 循环到 56 把颜色打印出来看看</param>
0980        public void SetCellBackGroundColor(string cell, int colorIndex)
0981        {
0982            Range sRange = GetRange(cell);
0983            sRange.Select();
0984            sRange.Font.ColorIndex = colorIndex;
0985            //释放资源
0986            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
0987            sRange = null;
0988        }
0989  
0990        /// <summary>
0991        /// 设置指定单元格范围的颜色
0992        /// </summary>
0993        /// <param name="startCell">开始的单元格</param>
0994        /// <param name="endCell">结束的单元格</param>
0995        /// <param name="colorIndex">颜色的代码,我查了MSDN但是没有颜色代码的说明,Excel中一共有56种颜色的代码,常用的几个是
0996        /// 1-黑色 2-白色 3-红色 4-草绿色 5-蓝色 6-黄色 7-紫色 ,如果想看仔细的颜色,就依次从 1 循环到 56 把颜色打印出来看看</param>
0997        public void SetAreaCellBackGroundColor(string startCell, string endCell, int colorIndex)
0998        {
0999            Range sRange = GetRange(startCell, endCell);
1000            sRange.Select();
1001            sRange.Font.ColorIndex = colorIndex;
1002            System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange);
1003            sRange = null;
1004  
1005        }
1006  
1007        /// <summary>
1008        /// 合并单元格
1009        /// <param name="startCell">开始的单元格</param>
1010        /// <param name="endCell">结束的单元格</param>
1011        /// </summary>
1012        public void SetMergeCells(string startCell, string endCell)
1013        {
1014            Range sRange = GetRange(startCell, endCell);
1015            sRange.MergeCells = true;
1016        }
1017        /// <summary>
1018        /// 合并单元格
1019        /// <param name="iStartRow">定位开始Range的Cell的行(A1=1,1)</param>
1020        /// <param name="iStartCol">定位开始Range的Cell的列(A1=1,1)</param>
1021        /// <param name="iEndRow">定位结束Range的Cell的行(A1=1,1)</param>
1022        /// <param name="iEndCol">定位结束Range的Cell的列(A1=1,1)</param>
1023        /// </summary>
1024        public void SetMergeCells(int iStartRow, int iStartCol, int iEndRow, int iEndCol)
1025        {
1026            Range sRange = GetRange(iStartRow, iStartCol, iEndRow, iEndCol);
1027            sRange.MergeCells = true;
1028        }
1029  
1030        /// <summary>
1031        /// 设置单元格背景颜色
1032        /// <param name="startCell">开始的单元格</param>
1033        /// <param name="endCell">结束的单元格</param>
1034        /// <param name="ColorIndex">颜色的代码,我查了MSDN但是没有颜色代码的说明,Excel中一共有56种颜色的代码,常用的几个是
1035        /// 1-黑色 2-白色 3-红色 4-草绿色 5-蓝色 6-黄色 7-紫色 ,如果想看仔细的颜色,就依次从 1 循环到 56 把颜色打印出来看看</param>
1036        /// </summary>
1037        public void SetInteriorColor(string startCell, string endCell, int ColorIndex)
1038        {
1039            Range sRange = GetRange(startCell, endCell);
1040            sRange.Interior.ColorIndex = ColorIndex;
1041        }
1042  
1043        /// <summary>
1044        /// 设置单元格背景颜色
1045        /// <param name="iStartRow">定位开始Range的Cell的行(A1=1,1)</param>
1046        /// <param name="iStartCol">定位开始Range的Cell的列(A1=1,1)</param>
1047        /// <param name="iEndRow">定位结束Range的Cell的行(A1=1,1)</param>
1048        /// <param name="iEndCol">定位结束Range的Cell的列(A1=1,1)</param>
1049        /// <param name="ColorIndex">颜色的代码,我查了MSDN但是没有颜色代码的说明,Excel中一共有56种颜色的代码,常用的几个是
1050        /// 1-黑色 2-白色 3-红色 4-草绿色 5-蓝色 6-黄色 7-紫色 ,如果想看仔细的颜色,就依次从 1 循环到 56 把颜色打印出来看看</param>
1051        /// </summary>
1052        public void SetInteriorColor(int iStartRow, int iStartCol, int iEndRow, int iEndCol, int ColorIndex)
1053        {
1054            Range sRange = GetRange(iStartRow, iStartCol, iEndRow, iEndCol);
1055            sRange.Interior.ColorIndex = ColorIndex;
1056        }
1057        #endregion
1058  
1059    }
1060}