CreateExcel.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using OfficeOpenXml;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System.IO;
  6. using Excel;
  7. using System.Reflection;
  8. using System;
  9. using UnityEngine.UI;
  10. using UnityEditor;
  11. using System.Data;
  12. /// <summary>
  13. /// 创建Excel表
  14. /// </summary>
  15. public class CreateExcel : MonoBehaviour
  16. {
  17. public Text mtext;
  18. List<string> mtestList = new List<string>();
  19. string outPutDir;
  20. string savePutDir;
  21. const string SheetName = "monster";
  22. void Awake()
  23. {
  24. outPutDir = Application.streamingAssetsPath + "\\" + "Excel/MyExcel.xlsx";
  25. savePutDir = Application.streamingAssetsPath + "\\" + "Excel/MyExcel.xlsx";
  26. }
  27. // Use this for initialization
  28. void Start()
  29. {
  30. ExcelType2WriteExcel();//写入数据
  31. LoadExcel();//读取数据
  32. WriteExcel(mtestList,savePutDir);//写入
  33. ReadExcel(outPutDir);//读取
  34. }
  35. public void ExcelType2WriteExcel()
  36. {
  37. string[] head = new string[5] { "id", "name", "level", "hp", "mp" };
  38. string[] data1 = new string[5] { "1001", "哥布林", "50", "1000", "1000" };
  39. string[] data2 = new string[5] { "1002", "猫妖", "70", "7000", "7000" };
  40. string[] data3 = new string[5] { "1003", "牛头怪", "100", "100000", "100000" };
  41. string[] data4 = new string[5] { "1004", "精英僵尸", "200", "200000", "200000" };
  42. List<string[]> data = new List<string[]>();
  43. data.Add(head);
  44. data.Add(data1);
  45. data.Add(data2);
  46. data.Add(data3);
  47. data.Add(data4);
  48. CreateExcels(savePutDir, SheetName, data);
  49. }
  50. void LoadExcel()
  51. {
  52. List<string[]> data = ReadExcelFuncTwo(outPutDir, SheetName);
  53. if (data == null || data.Count == 0) return;
  54. string allData = "";
  55. for (int i = 0; i < data.Count; i++)
  56. {
  57. string rowData = "";
  58. for (int k = 0; k < data[i].Length; k++)
  59. {
  60. rowData += data[i][k] + " ";
  61. }
  62. allData += rowData + "\n";
  63. }
  64. // print(allData);
  65. mtext.text = allData;
  66. }
  67. #region --ExcelFashion 1----
  68. /// <summary>
  69. ///返回数据的集合
  70. ///数据的格式为 每一行为一条数据
  71. ///例:赵一|党员|1年|赵一.png|
  72. /// </summary>
  73. /// <returns></returns>
  74. public List<string> ReadExcel(string moutPutDr)
  75. {
  76. // StreamingAssets目录下的 党员信息.xlsx文件的路径:Application.streamingAssetsPath + "/党员信息.xlsx"
  77. FileStream fileStream = File.Open(moutPutDr, FileMode.Open, FileAccess.Read);
  78. IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
  79. // 表格数据全部读取到result里
  80. DataSet result = excelDataReader.AsDataSet();
  81. // 获取表格有多少列
  82. int columns = result.Tables[0].Columns.Count;
  83. // 获取表格有多少行
  84. int rows = result.Tables[0].Rows.Count;
  85. // 根据行列依次打印表格中的每个数据
  86. List<string> excelDta = new List<string>();
  87. string value;
  88. string all;
  89. //第一行为表头,不读取
  90. for (int i = 1; i < rows; i++)
  91. {
  92. value = null;
  93. all = null;
  94. for (int j = 0; j < columns; j++)
  95. {
  96. // 获取表格中指定行指定列的数据
  97. value = result.Tables[0].Rows[i][j].ToString();
  98. if (value == "")
  99. {
  100. continue;
  101. }
  102. all = all + value + "|";
  103. }
  104. if (all != null)
  105. {
  106. print(all);
  107. excelDta.Add(all);
  108. }
  109. }
  110. return excelDta;
  111. }
  112. /// <summary>
  113. /// list内容格式
  114. /// 赵一|党员|1年|赵一.png|
  115. /// </summary>
  116. /// <param name="newList"></param>
  117. public void WriteExcel(List<string> newList, string savePutDir)
  118. {
  119. //自定义excel的路径
  120. //string path = Application.streamingAssetsPath + "/党员信息.xlsx";
  121. // print(Application.dataPath);
  122. FileInfo newFile = new FileInfo(savePutDir);
  123. if (newFile.Exists)
  124. {
  125. //创建一个新的excel文件
  126. newFile.Delete();
  127. newFile = new FileInfo(savePutDir);
  128. }
  129. //通过ExcelPackage打开文件
  130. using (ExcelPackage package = new ExcelPackage(newFile))
  131. {
  132. //在excel空文件添加新sheet
  133. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("message");
  134. //添加列名
  135. worksheet.Cells[1, 1].Value = "姓名";
  136. worksheet.Cells[1, 2].Value = "职务";
  137. worksheet.Cells[1, 3].Value = "党龄";
  138. worksheet.Cells[1, 4].Value = "图片名";
  139. for (int i = 0; i < newList.Count; i++)
  140. {
  141. string[] messages = newList[i].Split('|'); //赵一|党员|1年|赵一.png|
  142. string itemName = messages[0];
  143. string itemWork = messages[1];
  144. string itemYear = messages[2];
  145. string imageName = messages[3];
  146. //添加一行数据
  147. int num = i + 2;
  148. worksheet.Cells["A" + num].Value = itemName;
  149. worksheet.Cells["B" + num].Value = itemWork;
  150. worksheet.Cells["C" + num].Value = itemYear;
  151. worksheet.Cells["D" + num].Value = imageName;
  152. }
  153. //保存excel
  154. package.Save();
  155. print("重写完成");
  156. }
  157. }
  158. #endregion
  159. #region --ExcelFashion 2----
  160. /// <summary>
  161. /// 保存数据到新的Excel表中
  162. ///数据的格式为 每一行为一条数据
  163. /// </summary>
  164. void CreateExcels(string excelpath, string sheetname, List<string[]> data)
  165. {
  166. if (data == null || data.Count == 0) return;
  167. FileInfo fileInfo = new FileInfo(excelpath);
  168. if (fileInfo.Exists)
  169. {
  170. //创建一个新的excel文件
  171. fileInfo.Delete();
  172. fileInfo = new FileInfo(excelpath);
  173. }
  174. using (ExcelPackage package = new ExcelPackage(fileInfo))
  175. {
  176. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetname);
  177. for (int i = 0; i < data.Count; i++)
  178. {
  179. for (int k = 0; k < data[i].Length; k++)
  180. {
  181. worksheet.Cells[i + 1, k + 1].Value = data[i][k];
  182. }
  183. }
  184. package.Save();
  185. }
  186. Debug.Log("导出Excel成功");
  187. }
  188. /// <summary>
  189. /// 读取Excel表
  190. /// </summary>
  191. /// <param name="excelpath">表路径</param>
  192. /// <param name="sheetname">sheet名称</param>
  193. public static List<string[]> ReadExcelFuncTwo(string excelpath, string sheetname)
  194. {
  195. FileStream fileStream = File.Open(excelpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  196. ExcelPackage pck = new ExcelPackage(new MemoryStream(), fileStream);
  197. ExcelWorkbook ewb = pck.Workbook;
  198. ExcelWorksheet ew = ewb.Worksheets[sheetname];
  199. int columns = ew.Dimension.End.Column;//获取列数
  200. int rows = ew.Dimension.End.Row;//获取行数
  201. List<string[]> result = new List<string[]>();
  202. //第一行为表头,不读取
  203. for (int i = 1; i <= rows; i++)
  204. {
  205. string[] rowData = new string[columns];
  206. for (int j = 1; j <= columns; j++)
  207. {
  208. // 获取表格中指定行指定列的数据
  209. rowData[j - 1] = ew.Cells[i, j].Value.ToString();
  210. }
  211. result.Add(rowData);
  212. }
  213. return result;
  214. }
  215. #endregion
  216. }