ExclReportHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Office.Interop.Excel;
  6. using Microsoft.Win32;
  7. using System.Collections;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. namespace Core.StlMes.Client.Plan.Order
  11. {
  12. public partial class ExclReportHelper
  13. {
  14. #region excel报表界面的基础方法
  15. public Microsoft.Office.Interop.Excel.Application excel = null;
  16. public Object refmissing = System.Reflection.Missing.Value;
  17. public Workbook mybook = null;
  18. public Worksheet mySheet = null;
  19. public ArrayList pids = new ArrayList();
  20. //修改注册表,解决嵌入excel弹出问题
  21. public struct regkey
  22. {
  23. public RegistryKey rootKey;
  24. public string sKey;
  25. public string sItem;
  26. public int val;
  27. public bool isExist;
  28. }
  29. public regkey[] aryKeys = new regkey[5];
  30. public void RegSet()
  31. {
  32. try
  33. {
  34. //RegistryPermission f = new RegistryPermission(RegistryPermissionAccess.Read | RegistryPermissionAccess.Write,
  35. // "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes");
  36. //f.AddPathList(RegistryPermissionAccess.Read | RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT");
  37. RegistryKey rKey = Registry.LocalMachine;
  38. aryKeys[0].rootKey = rKey;
  39. aryKeys[0].sKey = "SOFTWARE\\Classes\\Excel.Sheet.12";
  40. aryKeys[0].isExist = false;
  41. aryKeys[0].sItem = "BrowserFlags";
  42. RegistryKey subKey = rKey.OpenSubKey(aryKeys[0].sKey, true);
  43. if (subKey != null)
  44. {
  45. if (subKey.GetValue(aryKeys[0].sItem, null) != null)
  46. {
  47. aryKeys[0].val = (int)subKey.GetValue(aryKeys[0].sItem);
  48. aryKeys[0].isExist = true;
  49. subKey.SetValue(aryKeys[0].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);
  50. subKey.Close();
  51. aryKeys[1].rootKey = rKey;
  52. aryKeys[1].sKey = "SOFTWARE\\Classes\\Excel.Sheet.8";
  53. aryKeys[1].isExist = false;
  54. aryKeys[1].sItem = "BrowserFlags";
  55. subKey = rKey.OpenSubKey(aryKeys[1].sKey, true);
  56. if (subKey != null)
  57. {
  58. aryKeys[1].val = (int)subKey.GetValue(aryKeys[1].sItem);
  59. aryKeys[1].isExist = true;
  60. subKey.SetValue(aryKeys[1].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);//,
  61. subKey.Close();
  62. }
  63. aryKeys[2].rootKey = rKey;
  64. aryKeys[2].sKey = "SOFTWARE\\Classes\\Excel.SheetMacroEnabled.12";
  65. aryKeys[2].isExist = false;
  66. aryKeys[2].sItem = "BrowserFlags";
  67. subKey = rKey.OpenSubKey(aryKeys[2].sKey, true);
  68. if (subKey != null)
  69. {
  70. aryKeys[2].val = (int)subKey.GetValue(aryKeys[2].sItem);
  71. aryKeys[2].isExist = true;
  72. subKey.SetValue(aryKeys[2].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);
  73. subKey.Close();
  74. }
  75. aryKeys[3].rootKey = rKey;
  76. aryKeys[3].sKey = "SOFTWARE\\Classes\\Excel.SheetBinaryMacroEnabled.12";
  77. aryKeys[3].isExist = false;
  78. aryKeys[3].sItem = "BrowserFlags";
  79. subKey = rKey.OpenSubKey(aryKeys[3].sKey, true);
  80. if (subKey != null)
  81. {
  82. aryKeys[3].val = (int)subKey.GetValue(aryKeys[3].sItem);
  83. aryKeys[3].isExist = true;
  84. subKey.SetValue(aryKeys[3].sItem, -(0xFFFFFFFF - 0x80000A00 + 1), RegistryValueKind.DWord);
  85. subKey.Close();
  86. }
  87. }
  88. }
  89. rKey.Close();
  90. rKey = Registry.ClassesRoot;
  91. aryKeys[4].rootKey = rKey;
  92. aryKeys[4].sKey = "Excel.Sheet.8";
  93. aryKeys[4].isExist = false;
  94. aryKeys[4].sItem = "EditFlags";
  95. subKey = rKey.OpenSubKey(aryKeys[4].sKey, true);
  96. if (subKey != null)
  97. {
  98. if (!(subKey.GetValue(aryKeys[4].sItem) is Array))
  99. aryKeys[4].val = (int)subKey.GetValue(aryKeys[4].sItem);
  100. else
  101. aryKeys[4].val = BitConverter.ToInt32((byte[])subKey.GetValue(aryKeys[4].sItem), 0);
  102. aryKeys[4].isExist = true;
  103. subKey.SetValue(aryKeys[4].sItem, 0x00010000, RegistryValueKind.DWord);
  104. subKey.Close();
  105. }
  106. rKey.Close();
  107. }
  108. catch (Exception ee)
  109. {
  110. MessageBox.Show(ee.Message, "提示");
  111. }
  112. }
  113. //注册表改回
  114. public void RegBack()
  115. {
  116. try
  117. {
  118. RegistryKey rKey, subKey;
  119. //if (aryKeys[0].isExist)
  120. //{
  121. // rKey = Registry.LocalMachine;
  122. // subKey = rKey.OpenSubKey(aryKeys[0].sKey, true);
  123. //}
  124. for (int i = 0; i < aryKeys.Length; i++)
  125. {
  126. if (aryKeys[i].isExist)
  127. {
  128. rKey = aryKeys[i].rootKey;
  129. subKey = rKey.OpenSubKey(aryKeys[i].sKey, true);
  130. subKey.SetValue(aryKeys[i].sItem, aryKeys[i].val, RegistryValueKind.DWord);
  131. subKey.Close();
  132. rKey.Close();
  133. }
  134. }
  135. }
  136. catch { }
  137. }
  138. public void RecrodPIDs()
  139. {
  140. //在创建进程前将所有excel进程记录下来,结束后将增加的excel进程kill
  141. pids.Clear();
  142. foreach (Process process in System.Diagnostics.Process.GetProcesses())
  143. {
  144. if (process.ProcessName.ToUpper().Equals("EXCEL"))
  145. {
  146. pids.Add(process.Id);
  147. }
  148. }
  149. }
  150. public void KillExcelProcess()
  151. {
  152. foreach (Process process in System.Diagnostics.Process.GetProcesses())
  153. {
  154. if (process.ProcessName.ToUpper().Equals("EXCEL"))
  155. {
  156. if (!pids.Contains(process.Id))
  157. {
  158. process.Kill();
  159. }
  160. }
  161. }
  162. }
  163. public void KillExcelProcess(Process process)
  164. {
  165. if (System.Diagnostics.Process.GetProcesses().Contains(process))
  166. {
  167. if (process.ProcessName.ToUpper().Equals("EXCEL"))
  168. {
  169. process.Kill();
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 设置单元格数据
  175. /// </summary>
  176. /// <param name="cellPosition"></param>
  177. /// <param name="value"></param>
  178. public void SetCellRangeValue(object cellPosition, string value)
  179. {
  180. string[] ranges = GetRange(cellPosition.ToString(), cellPosition.ToString());
  181. try
  182. {
  183. mySheet.get_Range(ranges[0], ranges[0]).Value2 = value;
  184. //mySheet.get_Range(ranges[0], ranges[0]).HorizontalAlignment = XlHAlign.xlHAlignRight;
  185. }
  186. catch
  187. { }
  188. }
  189. public void SetExcelRowHeight(int RowSum)
  190. {
  191. ((Microsoft.Office.Interop.Excel.Range)mySheet.Rows["3:" + RowSum + 3, System.Type.Missing]).RowHeight = 20;
  192. }
  193. public void SetExcelBorderStyle(object cellPosition, string value)
  194. {
  195. string[] ranges = GetRange(cellPosition.ToString(), cellPosition.ToString());
  196. try
  197. {
  198. mySheet.get_Range(ranges[0], ranges[0]).Borders.LineStyle = XlLineStyle.xlContinuous;
  199. }
  200. catch (Exception exp)
  201. {
  202. throw exp;
  203. }
  204. }
  205. /// <summary>
  206. /// 根据行数和列数(列数为字母,并且行号和列号放在一起)取出对应的数字数组
  207. /// </summary>
  208. /// <param name="str"></param>
  209. /// <returns></returns>
  210. public int[] GetPosition(string str)
  211. {
  212. char[] chars = str.ToUpper().ToCharArray();
  213. int[] position = new int[2];
  214. string column = "";
  215. string row = "";
  216. for (int i = 0; i < chars.Length; i++)
  217. {
  218. if ('A' <= chars[i] && chars[i] <= 'Z')
  219. column += chars[i].ToString();
  220. if ('0' <= chars[i] && chars[i] <= '9')
  221. row += chars[i].ToString();
  222. }
  223. position[0] = ReplaceString(column);
  224. position[1] = int.Parse(row);
  225. return position;
  226. }
  227. /// <summary>
  228. /// 根据excel中的两点,取出这两点范围的位置。如"A1,A2",以逗号分隔
  229. /// </summary>
  230. /// <param name="fromPosition"></param>
  231. /// <param name="toPosition"></param>
  232. /// <returns></returns>
  233. public string[] GetRange(string fromPosition, string toPosition)
  234. {
  235. int[] _formPosition = GetPosition(fromPosition);
  236. int[] _toPosition = GetPosition(toPosition);
  237. if (_formPosition[0] > _toPosition[0])
  238. {
  239. int temp = _toPosition[0];
  240. _toPosition[0] = _formPosition[0];
  241. _formPosition[0] = temp;
  242. }
  243. if (_formPosition[1] > _toPosition[1])
  244. {
  245. int temp = _toPosition[1];
  246. _toPosition[1] = _formPosition[1];
  247. _formPosition[1] = temp;
  248. }
  249. string result = "";
  250. for (int i = _formPosition[0]; i <= _toPosition[0]; i++)
  251. {
  252. for (int j = _formPosition[1]; j <= _toPosition[1]; j++)
  253. {
  254. result += ReplaceNumber(i) + j.ToString() + ",";
  255. }
  256. }
  257. if (result.EndsWith(","))
  258. result = result.Substring(0, result.Length - 1);
  259. return result.Split(',');
  260. }
  261. /// <summary>
  262. /// 把在excel中的字母列转换成相应的数字
  263. /// </summary>
  264. /// <param name="index"></param>
  265. /// <returns></returns>
  266. public string ReplaceNumber(int index)
  267. {
  268. int i = index;
  269. int j = 0;
  270. if (index > 26)
  271. {
  272. i = index % 26;
  273. j = index / 26;
  274. }
  275. if (j == 0)
  276. return ((char)((int)'A' + i - 1)).ToString();
  277. else
  278. return ((char)((int)'A' + j - 1)).ToString() + ((char)((int)'A' + i - 1)).ToString();
  279. }
  280. /// <summary>
  281. /// 与ReplaceNumber相反
  282. /// </summary>
  283. /// <param name="index"></param>
  284. /// <returns></returns>
  285. public int ReplaceString(string str)
  286. {
  287. str = str.ToUpper();
  288. char[] chars = str.ToCharArray();
  289. int index = 0;
  290. if (chars.Length == 1)
  291. index = (int)chars[0] - (int)'A' + 1;
  292. else if (chars.Length == 2)
  293. {
  294. index = (int)chars[1] - (int)'A' + 1;
  295. index += ((int)chars[0] - (int)'A' + 1) * 26;
  296. }
  297. return index;
  298. }
  299. /// <summary>
  300. /// 根据行号和列号取值
  301. /// </summary>
  302. /// <param name="position">如:B6</param>
  303. /// <returns></returns>
  304. public object GetCellValue(string position)
  305. {
  306. object obj = null;
  307. try
  308. {
  309. //obj = mySheet.get_Range(position, position).Value2;
  310. //obj = mySheet.get_Range("A1", Type.Missing).Value2;
  311. obj = mySheet.get_Range(position, Type.Missing).Value2;
  312. }
  313. catch (Exception exp)
  314. {
  315. throw exp;
  316. }
  317. return obj;
  318. }
  319. /// <summary>
  320. /// 取出范围内的单元格值
  321. /// </summary>
  322. /// <param name="fromPosition"></param>
  323. /// <param name="toPosition"></param>
  324. /// <returns></returns>
  325. public object[] GetCellRangeValue(string fromPosition, string toPosition)
  326. {
  327. string[] ranges = GetRange(fromPosition, toPosition);
  328. object[] obj = new object[ranges.Length];
  329. for (int i = 0; i < obj.Length; i++)
  330. {
  331. obj[i] = GetCellValue(ranges[i]);
  332. }
  333. return obj;
  334. }
  335. #endregion
  336. /// <summary>
  337. /// 根据excel中的两点,取出这两点范围的位置。如"A1,A2",以逗号分隔
  338. /// </summary>
  339. /// <param name="fromPosition"></param>
  340. /// <param name="toPosition"></param>
  341. /// <returns></returns>
  342. public string[,] GetRange2(string fromPosition, string toPosition)
  343. {
  344. int[] _formPosition = GetPosition(fromPosition);
  345. int[] _toPosition = GetPosition(toPosition);
  346. if (_formPosition[0] > _toPosition[0])
  347. {
  348. int temp = _toPosition[0];
  349. _toPosition[0] = _formPosition[0];
  350. _formPosition[0] = temp;
  351. }
  352. if (_formPosition[1] > _toPosition[1])
  353. {
  354. int temp = _toPosition[1];
  355. _toPosition[1] = _formPosition[1];
  356. _formPosition[1] = temp;
  357. }
  358. int colum = _toPosition[0] - _formPosition[0] + 1;
  359. int row = _toPosition[1] - _formPosition[1] + 1;
  360. string[,] result = new string[row,colum];
  361. for (int i = _formPosition[0]; i <= _toPosition[0]; i++)
  362. {
  363. for (int j = _formPosition[1]; j <= _toPosition[1]; j++)
  364. {
  365. result[(j - _formPosition[1]), (i - _formPosition[0])] = ReplaceNumber(i) + j.ToString();
  366. }
  367. }
  368. return result;
  369. }
  370. /// <summary>
  371. /// 取出范围内的单元格值
  372. /// </summary>
  373. /// <param name="fromPosition"></param>
  374. /// <param name="toPosition"></param>
  375. /// <returns></returns>
  376. public string[,] GetCellRangeValue2(string fromPosition, string toPosition)
  377. {
  378. try
  379. {
  380. object[,] obj = (object[,])mySheet.get_Range(fromPosition, toPosition).Value2;
  381. int rows = obj.GetLength(0); //获取m_objRangeArrayValue的行数
  382. int columns = obj.GetLength(1); //获取m_objRangeArrayValue的列数
  383. object[,] param = new object[rows, columns]; //转化为起始为0的数组
  384. Array.Copy(obj, param, rows * columns); //临时结果,学习用
  385. string[,] str = new string[param.GetLength(0), param.GetLength(1)];
  386. for (int i = 0; i < str.GetLength(0); i++)
  387. {
  388. for (int j = 0; j < str.GetLength(1); j++)
  389. {
  390. if (param[i, j] == null)
  391. {
  392. str[i, j] = "";
  393. }
  394. else
  395. {
  396. str[i, j] = param[i, j].ToString();
  397. }
  398. }
  399. }
  400. return str;
  401. }
  402. catch
  403. {
  404. return null;
  405. }
  406. }
  407. }
  408. }