C#操作Excel全能帮助类:
1,Excel单元格边框的线条的粗细枚举
2,Excel单元格边框枚举
3,彻底关闭Excel的资源和进程
4,打印预览Excel文件
5,设置工作簿的名称
6,设置指定单元格的内容,可以指定格式
7,设置指定范围的单元格格式
0003 | using System.Reflection; |
0007 | public class ExcelBase |
0009 | #region " Private Variable Definition " |
0011 | private Application exlApp; |
0012 | private _Workbook exlWorkBook; |
0013 | private _Worksheet exlWorkSheet; |
0014 | private int sheetNumber = 1; |
0018 | #region " Public Property and Constant Definition " |
0021 | /// Excel单元格边框的线条的粗细枚举 |
0023 | public enum ExcelBorderWeight |
0028 | Hairline = Excel.XlBorderWeight.xlHairline, |
0032 | Medium = Excel.XlBorderWeight.xlMedium, |
0036 | Thick = Excel.XlBorderWeight.xlThick, |
0040 | Thin = Excel.XlBorderWeight.xlThin |
0046 | public enum ExcelBordersIndex |
0051 | DiagonalDown = Excel.XlBordersIndex.xlDiagonalDown, |
0055 | DiagonUp = Excel.XlBordersIndex.xlDiagonalUp, |
0059 | EdgeBottom = Excel.XlBordersIndex.xlEdgeBottom, |
0063 | EdgeLeft = Excel.XlBordersIndex.xlEdgeLeft, |
0067 | EdgeRight = Excel.XlBordersIndex.xlEdgeRight, |
0071 | EdgeTop = Excel.XlBordersIndex.xlEdgeTop, |
0075 | InsideHorizontal = Excel.XlBordersIndex.xlInsideHorizontal, |
0079 | InsideVertical = Excel.XlBordersIndex.xlInsideVertical |
0083 | /// Excel单元格的竖直方法对齐枚举 |
0085 | public enum ExcelVerticalAlignment |
0090 | Center = Excel.Constants.xlCenter, |
0094 | Top = Excel.Constants.xlTop, |
0098 | Bottom = Excel.Constants.xlBottom, |
0102 | Justify = Excel.Constants.xlJustify, |
0106 | Distributed = Excel.Constants.xlDistributed |
0113 | public enum ExcelHorizontalAlignment |
0118 | General = Excel.Constants.xlGeneral, |
0122 | Left = Excel.Constants.xlLeft, |
0126 | Center = Excel.Constants.xlCenter, |
0130 | Right = Excel.Constants.xlRight, |
0134 | Fill = Excel.Constants.xlFill, |
0138 | Justify = Excel.Constants.xlJustify, |
0142 | CenterAcrossSelection = Excel.Constants.xlCenterAcrossSelection, |
0146 | Distributed = Excel.Constants.xlDistributed |
0154 | public enum ExcelStyleLine |
0159 | StyleNone = Excel.XlLineStyle.xlLineStyleNone, |
0163 | Continious = Excel.XlLineStyle.xlContinuous, |
0167 | Dot = Excel.XlLineStyle.xlDot, |
0171 | Double = Excel.XlLineStyle.xlDouble, |
0177 | public enum ExcelSortOrder |
0182 | Ascending = Excel.XlSortOrder.xlAscending, |
0186 | Descending = Excel.XlSortOrder.xlDescending, |
0193 | #region " Construction Method " |
0201 | exlApp = new Excel.Application(); |
0207 | /// <param name="ExcelVisible">Excel是否可见</param> |
0208 | public ExcelBase( bool ExcelVisible) |
0210 | exlApp = new Excel.Application(); |
0211 | exlApp.Visible = ExcelVisible; |
0216 | #region " Open and dispose method definition " |
0224 | exlWorkBook = (Workbook)exlApp.Workbooks.Add(Missing.Value); |
0225 | exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet; |
0229 | /// 打开已经存在的Excel文件模版 |
0231 | /// <param name="XLTPath">已经存在的文件模版的完整路径</param> |
0232 | public void Open( string XLTPath) |
0234 | if (System.IO.File.Exists(XLTPath)) |
0236 | exlWorkBook = (Workbook)exlApp.Workbooks.Add(XLTPath); |
0237 | exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet; |
0241 | throw new System.IO.FileNotFoundException( string .Format( "{0}不存在,请重新确定文件名" , XLTPath)); |
0248 | /// <param name="fileName">保存的文件名</param> |
0249 | public void SaveAs( string fileName) |
0251 | exlWorkSheet.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, false , false , Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value); |
0257 | public void Dispose() |
0263 | if (exlWorkBook != null ) |
0265 | System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWorkBook); |
0268 | System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWorkSheet); |
0269 | exlWorkSheet = null ; |
0270 | System.Runtime.InteropServices.Marshal.ReleaseComObject(exlApp); |
0276 | #region " Print and PrintPreview method definition " |
0279 | /// 打印Excel文件,可以设置是否是打印前预览打印的Excel文件 |
0281 | /// <param name="IsPrintPreview">打印前是否预览 , true:打印前预览false:直接打印,不预览 </param> |
0282 | public void Print( bool IsPrintPreview) |
0284 | bool flag = exlApp.Visible; |
0287 | exlApp.Visible = true ; |
0289 | exlWorkSheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, IsPrintPreview, Missing.Value, Missing.Value, Missing.Value, Missing.Value); |
0290 | exlApp.Visible = flag; |
0294 | /// 打印Excel文件,可以设置是否打印预览,以及打印的份数 |
0296 | /// <param name="IsPrintPreview">打印前是否预览 , true:打印前预览false:直接打印,不预览</param> |
0297 | /// <param name="iCopy">打印的份数</param> |
0298 | public void Print( bool IsPrintPreview, int iCopy) |
0304 | exlWorkSheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, IsPrintPreview, iCopy, Missing.Value, Missing.Value, Missing.Value); |
0310 | public void PrintPreview() |
0312 | exlWorkSheet.PrintPreview(Missing.Value); |
0317 | #region " Detail control excel method " |
0324 | exlApp.Visible = false ; |
0332 | exlApp.Visible = true ; |
0338 | /// <param name="WorkSheet"></param> |
0339 | public void SetWorkSheetName( string WorkSheet) |
0341 | exlWorkSheet.Name = WorkSheet; |
0347 | /// <param name="iRow">定位的行</param> |
0348 | /// <param name="iCol">定位的列</param> |
0349 | /// <returns>返回指定单元格的内容</returns> |
0350 | public string GetCellText( int iRow, int iCol) |
0352 | Range sRange = GetRange(iRow, iCol, iRow, iCol); |
0353 | string returnText = ( string )sRange.Text; |
0354 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
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) |
0367 | Range sRange = GetRange(startRow, startCol, startRow2, startCol2); |
0368 | string returnText = ( string )sRange.Text; |
0369 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0376 | /// 设置指定范围单元格的内容,通过单元格,比如从"A1" 到 "B3" |
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) |
0383 | Range sRange = exlWorkSheet.get_Range(startCell, endCell); |
0385 | sRange.Cells.Formula = text; |
0386 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0391 | /// 设置指定范围单元格的内容,通过单元格,比如从"A1" 到 "B3" |
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) |
0398 | Range sRange = exlWorkSheet.get_Range(startCell, endCell); |
0400 | sRange.Cells.Formula = text.ToString(); |
0401 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0406 | /// 设置指定范围的单元格的内容,通过行列来定位。如第1行第2列内容 |
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) |
0413 | Range sRange = this .GetRange(iRow, iCol, iRow, iCol); |
0414 | sRange.Cells.Formula = text; |
0415 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0421 | /// 设置指定范围的单元格的内容,通过行列来定位。如第1行第2列内容 |
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) |
0431 | if (System.Convert.ToInt32(text) == 0) |
0446 | Range sRange = this .GetRange(iRow, iCol, iRow, iCol); |
0447 | sRange.Cells.Formula = txt; |
0448 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0454 | /// 设置指定范围的单元格的内容,通过行列来定位。如第1行第2列内容 |
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) |
0461 | Range sRange = this .GetRange(iRow, iCol, iRow, iCol); |
0462 | sRange.Cells.Formula = text; |
0463 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0467 | /// 设置指定单元格的内容,比如设置"A1"单元格的内容 |
0469 | /// <param name="cell">指定的单元格</param> |
0470 | /// <param name="text">要设置的内容,可以使用Excel的公式,如sum(A1:A7)--合计A1到A7数值</param> |
0471 | public void SetCellText( string cell, string text) |
0473 | Range sRange = GetRange(cell); |
0474 | sRange.Cells.Formula = text; |
0475 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0482 | /// <param name="cell">指定的单元格</param> |
0483 | /// <param name="num">要设置的内容</param> |
0484 | public void SetCellText( string cell, Int32 num) |
0486 | Range sRange = GetRange(cell); |
0487 | sRange.Cells.Formula = num.ToString(); |
0488 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0493 | /// 设置指定单元格的内容,可以指定格式 |
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) |
0502 | Range sRange = GetRange(cell); |
0504 | if (StringFormat != "" ) |
0506 | sRange.NumberFormatLocal = StringFormat; |
0510 | sRange.Font.Name = FontName; |
0514 | sRange.Font.Size = FontSize; |
0516 | sRange.Cells.Formula = textValue; |
0519 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0524 | /// 设置指定单元格的内容,可以指定格式 |
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) |
0535 | Range sRange = GetRange(cell); |
0537 | if (StringFormat != "" ) |
0539 | sRange.Cells.NumberFormatLocal = StringFormat; |
0543 | sRange.Font.Name = FontName; |
0547 | sRange.Font.Size = FontSize; |
0549 | if (colorIndex != 0) |
0551 | sRange.Font.ColorIndex = colorIndex; |
0553 | sRange.Cells.Formula = textValue; |
0555 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0560 | /// 设置指定单元格的内容,可以指定格式 |
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) |
0572 | Range sRange = GetRange(cell); |
0574 | if (StringFormat != "" ) |
0576 | sRange.Cells.NumberFormatLocal = StringFormat; |
0580 | sRange.Font.Name = FontName; |
0584 | sRange.Font.Size = FontSize; |
0586 | if (colorIndex != 0) |
0588 | sRange.Font.ColorIndex = colorIndex; |
0590 | sRange.Font.Bold = Bold; |
0591 | sRange.Cells.Formula = textValue; |
0593 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0598 | /// 设置指定单元格的内容,可以指定格式 |
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) |
0611 | Range sRange = GetRange(cell); |
0613 | if (StringFormat != "" ) |
0615 | sRange.Cells.NumberFormatLocal = StringFormat; |
0619 | sRange.Font.Name = FontName; |
0623 | sRange.Font.Size = FontSize; |
0625 | if (colorIndex != 0) |
0627 | sRange.Font.ColorIndex = colorIndex; |
0629 | sRange.Font.Bold = Bold; |
0630 | sRange.Cells.Formula = textValue; |
0631 | sRange.Interior.ColorIndex = BcolorIndex; |
0633 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0639 | /// 设置单元格的内容(指定单元格的格式化字符串) |
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) |
0646 | Range sRange = GetRange(cell); |
0648 | if (stringFormat != "" ) |
0650 | sRange.Cells.NumberFormatLocal = stringFormat; |
0652 | sRange.Cells.Formula = textValue; |
0653 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0659 | /// <param name="cell">指定的单元格比如 A1,A2</param> |
0660 | /// <returns>返回指定的内容</returns> |
0661 | public object GetCellText( string cell) |
0664 | Range sRange = GetRange(cell); |
0665 | returnValue = sRange.Cells.Text; |
0666 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0672 | /// 设置指定单元格的内容,比如设置"A1"单元格的内容 |
0674 | /// <param name="cell">指定的单元格</param> |
0675 | /// <param name="text">要设置的内容,使用Excel里面的R1C1这样的格式(不知道是不是画蛇添足,因为Excel里的Macro中是这样使用的)</param> |
0676 | public void SetCellTextR1C1( string cell, string text) |
0678 | Range sRange = GetRange(cell); |
0679 | sRange.Cells.FormulaR1C1 = text; |
0680 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0687 | /// <param name="cell">要设定的单元格的坐标</param> |
0688 | /// <param name="formatString">单元格的格式化字符 |
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位,红色表示如果是负数的话则用红色表示) |
0695 | /// 特殊:"[DBNum1][$-804]G/通用格式"(能将数字转换成中文小写,如1234转换成一千二百三十四) "[DBNum2][$-804]G/通用格式"(能将数字转换成中文大写,如1234转换成 壹仟贰佰叁拾肆) |
0696 | /// 自定义:输入自定义的格式化字符串 |
0698 | public void SetCellFormat( string cell, string formatString) |
0700 | Range sRange = GetRange(cell); |
0702 | sRange.NumberFormatLocal = formatString; |
0703 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0710 | /// <param name="startCell">开始的单元格</param> |
0711 | /// <param name="endCell">结束的单元格</param> |
0712 | /// <param name="formatString">单元格的格式化字符 |
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位,红色表示如果是负数的话则用红色表示) |
0719 | /// 特殊:"[DBNum1][$-804]G/通用格式"(能将数字转换成中文小写,如1234转换成一千二百三十四) "[DBNum2][$-804]G/通用格式"(能将数字转换成中文大写,如1234转换成 壹仟贰佰叁拾肆) |
0720 | /// 自定义:输入自定义的格式化字符串</param> |
0721 | public void SetAreaCellFormat( string startCell, string endCell, string formatString) |
0723 | Range sRange = GetRange(startCell, endCell); |
0725 | sRange.NumberFormatLocal = formatString; |
0726 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0731 | /// 设置某一列,某几列的列宽为自动适应大小,比如要设置第1列为自动适应大小SetColumnAutoFit("A","A") |
0733 | /// <param name="startColumn">开始的列</param> |
0734 | /// <param name="endColumn">结束的列</param> |
0737 | public void SetColumnAutoFit( string startColumn, string endColumn) |
0739 | Range sRange = (Range)exlWorkSheet.Columns[String.Format( "{0}:{1}" , startColumn, endColumn), Missing.Value]; |
0741 | sRange.EntireColumn.AutoFit(); |
0742 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0747 | #region " Get excel range method definition " |
0750 | /// 根据行列的定位,返回选定的单元格。因为Range 是通过Cell来定位的,而Cell需要2个参数定位,所以需要四个参数。 |
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) |
0759 | return exlWorkSheet.get_Range(exlApp.Cells[iStartRow, iStartCol], exlApp.Cells[iEndRow, iEndCol]); |
0765 | /// <param name="cell">指定的单元格</param> |
0766 | /// <returns>返回指定的单元格</returns> |
0767 | public Range GetRange( string cell) |
0769 | return exlWorkSheet.get_Range(cell, Missing.Value); |
0775 | /// <param name="startCell">开始的单元格坐标</param> |
0776 | /// <param name="endCell">结束的单元格坐标</param> |
0777 | /// <returns>返回指定的单元格范围</returns> |
0778 | public Range GetRange( string startCell, string endCell) |
0780 | return exlWorkSheet.get_Range(startCell, endCell); |
0786 | public void AddWorkSheet() |
0788 | if ( this .sheetNumber <= 3) |
0790 | exlApp.ActiveWorkbook.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); |
0791 | exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet; |
0792 | exlWorkSheet.Select(Missing.Value); |
0797 | exlApp.ActiveWorkbook.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); |
0798 | exlWorkSheet = (Worksheet)exlWorkBook.ActiveSheet; |
0799 | exlWorkSheet.Select(Missing.Value); |
0808 | #region " Excel range style method definition " |
0812 | /// <param name="cell">指定的单元格</param> |
0813 | /// <param name="cellAlignment">垂直方向的对齐方式</param> |
0814 | public void SetCellVerticalAlignment( string cell, ExcelVerticalAlignment cellAlignment) |
0816 | Range sRange = GetRange(cell); |
0818 | sRange.VerticalAlignment = cellAlignment; |
0819 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0824 | /// 设定指定范围的单元格的垂直对齐方式 |
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) |
0831 | Range sRange = GetRange(startCell, endCell); |
0833 | sRange.VerticalAlignment = cellAlignment; |
0834 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0840 | /// 设置指定范围的单元格的水平方向的对齐方式 |
0842 | /// <param name="cell">指定的单元格</param> |
0843 | /// <param name="cellAlignment">水平方向的对齐方式</param> |
0844 | public void SetCellHorizontalAlignment( string cell, ExcelHorizontalAlignment cellAlignment) |
0846 | Range sRange = GetRange(cell); |
0848 | sRange.HorizontalAlignment = cellAlignment; |
0849 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0854 | /// 设定指定范围的单元格的水平对齐方式 |
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) |
0861 | Range sRange = GetRange(startCell, endCell); |
0863 | sRange.HorizontalAlignment = cellAlignment; |
0864 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0869 | /// 设置指定单元格的边框,这里只能设置单个单元格的边框 |
0871 | /// <param name="cell">要设定的单元格</param> |
0872 | public void SetCellBorder( string cell) |
0874 | Range sRange = GetRange(cell); |
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; |
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; |
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; |
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; |
0895 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0900 | /// 设置指定范围的Excel单元格的边框,包括外边框,内边框 |
0902 | /// <param name="startCell">开始的单元格坐标</param> |
0903 | /// <param name="endCell">结束的单元格坐标</param> |
0904 | public void SetAreaBorder1( string startCell, String endCell) |
0906 | Range sRange = GetRange(startCell, endCell); |
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; |
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; |
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; |
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; |
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; |
0931 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
0935 | /// 设置指定范围的Excel单元格的边框,包括外边框,内边框 |
0937 | /// <param name="startCell">开始的单元格坐标</param> |
0938 | /// <param name="endCell">结束的单元格坐标</param> |
0939 | public void SetAreaBorder( string startCell, String endCell) |
0941 | Range sRange = GetRange(startCell, endCell); |
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; |
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; |
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; |
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; |
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; |
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; |
0970 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
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) |
0982 | Range sRange = GetRange(cell); |
0984 | sRange.Font.ColorIndex = colorIndex; |
0986 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
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) |
0999 | Range sRange = GetRange(startCell, endCell); |
1001 | sRange.Font.ColorIndex = colorIndex; |
1002 | System.Runtime.InteropServices.Marshal.ReleaseComObject(sRange); |
1009 | /// <param name="startCell">开始的单元格</param> |
1010 | /// <param name="endCell">结束的单元格</param> |
1012 | public void SetMergeCells( string startCell, string endCell) |
1014 | Range sRange = GetRange(startCell, endCell); |
1015 | sRange.MergeCells = true ; |
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> |
1024 | public void SetMergeCells( int iStartRow, int iStartCol, int iEndRow, int iEndCol) |
1026 | Range sRange = GetRange(iStartRow, iStartCol, iEndRow, iEndCol); |
1027 | sRange.MergeCells = true ; |
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> |
1037 | public void SetInteriorColor( string startCell, string endCell, int ColorIndex) |
1039 | Range sRange = GetRange(startCell, endCell); |
1040 | sRange.Interior.ColorIndex = ColorIndex; |
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> |
1052 | public void SetInteriorColor( int iStartRow, int iStartCol, int iEndRow, int iEndCol, int ColorIndex) |
1054 | Range sRange = GetRange(iStartRow, iStartCol, iEndRow, iEndCol); |
1055 | sRange.Interior.ColorIndex = ColorIndex; |