123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- using System;
- using System.Collections;
- using System.Text.RegularExpressions;
- using Excel;
- using Microsoft.Office.Core;
- using Microsoft.CSharp;
- namespace ExcelReaderWrite
- {
- public class ExcelFunction
- {
- private static ExcelFunction instance;
- private readonly Hashtable htSequenceList;
- #region private ExcelFunction() 私有构造函数,防止用户实例化
- /// <summary>
- /// 私有构造函数,防止用户实例化
- /// </summary>
- private ExcelFunction()
- {
- htSequenceList = SetData();
- }
- #endregion
- #region private static Hashtable SetData() 初始化Hashtable
- /// <summary>
- /// 初始化Hashtable
- /// </summary>
- /// <returns>Hashtable</returns>
- private static Hashtable SetData()
- {
- Hashtable htSequenceList = new Hashtable();
- char letter = 'A';
- int number = 1;
- for (int i = 0; i < 26; i++)
- {
- htSequenceList.Add(Convert.ToString((char)(letter + i)), number + i);
- }
- for (int i = 0; i < 26; i++)
- {
- char tempLetter = (char)(letter + i);
- int tempNumber = number + i;
- for (int j = 0; j < 26; j++)
- {
- htSequenceList.Add(Convert.ToString(tempLetter) + Convert.ToString((char)(letter + j)),
- tempNumber * 26 + j + 1);
- }
- }
- return htSequenceList;
- }
- #endregion
- #region private static bool IsNumber(string) 验证字符串是否为数字
- /// <summary>
- /// 验证字符串是否为数字
- /// </summary>
- /// <param name="strNumber">需要验证的字符串</param>
- /// <returns>返回true为数字,返回false不为数字</returns>
- private static bool IsNumber(string strNumber)
- {
- return Regex.IsMatch(strNumber, @"^[0-9]*$");
- }
- #endregion
- #region public static ExcelFunction GetExcelFunction() 获取ExcelHelp实例
- /// <summary>
- /// 获取ExcelHelp实例
- /// </summary>
- /// <returns>ExcelHelp实例</returns>
- public static ExcelFunction GetExcelFunction()
- {
- if (instance == null)
- {
- instance = new ExcelFunction();
- }
- return instance;
- }
- #endregion
- #region public int[] GetPosition(string) 将坐标从Excel格式转换成int[]格式
- /// <summary>
- /// 将坐标从Excel格式转换成int[]格式
- /// </summary>
- /// <param name="position">Excel坐标</param>
- /// <returns>int[]坐标</returns>
- public int[] GetPosition(string position)
- {
- int[] positionvalue = new int[2];
- int xValue;
- int yValue;
- string xPosition;
- string yPosition;
- string c = position.Substring(1, 1);
- if (IsNumber(c))
- {
- xPosition = position.Substring(0, 1);
- yPosition = position.Substring(1, position.Length - 1);
- yValue = int.Parse(yPosition);
- }
- else
- {
- xPosition = position.Substring(0, 2);
- yPosition = position.Substring(2, position.Length - 2);
- yValue = int.Parse(yPosition);
- }
- if (htSequenceList.ContainsKey(xPosition))
- {
- xValue = (int)htSequenceList[xPosition];
- }
- else
- {
- xValue = 0;
- }
- positionvalue.SetValue(xValue, 1);
- positionvalue.SetValue(yValue, 0);
- return positionvalue;
- }
- #endregion
- #region public string GetXByNum(int) 将横坐标的数值转换为字母
- /// <summary>
- /// 将横坐标的数值转换为字母
- /// </summary>
- /// <param name="iIndex">横坐标的数值</param>
- /// <returns>转换后的字母</returns>
- public string GetXByNum(int iIndex)
- {
- string strPosionX = string.Empty;
- if (htSequenceList.ContainsValue(iIndex))
- {
- foreach (DictionaryEntry myDE in htSequenceList)
- {
- if ((int)myDE.Value == iIndex)
- {
- strPosionX = (string)myDE.Key;
- break;
- }
- }
- }
- return strPosionX;
- }
- #endregion
- #region public void SetRowHeight(_Worksheet, int, int, int) 设置行高
- /// <summary>
- /// 设置行高
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="endRow">结束行</param>
- /// <param name="height">行高</param>
- public void SetRowHeight(_Worksheet objWorkSheet, int startRow, int endRow, int height)
- {
- Range range = (Range)objWorkSheet.Rows[startRow + ":" + endRow, Type.Missing];
- range.RowHeight = height;
- }
- #endregion
- #region public void RowAutoFit(_Worksheet, int) 自动调整行高
- /// <summary>
- /// 自动调整行高
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="rowNum">行号</param>
- public void RowAutoFit(_Worksheet objWorkSheet, int rowNum)
- {
- Range range = (Range)objWorkSheet.Rows[rowNum + ":" + rowNum, Type.Missing];
- range.EntireColumn.AutoFit();
- }
- #endregion
- #region public void SetColumnWidth(_Worksheet, string, string, int) 设置列宽
- /// <summary>
- /// 设置列宽
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startColumn">起始列(列对应的字母)</param>
- /// <param name="endColumn">结束列(列对应的字母)</param>
- /// <param name="width">列宽</param>
- public void SetColumnWidth(_Worksheet objWorkSheet, string startColumn, string endColumn, int width)
- {
- Range range = (Range)objWorkSheet.Columns[startColumn + ":" + endColumn, Type.Missing];
- range.ColumnWidth = width;
- }
- #endregion
- #region public void ColumnAutoFit(_Worksheet, string) 自动调整列宽
- /// <summary>
- /// 自动调整列宽
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="column">列号</param>
- public void ColumnAutoFit(_Worksheet objWorkSheet, string column)
- {
- Range range = (Range)objWorkSheet.Columns[column + ":" + column, Type.Missing];
- range.EntireColumn.AutoFit();
- }
- #endregion
- #region public void SetNumberFormat(_Worksheet, string, string) 设置单元格字体
- /// <summary>
- /// 设置单元格字体
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- public void SetNumberFormat(_Worksheet objWorkSheet, string strStartP, string strEndP)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.NumberFormat = "@";
- }
- #endregion
- #region public void SetValueOrientation(_Worksheet, string, string, string) 设置单元格列的显示方向
- /// <summary>
- /// 设置单元格列的显示方向
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="StrOrientation">显示方向</param>
- public void SetValueOrientation(_Worksheet objWorkSheet, string strStartP, string strEndP, string StrOrientation)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.Orientation = StrOrientation;
- }
- #endregion
- #region public void CellsAlignment(_Worksheet, int, int, int, int, ExcelEnum.ExcelHAlign, ExcelEnum.ExcelVAlign) 单元格文字对齐方式
- /// <summary>
- /// 单元格文字对齐方式
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- /// <param name="hAlign">水平对齐</param>
- /// <param name="vAlign">垂直对齐</param>
- public void CellsAlignment(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
- ExcelEnum.ExcelHAlign hAlign, ExcelEnum.ExcelVAlign vAlign)
- {
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- range.HorizontalAlignment = hAlign;
- range.VerticalAlignment = vAlign;
- }
- #endregion
- #region public void CellsHAlignment(_Worksheet, string, string, ExcelEnum.ExcelHAlign) 单元格水平对齐方式
- /// <summary>
- /// 单元格水平对齐方式
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="hAlign">水平方式</param>
- public void CellsHAlignment(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ExcelHAlign hAlign)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.HorizontalAlignment = hAlign;
- }
- #endregion
- #region public void CellsVAlignment(_Worksheet, string, string, ExcelEnum.ExcelVAlign) 单元格垂直对齐方式
- /// <summary>
- /// 单元格垂直对齐方式
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="vAlign">垂直方式</param>
- public void CellsVAlignment(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ExcelVAlign vAlign)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.VerticalAlignment = vAlign;
- }
- #endregion
- #region public void DrawEdge(_Worksheet, string, string, XlBordersIndex, string, ExcelEnum.ColorIndex, string) 画单元格边框
- /// <summary>
- /// 画单元格边框
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="objEdge">边框</param>
- /// <param name="borderWeigth">线宽</param>
- /// <param name="lineColor">线颜色</param>
- /// <param name="lineStyle">边框类型</param>
- public void DrawEdge(_Worksheet objWorkSheet, string strStartP, string strEndP,
- XlBordersIndex objEdge, string borderWeigth, ExcelEnum.ColorIndex lineColor, string lineStyle)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- if (!borderWeigth.Equals("xlNone"))
- {
- range.Borders[objEdge].Weight = (ExcelEnum.BorderWeight)
- Enum.Parse(typeof(ExcelEnum.BorderWeight),
- borderWeigth, true);
- }
- if (!lineStyle.Equals("xlNone"))
- {
- range.Borders[objEdge].LineStyle = (ExcelEnum.LineStyle)
- Enum.Parse(typeof(ExcelEnum.LineStyle),
- lineStyle, true);
- }
- range.Borders[objEdge].ColorIndex = lineColor;
- }
- #endregion
- #region public void InsertPictures(_Worksheet, string, string, string, float, float, float) 在指定区域内插入图片
- /// <summary>
- /// 在指定区域内插入图片
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="Filename">图片文件名</param>
- /// <param name="Width">图片宽度</param>
- /// <param name="Height">图片高度</param>
- /// <param name="JudgeValue">调整值</param>
- public void InsertPictures(_Worksheet objWorkSheet, string Filename, string strStartP,
- string strEndP, float Width, float Height, float JudgeValue)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- objWorkSheet.Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue,
- Convert.ToSingle(range.Left) + JudgeValue,
- Convert.ToSingle(range.Top) + JudgeValue, Width, Height);
- }
- #endregion
- #region public void CellsBackColor 单元格背景色及填充方式
- /// <summary>
- /// 单元格背景色及填充方式
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- /// <param name="color">颜色索引</param>
- public void CellsBackColor(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn, ExcelEnum.ColorIndex color)
- {
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- range.Interior.ColorIndex = color;
- range.Interior.Pattern = ExcelEnum.Pattern.Solid;
- }
- /// <summary>
- /// 单元格背景色及填充方式
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="color">颜色索引</param>
- public void CellsBackColor(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ColorIndex color)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.Interior.ColorIndex = color;
- range.Interior.Pattern = ExcelEnum.Pattern.Solid;
- }
- /// <summary>
- /// 单元格背景色及填充方式
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- /// <param name="color">颜色索引</param>
- /// <param name="pattern">填充方式</param>
- public void CellsBackColor(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
- ExcelEnum.ColorIndex color, ExcelEnum.Pattern pattern)
- {
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- range.Interior.ColorIndex = color;
- range.Interior.Pattern = pattern;
- }
- #endregion
- #region public void FontColor 字体颜色
- /// <summary>
- /// 字体颜色
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- /// <param name="color">颜色索引</param>
- public void FontColor(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn, ExcelEnum.ColorIndex color)
- {
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- range.Font.ColorIndex = color;
- }
- /// <summary>
- /// 字体颜色
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="color">颜色索引</param>
- public void FontColor(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ColorIndex color)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.Font.ColorIndex = color;
- }
- #endregion
- #region public void FontStyle 字体样式(粗体,斜体,下划线)
- /// <summary>
- /// 字体样式(粗体,斜体,下划线)
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- /// <param name="isBold">是否加粗</param>
- /// <param name="isItalic">是否斜体</param>
- /// <param name="underline">下划线类型</param>
- public void FontStyle(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn, bool isBold, bool isItalic,
- ExcelEnum.UnderlineStyle underline)
- {
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- range.Font.Bold = isBold;
- range.Font.Underline = underline;
- range.Font.Italic = isItalic;
- }
- /// <summary>
- /// 字体样式(粗体,斜体,下划线)
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="isBold">是否加粗</param>
- /// <param name="isItalic">是否斜体</param>
- /// <param name="underline">下划线类型</param>
- public void FontStyle(_Worksheet objWorkSheet, string strStartP, string strEndP, bool isBold, bool isItalic,
- ExcelEnum.UnderlineStyle underline)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.Font.Bold = isBold;
- range.Font.Underline = underline;
- range.Font.Italic = isItalic;
- }
- /// <summary>
- /// 字体样式(粗体,斜体,下划线)
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="isBold">是否加粗</param>
- /// <param name="isItalic">是否斜体</param>
- public void FontStyle(_Worksheet objWorkSheet, string strStartP, string strEndP, bool isBold, bool isItalic)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.Font.Bold = isBold;
- range.Font.Italic = isItalic;
- }
- /// <summary>
- /// 字体样式(粗体,斜体,下划线)
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="underline">下划线类型</param>
- public void FontStyle(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.UnderlineStyle underline)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.Font.Underline = underline;
- }
- #endregion
- #region public void FontNameSize 单元格字体及大小
- /// <summary>
- /// 单元格字体及大小
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- /// <param name="fontName">字体名称</param>
- /// <param name="fontSize">字体大小</param>
- /// <param name="strValue"></param>
- public void FontNameSize(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
- string fontName, string fontSize, string strValue)
- {
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- range.Font.Name = fontName;
- range.Font.Size = fontSize;
- range.Value2 = strValue;
- }
- /// <summary>
- /// 单元格字体及大小
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- /// <param name="fontName">字体名称</param>
- /// <param name="fontSize">字体大小</param>
- /// <param name="strValue"></param>
- public void FontNameSize(_Worksheet objWorkSheet, string strStartP, string strEndP, string fontName, string fontSize, string strValue)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- if (!string.IsNullOrEmpty(fontName))
- {
- range.Font.Name = fontName;
- }
- if (!string.IsNullOrEmpty(fontSize))
- {
- range.Font.Size = fontSize;
- }
- range.Value2 = strValue;
- }
- #endregion
- #region public void CellsUnite 合并单元格
- /// <summary>
- /// 合并单元格
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- public void CellsUnite(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn)
- {
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- range.MergeCells = true;
- }
- /// <summary>
- /// 合并单元格
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="strStartP">起始位置</param>
- /// <param name="strEndP">结束位置</param>
- public void CellsUnite(_Worksheet objWorkSheet, string strStartP, string strEndP)
- {
- Range range = objWorkSheet.get_Range(strStartP, strEndP);
- range.MergeCells = true;
- }
- #endregion
- #region public void CellsDrawFrame 绘制指定单元格的边框
- /// <summary>
- /// 绘制指定单元格的边框
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- public void CellsDrawFrame(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn)
- {
- CellsDrawFrame(objWorkSheet, startRow, startColumn, endRow, endColumn,
- true, true, true, true, true, true, false, false,
- ExcelEnum.LineStyle.xlContinuous, ExcelEnum.BorderWeight.xlThin, ExcelEnum.ColorIndex.Auto);
- }
- /// <summary>
- /// 绘制指定单元格的边框
- /// </summary>
- /// <param name="objWorkSheet">工作区</param>
- /// <param name="startRow">起始行</param>
- /// <param name="startColumn">起始列</param>
- /// <param name="endRow">结束行</param>
- /// <param name="endColumn">结束列</param>
- /// <param name="isDrawTop">是否画上外框</param>
- /// <param name="isDrawBottom">是否画下外框</param>
- /// <param name="isDrawLeft">是否画左外框</param>
- /// <param name="isDrawRight">是否画右外框</param>
- /// <param name="isDrawHInside">是否画水平内框</param>
- /// <param name="isDrawVInside">是否画垂直内框</param>
- /// <param name="isDrawDiagonalDown">是否画斜向下线</param>
- /// <param name="isDrawDiagonalUp">是否画斜向上线</param>
- /// <param name="lineStyle">线类型</param>
- /// <param name="borderWeight">线粗细</param>
- /// <param name="color">线颜色</param>
- public void CellsDrawFrame(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
- bool isDrawTop, bool isDrawBottom, bool isDrawLeft, bool isDrawRight,
- bool isDrawHInside, bool isDrawVInside, bool isDrawDiagonalDown, bool isDrawDiagonalUp,
- ExcelEnum.LineStyle lineStyle, ExcelEnum.BorderWeight borderWeight, ExcelEnum.ColorIndex color)
- {
- //获取画边框的单元格
- Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
- //清除所有边框
- range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = ExcelEnum.LineStyle.xlNone;
- range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = ExcelEnum.LineStyle.xlNone;
- range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = ExcelEnum.LineStyle.xlNone;
- range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = ExcelEnum.LineStyle.xlNone;
- range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = ExcelEnum.LineStyle.xlNone;
- range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = ExcelEnum.LineStyle.xlNone;
- range.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = ExcelEnum.LineStyle.xlNone;
- range.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = ExcelEnum.LineStyle.xlNone;
- #region 按参数画边框
- if (isDrawTop)
- {
- range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlEdgeTop].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlEdgeTop].ColorIndex = color;
- }
- if (isDrawBottom)
- {
- range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlEdgeBottom].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlEdgeBottom].ColorIndex = color;
- }
- if (isDrawLeft)
- {
- range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlEdgeLeft].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlEdgeLeft].ColorIndex = color;
- }
- if (isDrawRight)
- {
- range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlEdgeRight].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlEdgeRight].ColorIndex = color;
- }
- if (isDrawVInside)
- {
- range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlInsideVertical].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlInsideVertical].ColorIndex = color;
- }
- if (isDrawHInside)
- {
- range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlInsideHorizontal].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlInsideHorizontal].ColorIndex = color;
- }
- if (isDrawDiagonalDown)
- {
- range.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlDiagonalDown].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlDiagonalDown].ColorIndex = color;
- }
- if (isDrawDiagonalUp)
- {
- range.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = lineStyle;
- range.Borders[XlBordersIndex.xlDiagonalUp].Weight = borderWeight;
- range.Borders[XlBordersIndex.xlDiagonalUp].ColorIndex = color;
- }
- #endregion
- }
- #endregion
- }
- }
|