当前位置:网站首页>About the operation of unit file reading (I)

About the operation of unit file reading (I)

2022-04-23 18:36:00 Meng Chen Shuangxi

Make a note of unity How to read files

1. About Excel Read and write files :

The clue here comes from buying a Book , Title 《unity3D Game development ( The second edition )》, The author is Xuan Yusong. He mentioned using a DLL The plug-in can easily read, write and create files .Epplus This can be B Station or VS Package management can also be imported directly , Or you can come to me if you need . The code is as follows :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using OfficeOpenXml;

public class PrintExcel : MonoBehaviour
{
void Start()
{
LoadExcel();
AddExcel();
}

public static void LoadExcel()
{
    string path = Application.dataPath + "/Excel/test.xlsx";
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (ExcelPackage excel=new ExcelPackage(fs))
        {
            ExcelWorksheets excelWorksheets = excel.Workbook.Worksheets;// Get all the forms 
            Debug.Log(" Table number :"+excelWorksheets.Count);
            for (int i = 1; i <= excelWorksheets.Count; i++)// From the subscript 1 Start 
            {                    
                ExcelWorksheet worksheet = excelWorksheets[i];
                int colCount = worksheet.Dimension.End.Column;
                Debug.Log(worksheet.Name);

                for(int row = 1, count = worksheet.Dimension.End.Row; row <= count; row++)
                {
                    for(int col = 1; col <= colCount; col++)
                    {
                        // Read the contents of each cell 
                        var text = worksheet.Cells[row, col].Text;
                        Debug.Log(text);
                    }
                }
            }
        }
    }
}

public static void AddExcel()
{
    string path = Application.dataPath + "/Excel/test.xlsx";
    var file = new FileInfo(path);
    using (ExcelPackage excel=new ExcelPackage(file))
    {
        ExcelWorksheet worksheet2 = excel.Workbook.Worksheets.Add("Sheet3");//( If it already exists, an error will be reported , So we need to judge here )
        worksheet2.Cells[1, 1].Value = " Company name ";
        worksheet2.Cells[1, 2].Value = " Address ";
        excel.Save();
    }
    AssetDatabase.Refresh();
}
}

It should be noted that string path = Application.dataPath + “/Excel/test.xlsx”, The path must be written correctly , What I write in this code is to read all and show . And a little bit more for (int i = 1; i <= excelWorksheets.Count; i++), The table is actually subscript 1 At the beginning

2. About word Document reading

This is a long time ago , It's also found by checking someone else's code , The link can't be given . The code is as follows :

			DirectoryInfo d = new DirectoryInfo(LoadVariables.rootPATH + "/"+LoadVariables.loadScene);// route 
            DirectoryInfo[] dc = d.GetDirectories();// Get the folder 
            
                FileInfo[] files = d.GetFiles("*.txt");// get txt file 
                Debug.Log(files.Length);                 
                for (int i = 0; i < files.Length; i++)
                {
                    string a=File.ReadAllText(LoadVariables.rootPATH + "/"+LoadVariables.loadScene+"/"+files[i].Name);// Read the file              
                }

版权声明
本文为[Meng Chen Shuangxi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231830134460.html