当前位置:网站首页>关于unity文件读取的操作(一)
关于unity文件读取的操作(一)
2022-04-23 18:32:00 【孟尘双喜】
记录一下unity如何进行文件读取的操作
1.关于Excel文件的读取和写入:
这里的线索来源于买的一本书,书名《unity3D游戏开发(第二版)》,作者是宣雨松他这里面提到的使用一个DLL插件可以很方便的进行文件的读写创建等操作。Epplus这个可以在B站或者是VS的包管理也可以直接导入,或者需要的也可以找我。代码如下:
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;//获得所有表格
Debug.Log("表数:"+excelWorksheets.Count);
for (int i = 1; i <= excelWorksheets.Count; i++)//从下标1开始
{
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++)
{
//读取每个单元格的内容
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");//(如果已存在会报错,所以这里需要判断)
worksheet2.Cells[1, 1].Value = "公司名";
worksheet2.Cells[1, 2].Value = "地址";
excel.Save();
}
AssetDatabase.Refresh();
}
}
需要注意的是 string path = Application.dataPath + “/Excel/test.xlsx”,路径一定要写对,我这个代码写的是全部读取并展示。 还有一点 for (int i = 1; i <= excelWorksheets.Count; i++),表格实际上是从从下标1开始的
2.关于word文档的读取
这个比较久远了,也是查别人的代码找到的,链接没法给了。代码如下:
DirectoryInfo d = new DirectoryInfo(LoadVariables.rootPATH + "/"+LoadVariables.loadScene);//路径
DirectoryInfo[] dc = d.GetDirectories();//获得文件夹
FileInfo[] files = d.GetFiles("*.txt");//获得txt文件
Debug.Log(files.Length);
for (int i = 0; i < files.Length; i++)
{
string a=File.ReadAllText(LoadVariables.rootPATH + "/"+LoadVariables.loadScene+"/"+files[i].Name);//读取文件
}
版权声明
本文为[孟尘双喜]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_37900417/article/details/124294692
边栏推荐
- kettle庖丁解牛第17篇之文本文件输出
- JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
- Linux installs MySQL in RPM (super simple)
- Can filter
- Introduction to quantexa CDI syneo platform
- ctfshow-web362(SSTI)
- Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
- logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
- Introduction to quantexa CDI syneo platform
- 硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
猜你喜欢
Resolves the interface method that allows annotation requests to be written in postman
The vivado project corresponding to the board is generated by TCL script
玻璃体中的硫酸软骨素
In win10 system, all programs run as administrator by default
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
Function recursion and solving interesting problems
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
WIN1 remote "this may be due to credssp encryption Oracle correction" solution
listener.log
随机推荐
SSD硬盘SATA接口和M.2接口区别(详细)总结
QT add external font ttf
22年字节跳动飞书人力套件三面面经
Daily CISSP certification common mistakes (April 15, 2022)
Cells in rust share variable pointers
Keil RVMDK compiled data type
Creation and use of QT dynamic link library
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
14 py games source code share the second bullet
深度学习经典网络解析目标检测篇(一):R-CNN
Jeecg boot microservice architecture
Const keyword, variable and function are decorated with const
Promote QT default control to custom control
JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
Rust: a simple example of TCP server and client
Log4j2 cross thread print traceid
Resolves the interface method that allows annotation requests to be written in postman
QT notes on qmap container freeing memory
Gst-launch-1.0 usage notes