AsposeExcelTools.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. using Aspose.Cells.Drawing;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms.VisualStyles;
  11. namespace ProjectBase.Controls.Others
  12. {
  13. internal class AsposeExcelTools
  14. {
  15. /// <summary>
  16. /// DataTabel转换成Excel文件
  17. /// </summary>
  18. /// <param name="datatable">DataTable对象</param>
  19. /// <param name="colNameList">返回的字段列表</param>
  20. /// <param name="fromfile">Excel文件的全路径</param>
  21. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  22. /// <param name="beginColumn">开始列的序号</param>
  23. /// <param name="beginRow">开始的行序号</param>
  24. /// <returns>true:函数正确执行 false:函数执行错误</returns>
  25. public static bool DataTableInsertToExcel(DataTable datatable, ArrayList colNameList, string fromfile, out string error, int beginRow, int beginColumn)
  26. {
  27. error = "";
  28. if (datatable == null)
  29. {
  30. error = "DataTableToExcel:datatable 为空";
  31. return false;
  32. }
  33. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(fromfile);
  34. Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
  35. Aspose.Cells.Cells cells = sheet.Cells;
  36. //-------------插入数据-------------
  37. int nRow = 0;
  38. foreach (DataRow row in datatable.Rows)
  39. {
  40. nRow++;
  41. try
  42. {
  43. cells.InsertRow(beginRow);
  44. for (int i = 0; i < colNameList.Count; i++)
  45. {
  46. string colName = colNameList[i].ToString();
  47. for (int j = 0; j < datatable.Columns.Count; j++)
  48. {
  49. if (colName == datatable.Columns[j].ColumnName)
  50. {
  51. object temp = row[datatable.Columns[j].ColumnName];
  52. cells[beginRow, beginColumn + i].PutValue(row[datatable.Columns[j].ColumnName]);
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. catch (System.Exception e)
  59. {
  60. error = error + " DataTableInsertToExcel: " + e.Message;
  61. }
  62. }
  63. //-------------保存-------------
  64. workbook.Save(fromfile);
  65. return true;
  66. }
  67. /// <summary>
  68. /// 把DataTabel转换成Excel文件
  69. /// </summary>
  70. /// <param name="datatable">DataTable对象</param>
  71. /// <param name="filepath">目标文件路径,Excel文件的全路径</param>
  72. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  73. /// <returns></returns>
  74. public static bool DataTableToExcel(DataTable datatable, string filepath, out string error)
  75. {
  76. error = "";
  77. try
  78. {
  79. if (datatable == null)
  80. {
  81. error = "DataTableToExcel:datatable 为空";
  82. return false;
  83. }
  84. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
  85. Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
  86. Aspose.Cells.Cells cells = sheet.Cells;
  87. int nRow = 0;
  88. foreach (DataRow row in datatable.Rows)
  89. {
  90. nRow++;
  91. try
  92. {
  93. for (int i = 0; i < datatable.Columns.Count; i++)
  94. {
  95. if (row[i].GetType().ToString() == "System.Drawing.Bitmap")
  96. {
  97. //------插入图片数据-------
  98. System.Drawing.Image image = (System.Drawing.Image)row[i];
  99. MemoryStream mstream = new MemoryStream();
  100. image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
  101. sheet.Pictures.Add(nRow, i, mstream);
  102. }
  103. else
  104. {
  105. cells[nRow, i].PutValue(row[i]);
  106. }
  107. }
  108. }
  109. catch (System.Exception e)
  110. {
  111. error = error + " DataTableToExcel: " + e.Message;
  112. }
  113. }
  114. workbook.Save(filepath);
  115. return true;
  116. }
  117. catch (System.Exception e)
  118. {
  119. error = error + " DataTableToExcel: " + e.Message;
  120. return false;
  121. }
  122. }
  123. /// <summary>
  124. /// 把DataTabel转换成Excel文件
  125. /// </summary>
  126. /// <param name="datatable">DataTable对象</param>
  127. /// <param name="filepath">目标文件路径,Excel文件的全路径</param>
  128. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  129. /// <returns></returns>
  130. public static bool DataTableToExcel2(DataTable datatable, string filepath, out string error)
  131. {
  132. error = "";
  133. Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
  134. try
  135. {
  136. if (datatable == null)
  137. {
  138. error = "DataTableToExcel:datatable 为空";
  139. return false;
  140. }
  141. //为单元格添加样式
  142. Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()];
  143. //设置居中
  144. style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
  145. //设置背景颜色
  146. style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
  147. style.Pattern = Aspose.Cells.BackgroundType.Solid;
  148. style.Font.IsBold = true;
  149. int rowIndex = 0;
  150. for (int i = 0; i < datatable.Columns.Count; i++)
  151. {
  152. DataColumn col = datatable.Columns[i];
  153. string columnName = col.Caption ?? col.ColumnName;
  154. wb.Worksheets[0].Cells[rowIndex, i].PutValue(columnName);
  155. wb.Worksheets[0].Cells[rowIndex, i].SetStyle(style);
  156. }
  157. rowIndex++;
  158. foreach (DataRow row in datatable.Rows)
  159. {
  160. for (int i = 0; i < datatable.Columns.Count; i++)
  161. {
  162. wb.Worksheets[0].Cells[rowIndex, i].PutValue(row[i].ToString());
  163. }
  164. rowIndex++;
  165. }
  166. for (int k = 0; k < datatable.Columns.Count; k++)
  167. {
  168. wb.Worksheets[0].AutoFitColumn(k, 0, 150);
  169. }
  170. wb.Worksheets[0].FreezePanes(1, 0, 1, datatable.Columns.Count);
  171. wb.Save(filepath);
  172. return true;
  173. }
  174. catch (Exception e)
  175. {
  176. error = error + " DataTableToExcel: " + e.Message;
  177. return false;
  178. }
  179. }
  180. /// <summary>
  181. /// DataSet导出到Excel文件
  182. /// 默认导出第一页的数据
  183. /// </summary>
  184. /// <param name="dataset">DataSet</param>
  185. /// <param name="filepath">Excel文件的全路径</param>
  186. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  187. /// <returns>true:函数正确执行 false:函数执行错误</returns>
  188. public static bool DataSetToExcel(DataSet dataset, string filepath, out string error)
  189. {
  190. if (DataTableToExcel(dataset.Tables[0], filepath, out error))
  191. {
  192. return true;
  193. }
  194. else
  195. {
  196. error = "DataTableToExcel: " + error;
  197. return false;
  198. }
  199. }
  200. /// <summary>
  201. /// Excel文件转换为DataTable(第一个sheet)
  202. /// </summary>
  203. /// <param name="filepath">Excel文件的全路径</param>
  204. /// <param name="datatable">DataTable:返回值</param>
  205. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  206. /// <returns>true:函数正确执行 false:函数执行错误</returns>
  207. public static bool ExcelFileToDataTable(string filepath, out DataTable datatable, out string error)
  208. {
  209. bool exportColumnName = true;
  210. return ExcelFileToDataTable(filepath, out datatable, exportColumnName, out error);
  211. }
  212. /// <summary>
  213. /// Excel文件转换为DataTable.
  214. /// </summary>
  215. /// <param name="filepath">Excel文件的全路径</param>
  216. /// <param name="datatable">DataTable:返回值</param>
  217. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  218. /// <returns>true:函数正确执行 false:函数执行错误</returns>
  219. public static bool ExcelFileToDataTable(string filepath, out DataTable datatable, bool exportColumnName, out string error)
  220. {
  221. error = "";
  222. datatable = null;
  223. try
  224. {
  225. if (File.Exists(filepath) == false)
  226. {
  227. error = "文件不存在";
  228. datatable = null;
  229. return false;
  230. }
  231. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filepath);
  232. Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];
  233. datatable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, exportColumnName);
  234. //-------------图片处理-------------
  235. Aspose.Cells.Drawing.PictureCollection pictures = worksheet.Pictures;
  236. if (pictures.Count > 0)
  237. {
  238. string error2 = "";
  239. if (InsertPicturesIntoDataTable(pictures, datatable, out datatable, out error2) == false)
  240. {
  241. error = error + error2;
  242. }
  243. }
  244. return true;
  245. }
  246. catch (System.Exception e)
  247. {
  248. error = e.Message;
  249. return false;
  250. }
  251. }
  252. /// <summary>
  253. /// 把所有Sheet里面的内容,分别导入到不同的DataTable对象里面
  254. /// </summary>
  255. /// <param name="filepath">Excel文件的全路径</param>
  256. /// <param name="datatables">DataTable对象集合</param>
  257. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  258. /// <returns></returns>
  259. public static bool ExcelFileToDataTables(string filepath, out DataTable[] datatables, out string error)
  260. {
  261. bool exportColumnName = true;
  262. return ExcelFileToDataTables(filepath, out datatables, exportColumnName, out error);
  263. }
  264. /// <summary>
  265. /// 把所有Sheet里面的内容,分别导入到不同的DataTable对象里面
  266. /// </summary>
  267. /// <param name="filepath">Excel文件的全路径</param>
  268. /// <param name="datatables">DataTable对象集合</param>
  269. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  270. /// <returns></returns>
  271. public static bool ExcelFileToDataTables(string filepath, out DataTable[] datatables, bool exportColumnName, out string error)
  272. {
  273. error = "";
  274. datatables = null;
  275. int nSheetsCount = 0;
  276. try
  277. {
  278. if (File.Exists(filepath) == false)
  279. {
  280. error = "文件不存在";
  281. datatables = null;
  282. return false;
  283. }
  284. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filepath);
  285. nSheetsCount = workbook.Worksheets.Count;
  286. datatables = new DataTable[nSheetsCount];
  287. for (int i = 0; i < nSheetsCount; i++)
  288. {
  289. Aspose.Cells.Worksheet worksheet = workbook.Worksheets[i];
  290. datatables[i] = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, exportColumnName);
  291. //-------------图片处理-------------
  292. Aspose.Cells.Drawing.PictureCollection pictures = worksheet.Pictures;
  293. if (pictures.Count > 0)
  294. {
  295. string error2 = "";
  296. if (InsertPicturesIntoDataTable(pictures, datatables[i], out datatables[i], out error2) == false)
  297. {
  298. error = error + error2;
  299. }
  300. }
  301. }
  302. return true;
  303. }
  304. catch (System.Exception e)
  305. {
  306. error = e.Message;
  307. return false;
  308. }
  309. }
  310. /// <summary>
  311. /// Excel文件转换为DataTable.(指定开始行列,以及导入的记录行数)
  312. /// </summary>
  313. /// <param name="filepath">Excel文件的全路径</param>
  314. /// <param name="datatable">DataTable:返回值</param>
  315. /// <param name="iFirstRow">起始行</param>
  316. /// <param name="iFirstCol">起始列</param>
  317. /// <param name="rowNum">导入行数</param>
  318. /// <param name="colNum">列数</param>
  319. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  320. /// <returns></returns>
  321. public static bool ExcelFileToDataTable(string filepath, out DataTable datatable, int iFirstRow, int iFirstCol, int rowNum, int colNum, bool exportColumnName, out string error)
  322. {
  323. error = "";
  324. datatable = null;
  325. try
  326. {
  327. if (File.Exists(filepath) == false)
  328. {
  329. error = "文件不存在";
  330. datatable = null;
  331. return false;
  332. }
  333. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filepath);
  334. Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];
  335. datatable = worksheet.Cells.ExportDataTableAsString(iFirstRow, iFirstCol, rowNum + 1, colNum + 1, exportColumnName);
  336. return true;
  337. }
  338. catch (System.Exception e)
  339. {
  340. error = e.Message;
  341. return false;
  342. }
  343. }
  344. /// <summary>
  345. /// Excel文件转换为DataSet.
  346. /// </summary>
  347. /// <param name="filepath">Excel文件的全路径</param>
  348. /// <param name="dataset">DataSet:返回值</param>
  349. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  350. /// <returns>true:函数正确执行 false:函数执行错误</returns>
  351. public static bool ExcelFileToDataSet(string filepath, out DataSet dataset, out string error)
  352. {
  353. DataTable datatable = new DataTable();
  354. dataset = new System.Data.DataSet();
  355. if (ExcelFileToDataTable(filepath, out datatable, out error))
  356. {
  357. dataset.Tables.Add(datatable);
  358. return true;
  359. }
  360. else
  361. {
  362. error = "ExcelFileToDataSet: " + error;
  363. return false;
  364. }
  365. }
  366. /// <summary>
  367. /// 获取Excel文件里面的图片,并把图片存储到图片对象列表中
  368. /// </summary>
  369. /// <param name="filepath">Excel文件的全路径</param>
  370. /// <param name="pictures">图片对象列表</param>
  371. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  372. /// <returns></returns>
  373. public static bool GetPicturesFromExcelFile(string filepath, out PictureCollection[] pictures, out string error)
  374. {
  375. error = "";
  376. pictures = null;
  377. try
  378. {
  379. if (File.Exists(filepath) == false)
  380. {
  381. error = "文件不存在";
  382. pictures = null;
  383. return false;
  384. }
  385. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filepath);
  386. pictures = new Aspose.Cells.Drawing.PictureCollection[workbook.Worksheets.Count];
  387. for (int i = 0; i < workbook.Worksheets.Count; i++)
  388. {
  389. //pictures.Add();
  390. pictures[i] = workbook.Worksheets[i].Pictures;
  391. }
  392. return true;
  393. }
  394. catch (System.Exception e)
  395. {
  396. error = e.Message;
  397. return false;
  398. }
  399. }
  400. private static bool InsertPicturesIntoDataTable(Aspose.Cells.Drawing.PictureCollection pictures, DataTable fromdatatable, out DataTable datatable, out string error)
  401. {
  402. error = "";
  403. datatable = fromdatatable;
  404. //把图片按位置插入Table中
  405. DataRow[] rows = datatable.Select();
  406. foreach (Picture picture in pictures)
  407. {
  408. try
  409. {
  410. System.Console.WriteLine(picture.GetType().ToString());
  411. //----把图片转换成System.Drawing.Image----
  412. MemoryStream mstream = new MemoryStream();
  413. mstream.Write(picture.Data, 0, picture.Data.Length);
  414. System.Drawing.Image image = System.Drawing.Image.FromStream(mstream);
  415. //----Image放入DataTable------
  416. //datatable.Columns[picture.UpperLeftColumn].DataType = image.GetType();
  417. rows[picture.UpperLeftRow][picture.UpperLeftColumn] = image;
  418. }
  419. catch (System.Exception e)
  420. {
  421. error = error + " InsertPicturesIntoDataTable: " + e.Message;
  422. }
  423. }
  424. return true;
  425. }
  426. /// <summary>
  427. /// 把Excel文件内容导入到List对象
  428. /// </summary>
  429. /// <param name="filepath">Excel文件的全路径</param>
  430. /// <param name="lists">列表对象</param>
  431. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  432. /// <returns></returns>
  433. public static bool ExcelFileToLists(string filepath, out IList[] lists, out string error)
  434. {
  435. error = "";
  436. lists = null;
  437. DataTable datatable = new DataTable();
  438. IList list = new ArrayList();
  439. Aspose.Cells.Drawing.PictureCollection[] pictures;
  440. if (ExcelFileToDataTable(filepath, out datatable, out error) && GetPicturesFromExcelFile(filepath, out pictures, out error))
  441. {
  442. lists = new ArrayList[datatable.Rows.Count];
  443. //------------DataTable转换成IList[]--------------
  444. //数据
  445. int nRow = 0;
  446. foreach (DataRow row in datatable.Rows)
  447. {
  448. lists[nRow] = new ArrayList(datatable.Columns.Count);
  449. for (int i = 0; i <= datatable.Columns.Count - 1; i++)
  450. {
  451. lists[nRow].Add(row[i]);
  452. }
  453. nRow++;
  454. }
  455. //图片
  456. for (int i = 0; i < pictures.Length; i++)
  457. {
  458. foreach (Picture picture in pictures[i])
  459. {
  460. try
  461. {
  462. //----把图片转换成System.Drawing.Image----
  463. //MemoryStream mstream = new MemoryStream();
  464. //mstream.Write(picture.Data, 0, picture.Data.Length);
  465. //System.Drawing.Image image = System.Drawing.Image.FromStream(mstream);
  466. //----Image放入IList------
  467. //图片有可能越界
  468. if (picture.UpperLeftRow <= datatable.Rows.Count && picture.UpperLeftColumn <= datatable.Columns.Count)
  469. {
  470. lists[picture.UpperLeftRow][picture.UpperLeftColumn] = picture.Data;
  471. }
  472. }
  473. catch (System.Exception e)
  474. {
  475. error = error + e.Message;
  476. }
  477. }
  478. }
  479. }
  480. else
  481. {
  482. return false;
  483. }
  484. return true;
  485. }
  486. /// <summary>
  487. /// 把列表导出到Excle文件
  488. /// </summary>
  489. /// <param name="filepath">Excel文件的全路径</param>
  490. /// <param name="lists">列表对象</param>
  491. /// <param name="error">错误信息:返回错误信息,没有错误返回""</param>
  492. /// <returns></returns>
  493. public static bool ListsToExcelFile(string filepath, IList[] lists, out string error)
  494. {
  495. error = "";
  496. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
  497. Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
  498. Aspose.Cells.Cells cells = sheet.Cells;
  499. int nRow = 0;
  500. sheet.Pictures.Clear();
  501. cells.Clear();
  502. foreach (IList list in lists)
  503. {
  504. for (int i = 0; i <= list.Count - 1; i++)
  505. {
  506. try
  507. {
  508. System.Console.WriteLine(i.ToString() + " " + list[i].GetType());
  509. if (list[i].GetType().ToString() == "System.Drawing.Bitmap")
  510. {
  511. //插入图片数据
  512. System.Drawing.Image image = (System.Drawing.Image)list[i];
  513. MemoryStream mstream = new MemoryStream();
  514. image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
  515. sheet.Pictures.Add(nRow, i, mstream);
  516. }
  517. else
  518. {
  519. cells[nRow, i].PutValue(list[i]);
  520. }
  521. }
  522. catch (System.Exception e)
  523. {
  524. error = error + e.Message;
  525. }
  526. }
  527. nRow++;
  528. }
  529. workbook.Save(filepath);
  530. return true;
  531. }
  532. }
  533. }