ExcelAccess.cs
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:9k
源码类别:

打印编程

开发平台:

Others

  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using GoldPrinter.ExcelConstants;
  5. namespace GoldPrinter
  6. {
  7. /// <summary>
  8. /// 该类主要定义Excel的程序对象,启动Excel并打印及保存。可能依赖于Interop.VBIDE及Interop.Microsoft.Office.Core,如果需要,请加上。
  9. /// 
  10. /// 作 者:长江支流(周方勇)
  11. /// Email:flygoldfish@163.com  QQ:150439795
  12. /// 网 址:www.webmis.com.cn
  13. /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
  14. /// 
  15. /// </summary>
  16. public class ExcelAccess:ExcelBase
  17. {
  18. public ExcelAccess()
  19. {
  20. }
  21. //核心函数,GetRange(),获取指定范围内的单元格
  22. /*
  23. public Excel.Range GetRange(int p_rowIndex,int p_colIndex)
  24. public Excel.Range GetRange(int p_rowIndex,string p_colChars)
  25. public Excel.Range GetRange(int p_startRowIndex,int p_startColIndex,int p_endRowIndex,int p_endColIndex)
  26. public Excel.Range GetRange(int p_startRowIndex,string p_startColChars,int p_endRowIndex,string p_endColChars)
  27. */
  28. #region MergeCells()合并单元格,合并后,默认居中,用Range或它的指定范围作为参数
  29. /// <summary>
  30. /// 合并指定范围内单元格
  31. /// </summary>
  32. /// <param name="p_rowIndex">行索引,也可以指定起始行、终止行索引</param>
  33. /// <param name="p_colIndex">列索引,也可以指定起始列、终止列数字或字母及组合索引</param>
  34. /// 
  35. /// <summary>
  36. /// 合并指定范围内单元格
  37. /// </summary>
  38. /// <param name="p_startRowIndex">起始行索引</param>
  39. /// <param name="p_startColIndex">起始列索引,可以是数字或字母及组合索引</param>
  40. /// <param name="p_endRowIndex">结束行索引</param>
  41. /// <param name="p_endColIndex">结束列索引,可以是数字或字母及组合索引</param>
  42. public void MergeCells(int p_startRowIndex,int p_startColIndex,int p_endRowIndex,int p_endColIndex)
  43. {
  44. MergeCells(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex));
  45. }
  46. /// <summary>
  47. /// 合并指定范围内单元格
  48. /// </summary>
  49. /// <param name="p_startRowIndex">起始行索引</param>
  50. /// <param name="p_startColChars">起始列索引,可以是数字或字母及组合索引</param>
  51. /// <param name="p_endRowIndex">结束行索引</param>
  52. /// <param name="p_endColChars">结束列索引,可以是数字或字母及组合索引</param>
  53. public void MergeCells(int p_startRowIndex,string p_startColChars,int p_endRowIndex,string p_endColChars)
  54. {
  55. MergeCells(GetRange(p_startRowIndex,p_startColChars,p_endRowIndex,p_endColChars));
  56. }
  57. #endregion
  58. #region SetFont(Excel.Range p_Range,Font p_Font[,Color p_color])
  59. public void SetFont(int p_startRowIndex,int p_startColIndex,int p_endRowIndex,int p_endColIndex,Font p_Font)
  60. {
  61. SetFont(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),p_Font,Color.Black);
  62. }
  63. #endregion 
  64. #region SetBordersEdge 设置指定范围边框(左、顶、右、底、往右下对角线、往右上对角线、内部水平线、内部垂直线、无线)线,并可指定线条的样式(无、虚线、点线等)及线粗细
  65. /// <summary>
  66. /// 设置指定范围边框(左、顶、右、底、往右下对角线、往右上对角线、内部水平线、内部垂直线、无线)线,并可指定线的样式及线粗细
  67. /// </summary>
  68. /// <param name="p_rowIndex">行索引,也可以指定起始行、终止行索引</param>
  69. /// <param name="p_colIndex">列索引,也可以指定起始列、终止列数字或字母及组合索引</param>
  70. /// <param name="p_BordersEdge">边框:左、顶、右、底、往右下对角线、往右上对角线、内部水平线、内部垂直线、无线</param>
  71. /// <param name="p_BordersLineStyle">线条样式:无、虚线、点线等,看Excel便知</param>
  72. /// <param name="p_BordersWeight">粗细</param>
  73. public void SetBordersEdge(int p_rowIndex,int p_colIndex,BordersEdge p_BordersEdge,BordersLineStyle p_BordersLineStyle,BordersWeight p_BordersWeight)
  74. {
  75. SetBordersEdge(GetRange(p_rowIndex,p_colIndex),p_BordersEdge,p_BordersLineStyle,p_BordersWeight);
  76. }
  77. public void SetBordersEdge(int p_rowIndex,string p_colChars,BordersEdge p_BordersEdge,BordersLineStyle p_BordersLineStyle,BordersWeight p_BordersWeight)
  78. {
  79. SetBordersEdge(GetRange(p_rowIndex,p_colChars),p_BordersEdge,p_BordersLineStyle,p_BordersWeight);
  80. }
  81. public void SetBordersEdge(int p_startRowIndex,int p_startColIndex,int p_endRowIndex,int p_endColIndex,BordersEdge p_BordersEdge,BordersLineStyle p_BordersLineStyle,BordersWeight p_BordersWeight)
  82. {
  83. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),p_BordersEdge,p_BordersLineStyle,p_BordersWeight);
  84. }
  85. public void SetBordersEdge(int p_startRowIndex,string p_startColChars,int p_endRowIndex,string p_endColChars,BordersEdge p_BordersEdge,BordersLineStyle p_BordersLineStyle,BordersWeight p_BordersWeight)
  86. {
  87. SetBordersEdge(GetRange(p_startRowIndex,p_startColChars,p_endRowIndex,p_endColChars),p_BordersEdge,p_BordersLineStyle,p_BordersWeight);
  88. }
  89. /// <summary>
  90. /// 设置指定范围内边界及内部网格线
  91. /// </summary>
  92. /// <param name="p_startRowIndex"></param>
  93. /// <param name="p_startColIndex"></param>
  94. /// <param name="p_endRowIndex"></param>
  95. /// <param name="p_endColIndex"></param>
  96. /// <param name="p_endColIndex">IsBordersOrBordersGrid,true只输出四周的边框,否则输出边框与网格线</param>
  97. public void SetBordersEdge(int p_startRowIndex,int p_startColIndex,int p_endRowIndex,int p_endColIndex,bool IsBordersOrBordersGrid)
  98. {
  99. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),BordersEdge.xlLeft);
  100. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),BordersEdge.xlTop);
  101. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),BordersEdge.xlRight);
  102. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),BordersEdge.xlBottom);
  103. if (!IsBordersOrBordersGrid)
  104. {
  105. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),BordersEdge.xlInsideHorizontal);
  106. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),BordersEdge.xlInsideVertical);
  107. }
  108. }
  109. #endregion 
  110. #region ClearBordersEdge,清除指定范围内的所有线,以SetBordersEdge设置边框为基础
  111. /// <summary>
  112. /// 清除用SetBordersEdge设置的边框内的所有线
  113. /// </summary>
  114. /// <param name="p_rowIndex">行索引,也可以指定起始行、终止行索引</param>
  115. /// <param name="p_colIndex">列索引,也可以指定起始列、终止列数字或字母及组合索引</param>
  116. public void ClearBordersEdge(int p_rowIndex,int p_colIndex)
  117. {
  118. SetBordersEdge(GetRange(p_rowIndex,p_colIndex),BordersEdge.xlLineStyleNone);
  119. }
  120. public void ClearBordersEdge(int p_rowIndex,string p_colChars)
  121. {
  122. SetBordersEdge(GetRange(p_rowIndex,p_colChars),BordersEdge.xlLineStyleNone);
  123. }
  124. public void ClearBordersEdge(int p_startRowIndex,int p_startColIndex,int p_endRowIndex,int p_endColIndex)
  125. {
  126. SetBordersEdge(GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex),BordersEdge.xlLineStyleNone);
  127. }
  128. public void ClearBordersEdge(int p_startRowIndex,string p_startColChars,int p_endRowIndex,string p_endColChars)
  129. {
  130. SetBordersEdge(GetRange(p_startRowIndex,p_startColChars,p_endRowIndex,p_endColChars),BordersEdge.xlLineStyleNone);
  131. }
  132. #endregion
  133. #region GetCellText(int p_rowIndex,int p_colIndex[/string p_colChars]),用Range或它的指定范围作为参数
  134. public string GetCellText(int p_rowIndex,int p_colIndex)
  135. {
  136. string strReturn = "";
  137. Excel.Range range;
  138. range = GetRange(p_rowIndex,p_colIndex);
  139. strReturn = range.Text.ToString();
  140. range = null;
  141. return strReturn;
  142. }
  143. public string GetCellText(int p_rowIndex,string p_colChars)
  144. {
  145. string strReturn = "";
  146. Excel.Range range;
  147. range = GetRange(p_rowIndex,p_colChars);
  148. strReturn = range.Text.ToString();
  149. range = null;
  150. return strReturn;
  151. }
  152. #endregion
  153. #region SetCellText(...),参数对应于Range(...),可以一个单元格也可以区域内的单元格一起设置同样的文本。用Range或它的指定范围作为参数
  154. public void SetCellText(int p_rowIndex,int p_colIndex,string p_text)
  155. {
  156. // xlApp.Cells[p_rowIndex,p_colIndex] = p_text;
  157. Excel.Range range;
  158. range = GetRange(p_rowIndex,p_colIndex);
  159. range.Cells.FormulaR1C1 = p_text;
  160. range = null;
  161. }
  162. public void SetCellText(int p_rowIndex,string p_colChars,string p_text)
  163. {
  164. Excel.Range range;
  165. range = GetRange(p_rowIndex,p_colChars);
  166. range.Cells.FormulaR1C1 = p_text;
  167. range = null;
  168. }
  169. public void SetCellText(int p_startRowIndex,int p_startColIndex,int p_endRowIndex,int p_endColIndex,string p_text)
  170. {
  171. Excel.Range range;
  172. range = GetRange(p_startRowIndex,p_startColIndex,p_endRowIndex,p_endColIndex);
  173. range.Cells.FormulaR1C1 = p_text;
  174. range = null;
  175. }
  176. public void SetCellText(int p_startRowIndex,string p_startColChars,int p_endRowIndex,string p_endColChars,string p_text)
  177. {
  178. Excel.Range range;
  179. range = GetRange(p_startRowIndex,p_startColChars,p_endRowIndex,p_endColChars);
  180. range.Cells.FormulaR1C1 = p_text;
  181. range = null;
  182. }
  183. #endregion
  184. public void SetCellText(DataTable p_DataTable,int p_startExcelRowIndex,int p_startExcelColIndex,bool IsDrawGridLine)
  185. {
  186. for(int i=0;i< p_DataTable.Rows.Count;i++)
  187. {
  188. for(int j=0;j<p_DataTable.Columns.Count;j++)
  189. {
  190. SetCellText(p_startExcelRowIndex + i , p_startExcelColIndex + j ,p_DataTable.Rows[i][j].ToString());
  191. }
  192. }
  193. if (IsDrawGridLine)
  194. {
  195. SetBordersEdge(p_startExcelRowIndex,p_startExcelColIndex,p_startExcelRowIndex + p_DataTable.Rows.Count - 1,p_startExcelColIndex + p_DataTable.Columns.Count - 1,false);
  196. }
  197. }
  198. }//End class
  199. }//End Namespace