ExcelHelper.cs
上传用户:clhwcc
上传日期:2022-08-07
资源大小:7k
文件大小:57k
源码类别:

文件操作

开发平台:

C#

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Diagnostics;
  7. using System.Collections;
  8. //using cfg = System.Configuration;
  9. namespace GDBI.Common
  10. {
  11. public class ExcelHelper
  12. {
  13. #region 成员变量
  14. private string templetFile = null;
  15. private string outputFile = null;
  16. private object missing = Missing.Value;
  17. private DateTime beforeTime; //Excel启动之前时间
  18. private DateTime afterTime; //Excel启动之后时间
  19. Excel.Application app;
  20. Excel.Workbook workBook;
  21. Excel.Worksheet workSheet;
  22. Excel.Range range;
  23. Excel.Range range1;
  24. Excel.Range range2;
  25. Excel.TextBox textBox;
  26. private int sheetCount = 1; //WorkSheet数量
  27. private string sheetPrefixName = "页";
  28. #endregion
  29. #region 公共属性
  30. /// <summary>
  31. /// WorkSheet前缀名,比如:前缀名为“页”,那么WorkSheet名称依次为“页-1,页-2...”
  32. /// </summary>
  33. public string SheetPrefixName
  34. {
  35. set { this.sheetPrefixName = value; }
  36. }
  37. /// <summary>
  38. /// WorkSheet数量
  39. /// </summary>
  40. public int WorkSheetCount
  41. {
  42. get { return workBook.Sheets.Count; }
  43. }
  44. /// <summary>
  45. /// Excel模板文件路径
  46. /// </summary>
  47. public string TempletFilePath
  48. {
  49. set { this.templetFile = value; }
  50. }
  51. /// <summary>
  52. /// 输出Excel文件路径
  53. /// </summary>
  54. public string OutputFilePath
  55. {
  56. set { this.outputFile = value; }
  57. }
  58. #endregion
  59. #region 公共方法
  60. #region ExcelHelper
  61. /// <summary>
  62. /// 构造函数,将一个已有Excel工作簿作为模板,并指定输出路径
  63. /// </summary>
  64. /// <param name="templetFilePath">Excel模板文件路径</param>
  65. /// <param name="outputFilePath">输出Excel文件路径</param>
  66. public ExcelHelper(string templetFilePath,string outputFilePath)
  67. {
  68. if(templetFilePath == null)
  69. throw new Exception("Excel模板文件路径不能为空!");
  70. if(outputFilePath == null)
  71. throw new Exception("输出Excel文件路径不能为空!");
  72. if(!File.Exists(templetFilePath))
  73. throw new Exception("指定路径的Excel模板文件不存在!");
  74. this.templetFile = templetFilePath;
  75. this.outputFile = outputFilePath;
  76. //创建一个Application对象并使其可见
  77. beforeTime = DateTime.Now;
  78. app = new Excel.ApplicationClass();
  79. app.Visible = true;
  80. afterTime = DateTime.Now;
  81. //打开模板文件,得到WorkBook对象
  82. workBook = app.Workbooks.Open(templetFile,missing,missing,missing,missing,missing,
  83. missing,missing,missing,missing,missing,missing,missing);
  84. //得到WorkSheet对象
  85. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
  86. }
  87. /// <summary>
  88. /// 构造函数,打开一个已有的工作簿
  89. /// </summary>
  90. /// <param name="fileName">Excel文件名</param>
  91. public ExcelHelper(string fileName)
  92. {
  93. if(!File.Exists(fileName))
  94. throw new Exception("指定路径的Excel文件不存在!");
  95. //创建一个Application对象并使其可见
  96. beforeTime = DateTime.Now;
  97. app = new Excel.ApplicationClass();
  98. app.Visible = true;
  99. afterTime = DateTime.Now;
  100. //打开一个WorkBook
  101. workBook = app.Workbooks.Open(fileName,
  102. Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  103. Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  104. Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  105. //得到WorkSheet对象
  106. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
  107. }
  108. /// <summary>
  109. /// 构造函数,新建一个工作簿
  110. /// </summary>
  111. public ExcelHelper()
  112. {
  113. //创建一个Application对象并使其可见
  114. beforeTime = DateTime.Now;
  115. app = new Excel.ApplicationClass();
  116. app.Visible = true;
  117. afterTime = DateTime.Now;
  118. //新建一个WorkBook
  119. workBook = app.Workbooks.Add(Type.Missing);
  120. //得到WorkSheet对象
  121. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
  122. }
  123. #endregion
  124. #region Data Export Methods
  125. /// <summary>
  126. /// 将DataTable数据写入Excel文件(自动分页)
  127. /// </summary>
  128. /// <param name="dt">DataTable</param>
  129. /// <param name="rows">每个WorkSheet写入多少行数据</param>
  130. /// <param name="top">表格数据起始行索引</param>
  131. /// <param name="left">表格数据起始列索引</param>
  132. public void DataTableToExcel(DataTable dt,int rows,int top,int left)
  133. {
  134. int rowCount = dt.Rows.Count; //DataTable行数
  135. int colCount = dt.Columns.Count; //DataTable列数
  136. sheetCount = this.GetSheetCount(rowCount,rows); //WorkSheet个数
  137. // StringBuilder sb;
  138. //复制sheetCount-1个WorkSheet对象
  139. for(int i=1;i<sheetCount;i++)
  140. {
  141. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  142. workSheet.Copy(missing,workBook.Worksheets[i]);
  143. }
  144. for(int i=1;i<=sheetCount;i++)
  145. {
  146. int startRow = (i - 1) * rows; //记录起始行索引
  147. int endRow = i * rows; //记录结束行索引
  148. //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
  149. if(i == sheetCount)
  150. endRow = rowCount;
  151. //获取要写入数据的WorkSheet对象,并重命名
  152. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  153. workSheet.Name = sheetPrefixName + "-" + i.ToString();
  154. //将dt中的数据写入WorkSheet
  155. // for(int j=0;j<endRow-startRow;j++)
  156. // {
  157. // for(int k=0;k<colCount;k++)
  158. // {
  159. // workSheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString();
  160. // }
  161. // }
  162. //利用二维数组批量写入
  163. int row = endRow-startRow;
  164. string[,] ss = new string[row,colCount];
  165. for(int j=0;j<row;j++)
  166. {
  167. for(int k=0;k<colCount;k++)
  168. {
  169. ss[j,k] = dt.Rows[startRow + j][k].ToString();
  170. }
  171. }
  172. range = (Excel.Range)workSheet.Cells[top,left];
  173. range = range.get_Resize(row,colCount);
  174. range.Value = ss;
  175. #region 利用Windwo粘贴板批量拷贝数据(在Web下面行不通)
  176. /*sb = new StringBuilder();
  177. for(int j=0;j<endRow-startRow;j++)
  178. {
  179. for(int k=0;k<colCount;k++)
  180. {
  181. sb.Append( dt.Rows[startRow + j][k].ToString() );
  182. sb.Append("t");
  183. }
  184. sb.Append("n");
  185. }
  186. System.Windows.Forms.Clipboard.SetDataObject(sb.ToString());
  187. range = (Excel.Range)workSheet.Cells[top,left];
  188. workSheet.Paste(range,false);*/
  189. #endregion
  190. }
  191. }
  192. /// <summary>
  193. /// 将DataTable数据写入Excel文件(不分页)
  194. /// </summary>
  195. /// <param name="dt">DataTable</param>
  196. /// <param name="top">表格数据起始行索引</param>
  197. /// <param name="left">表格数据起始列索引</param>
  198. public void DataTableToExcel(DataTable dt,int top,int left)
  199. {
  200. int rowCount = dt.Rows.Count; //DataTable行数
  201. int colCount = dt.Columns.Count; //DataTable列数
  202. //利用二维数组批量写入
  203. string[,] arr = new string[rowCount,colCount];
  204. for(int j=0;j<rowCount;j++)
  205. {
  206. for(int k=0;k<colCount;k++)
  207. {
  208. arr[j,k] = dt.Rows[j][k].ToString();
  209. }
  210. }
  211. range = (Excel.Range)workSheet.Cells[top,left];
  212. range = range.get_Resize(rowCount,colCount);
  213. range.Value = arr;
  214. }
  215. /// <summary>
  216. /// 将DataTable数据写入Excel文件(自动分页,并指定要合并的列索引)
  217. /// </summary>
  218. /// <param name="dt">DataTable</param>
  219. /// <param name="rows">每个WorkSheet写入多少行数据</param>
  220. /// <param name="top">表格数据起始行索引</param>
  221. /// <param name="left">表格数据起始列索引</param>
  222. /// <param name="mergeColumnIndex">DataTable中要合并相同行的列索引,从0开始</param>
  223. public void DataTableToExcel(DataTable dt,int rows,int top,int left,int mergeColumnIndex)
  224. {
  225. int rowCount = dt.Rows.Count; //源DataTable行数
  226. int colCount = dt.Columns.Count; //源DataTable列数
  227. sheetCount = this.GetSheetCount(rowCount,rows); //WorkSheet个数
  228. // StringBuilder sb;
  229. //复制sheetCount-1个WorkSheet对象
  230. for(int i=1;i<sheetCount;i++)
  231. {
  232. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  233. workSheet.Copy(missing,workBook.Worksheets[i]);
  234. }
  235. for(int i=1;i<=sheetCount;i++)
  236. {
  237. int startRow = (i - 1) * rows; //记录起始行索引
  238. int endRow = i * rows; //记录结束行索引
  239. //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
  240. if(i == sheetCount)
  241. endRow = rowCount;
  242. //获取要写入数据的WorkSheet对象,并重命名
  243. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  244. workSheet.Name = sheetPrefixName + "-" + i.ToString();
  245. //将dt中的数据写入WorkSheet
  246. // for(int j=0;j<endRow-startRow;j++)
  247. // {
  248. // for(int k=0;k<colCount;k++)
  249. // {
  250. // workSheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString();
  251. // }
  252. // }
  253. //利用二维数组批量写入
  254. int row = endRow-startRow;
  255. string[,] ss = new string[row,colCount];
  256. for(int j=0;j<row;j++)
  257. {
  258. for(int k=0;k<colCount;k++)
  259. {
  260. ss[j,k] = dt.Rows[startRow + j][k].ToString();
  261. }
  262. }
  263. range = (Excel.Range)workSheet.Cells[top,left];
  264. range = range.get_Resize(row,colCount);
  265. range.Value = ss;
  266. //合并相同行
  267. this.MergeRows(workSheet,left+mergeColumnIndex,top,rows);
  268. }
  269. }
  270. /// <summary>
  271. /// 将二维数组数据写入Excel文件(自动分页)
  272. /// </summary>
  273. /// <param name="arr">二维数组</param>
  274. /// <param name="rows">每个WorkSheet写入多少行数据</param>
  275. /// <param name="top">行索引</param>
  276. /// <param name="left">列索引</param>
  277. public void ArrayToExcel(string[,] arr,int rows,int top,int left)
  278. {
  279. int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
  280. int colCount = arr.GetLength(1); //二维数据列数(二维长度)
  281. sheetCount = this.GetSheetCount(rowCount,rows); //WorkSheet个数
  282. //复制sheetCount-1个WorkSheet对象
  283. for(int i=1;i<sheetCount;i++)
  284. {
  285. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  286. workSheet.Copy(missing,workBook.Worksheets[i]);
  287. }
  288. //将二维数组数据写入Excel
  289. for(int i=sheetCount;i>=1;i--)
  290. {
  291. int startRow = (i - 1) * rows; //记录起始行索引
  292. int endRow = i * rows; //记录结束行索引
  293. //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
  294. if(i == sheetCount)
  295. endRow = rowCount;
  296. //获取要写入数据的WorkSheet对象,并重命名
  297. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  298. workSheet.Name = sheetPrefixName + "-" + i.ToString();
  299. //将二维数组中的数据写入WorkSheet
  300. // for(int j=0;j<endRow-startRow;j++)
  301. // {
  302. // for(int k=0;k<colCount;k++)
  303. // {
  304. // workSheet.Cells[top + j,left + k] = arr[startRow + j,k];
  305. // }
  306. // }
  307. //利用二维数组批量写入
  308. int row = endRow-startRow;
  309. string[,] ss = new string[row,colCount];
  310. for(int j=0;j<row;j++)
  311. {
  312. for(int k=0;k<colCount;k++)
  313. {
  314. ss[j,k] = arr[startRow + j,k];
  315. }
  316. }
  317. range = (Excel.Range)workSheet.Cells[top,left];
  318. range = range.get_Resize(row,colCount);
  319. range.Value = ss;
  320. }
  321. }//end ArrayToExcel
  322. /// <summary>
  323. /// 将二维数组数据写入Excel文件(不分页)
  324. /// </summary>
  325. /// <param name="arr">二维数组</param>
  326. /// <param name="top">行索引</param>
  327. /// <param name="left">列索引</param>
  328. public void ArrayToExcel(string[,] arr,int top,int left)
  329. {
  330. int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
  331. int colCount = arr.GetLength(1); //二维数据列数(二维长度)
  332. range = (Excel.Range)workSheet.Cells[top,left];
  333. range = range.get_Resize(rowCount,colCount);
  334. range.FormulaArray = arr;
  335. }//end ArrayToExcel
  336. /// <summary>
  337. /// 将二维数组数据写入Excel文件(不分页)
  338. /// </summary>
  339. /// <param name="arr">二维数组</param>
  340. /// <param name="top">行索引</param>
  341. /// <param name="left">列索引</param>
  342. /// <param name="isFormula">填充的数据是否需要计算</param>
  343. public void ArrayToExcel(string[,] arr,int top,int left,bool isFormula)
  344. {
  345. int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
  346. int colCount = arr.GetLength(1); //二维数据列数(二维长度)
  347. range = (Excel.Range)workSheet.Cells[top,left];
  348. range = range.get_Resize(rowCount,colCount);
  349. //注意:使用range.FormulaArray写合并的单元格会出问题
  350. if(isFormula)
  351. range.FormulaArray = arr;
  352. else
  353. range.Value = arr;
  354. }//end ArrayToExcel
  355. /// <summary>
  356. /// 将二维数组数据写入Excel文件(不分页),合并指定列的相同行
  357. /// </summary>
  358. /// <param name="arr">二维数组</param>
  359. /// <param name="top">行索引</param>
  360. /// <param name="left">列索引</param>
  361. /// <param name="isFormula">填充的数据是否需要计算</param>
  362. /// <param name="mergeColumnIndex">需要合并行的列索引</param>
  363. public void ArrayToExcel(string[,] arr,int top,int left,bool isFormula,int mergeColumnIndex)
  364. {
  365. int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
  366. int colCount = arr.GetLength(1); //二维数据列数(二维长度)
  367. range = (Excel.Range)workSheet.Cells[top,left];
  368. range = range.get_Resize(rowCount,colCount);
  369. //注意:使用range.FormulaArray写合并的单元格会出问题
  370. if(isFormula)
  371. range.FormulaArray = arr;
  372. else
  373. range.Value = arr;
  374. this.MergeRows(workSheet,mergeColumnIndex,top,rowCount);
  375. }//end ArrayToExcel
  376. /// <summary>
  377. /// 将二维数组数据写入Excel文件(不分页)
  378. /// </summary>
  379. /// <param name="sheetIndex">工作表索引</param>
  380. /// <param name="arr">二维数组</param>
  381. /// <param name="top">行索引</param>
  382. /// <param name="left">列索引</param>
  383. public void ArrayToExcel(int sheetIndex,string[,] arr,int top,int left)
  384. {
  385. if(sheetIndex > this.WorkSheetCount)
  386. {
  387. this.KillExcelProcess();
  388. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  389. }
  390. // 改变当前工作表
  391. this.workSheet = (Excel.Worksheet)this.workBook.Sheets.get_Item(sheetIndex);
  392. int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
  393. int colCount = arr.GetLength(1); //二维数据列数(二维长度)
  394. range = (Excel.Range)workSheet.Cells[top,left];
  395. range = range.get_Resize(rowCount,colCount);
  396. range.Value2 = arr;
  397. }//end ArrayToExcel
  398. /// <summary>
  399. /// 将二维数组数据写入Excel文件(自动分页,并指定要合并的列索引)
  400. /// </summary>
  401. /// <param name="arr">二维数组</param>
  402. /// <param name="rows">每个WorkSheet写入多少行数据</param>
  403. /// <param name="top">行索引</param>
  404. /// <param name="left">列索引</param>
  405. /// <param name="mergeColumnIndex">数组的二维索引,相当于DataTable的列索引,索引从0开始</param>
  406. public void ArrayToExcel(string[,] arr,int rows,int top,int left,int mergeColumnIndex)
  407. {
  408. int rowCount = arr.GetLength(0); //二维数组行数(一维长度)
  409. int colCount = arr.GetLength(1); //二维数据列数(二维长度)
  410. sheetCount = this.GetSheetCount(rowCount,rows); //WorkSheet个数
  411. //复制sheetCount-1个WorkSheet对象
  412. for(int i=1;i<sheetCount;i++)
  413. {
  414. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  415. workSheet.Copy(missing,workBook.Worksheets[i]);
  416. }
  417. //将二维数组数据写入Excel
  418. for(int i=sheetCount;i>=1;i--)
  419. {
  420. int startRow = (i - 1) * rows; //记录起始行索引
  421. int endRow = i * rows; //记录结束行索引
  422. //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
  423. if(i == sheetCount)
  424. endRow = rowCount;
  425. //获取要写入数据的WorkSheet对象,并重命名
  426. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  427. workSheet.Name = sheetPrefixName + "-" + i.ToString();
  428. //将二维数组中的数据写入WorkSheet
  429. for(int j=0;j<endRow-startRow;j++)
  430. {
  431. for(int k=0;k<colCount;k++)
  432. {
  433. workSheet.Cells[top + j,left + k] = arr[startRow + j,k];
  434. }
  435. }
  436. //利用二维数组批量写入
  437. int row = endRow-startRow;
  438. string[,] ss = new string[row,colCount];
  439. for(int j=0;j<row;j++)
  440. {
  441. for(int k=0;k<colCount;k++)
  442. {
  443. ss[j,k] = arr[startRow + j,k];
  444. }
  445. }
  446. range = (Excel.Range)workSheet.Cells[top,left];
  447. range = range.get_Resize(row,colCount);
  448. range.Value = ss;
  449. //合并相同行
  450. this.MergeRows(workSheet,left+mergeColumnIndex,top,rows);
  451. }
  452. }//end ArrayToExcel
  453. #endregion
  454. #region WorkSheet Methods
  455. /// <summary>
  456. /// 改变当前工作表
  457. /// </summary>
  458. /// <param name="sheetIndex">工作表索引</param>
  459. public void ChangeCurrentWorkSheet(int sheetIndex)
  460. {
  461. //若指定工作表索引超出范围,则不改变当前工作表
  462. if(sheetIndex < 1)
  463. return;
  464. if(sheetIndex > this.WorkSheetCount)
  465. return;
  466. this.workSheet = (Excel.Worksheet)this.workBook.Sheets.get_Item(sheetIndex);
  467. }
  468. /// <summary>
  469. /// 隐藏指定名称的工作表
  470. /// </summary>
  471. /// <param name="sheetName">工作表名称</param>
  472. public void HiddenWorkSheet(string sheetName)
  473. {
  474. try
  475. {
  476. Excel.Worksheet sheet = null;
  477. for(int i=1;i<=this.WorkSheetCount;i++)
  478. {
  479. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
  480. if(workSheet.Name == sheetName)
  481. sheet = workSheet;
  482. }
  483. if(sheet != null)
  484. sheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
  485. else
  486. {
  487. this.KillExcelProcess();
  488. throw new Exception("名称为"" + sheetName + ""的工作表不存在");
  489. }
  490. }
  491. catch(Exception e)
  492. {
  493. this.KillExcelProcess();
  494. throw e;
  495. }
  496. }
  497. /// <summary>
  498. /// 隐藏指定索引的工作表
  499. /// </summary>
  500. /// <param name="sheetIndex"></param>
  501. public void HiddenWorkSheet(int sheetIndex)
  502. {
  503. if(sheetIndex > this.WorkSheetCount)
  504. {
  505. this.KillExcelProcess();
  506. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  507. }
  508. try
  509. {
  510. Excel.Worksheet sheet = null;
  511. sheet = (Excel.Worksheet)workBook.Sheets.get_Item(sheetIndex);
  512. sheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
  513. }
  514. catch(Exception e)
  515. {
  516. this.KillExcelProcess();
  517. throw e;
  518. }
  519. }
  520. /// <summary>
  521. /// 在指定名称的工作表后面拷贝指定个数的该工作表的副本,并重命名
  522. /// </summary>
  523. /// <param name="sheetName">工作表名称</param>
  524. /// <param name="sheetCount">工作表个数</param>
  525. public void CopyWorkSheets(string sheetName,int sheetCount)
  526. {
  527. try
  528. {
  529. Excel.Worksheet sheet = null;
  530. int sheetIndex = 0;
  531. for(int i=1;i<=this.WorkSheetCount;i++)
  532. {
  533. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
  534. if(workSheet.Name == sheetName)
  535. {
  536. sheet = workSheet;
  537. sheetIndex = workSheet.Index;
  538. }
  539. }
  540. if(sheet != null)
  541. {
  542. for(int i=sheetCount;i>=1;i--)
  543. {
  544. sheet.Copy(this.missing,sheet);
  545. }
  546. //重命名
  547. for(int i=sheetIndex;i<=sheetIndex+sheetCount;i++)
  548. {
  549. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
  550. workSheet.Name = sheetName + "-" + Convert.ToString(i - sheetIndex + 1);
  551. }
  552. }
  553. else
  554. {
  555. this.KillExcelProcess();
  556. throw new Exception("名称为"" + sheetName + ""的工作表不存在");
  557. }
  558. }
  559. catch(Exception e)
  560. {
  561. this.KillExcelProcess();
  562. throw e;
  563. }
  564. }
  565. /// <summary>
  566. /// 将一个工作表拷贝到另一个工作表后面,并重命名
  567. /// </summary>
  568. /// <param name="srcSheetIndex">拷贝源工作表索引</param>
  569. /// <param name="aimSheetIndex">参照位置工作表索引,新工作表拷贝在该工作表后面</param>
  570. /// <param name="newSheetName"></param>
  571. public void CopyWorkSheet(int srcSheetIndex,int aimSheetIndex,string newSheetName)
  572. {
  573. if(srcSheetIndex > this.WorkSheetCount || aimSheetIndex > this.WorkSheetCount)
  574. {
  575. this.KillExcelProcess();
  576. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  577. }
  578. try
  579. {
  580. Excel.Worksheet srcSheet = (Excel.Worksheet)workBook.Sheets.get_Item(srcSheetIndex);
  581. Excel.Worksheet aimSheet = (Excel.Worksheet)workBook.Sheets.get_Item(aimSheetIndex);
  582. srcSheet.Copy(this.missing,aimSheet);
  583. //重命名
  584. workSheet = (Excel.Worksheet)aimSheet.Next; //获取新拷贝的工作表
  585. workSheet.Name = newSheetName;
  586. }
  587. catch(Exception e)
  588. {
  589. this.KillExcelProcess();
  590. throw e;
  591. }
  592. }
  593. /// <summary>
  594. /// 根据名称删除工作表
  595. /// </summary>
  596. /// <param name="sheetName"></param>
  597. public void DeleteWorkSheet(string sheetName)
  598. {
  599. try
  600. {
  601. Excel.Worksheet sheet = null;
  602. //找到名称位sheetName的工作表
  603. for(int i=1;i<=this.WorkSheetCount;i++)
  604. {
  605. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
  606. if(workSheet.Name == sheetName)
  607. {
  608. sheet = workSheet;
  609. }
  610. }
  611. if(sheet != null)
  612. {
  613. sheet.Delete();
  614. }
  615. else
  616. {
  617. this.KillExcelProcess();
  618. throw new Exception("名称为"" + sheetName + ""的工作表不存在");
  619. }
  620. }
  621. catch(Exception e)
  622. {
  623. this.KillExcelProcess();
  624. throw e;
  625. }
  626. }
  627. /// <summary>
  628. /// 根据索引删除工作表
  629. /// </summary>
  630. /// <param name="sheetIndex"></param>
  631. public void DeleteWorkSheet(int sheetIndex)
  632. {
  633. if(sheetIndex > this.WorkSheetCount)
  634. {
  635. this.KillExcelProcess();
  636. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  637. }
  638. try
  639. {
  640. Excel.Worksheet sheet = null;
  641. sheet = (Excel.Worksheet)workBook.Sheets.get_Item(sheetIndex);
  642. sheet.Delete();
  643. }
  644. catch(Exception e)
  645. {
  646. this.KillExcelProcess();
  647. throw e;
  648. }
  649. }
  650. #endregion
  651. #region TextBox Methods
  652. /// <summary>
  653. /// 向指定文本框写入数据,对每个WorkSheet操作
  654. /// </summary>
  655. /// <param name="textboxName">文本框名称</param>
  656. /// <param name="text">要写入的文本</param>
  657. public void SetTextBox(string textboxName,string text)
  658. {
  659. for(int i=1;i<=this.WorkSheetCount;i++)
  660. {
  661. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  662. try
  663. {
  664. textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
  665. textBox.Text = text;
  666. }
  667. catch
  668. {
  669. this.KillExcelProcess();
  670. throw new Exception("不存在ID为"" + textboxName + ""的文本框!");
  671. }
  672. }
  673. }
  674. /// <summary>
  675. /// 向指定文本框写入数据,对指定WorkSheet操作
  676. /// </summary>
  677. /// <param name="sheetIndex">工作表索引</param>
  678. /// <param name="textboxName">文本框名称</param>
  679. /// <param name="text">要写入的文本</param>
  680. public void SetTextBox(int sheetIndex,string textboxName,string text)
  681. {
  682. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
  683. try
  684. {
  685. textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
  686. textBox.Text = text;
  687. }
  688. catch
  689. {
  690. this.KillExcelProcess();
  691. throw new Exception("不存在ID为"" + textboxName + ""的文本框!");
  692. }
  693. }
  694. /// <summary>
  695. /// 向文本框写入数据,对每个WorkSheet操作
  696. /// </summary>
  697. /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
  698. public void SetTextBoxes(Hashtable ht)
  699. {
  700. if(ht.Count == 0) return;
  701. for(int i=1;i<=this.WorkSheetCount;i++)
  702. {
  703. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  704. foreach(DictionaryEntry dic in ht)
  705. {
  706. try
  707. {
  708. textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
  709. textBox.Text = dic.Value.ToString();
  710. }
  711. catch
  712. {
  713. this.KillExcelProcess();
  714. throw new Exception("不存在ID为"" + dic.Key.ToString() + ""的文本框!");
  715. }
  716. }
  717. }
  718. }
  719. /// <summary>
  720. /// 向文本框写入数据,对指定WorkSheet操作
  721. /// </summary>
  722. /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
  723. public void SetTextBoxes(int sheetIndex,Hashtable ht)
  724. {
  725. if(ht.Count == 0) return;
  726. if(sheetIndex > this.WorkSheetCount)
  727. {
  728. this.KillExcelProcess();
  729. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  730. }
  731. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
  732. foreach(DictionaryEntry dic in ht)
  733. {
  734. try
  735. {
  736. textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
  737. textBox.Text = dic.Value.ToString();
  738. }
  739. catch
  740. {
  741. this.KillExcelProcess();
  742. throw new Exception("不存在ID为"" + dic.Key.ToString() + ""的文本框!");
  743. }
  744. }
  745. }
  746. #endregion
  747. #region Cell Methods
  748. /// <summary>
  749. /// 向单元格写入数据,对当前WorkSheet操作
  750. /// </summary>
  751. /// <param name="rowIndex">行索引</param>
  752. /// <param name="columnIndex">列索引</param>
  753. /// <param name="text">要写入的文本值</param>
  754. public void SetCells(int rowIndex,int columnIndex,string text)
  755. {
  756. try
  757. {
  758. workSheet.Cells[rowIndex,columnIndex] = text;
  759. }
  760. catch
  761. {
  762. this.KillExcelProcess();
  763. throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
  764. }
  765. }
  766. /// <summary>
  767. /// 向单元格写入数据,对指定WorkSheet操作
  768. /// </summary>
  769. /// <param name="sheetIndex">工作表索引</param>
  770. /// <param name="rowIndex">行索引</param>
  771. /// <param name="columnIndex">列索引</param>
  772. /// <param name="text">要写入的文本值</param>
  773. public void SetCells(int sheetIndex,int rowIndex,int columnIndex,string text)
  774. {
  775. try
  776. {
  777. this.ChangeCurrentWorkSheet(sheetIndex); //改变当前工作表为指定工作表
  778. workSheet.Cells[rowIndex,columnIndex] = text;
  779. }
  780. catch
  781. {
  782. this.KillExcelProcess();
  783. throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
  784. }
  785. }
  786. /// <summary>
  787. /// 向单元格写入数据,对每个WorkSheet操作
  788. /// </summary>
  789. /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
  790. public void SetCells(Hashtable ht)
  791. {
  792. int rowIndex;
  793. int columnIndex;
  794. string position;
  795. if(ht.Count == 0) return;
  796. for(int i=1;i<=this.WorkSheetCount;i++)
  797. {
  798. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  799. foreach(DictionaryEntry dic in ht)
  800. {
  801. try
  802. {
  803. position = dic.Key.ToString();
  804. rowIndex = Convert.ToInt32(position.Split(',')[0]);
  805. columnIndex = Convert.ToInt32(position.Split(',')[1]);
  806. workSheet.Cells[rowIndex,columnIndex] = dic.Value;
  807. }
  808. catch
  809. {
  810. this.KillExcelProcess();
  811. throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
  812. }
  813. }
  814. }
  815. }
  816. /// <summary>
  817. /// 向单元格写入数据,对指定WorkSheet操作
  818. /// </summary>
  819. /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
  820. public void SetCells(int sheetIndex,Hashtable ht)
  821. {
  822. int rowIndex;
  823. int columnIndex;
  824. string position;
  825. if(sheetIndex > this.WorkSheetCount)
  826. {
  827. this.KillExcelProcess();
  828. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  829. }
  830. if(ht.Count == 0) return;
  831. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
  832. foreach(DictionaryEntry dic in ht)
  833. {
  834. try
  835. {
  836. position = dic.Key.ToString();
  837. rowIndex = Convert.ToInt32(position.Split(',')[0]);
  838. columnIndex = Convert.ToInt32(position.Split(',')[1]);
  839. workSheet.Cells[rowIndex,columnIndex] = dic.Value;
  840. }
  841. catch
  842. {
  843. this.KillExcelProcess();
  844. throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
  845. }
  846. }
  847. }
  848. /// <summary>
  849. /// 设置单元格为可计算的
  850. /// </summary>
  851. /// <remarks>
  852. /// 如果Excel的单元格格式设置为数字,日期或者其他类型时,需要设置这些单元格的FormulaR1C1属性,
  853. /// 否则写到这些单元格的数据将不会按照预先设定的格式显示
  854. /// </remarks>
  855. /// <param name="arr">保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
  856. public void SetCells(int sheetIndex,string[] arr)
  857. {
  858. int rowIndex;
  859. int columnIndex;
  860. string position;
  861. if(sheetIndex > this.WorkSheetCount)
  862. {
  863. this.KillExcelProcess();
  864. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  865. }
  866. if(arr.Length == 0) return;
  867. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
  868. for(int i=0;i<arr.Length;i++)
  869. {
  870. try
  871. {
  872. position = arr[i];
  873. rowIndex = Convert.ToInt32(position.Split(',')[0]);
  874. columnIndex = Convert.ToInt32(position.Split(',')[1]);
  875. Excel.Range cell = (Excel.Range)workSheet.Cells[rowIndex,columnIndex];
  876. cell.FormulaR1C1 = cell.Text;
  877. }
  878. catch
  879. {
  880. this.KillExcelProcess();
  881. throw new Exception(string.Format("计算单元格{0}出错!",arr[i]));
  882. }
  883. }
  884. }
  885. /// <summary>
  886. /// 向单元格写入数据,对指定WorkSheet操作
  887. /// </summary>
  888. /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
  889. public void SetCells(string sheetName,Hashtable ht)
  890. {
  891. int rowIndex;
  892. int columnIndex;
  893. string position;
  894. Excel.Worksheet sheet = null;
  895. int sheetIndex = 0;
  896. if(ht.Count == 0) return;
  897. try
  898. {
  899. for(int i=1;i<=this.WorkSheetCount;i++)
  900. {
  901. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
  902. if(workSheet.Name == sheetName)
  903. {
  904. sheet = workSheet;
  905. sheetIndex = workSheet.Index;
  906. }
  907. }
  908. if(sheet != null)
  909. {
  910. foreach(DictionaryEntry dic in ht)
  911. {
  912. try
  913. {
  914. position = dic.Key.ToString();
  915. rowIndex = Convert.ToInt32(position.Split(',')[0]);
  916. columnIndex = Convert.ToInt32(position.Split(',')[1]);
  917. sheet.Cells[rowIndex,columnIndex] = dic.Value;
  918. }
  919. catch
  920. {
  921. this.KillExcelProcess();
  922. throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
  923. }
  924. }
  925. }
  926. else
  927. {
  928. this.KillExcelProcess();
  929. throw new Exception("名称为"" + sheetName + ""的工作表不存在");
  930. }
  931. }
  932. catch(Exception e)
  933. {
  934. this.KillExcelProcess();
  935. throw e;
  936. }
  937. }
  938. /// <summary>
  939. /// 合并单元格,并赋值,对每个WorkSheet操作
  940. /// </summary>
  941. /// <param name="beginRowIndex">开始行索引</param>
  942. /// <param name="beginColumnIndex">开始列索引</param>
  943. /// <param name="endRowIndex">结束行索引</param>
  944. /// <param name="endColumnIndex">结束列索引</param>
  945. /// <param name="text">合并后Range的值</param>
  946. public void MergeCells(int beginRowIndex,int beginColumnIndex,int endRowIndex,int endColumnIndex,string text)
  947. {
  948. for(int i=1;i<=this.WorkSheetCount;i++)
  949. {
  950. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  951. range = workSheet.get_Range(workSheet.Cells[beginRowIndex,beginColumnIndex],workSheet.Cells[endRowIndex,endColumnIndex]);
  952. range.ClearContents(); //先把Range内容清除,合并才不会出错
  953. range.MergeCells = true;
  954. range.Value = text;
  955. range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  956. range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
  957. }
  958. }
  959. /// <summary>
  960. /// 合并单元格,并赋值,对指定WorkSheet操作
  961. /// </summary>
  962. /// <param name="sheetIndex">WorkSheet索引</param>
  963. /// <param name="beginRowIndex">开始行索引</param>
  964. /// <param name="beginColumnIndex">开始列索引</param>
  965. /// <param name="endRowIndex">结束行索引</param>
  966. /// <param name="endColumnIndex">结束列索引</param>
  967. /// <param name="text">合并后Range的值</param>
  968. public void MergeCells(int sheetIndex,int beginRowIndex,int beginColumnIndex,int endRowIndex,int endColumnIndex,string text)
  969. {
  970. if(sheetIndex > this.WorkSheetCount)
  971. {
  972. this.KillExcelProcess();
  973. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  974. }
  975. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
  976. range = workSheet.get_Range(workSheet.Cells[beginRowIndex,beginColumnIndex],workSheet.Cells[endRowIndex,endColumnIndex]);
  977. range.ClearContents(); //先把Range内容清除,合并才不会出错
  978. range.MergeCells = true;
  979. range.Value = text;
  980. range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  981. range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
  982. }
  983. #endregion
  984. #region Row Methods
  985. /// <summary>
  986. /// 将指定索引列的数据相同的行合并,对每个WorkSheet操作
  987. /// </summary>
  988. /// <param name="columnIndex">列索引</param>
  989. /// <param name="beginRowIndex">开始行索引</param>
  990. /// <param name="endRowIndex">结束行索引</param>
  991. public void MergeRows(int columnIndex,int beginRowIndex,int endRowIndex)
  992. {
  993. if(endRowIndex - beginRowIndex < 1)
  994. return;
  995. for(int i=1;i<=this.WorkSheetCount;i++)
  996. {
  997. int beginIndex = beginRowIndex;
  998. int count = 0;
  999. string text1;
  1000. string text2;
  1001. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  1002. for(int j=beginRowIndex;j<=endRowIndex;j++)
  1003. {
  1004. range = (Excel.Range)workSheet.Cells[j,columnIndex];
  1005. text1 = range.Text.ToString();
  1006. range = (Excel.Range)workSheet.Cells[j+1,columnIndex];
  1007. text2 = range.Text.ToString();
  1008. if(text1 == text2)
  1009. {
  1010. ++count;
  1011. }
  1012. else
  1013. {
  1014. if(count > 0)
  1015. {
  1016. this.MergeCells(workSheet,beginIndex,columnIndex,beginIndex+count,columnIndex,text1);
  1017. }
  1018. beginIndex = j + 1; //设置开始合并行索引
  1019. count = 0; //计数器清0
  1020. }
  1021. }
  1022. }
  1023. }
  1024. /// <summary>
  1025. /// 将指定索引列的数据相同的行合并,对指定WorkSheet操作
  1026. /// </summary>
  1027. /// <param name="sheetIndex">WorkSheet索引</param>
  1028. /// <param name="columnIndex">列索引</param>
  1029. /// <param name="beginRowIndex">开始行索引</param>
  1030. /// <param name="endRowIndex">结束行索引</param>
  1031. public void MergeRows(int sheetIndex,int columnIndex,int beginRowIndex,int endRowIndex)
  1032. {
  1033. if(sheetIndex > this.WorkSheetCount)
  1034. {
  1035. this.KillExcelProcess();
  1036. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1037. }
  1038. if(endRowIndex - beginRowIndex < 1)
  1039. return;
  1040. int beginIndex = beginRowIndex;
  1041. int count = 0;
  1042. string text1;
  1043. string text2;
  1044. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
  1045. for(int j=beginRowIndex;j<=endRowIndex;j++)
  1046. {
  1047. range = (Excel.Range)workSheet.Cells[j,columnIndex];
  1048. text1 = range.Text.ToString();
  1049. range = (Excel.Range)workSheet.Cells[j+1,columnIndex];
  1050. text2 = range.Text.ToString();
  1051. if(text1 == text2)
  1052. {
  1053. ++count;
  1054. }
  1055. else
  1056. {
  1057. if(count > 0)
  1058. {
  1059. this.MergeCells(workSheet,beginIndex,columnIndex,beginIndex+count,columnIndex,text1);
  1060. }
  1061. beginIndex = j + 1; //设置开始合并行索引
  1062. count = 0; //计数器清0
  1063. }
  1064. }
  1065. }
  1066. /// <summary>
  1067. /// 插行(在指定行上面插入指定数量行)
  1068. /// </summary>
  1069. /// <param name="rowIndex"></param>
  1070. /// <param name="count"></param>
  1071. public void InsertRows(int rowIndex,int count)
  1072. {
  1073. try
  1074. {
  1075. for(int n=1;n<=this.WorkSheetCount;n++)
  1076. {
  1077. workSheet = (Excel.Worksheet)workBook.Worksheets[n];
  1078. range = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
  1079. for(int i=0;i<count;i++)
  1080. {
  1081. range.Insert(Excel.XlDirection.xlDown);
  1082. }
  1083. }
  1084. }
  1085. catch(Exception e)
  1086. {
  1087. this.KillExcelProcess();
  1088. throw e;
  1089. }
  1090. }
  1091. /// <summary>
  1092. /// 插行(在指定WorkSheet指定行上面插入指定数量行)
  1093. /// </summary>
  1094. /// <param name="sheetIndex"></param>
  1095. /// <param name="rowIndex"></param>
  1096. /// <param name="count"></param>
  1097. public void InsertRows(int sheetIndex,int rowIndex,int count)
  1098. {
  1099. if(sheetIndex > this.WorkSheetCount)
  1100. {
  1101. this.KillExcelProcess();
  1102. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1103. }
  1104. try
  1105. {
  1106. workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
  1107. range = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
  1108. for(int i=0;i<count;i++)
  1109. {
  1110. range.Insert(Excel.XlDirection.xlDown);
  1111. }
  1112. }
  1113. catch(Exception e)
  1114. {
  1115. this.KillExcelProcess();
  1116. throw e;
  1117. }
  1118. }
  1119. /// <summary>
  1120. /// 复制行(在指定行下面复制指定数量行)
  1121. /// </summary>
  1122. /// <param name="rowIndex"></param>
  1123. /// <param name="count"></param>
  1124. public void CopyRows(int rowIndex,int count)
  1125. {
  1126. try
  1127. {
  1128. for(int n=1;n<=this.WorkSheetCount;n++)
  1129. {
  1130. workSheet = (Excel.Worksheet)workBook.Worksheets[n];
  1131. range1 = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
  1132. for(int i=1;i<=count;i++)
  1133. {
  1134. range2 = (Excel.Range)workSheet.Rows[rowIndex + i,this.missing];
  1135. range1.Copy(range2);
  1136. }
  1137. }
  1138. }
  1139. catch(Exception e)
  1140. {
  1141. this.KillExcelProcess();
  1142. throw e;
  1143. }
  1144. }
  1145. /// <summary>
  1146. /// 复制行(在指定WorkSheet指定行下面复制指定数量行)
  1147. /// </summary>
  1148. /// <param name="sheetIndex"></param>
  1149. /// <param name="rowIndex"></param>
  1150. /// <param name="count"></param>
  1151. public void CopyRows(int sheetIndex,int rowIndex,int count)
  1152. {
  1153. if(sheetIndex > this.WorkSheetCount)
  1154. {
  1155. this.KillExcelProcess();
  1156. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1157. }
  1158. try
  1159. {
  1160. workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
  1161. range1 = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
  1162. for(int i=1;i<=count;i++)
  1163. {
  1164. range2 = (Excel.Range)workSheet.Rows[rowIndex + i,this.missing];
  1165. range1.Copy(range2);
  1166. }
  1167. }
  1168. catch(Exception e)
  1169. {
  1170. this.KillExcelProcess();
  1171. throw e;
  1172. }
  1173. }
  1174. /// <summary>
  1175. /// 删除行
  1176. /// </summary>
  1177. /// <param name="rowIndex"></param>
  1178. /// <param name="count"></param>
  1179. public void DeleteRows(int rowIndex,int count)
  1180. {
  1181. try
  1182. {
  1183. for(int n=1;n<=this.WorkSheetCount;n++)
  1184. {
  1185. workSheet = (Excel.Worksheet)workBook.Worksheets[n];
  1186. range = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
  1187. for(int i=0;i<count;i++)
  1188. {
  1189. range.Delete(Excel.XlDirection.xlDown);
  1190. }
  1191. }
  1192. }
  1193. catch(Exception e)
  1194. {
  1195. this.KillExcelProcess();
  1196. throw e;
  1197. }
  1198. }
  1199. /// <summary>
  1200. /// 删除行
  1201. /// </summary>
  1202. /// <param name="sheetIndex"></param>
  1203. /// <param name="rowIndex"></param>
  1204. /// <param name="count"></param>
  1205. public void DeleteRows(int sheetIndex,int rowIndex,int count)
  1206. {
  1207. if(sheetIndex > this.WorkSheetCount)
  1208. {
  1209. this.KillExcelProcess();
  1210. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1211. }
  1212. try
  1213. {
  1214. workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
  1215. range = (Excel.Range)workSheet.Rows[rowIndex,this.missing];
  1216. for(int i=0;i<count;i++)
  1217. {
  1218. range.Delete(Excel.XlDirection.xlDown);
  1219. }
  1220. }
  1221. catch(Exception e)
  1222. {
  1223. this.KillExcelProcess();
  1224. throw e;
  1225. }
  1226. }
  1227. #endregion
  1228. #region Column Methods
  1229. /// <summary>
  1230. /// 插列(在指定列右边插入指定数量列)
  1231. /// </summary>
  1232. /// <param name="columnIndex"></param>
  1233. /// <param name="count"></param>
  1234. public void InsertColumns(int columnIndex,int count)
  1235. {
  1236. try
  1237. {
  1238. for(int n=1;n<=this.WorkSheetCount;n++)
  1239. {
  1240. workSheet = (Excel.Worksheet)workBook.Worksheets[n];
  1241. range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
  1242. for(int i=0;i<count;i++)
  1243. {
  1244. range.Insert(Excel.XlDirection.xlDown);
  1245. }
  1246. }
  1247. }
  1248. catch(Exception e)
  1249. {
  1250. this.KillExcelProcess();
  1251. throw e;
  1252. }
  1253. }
  1254. /// <summary>
  1255. /// 插列(在指定WorkSheet指定列右边插入指定数量列)
  1256. /// </summary>
  1257. /// <param name="sheetIndex"></param>
  1258. /// <param name="columnIndex"></param>
  1259. /// <param name="count"></param>
  1260. public void InsertColumns(int sheetIndex,int columnIndex,int count)
  1261. {
  1262. if(sheetIndex > this.WorkSheetCount)
  1263. {
  1264. this.KillExcelProcess();
  1265. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1266. }
  1267. try
  1268. {
  1269. workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
  1270. range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
  1271. for(int i=0;i<count;i++)
  1272. {
  1273. range.Insert(Excel.XlDirection.xlDown);
  1274. }
  1275. }
  1276. catch(Exception e)
  1277. {
  1278. this.KillExcelProcess();
  1279. throw e;
  1280. }
  1281. }
  1282. /// <summary>
  1283. /// 复制列(在指定列右边复制指定数量列)
  1284. /// </summary>
  1285. /// <param name="columnIndex"></param>
  1286. /// <param name="count"></param>
  1287. public void CopyColumns(int columnIndex,int count)
  1288. {
  1289. try
  1290. {
  1291. for(int n=1;n<=this.WorkSheetCount;n++)
  1292. {
  1293. workSheet = (Excel.Worksheet)workBook.Worksheets[n];
  1294. // range1 = (Excel.Range)workSheet.Columns[columnIndex,this.missing];
  1295. range1 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex)+"1",this.IntToLetter(columnIndex)+"10000");
  1296. for(int i=1;i<=count;i++)
  1297. {
  1298. // range2 = (Excel.Range)workSheet.Columns[this.missing,columnIndex + i];
  1299. range2 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex+i)+"1",this.IntToLetter(columnIndex+i)+"10000");
  1300. range1.Copy(range2);
  1301. }
  1302. }
  1303. }
  1304. catch(Exception e)
  1305. {
  1306. this.KillExcelProcess();
  1307. throw e;
  1308. }
  1309. }
  1310. /// <summary>
  1311. /// 复制列(在指定WorkSheet指定列右边复制指定数量列)
  1312. /// </summary>
  1313. /// <param name="sheetIndex"></param>
  1314. /// <param name="columnIndex"></param>
  1315. /// <param name="count"></param>
  1316. public void CopyColumns(int sheetIndex,int columnIndex,int count)
  1317. {
  1318. if(sheetIndex > this.WorkSheetCount)
  1319. {
  1320. this.KillExcelProcess();
  1321. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1322. }
  1323. try
  1324. {
  1325. workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
  1326. // range1 = (Excel.Range)workSheet.Columns[Type.Missing,columnIndex];
  1327. range1 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex)+"1",this.IntToLetter(columnIndex)+"10000");
  1328. for(int i=1;i<=count;i++)
  1329. {
  1330. // range2 = (Excel.Range)workSheet.Columns[Type.Missing,columnIndex + i];
  1331. range2 = (Excel.Range)workSheet.get_Range(this.IntToLetter(columnIndex+i)+"1",this.IntToLetter(columnIndex+i)+"10000");
  1332. range1.Copy(range2);
  1333. }
  1334. }
  1335. catch(Exception e)
  1336. {
  1337. this.KillExcelProcess();
  1338. throw e;
  1339. }
  1340. }
  1341. /// <summary>
  1342. /// 删除列
  1343. /// </summary>
  1344. /// <param name="columnIndex"></param>
  1345. /// <param name="count"></param>
  1346. public void DeleteColumns(int columnIndex,int count)
  1347. {
  1348. try
  1349. {
  1350. for(int n=1;n<=this.WorkSheetCount;n++)
  1351. {
  1352. workSheet = (Excel.Worksheet)workBook.Worksheets[n];
  1353. range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
  1354. for(int i=0;i<count;i++)
  1355. {
  1356. range.Delete(Excel.XlDirection.xlDown);
  1357. }
  1358. }
  1359. }
  1360. catch(Exception e)
  1361. {
  1362. this.KillExcelProcess();
  1363. throw e;
  1364. }
  1365. }
  1366. /// <summary>
  1367. /// 删除列
  1368. /// </summary>
  1369. /// <param name="sheetIndex"></param>
  1370. /// <param name="columnIndex"></param>
  1371. /// <param name="count"></param>
  1372. public void DeleteColumns(int sheetIndex,int columnIndex,int count)
  1373. {
  1374. if(sheetIndex > this.WorkSheetCount)
  1375. {
  1376. this.KillExcelProcess();
  1377. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1378. }
  1379. try
  1380. {
  1381. workSheet = (Excel.Worksheet)workBook.Worksheets[sheetIndex];
  1382. range = (Excel.Range)workSheet.Columns[this.missing,columnIndex];
  1383. for(int i=0;i<count;i++)
  1384. {
  1385. range.Delete(Excel.XlDirection.xlDown);
  1386. }
  1387. }
  1388. catch(Exception e)
  1389. {
  1390. this.KillExcelProcess();
  1391. throw e;
  1392. }
  1393. }
  1394. #endregion
  1395. #region Range Methods
  1396. /// <summary>
  1397. /// 将指定范围区域拷贝到目标区域
  1398. /// </summary>
  1399. /// <param name="sheetIndex">WorkSheet索引</param>
  1400. /// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param>
  1401. /// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param>
  1402. /// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param>
  1403. public void RangeCopy(int sheetIndex,string startCell,string endCell,string targetCell)
  1404. {
  1405. if(sheetIndex > this.WorkSheetCount)
  1406. {
  1407. this.KillExcelProcess();
  1408. throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
  1409. }
  1410. try
  1411. {
  1412. workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
  1413. range1 = workSheet.get_Range(startCell,endCell);
  1414. range2 = workSheet.get_Range(targetCell,this.missing);
  1415. range1.Copy(range2);
  1416. }
  1417. catch(Exception e)
  1418. {
  1419. this.KillExcelProcess();
  1420. throw e;
  1421. }
  1422. }
  1423. /// <summary>
  1424. /// 将指定范围区域拷贝到目标区域
  1425. /// </summary>
  1426. /// <param name="sheetName">WorkSheet名称</param>
  1427. /// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param>
  1428. /// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param>
  1429. /// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param>
  1430. public void RangeCopy(string sheetName,string startCell,string endCell,string targetCell)
  1431. {
  1432. try
  1433. {
  1434. Excel.Worksheet sheet = null;
  1435. for(int i=1;i<=this.WorkSheetCount;i++)
  1436. {
  1437. workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
  1438. if(workSheet.Name == sheetName)
  1439. {
  1440. sheet = workSheet;
  1441. }
  1442. }
  1443. if(sheet != null)
  1444. {
  1445. for(int i=sheetCount;i>=1;i--)
  1446. {
  1447. range1 = sheet.get_Range(startCell,endCell);
  1448. range2 = sheet.get_Range(targetCell,this.missing);
  1449. range1.Copy(range2);
  1450. }
  1451. }
  1452. else
  1453. {
  1454. this.KillExcelProcess();
  1455. throw new Exception("名称为"" + sheetName + ""的工作表不存在");
  1456. }
  1457. }
  1458. catch(Exception e)
  1459. {
  1460. this.KillExcelProcess();
  1461. throw e;
  1462. }
  1463. }
  1464. /// <summary>
  1465. /// 自动填充
  1466. /// </summary>
  1467. public void RangAutoFill()
  1468. {
  1469. Excel.Range rng = workSheet.get_Range("B4", Type.Missing); 
  1470. rng.Value2 = "星期一 ";
  1471. rng.AutoFill(workSheet.get_Range("B4","B9"),
  1472. Excel.XlAutoFillType.xlFillWeekdays); 
  1473. rng = workSheet.get_Range("C4", Type.Missing); 
  1474. rng.Value = "一月";
  1475. rng.AutoFill(workSheet.get_Range("C4","C9"),
  1476. Excel.XlAutoFillType.xlFillMonths); 
  1477. rng = workSheet.get_Range("D4",Type.Missing);
  1478. rng.Value2 = "1";
  1479. rng.AutoFill(workSheet.get_Range("D4","D9"), 
  1480. Excel.XlAutoFillType.xlFillSeries);
  1481. rng = workSheet.get_Range("E4",Type.Missing); 
  1482. rng.Value2 = "3";
  1483. rng = workSheet.get_Range("E5",Type.Missing);
  1484. rng.Value2 = "6";
  1485. rng = workSheet.get_Range("E4","E5");
  1486. rng.AutoFill(workSheet.get_Range("E4","E9"), 
  1487. Excel.XlAutoFillType.xlFillSeries);
  1488. }
  1489. /// <summary>
  1490. /// 应用样式
  1491. /// </summary>
  1492. public void ApplyStyle()
  1493. {
  1494. object missingValue = Type.Missing;
  1495. Excel.Range rng = workSheet.get_Range("B3","L23");
  1496. Excel.Style style;
  1497. try
  1498. {
  1499. style = workBook.Styles["NewStyle"];
  1500. }
  1501. // Style doesn't exist yet.
  1502. catch
  1503. {
  1504. style = workBook.Styles.Add("NewStyle", missingValue);
  1505. style.Font.Name = "Verdana";
  1506. style.Font.Size = 12;
  1507. style.Font.Color = 255;
  1508. style.Interior.Color = (200 << 16) | (200 << 8) | 200;
  1509. style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
  1510. }
  1511. rng.Value2 = "'Style Test";
  1512. rng.Style = "NewStyle";
  1513. rng.Columns.AutoFit();
  1514. }
  1515. #endregion
  1516. #region ExcelHelper Kit
  1517. /// <summary>
  1518. /// 将Excel列的字母索引值转换成整数索引值
  1519. /// </summary>
  1520. /// <param name="letter"></param>
  1521. /// <returns></returns>
  1522. public int LetterToInt(string letter)
  1523. {
  1524. int n = 0;
  1525. if(letter.Trim().Length == 0)
  1526. throw new Exception("不接受空字符串!");
  1527. if(letter.Length >= 2)
  1528. {
  1529. char c1 = letter.ToCharArray(0,2)[0];
  1530. char c2 = letter.ToCharArray(0,2)[1];
  1531. if(!char.IsLetter(c1) || !char.IsLetter(c2))
  1532. {
  1533. throw new Exception("格式不正确,必须是字母!");
  1534. }
  1535. c1 = char.ToUpper(c1);
  1536. c2 = char.ToUpper(c2);
  1537. int i = Convert.ToInt32(c1) - 64;
  1538. int j = Convert.ToInt32(c2) - 64;
  1539. n = i * 26 + j;
  1540. }
  1541. if(letter.Length == 1)
  1542. {
  1543. char c1 = letter.ToCharArray()[0];
  1544. if(!char.IsLetter(c1))
  1545. {
  1546. throw new Exception("格式不正确,必须是字母!");
  1547. }
  1548. c1 = char.ToUpper(c1);
  1549. n = Convert.ToInt32(c1) - 64;
  1550. }
  1551. if(n > 256)
  1552. throw new Exception("索引超出范围,Excel的列索引不能超过256!");
  1553. return n;
  1554. }
  1555. /// <summary>
  1556. /// 将Excel列的整数索引值转换为字符索引值
  1557. /// </summary>
  1558. /// <param name="n"></param>
  1559. /// <returns></returns>
  1560. public string IntToLetter(int n)
  1561. {
  1562. if(n > 256)
  1563. throw new Exception("索引超出范围,Excel的列索引不能超过256!");
  1564. int i = Convert.ToInt32(n / 26);
  1565. int j = n % 26;
  1566. char c1 = Convert.ToChar( i + 64 );
  1567. char c2 = Convert.ToChar( j + 64 );
  1568. if(n > 26)
  1569. return c1.ToString() + c2.ToString();
  1570. else if(n == 26)
  1571. return "Z";
  1572. else
  1573. return c2.ToString();
  1574. }
  1575. #endregion
  1576. #region Output File(注意:如果目标文件已存在的话会出错)
  1577. /// <summary>
  1578. /// 输出Excel文件并退出
  1579. /// </summary>
  1580. public void OutputExcelFile()
  1581. {
  1582. if(this.outputFile == null)
  1583. throw new Exception("没有指定输出文件路径!");
  1584. try
  1585. {
  1586. workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1587. }
  1588. catch(Exception e)
  1589. {
  1590. throw e;
  1591. }
  1592. finally
  1593. {
  1594. this.Dispose();
  1595. }
  1596. }
  1597. /// <summary>
  1598. /// 输出指定格式的文件(支持格式:HTML,CSV,TEXT,EXCEL)
  1599. /// </summary>
  1600. /// <param name="format">HTML,CSV,TEXT,EXCEL,XML</param>
  1601. public void OutputFile(string format)
  1602. {
  1603. if(this.outputFile == null)
  1604. throw new Exception("没有指定输出文件路径!");
  1605. try
  1606. {
  1607. switch(format)
  1608. {
  1609. case "HTML":
  1610. {
  1611. workBook.SaveAs(outputFile,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1612. break;
  1613. }
  1614. case "CSV":
  1615. {
  1616. workBook.SaveAs(outputFile,Excel.XlFileFormat.xlCSV,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1617. break;
  1618. }
  1619. case "TEXT":
  1620. {
  1621. workBook.SaveAs(outputFile,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1622. break;
  1623. }
  1624. // case "XML":
  1625. // {
  1626. // workBook.SaveAs(outputFile,Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
  1627. // Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
  1628. // Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  1629. // break;
  1630. //
  1631. // }
  1632. default:
  1633. {
  1634. workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1635. break;
  1636. }
  1637. }
  1638. }
  1639. catch(Exception e)
  1640. {
  1641. throw e;
  1642. }
  1643. finally
  1644. {
  1645. this.Dispose();
  1646. }
  1647. }
  1648. /// <summary>
  1649. /// 保存文件
  1650. /// </summary>
  1651. public void SaveFile()
  1652. {
  1653. try
  1654. {
  1655. workBook.Save();
  1656. }
  1657. catch(Exception e)
  1658. {
  1659. throw e;
  1660. }
  1661. finally
  1662. {
  1663. this.Dispose();
  1664. }
  1665. }
  1666. /// <summary>
  1667. /// 另存文件
  1668. /// </summary>
  1669. public void SaveAsFile()
  1670. {
  1671. if(this.outputFile == null)
  1672. throw new Exception("没有指定输出文件路径!");
  1673. try
  1674. {
  1675. workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1676. }
  1677. catch(Exception e)
  1678. {
  1679. throw e;
  1680. }
  1681. finally
  1682. {
  1683. this.Dispose();
  1684. }
  1685. }
  1686. /// <summary>
  1687. /// 将Excel文件另存为指定格式
  1688. /// </summary>
  1689. /// <param name="format">HTML,CSV,TEXT,EXCEL,XML</param>
  1690. public void SaveAsFile(string format)
  1691. {
  1692. if(this.outputFile == null)
  1693. throw new Exception("没有指定输出文件路径!");
  1694. try
  1695. {
  1696. switch(format)
  1697. {
  1698. case "HTML":
  1699. {
  1700. workBook.SaveAs(outputFile,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1701. break;
  1702. }
  1703. case "CSV":
  1704. {
  1705. workBook.SaveAs(outputFile,Excel.XlFileFormat.xlCSV,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1706. break;
  1707. }
  1708. case "TEXT":
  1709. {
  1710. workBook.SaveAs(outputFile,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1711. break;
  1712. }
  1713. // case "XML":
  1714. // {
  1715. // workBook.SaveAs(outputFile,Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
  1716. // Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
  1717. // Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  1718. // break;
  1719. // }
  1720. default:
  1721. {
  1722. workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1723. break;
  1724. }
  1725. }
  1726. }
  1727. catch(Exception e)
  1728. {
  1729. throw e;
  1730. }
  1731. finally
  1732. {
  1733. this.Dispose();
  1734. }
  1735. }
  1736. /// <summary>
  1737. /// 另存文件
  1738. /// </summary>
  1739. /// <param name="fileName">文件名</param>
  1740. public void SaveFile(string fileName)
  1741. {
  1742. try
  1743. {
  1744. workBook.SaveAs(fileName,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1745. }
  1746. catch(Exception e)
  1747. {
  1748. throw e;
  1749. }
  1750. finally
  1751. {
  1752. this.Dispose();
  1753. }
  1754. }
  1755. /// <summary>
  1756. /// 将Excel文件另存为指定格式
  1757. /// </summary>
  1758. /// <param name="fileName">文件名</param>
  1759. /// <param name="format">HTML,CSV,TEXT,EXCEL,XML</param>
  1760. public void SaveAsFile(string fileName,string format)
  1761. {
  1762. try
  1763. {
  1764. switch(format)
  1765. {
  1766. case "HTML":
  1767. {
  1768. workBook.SaveAs(fileName,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1769. break;
  1770. }
  1771. case "CSV":
  1772. {
  1773. workBook.SaveAs(fileName,Excel.XlFileFormat.xlCSV,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1774. break;
  1775. }
  1776. case "TEXT":
  1777. {
  1778. workBook.SaveAs(fileName,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1779. break;
  1780. }
  1781. // case "XML":
  1782. // {
  1783. // workBook.SaveAs(fileName,Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
  1784. // Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
  1785. // Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  1786. // break;
  1787. // }
  1788. default:
  1789. {
  1790. workBook.SaveAs(fileName,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
  1791. break;
  1792. }
  1793. }
  1794. }
  1795. catch(Exception e)
  1796. {
  1797. throw e;
  1798. }
  1799. finally
  1800. {
  1801. this.Dispose();
  1802. }
  1803. }
  1804. #endregion
  1805. #endregion
  1806. #region 私有方法
  1807. /// <summary>
  1808. /// 合并单元格,并赋值,对指定WorkSheet操作
  1809. /// </summary>
  1810. /// <param name="beginRowIndex">开始行索引</param>
  1811. /// <param name="beginColumnIndex">开始列索引</param>
  1812. /// <param name="endRowIndex">结束行索引</param>
  1813. /// <param name="endColumnIndex">结束列索引</param>
  1814. /// <param name="text">合并后Range的值</param>
  1815. private void MergeCells(Excel.Worksheet sheet,int beginRowIndex,int beginColumnIndex,int endRowIndex,int endColumnIndex,string text)
  1816. {
  1817. if(sheet == null)
  1818. return;
  1819. range = sheet.get_Range(sheet.Cells[beginRowIndex,beginColumnIndex],sheet.Cells[endRowIndex,endColumnIndex]);
  1820. range.ClearContents(); //先把Range内容清除,合并才不会出错
  1821. range.MergeCells = true;
  1822. range.Value = text;
  1823. range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  1824. range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
  1825. }
  1826. /// <summary>
  1827. /// 将指定索引列的数据相同的行合并,对指定WorkSheet操作
  1828. /// </summary>
  1829. /// <param name="columnIndex">要合并的列索引</param>
  1830. /// <param name="beginRowIndex">合并开始行索引</param>
  1831. /// <param name="rows">要合并的行数</param>
  1832. private void MergeRows(Excel.Worksheet sheet,int columnIndex,int beginRowIndex,int rows)
  1833. {
  1834. int beginIndex = beginRowIndex;
  1835. int count = 0;
  1836. string text1;
  1837. string text2;
  1838. if(sheet == null)
  1839. return;
  1840. for(int j=beginRowIndex;j<beginRowIndex+rows;j++)
  1841. {
  1842. range1 = (Excel.Range)sheet.Cells[j,columnIndex];
  1843. range2 = (Excel.Range)sheet.Cells[j+1,columnIndex];
  1844. text1 = range1.Text.ToString();
  1845. text2 = range2.Text.ToString();
  1846. if(text1 == text2)
  1847. {
  1848. ++count;
  1849. }
  1850. else
  1851. {
  1852. if(count > 0)
  1853. {
  1854. this.MergeCells(sheet,beginIndex,columnIndex,beginIndex+count,columnIndex,text1);
  1855. }
  1856. beginIndex = j + 1; //设置开始合并行索引
  1857. count = 0; //计数器清0
  1858. }
  1859. }
  1860. }
  1861. /// <summary>
  1862. /// 计算WorkSheet数量
  1863. /// </summary>
  1864. /// <param name="rowCount">记录总行数</param>
  1865. /// <param name="rows">每WorkSheet行数</param>
  1866. public int GetSheetCount(int rowCount,int rows)
  1867. {
  1868. int n = rowCount % rows; //余数
  1869. if(n == 0)
  1870. return rowCount / rows;
  1871. else
  1872. return Convert.ToInt32(rowCount / rows) + 1;
  1873. }
  1874. /// <summary>
  1875. /// 结束Excel进程
  1876. /// </summary>
  1877. public void KillExcelProcess()
  1878. {
  1879. Process[] myProcesses;
  1880. DateTime startTime;
  1881. myProcesses = Process.GetProcessesByName("Excel");
  1882. //得不到Excel进程ID,暂时只能判断进程启动时间
  1883. foreach(Process myProcess in myProcesses)
  1884. {
  1885. startTime = myProcess.StartTime;
  1886. if(startTime > beforeTime && startTime < afterTime)
  1887. {
  1888. myProcess.Kill();
  1889. }
  1890. }
  1891. }
  1892.         private void Dispose()
  1893. {
  1894. workBook.Close(null,null,null);
  1895. app.Workbooks.Close();
  1896. app.Quit();
  1897. if(range != null)
  1898. {
  1899. System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
  1900. range = null;
  1901. }
  1902. if(range1 != null)
  1903. {
  1904. System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
  1905. range1 = null;
  1906. }
  1907. if(range2 != null)
  1908. {
  1909. System.Runtime.InteropServices.Marshal.ReleaseComObject(range2);
  1910. range2 = null;
  1911. }
  1912. if(textBox != null)
  1913. {
  1914. System.Runtime.InteropServices.Marshal.ReleaseComObject(textBox);
  1915. textBox = null;
  1916. }
  1917. if(workSheet != null)
  1918. {
  1919. System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
  1920. workSheet = null;
  1921. }
  1922. if(workBook != null)
  1923. {
  1924. System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
  1925. workBook = null;
  1926. }
  1927. if(app != null)
  1928. {
  1929. System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
  1930. app = null;
  1931. }
  1932. GC.Collect();
  1933. this.KillExcelProcess();
  1934. }
  1935. #endregion
  1936.     }
  1937. }