ExcelFly.cs
上传用户:tiancihang
上传日期:2014-03-12
资源大小:21387k
文件大小:64k
源码类别:

.net编程

开发平台:

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