1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using OfficeOpenXml;
    6. public class ExcelDemo : MonoBehaviour
    7. {
    8. // Start is called before the first frame update
    9. void Start()
    10. {
    11. //CreateExcel();
    12. ReadExcel();
    13. }
    14. ///创建
    15. private void CreateExcel()
    16. {
    17. string createPath = Application.dataPath + "/Resources/Excel/test.xlsx";
    18. FileInfo newFile = new FileInfo(createPath);
    19. if (newFile.Exists)
    20. {
    21. newFile.Delete();
    22. newFile = new FileInfo(createPath);
    23. }
    24. using(ExcelPackage package=new ExcelPackage(newFile))
    25. {
    26. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("tableOne");
    27. worksheet.Cells[1, 1].Value = "明晨";
    28. worksheet.Cells["A2"].Value = "命辰";
    29. package.Save();
    30. }
    31. }
    32. ///读取
    33. private void ReadExcel()
    34. {
    35. Debug.Log("ReadExcel");
    36. string readPath = Application.dataPath + "/Resources/Excel/test.xlsx";
    37. FileInfo newFile = new FileInfo(readPath);
    38. if (!newFile.Exists)
    39. {
    40. Debug.Log("没有该文件" + newFile.FullName);
    41. return;
    42. }
    43. using (ExcelPackage package=new ExcelPackage(newFile))
    44. {
    45. ExcelWorksheet worksheet = package.Workbook.Worksheets["tableOne"];//根据sheet获取Sheet
    46. int maxRow_Data = worksheet.Dimension.End.Row;//行
    47. int maxColumn_Data = worksheet.Dimension.End.Column;//列
    48. //按照索引字典,从数据Sheet中索取数据列
    49. //实际数据从第二行开始
    50. //List<content> contentList=GameFacade.
    51. for(int i = 1; i <= maxRow_Data; i++)
    52. {
    53. for(int j = 1; j <= maxColumn_Data; j++)
    54. {
    55. string data = System.Convert.ToString(worksheet.Cells[i, j].Value);
    56. Debug.Log("数据是" + data);
    57. }
    58. }
    59. }
    60. }
    61. }