ExcelFunction.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. using System;
  2. using System.Collections;
  3. using System.Text.RegularExpressions;
  4. using Excel;
  5. using Microsoft.Office.Core;
  6. using Microsoft.CSharp;
  7. namespace ExcelReaderWrite
  8. {
  9. public class ExcelFunction
  10. {
  11. private static ExcelFunction instance;
  12. private readonly Hashtable htSequenceList;
  13. #region private ExcelFunction() 私有构造函数,防止用户实例化
  14. /// <summary>
  15. /// 私有构造函数,防止用户实例化
  16. /// </summary>
  17. private ExcelFunction()
  18. {
  19. htSequenceList = SetData();
  20. }
  21. #endregion
  22. #region private static Hashtable SetData() 初始化Hashtable
  23. /// <summary>
  24. /// 初始化Hashtable
  25. /// </summary>
  26. /// <returns>Hashtable</returns>
  27. private static Hashtable SetData()
  28. {
  29. Hashtable htSequenceList = new Hashtable();
  30. char letter = 'A';
  31. int number = 1;
  32. for (int i = 0; i < 26; i++)
  33. {
  34. htSequenceList.Add(Convert.ToString((char)(letter + i)), number + i);
  35. }
  36. for (int i = 0; i < 26; i++)
  37. {
  38. char tempLetter = (char)(letter + i);
  39. int tempNumber = number + i;
  40. for (int j = 0; j < 26; j++)
  41. {
  42. htSequenceList.Add(Convert.ToString(tempLetter) + Convert.ToString((char)(letter + j)),
  43. tempNumber * 26 + j + 1);
  44. }
  45. }
  46. return htSequenceList;
  47. }
  48. #endregion
  49. #region private static bool IsNumber(string) 验证字符串是否为数字
  50. /// <summary>
  51. /// 验证字符串是否为数字
  52. /// </summary>
  53. /// <param name="strNumber">需要验证的字符串</param>
  54. /// <returns>返回true为数字,返回false不为数字</returns>
  55. private static bool IsNumber(string strNumber)
  56. {
  57. return Regex.IsMatch(strNumber, @"^[0-9]*$");
  58. }
  59. #endregion
  60. #region public static ExcelFunction GetExcelFunction() 获取ExcelHelp实例
  61. /// <summary>
  62. /// 获取ExcelHelp实例
  63. /// </summary>
  64. /// <returns>ExcelHelp实例</returns>
  65. public static ExcelFunction GetExcelFunction()
  66. {
  67. if (instance == null)
  68. {
  69. instance = new ExcelFunction();
  70. }
  71. return instance;
  72. }
  73. #endregion
  74. #region public int[] GetPosition(string) 将坐标从Excel格式转换成int[]格式
  75. /// <summary>
  76. /// 将坐标从Excel格式转换成int[]格式
  77. /// </summary>
  78. /// <param name="position">Excel坐标</param>
  79. /// <returns>int[]坐标</returns>
  80. public int[] GetPosition(string position)
  81. {
  82. int[] positionvalue = new int[2];
  83. int xValue;
  84. int yValue;
  85. string xPosition;
  86. string yPosition;
  87. string c = position.Substring(1, 1);
  88. if (IsNumber(c))
  89. {
  90. xPosition = position.Substring(0, 1);
  91. yPosition = position.Substring(1, position.Length - 1);
  92. yValue = int.Parse(yPosition);
  93. }
  94. else
  95. {
  96. xPosition = position.Substring(0, 2);
  97. yPosition = position.Substring(2, position.Length - 2);
  98. yValue = int.Parse(yPosition);
  99. }
  100. if (htSequenceList.ContainsKey(xPosition))
  101. {
  102. xValue = (int)htSequenceList[xPosition];
  103. }
  104. else
  105. {
  106. xValue = 0;
  107. }
  108. positionvalue.SetValue(xValue, 1);
  109. positionvalue.SetValue(yValue, 0);
  110. return positionvalue;
  111. }
  112. #endregion
  113. #region public string GetXByNum(int) 将横坐标的数值转换为字母
  114. /// <summary>
  115. /// 将横坐标的数值转换为字母
  116. /// </summary>
  117. /// <param name="iIndex">横坐标的数值</param>
  118. /// <returns>转换后的字母</returns>
  119. public string GetXByNum(int iIndex)
  120. {
  121. string strPosionX = string.Empty;
  122. if (htSequenceList.ContainsValue(iIndex))
  123. {
  124. foreach (DictionaryEntry myDE in htSequenceList)
  125. {
  126. if ((int)myDE.Value == iIndex)
  127. {
  128. strPosionX = (string)myDE.Key;
  129. break;
  130. }
  131. }
  132. }
  133. return strPosionX;
  134. }
  135. #endregion
  136. #region public void SetRowHeight(_Worksheet, int, int, int) 设置行高
  137. /// <summary>
  138. /// 设置行高
  139. /// </summary>
  140. /// <param name="objWorkSheet">工作区</param>
  141. /// <param name="startRow">起始行</param>
  142. /// <param name="endRow">结束行</param>
  143. /// <param name="height">行高</param>
  144. public void SetRowHeight(_Worksheet objWorkSheet, int startRow, int endRow, int height)
  145. {
  146. Range range = (Range)objWorkSheet.Rows[startRow + ":" + endRow, Type.Missing];
  147. range.RowHeight = height;
  148. }
  149. #endregion
  150. #region public void RowAutoFit(_Worksheet, int) 自动调整行高
  151. /// <summary>
  152. /// 自动调整行高
  153. /// </summary>
  154. /// <param name="objWorkSheet">工作区</param>
  155. /// <param name="rowNum">行号</param>
  156. public void RowAutoFit(_Worksheet objWorkSheet, int rowNum)
  157. {
  158. Range range = (Range)objWorkSheet.Rows[rowNum + ":" + rowNum, Type.Missing];
  159. range.EntireColumn.AutoFit();
  160. }
  161. #endregion
  162. #region public void SetColumnWidth(_Worksheet, string, string, int) 设置列宽
  163. /// <summary>
  164. /// 设置列宽
  165. /// </summary>
  166. /// <param name="objWorkSheet">工作区</param>
  167. /// <param name="startColumn">起始列(列对应的字母)</param>
  168. /// <param name="endColumn">结束列(列对应的字母)</param>
  169. /// <param name="width">列宽</param>
  170. public void SetColumnWidth(_Worksheet objWorkSheet, string startColumn, string endColumn, int width)
  171. {
  172. Range range = (Range)objWorkSheet.Columns[startColumn + ":" + endColumn, Type.Missing];
  173. range.ColumnWidth = width;
  174. }
  175. #endregion
  176. #region public void ColumnAutoFit(_Worksheet, string) 自动调整列宽
  177. /// <summary>
  178. /// 自动调整列宽
  179. /// </summary>
  180. /// <param name="objWorkSheet">工作区</param>
  181. /// <param name="column">列号</param>
  182. public void ColumnAutoFit(_Worksheet objWorkSheet, string column)
  183. {
  184. Range range = (Range)objWorkSheet.Columns[column + ":" + column, Type.Missing];
  185. range.EntireColumn.AutoFit();
  186. }
  187. #endregion
  188. #region public void SetNumberFormat(_Worksheet, string, string) 设置单元格字体
  189. /// <summary>
  190. /// 设置单元格字体
  191. /// </summary>
  192. /// <param name="objWorkSheet">工作区</param>
  193. /// <param name="strStartP">起始位置</param>
  194. /// <param name="strEndP">结束位置</param>
  195. public void SetNumberFormat(_Worksheet objWorkSheet, string strStartP, string strEndP)
  196. {
  197. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  198. range.NumberFormat = "@";
  199. }
  200. #endregion
  201. #region public void SetValueOrientation(_Worksheet, string, string, string) 设置单元格列的显示方向
  202. /// <summary>
  203. /// 设置单元格列的显示方向
  204. /// </summary>
  205. /// <param name="objWorkSheet">工作区</param>
  206. /// <param name="strStartP">起始位置</param>
  207. /// <param name="strEndP">结束位置</param>
  208. /// <param name="StrOrientation">显示方向</param>
  209. public void SetValueOrientation(_Worksheet objWorkSheet, string strStartP, string strEndP, string StrOrientation)
  210. {
  211. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  212. range.Orientation = StrOrientation;
  213. }
  214. #endregion
  215. #region public void CellsAlignment(_Worksheet, int, int, int, int, ExcelEnum.ExcelHAlign, ExcelEnum.ExcelVAlign) 单元格文字对齐方式
  216. /// <summary>
  217. /// 单元格文字对齐方式
  218. /// </summary>
  219. /// <param name="objWorkSheet">工作区</param>
  220. /// <param name="startRow">起始行</param>
  221. /// <param name="startColumn">起始列</param>
  222. /// <param name="endRow">结束行</param>
  223. /// <param name="endColumn">结束列</param>
  224. /// <param name="hAlign">水平对齐</param>
  225. /// <param name="vAlign">垂直对齐</param>
  226. public void CellsAlignment(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
  227. ExcelEnum.ExcelHAlign hAlign, ExcelEnum.ExcelVAlign vAlign)
  228. {
  229. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  230. range.HorizontalAlignment = hAlign;
  231. range.VerticalAlignment = vAlign;
  232. }
  233. #endregion
  234. #region public void CellsHAlignment(_Worksheet, string, string, ExcelEnum.ExcelHAlign) 单元格水平对齐方式
  235. /// <summary>
  236. /// 单元格水平对齐方式
  237. /// </summary>
  238. /// <param name="objWorkSheet">工作区</param>
  239. /// <param name="strStartP">起始位置</param>
  240. /// <param name="strEndP">结束位置</param>
  241. /// <param name="hAlign">水平方式</param>
  242. public void CellsHAlignment(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ExcelHAlign hAlign)
  243. {
  244. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  245. range.HorizontalAlignment = hAlign;
  246. }
  247. #endregion
  248. #region public void CellsVAlignment(_Worksheet, string, string, ExcelEnum.ExcelVAlign) 单元格垂直对齐方式
  249. /// <summary>
  250. /// 单元格垂直对齐方式
  251. /// </summary>
  252. /// <param name="objWorkSheet">工作区</param>
  253. /// <param name="strStartP">起始位置</param>
  254. /// <param name="strEndP">结束位置</param>
  255. /// <param name="vAlign">垂直方式</param>
  256. public void CellsVAlignment(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ExcelVAlign vAlign)
  257. {
  258. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  259. range.VerticalAlignment = vAlign;
  260. }
  261. #endregion
  262. #region public void DrawEdge(_Worksheet, string, string, XlBordersIndex, string, ExcelEnum.ColorIndex, string) 画单元格边框
  263. /// <summary>
  264. /// 画单元格边框
  265. /// </summary>
  266. /// <param name="objWorkSheet">工作区</param>
  267. /// <param name="strStartP">起始位置</param>
  268. /// <param name="strEndP">结束位置</param>
  269. /// <param name="objEdge">边框</param>
  270. /// <param name="borderWeigth">线宽</param>
  271. /// <param name="lineColor">线颜色</param>
  272. /// <param name="lineStyle">边框类型</param>
  273. public void DrawEdge(_Worksheet objWorkSheet, string strStartP, string strEndP,
  274. XlBordersIndex objEdge, string borderWeigth, ExcelEnum.ColorIndex lineColor, string lineStyle)
  275. {
  276. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  277. if (!borderWeigth.Equals("xlNone"))
  278. {
  279. range.Borders[objEdge].Weight = (ExcelEnum.BorderWeight)
  280. Enum.Parse(typeof(ExcelEnum.BorderWeight),
  281. borderWeigth, true);
  282. }
  283. if (!lineStyle.Equals("xlNone"))
  284. {
  285. range.Borders[objEdge].LineStyle = (ExcelEnum.LineStyle)
  286. Enum.Parse(typeof(ExcelEnum.LineStyle),
  287. lineStyle, true);
  288. }
  289. range.Borders[objEdge].ColorIndex = lineColor;
  290. }
  291. #endregion
  292. #region public void InsertPictures(_Worksheet, string, string, string, float, float, float) 在指定区域内插入图片
  293. /// <summary>
  294. /// 在指定区域内插入图片
  295. /// </summary>
  296. /// <param name="objWorkSheet">工作区</param>
  297. /// <param name="strStartP">起始位置</param>
  298. /// <param name="strEndP">结束位置</param>
  299. /// <param name="Filename">图片文件名</param>
  300. /// <param name="Width">图片宽度</param>
  301. /// <param name="Height">图片高度</param>
  302. /// <param name="JudgeValue">调整值</param>
  303. public void InsertPictures(_Worksheet objWorkSheet, string Filename, string strStartP,
  304. string strEndP, float Width, float Height, float JudgeValue)
  305. {
  306. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  307. objWorkSheet.Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue,
  308. Convert.ToSingle(range.Left) + JudgeValue,
  309. Convert.ToSingle(range.Top) + JudgeValue, Width, Height);
  310. }
  311. #endregion
  312. #region public void CellsBackColor 单元格背景色及填充方式
  313. /// <summary>
  314. /// 单元格背景色及填充方式
  315. /// </summary>
  316. /// <param name="objWorkSheet">工作区</param>
  317. /// <param name="startRow">起始行</param>
  318. /// <param name="startColumn">起始列</param>
  319. /// <param name="endRow">结束行</param>
  320. /// <param name="endColumn">结束列</param>
  321. /// <param name="color">颜色索引</param>
  322. public void CellsBackColor(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn, ExcelEnum.ColorIndex color)
  323. {
  324. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  325. range.Interior.ColorIndex = color;
  326. range.Interior.Pattern = ExcelEnum.Pattern.Solid;
  327. }
  328. /// <summary>
  329. /// 单元格背景色及填充方式
  330. /// </summary>
  331. /// <param name="objWorkSheet">工作区</param>
  332. /// <param name="strStartP">起始位置</param>
  333. /// <param name="strEndP">结束位置</param>
  334. /// <param name="color">颜色索引</param>
  335. public void CellsBackColor(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ColorIndex color)
  336. {
  337. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  338. range.Interior.ColorIndex = color;
  339. range.Interior.Pattern = ExcelEnum.Pattern.Solid;
  340. }
  341. /// <summary>
  342. /// 单元格背景色及填充方式
  343. /// </summary>
  344. /// <param name="objWorkSheet">工作区</param>
  345. /// <param name="startRow">起始行</param>
  346. /// <param name="startColumn">起始列</param>
  347. /// <param name="endRow">结束行</param>
  348. /// <param name="endColumn">结束列</param>
  349. /// <param name="color">颜色索引</param>
  350. /// <param name="pattern">填充方式</param>
  351. public void CellsBackColor(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
  352. ExcelEnum.ColorIndex color, ExcelEnum.Pattern pattern)
  353. {
  354. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  355. range.Interior.ColorIndex = color;
  356. range.Interior.Pattern = pattern;
  357. }
  358. #endregion
  359. #region public void FontColor 字体颜色
  360. /// <summary>
  361. /// 字体颜色
  362. /// </summary>
  363. /// <param name="objWorkSheet">工作区</param>
  364. /// <param name="startRow">起始行</param>
  365. /// <param name="startColumn">起始列</param>
  366. /// <param name="endRow">结束行</param>
  367. /// <param name="endColumn">结束列</param>
  368. /// <param name="color">颜色索引</param>
  369. public void FontColor(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn, ExcelEnum.ColorIndex color)
  370. {
  371. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  372. range.Font.ColorIndex = color;
  373. }
  374. /// <summary>
  375. /// 字体颜色
  376. /// </summary>
  377. /// <param name="objWorkSheet">工作区</param>
  378. /// <param name="strStartP">起始位置</param>
  379. /// <param name="strEndP">结束位置</param>
  380. /// <param name="color">颜色索引</param>
  381. public void FontColor(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.ColorIndex color)
  382. {
  383. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  384. range.Font.ColorIndex = color;
  385. }
  386. #endregion
  387. #region public void FontStyle 字体样式(粗体,斜体,下划线)
  388. /// <summary>
  389. /// 字体样式(粗体,斜体,下划线)
  390. /// </summary>
  391. /// <param name="objWorkSheet">工作区</param>
  392. /// <param name="startRow">起始行</param>
  393. /// <param name="startColumn">起始列</param>
  394. /// <param name="endRow">结束行</param>
  395. /// <param name="endColumn">结束列</param>
  396. /// <param name="isBold">是否加粗</param>
  397. /// <param name="isItalic">是否斜体</param>
  398. /// <param name="underline">下划线类型</param>
  399. public void FontStyle(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn, bool isBold, bool isItalic,
  400. ExcelEnum.UnderlineStyle underline)
  401. {
  402. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  403. range.Font.Bold = isBold;
  404. range.Font.Underline = underline;
  405. range.Font.Italic = isItalic;
  406. }
  407. /// <summary>
  408. /// 字体样式(粗体,斜体,下划线)
  409. /// </summary>
  410. /// <param name="objWorkSheet">工作区</param>
  411. /// <param name="strStartP">起始位置</param>
  412. /// <param name="strEndP">结束位置</param>
  413. /// <param name="isBold">是否加粗</param>
  414. /// <param name="isItalic">是否斜体</param>
  415. /// <param name="underline">下划线类型</param>
  416. public void FontStyle(_Worksheet objWorkSheet, string strStartP, string strEndP, bool isBold, bool isItalic,
  417. ExcelEnum.UnderlineStyle underline)
  418. {
  419. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  420. range.Font.Bold = isBold;
  421. range.Font.Underline = underline;
  422. range.Font.Italic = isItalic;
  423. }
  424. /// <summary>
  425. /// 字体样式(粗体,斜体,下划线)
  426. /// </summary>
  427. /// <param name="objWorkSheet">工作区</param>
  428. /// <param name="strStartP">起始位置</param>
  429. /// <param name="strEndP">结束位置</param>
  430. /// <param name="isBold">是否加粗</param>
  431. /// <param name="isItalic">是否斜体</param>
  432. public void FontStyle(_Worksheet objWorkSheet, string strStartP, string strEndP, bool isBold, bool isItalic)
  433. {
  434. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  435. range.Font.Bold = isBold;
  436. range.Font.Italic = isItalic;
  437. }
  438. /// <summary>
  439. /// 字体样式(粗体,斜体,下划线)
  440. /// </summary>
  441. /// <param name="objWorkSheet">工作区</param>
  442. /// <param name="strStartP">起始位置</param>
  443. /// <param name="strEndP">结束位置</param>
  444. /// <param name="underline">下划线类型</param>
  445. public void FontStyle(_Worksheet objWorkSheet, string strStartP, string strEndP, ExcelEnum.UnderlineStyle underline)
  446. {
  447. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  448. range.Font.Underline = underline;
  449. }
  450. #endregion
  451. #region public void FontNameSize 单元格字体及大小
  452. /// <summary>
  453. /// 单元格字体及大小
  454. /// </summary>
  455. /// <param name="objWorkSheet">工作区</param>
  456. /// <param name="startRow">起始行</param>
  457. /// <param name="startColumn">起始列</param>
  458. /// <param name="endRow">结束行</param>
  459. /// <param name="endColumn">结束列</param>
  460. /// <param name="fontName">字体名称</param>
  461. /// <param name="fontSize">字体大小</param>
  462. /// <param name="strValue"></param>
  463. public void FontNameSize(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
  464. string fontName, string fontSize, string strValue)
  465. {
  466. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  467. range.Font.Name = fontName;
  468. range.Font.Size = fontSize;
  469. range.Value2 = strValue;
  470. }
  471. /// <summary>
  472. /// 单元格字体及大小
  473. /// </summary>
  474. /// <param name="objWorkSheet">工作区</param>
  475. /// <param name="strStartP">起始位置</param>
  476. /// <param name="strEndP">结束位置</param>
  477. /// <param name="fontName">字体名称</param>
  478. /// <param name="fontSize">字体大小</param>
  479. /// <param name="strValue"></param>
  480. public void FontNameSize(_Worksheet objWorkSheet, string strStartP, string strEndP, string fontName, string fontSize, string strValue)
  481. {
  482. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  483. if (!string.IsNullOrEmpty(fontName))
  484. {
  485. range.Font.Name = fontName;
  486. }
  487. if (!string.IsNullOrEmpty(fontSize))
  488. {
  489. range.Font.Size = fontSize;
  490. }
  491. range.Value2 = strValue;
  492. }
  493. #endregion
  494. #region public void CellsUnite 合并单元格
  495. /// <summary>
  496. /// 合并单元格
  497. /// </summary>
  498. /// <param name="objWorkSheet">工作区</param>
  499. /// <param name="startRow">起始行</param>
  500. /// <param name="startColumn">起始列</param>
  501. /// <param name="endRow">结束行</param>
  502. /// <param name="endColumn">结束列</param>
  503. public void CellsUnite(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn)
  504. {
  505. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  506. range.MergeCells = true;
  507. }
  508. /// <summary>
  509. /// 合并单元格
  510. /// </summary>
  511. /// <param name="objWorkSheet">工作区</param>
  512. /// <param name="strStartP">起始位置</param>
  513. /// <param name="strEndP">结束位置</param>
  514. public void CellsUnite(_Worksheet objWorkSheet, string strStartP, string strEndP)
  515. {
  516. Range range = objWorkSheet.get_Range(strStartP, strEndP);
  517. range.MergeCells = true;
  518. }
  519. #endregion
  520. #region public void CellsDrawFrame 绘制指定单元格的边框
  521. /// <summary>
  522. /// 绘制指定单元格的边框
  523. /// </summary>
  524. /// <param name="objWorkSheet">工作区</param>
  525. /// <param name="startRow">起始行</param>
  526. /// <param name="startColumn">起始列</param>
  527. /// <param name="endRow">结束行</param>
  528. /// <param name="endColumn">结束列</param>
  529. public void CellsDrawFrame(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn)
  530. {
  531. CellsDrawFrame(objWorkSheet, startRow, startColumn, endRow, endColumn,
  532. true, true, true, true, true, true, false, false,
  533. ExcelEnum.LineStyle.xlContinuous, ExcelEnum.BorderWeight.xlThin, ExcelEnum.ColorIndex.Auto);
  534. }
  535. /// <summary>
  536. /// 绘制指定单元格的边框
  537. /// </summary>
  538. /// <param name="objWorkSheet">工作区</param>
  539. /// <param name="startRow">起始行</param>
  540. /// <param name="startColumn">起始列</param>
  541. /// <param name="endRow">结束行</param>
  542. /// <param name="endColumn">结束列</param>
  543. /// <param name="isDrawTop">是否画上外框</param>
  544. /// <param name="isDrawBottom">是否画下外框</param>
  545. /// <param name="isDrawLeft">是否画左外框</param>
  546. /// <param name="isDrawRight">是否画右外框</param>
  547. /// <param name="isDrawHInside">是否画水平内框</param>
  548. /// <param name="isDrawVInside">是否画垂直内框</param>
  549. /// <param name="isDrawDiagonalDown">是否画斜向下线</param>
  550. /// <param name="isDrawDiagonalUp">是否画斜向上线</param>
  551. /// <param name="lineStyle">线类型</param>
  552. /// <param name="borderWeight">线粗细</param>
  553. /// <param name="color">线颜色</param>
  554. public void CellsDrawFrame(_Worksheet objWorkSheet, int startRow, int startColumn, int endRow, int endColumn,
  555. bool isDrawTop, bool isDrawBottom, bool isDrawLeft, bool isDrawRight,
  556. bool isDrawHInside, bool isDrawVInside, bool isDrawDiagonalDown, bool isDrawDiagonalUp,
  557. ExcelEnum.LineStyle lineStyle, ExcelEnum.BorderWeight borderWeight, ExcelEnum.ColorIndex color)
  558. {
  559. //获取画边框的单元格
  560. Range range = objWorkSheet.get_Range(objWorkSheet.Cells[startRow, startColumn], objWorkSheet.Cells[endRow, endColumn]);
  561. //清除所有边框
  562. range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = ExcelEnum.LineStyle.xlNone;
  563. range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = ExcelEnum.LineStyle.xlNone;
  564. range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = ExcelEnum.LineStyle.xlNone;
  565. range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = ExcelEnum.LineStyle.xlNone;
  566. range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = ExcelEnum.LineStyle.xlNone;
  567. range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = ExcelEnum.LineStyle.xlNone;
  568. range.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = ExcelEnum.LineStyle.xlNone;
  569. range.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = ExcelEnum.LineStyle.xlNone;
  570. #region 按参数画边框
  571. if (isDrawTop)
  572. {
  573. range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = lineStyle;
  574. range.Borders[XlBordersIndex.xlEdgeTop].Weight = borderWeight;
  575. range.Borders[XlBordersIndex.xlEdgeTop].ColorIndex = color;
  576. }
  577. if (isDrawBottom)
  578. {
  579. range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = lineStyle;
  580. range.Borders[XlBordersIndex.xlEdgeBottom].Weight = borderWeight;
  581. range.Borders[XlBordersIndex.xlEdgeBottom].ColorIndex = color;
  582. }
  583. if (isDrawLeft)
  584. {
  585. range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = lineStyle;
  586. range.Borders[XlBordersIndex.xlEdgeLeft].Weight = borderWeight;
  587. range.Borders[XlBordersIndex.xlEdgeLeft].ColorIndex = color;
  588. }
  589. if (isDrawRight)
  590. {
  591. range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = lineStyle;
  592. range.Borders[XlBordersIndex.xlEdgeRight].Weight = borderWeight;
  593. range.Borders[XlBordersIndex.xlEdgeRight].ColorIndex = color;
  594. }
  595. if (isDrawVInside)
  596. {
  597. range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = lineStyle;
  598. range.Borders[XlBordersIndex.xlInsideVertical].Weight = borderWeight;
  599. range.Borders[XlBordersIndex.xlInsideVertical].ColorIndex = color;
  600. }
  601. if (isDrawHInside)
  602. {
  603. range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = lineStyle;
  604. range.Borders[XlBordersIndex.xlInsideHorizontal].Weight = borderWeight;
  605. range.Borders[XlBordersIndex.xlInsideHorizontal].ColorIndex = color;
  606. }
  607. if (isDrawDiagonalDown)
  608. {
  609. range.Borders[XlBordersIndex.xlDiagonalDown].LineStyle = lineStyle;
  610. range.Borders[XlBordersIndex.xlDiagonalDown].Weight = borderWeight;
  611. range.Borders[XlBordersIndex.xlDiagonalDown].ColorIndex = color;
  612. }
  613. if (isDrawDiagonalUp)
  614. {
  615. range.Borders[XlBordersIndex.xlDiagonalUp].LineStyle = lineStyle;
  616. range.Borders[XlBordersIndex.xlDiagonalUp].Weight = borderWeight;
  617. range.Borders[XlBordersIndex.xlDiagonalUp].ColorIndex = color;
  618. }
  619. #endregion
  620. }
  621. #endregion
  622. }
  623. }