当前位置:网站首页>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
边栏推荐
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
- Teach you to quickly rename folder names in a few simple steps
- 数据库上机实验四(数据完整性与存储过程)
- If condition judgment in shell language
- Install the yapiupload plug-in in idea and upload the API interface to the Yapi document
- Log4j2 cross thread print traceid
- CISSP certified daily knowledge points (April 18, 2022)
- 配置iptables
- Using transmittablethreadlocal to realize parameter cross thread transmission
- 七、DOM(下) - 章节课后练习题及答案
猜你喜欢
Tangle
os_authent_prefix
Jeecg boot microservice architecture
iptables初探
玻璃体中的硫酸软骨素
Teach you to quickly rename folder names in a few simple steps
iptables -L执行缓慢
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
根据快递单号查询物流查询更新量
Cygwin64 right click to add menu, and open cygwin64 here
随机推荐
K210 serial communication
Creation and use of QT dynamic link library
关于unity文件读取的操作(一)
Setting up keil environment of GD single chip microcomputer
Robocode tutorial 7 - Radar locking
ctfshow-web362(SSTI)
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
Multifunctional toolbox wechat applet source code
Mysql database backup command -- mysqldump
纠结
Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
七、DOM(下) - 章节课后练习题及答案
Daily CISSP certification common mistakes (April 15, 2022)
Function recursion and solving interesting problems
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
Teach you to quickly rename folder names in a few simple steps
Permission management with binary
Daily CISSP certification common mistakes (April 13, 2022)
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
串口调试工具cutecom和minicom