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() 私有构造函数,防止用户实例化 /// /// 私有构造函数,防止用户实例化 /// private ExcelFunction() { htSequenceList = SetData(); } #endregion #region private static Hashtable SetData() 初始化Hashtable /// /// 初始化Hashtable /// /// Hashtable 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) 验证字符串是否为数字 /// /// 验证字符串是否为数字 /// /// 需要验证的字符串 /// 返回true为数字,返回false不为数字 private static bool IsNumber(string strNumber) { return Regex.IsMatch(strNumber, @"^[0-9]*$"); } #endregion #region public static ExcelFunction GetExcelFunction() 获取ExcelHelp实例 /// /// 获取ExcelHelp实例 /// /// ExcelHelp实例 public static ExcelFunction GetExcelFunction() { if (instance == null) { instance = new ExcelFunction(); } return instance; } #endregion #region public int[] GetPosition(string) 将坐标从Excel格式转换成int[]格式 /// /// 将坐标从Excel格式转换成int[]格式 /// /// Excel坐标 /// int[]坐标 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) 将横坐标的数值转换为字母 /// /// 将横坐标的数值转换为字母 /// /// 横坐标的数值 /// 转换后的字母 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) 设置行高 /// /// 设置行高 /// /// 工作区 /// 起始行 /// 结束行 /// 行高 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) 自动调整行高 /// /// 自动调整行高 /// /// 工作区 /// 行号 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) 设置列宽 /// /// 设置列宽 /// /// 工作区 /// 起始列(列对应的字母) /// 结束列(列对应的字母) /// 列宽 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) 自动调整列宽 /// /// 自动调整列宽 /// /// 工作区 /// 列号 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) 设置单元格字体 /// /// 设置单元格字体 /// /// 工作区 /// 起始位置 /// 结束位置 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) 设置单元格列的显示方向 /// /// 设置单元格列的显示方向 /// /// 工作区 /// 起始位置 /// 结束位置 /// 显示方向 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) 单元格文字对齐方式 /// /// 单元格文字对齐方式 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 /// 水平对齐 /// 垂直对齐 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) 单元格水平对齐方式 /// /// 单元格水平对齐方式 /// /// 工作区 /// 起始位置 /// 结束位置 /// 水平方式 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) 单元格垂直对齐方式 /// /// 单元格垂直对齐方式 /// /// 工作区 /// 起始位置 /// 结束位置 /// 垂直方式 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) 画单元格边框 /// /// 画单元格边框 /// /// 工作区 /// 起始位置 /// 结束位置 /// 边框 /// 线宽 /// 线颜色 /// 边框类型 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) 在指定区域内插入图片 /// /// 在指定区域内插入图片 /// /// 工作区 /// 起始位置 /// 结束位置 /// 图片文件名 /// 图片宽度 /// 图片高度 /// 调整值 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 单元格背景色及填充方式 /// /// 单元格背景色及填充方式 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 /// 颜色索引 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; } /// /// 单元格背景色及填充方式 /// /// 工作区 /// 起始位置 /// 结束位置 /// 颜色索引 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; } /// /// 单元格背景色及填充方式 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 /// 颜色索引 /// 填充方式 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 字体颜色 /// /// 字体颜色 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 /// 颜色索引 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; } /// /// 字体颜色 /// /// 工作区 /// 起始位置 /// 结束位置 /// 颜色索引 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 字体样式(粗体,斜体,下划线) /// /// 字体样式(粗体,斜体,下划线) /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 /// 是否加粗 /// 是否斜体 /// 下划线类型 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; } /// /// 字体样式(粗体,斜体,下划线) /// /// 工作区 /// 起始位置 /// 结束位置 /// 是否加粗 /// 是否斜体 /// 下划线类型 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; } /// /// 字体样式(粗体,斜体,下划线) /// /// 工作区 /// 起始位置 /// 结束位置 /// 是否加粗 /// 是否斜体 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; } /// /// 字体样式(粗体,斜体,下划线) /// /// 工作区 /// 起始位置 /// 结束位置 /// 下划线类型 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 单元格字体及大小 /// /// 单元格字体及大小 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 /// 字体名称 /// 字体大小 /// 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; } /// /// 单元格字体及大小 /// /// 工作区 /// 起始位置 /// 结束位置 /// 字体名称 /// 字体大小 /// 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 合并单元格 /// /// 合并单元格 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 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; } /// /// 合并单元格 /// /// 工作区 /// 起始位置 /// 结束位置 public void CellsUnite(_Worksheet objWorkSheet, string strStartP, string strEndP) { Range range = objWorkSheet.get_Range(strStartP, strEndP); range.MergeCells = true; } #endregion #region public void CellsDrawFrame 绘制指定单元格的边框 /// /// 绘制指定单元格的边框 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 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); } /// /// 绘制指定单元格的边框 /// /// 工作区 /// 起始行 /// 起始列 /// 结束行 /// 结束列 /// 是否画上外框 /// 是否画下外框 /// 是否画左外框 /// 是否画右外框 /// 是否画水平内框 /// 是否画垂直内框 /// 是否画斜向下线 /// 是否画斜向上线 /// 线类型 /// 线粗细 /// 线颜色 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 } }