当前位置:网站首页>Unity package exe to read and write excel table files
Unity package exe to read and write excel table files
2022-07-28 20:35:00 【Mark_ source】
Unity Read user configuration information when running , Export the information to be saved in the application to Excel form
1、 Import Excel Read plug-ins

2、 Import opens Windows File and folder tools ( I wrote it before )

3、 Usage method
1)、 New script , And define parameters
public Button saveBtn;
public Button readBtn;
private Action<string> readAction;
private Action<string> saveAction;
public Text des;2)、 Binding of events
private void Start()
{
saveBtn.onClick.AddListener(OpenFolder);
readBtn.onClick.AddListener(OpenFile);
saveAction += SaveFile;
readAction += GetFile;
}3)、 Function realization
public void OpenFile()
{
Mark.OpenDialogHelper.SelectFile(readAction, "Excel file (*.xlsx*)\0*.xlsx*"); // FBX file (*.fbx)\0*.fbx\0
}
public void OpenFolder()
{
Mark.OpenDialogHelper.SelectFolder(saveAction);
}
private void GetFile(string filePath)
{
Debug.Log(filePath);
Read(filePath);
}
private void SaveFile(string filePath)
{
Debug.Log(filePath);
Create(filePath);
}
private void Create(string path)
{
if (Directory.Exists(path))
{
string ph = path + "/" + "Tex2.xlsx";
FileInfo newFile = new FileInfo(ph);
if (newFile.Exists)
{
newFile.Delete();
newFile = new FileInfo(ph);
}
using (ExcelPackage package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("ModelInfo");// establish worksheet
CreateTitle(worksheet);
CreateContent(worksheet);
package.Save();
}
}
}
private void Read(string path)
{
StringBuilder sb = new StringBuilder();
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
sb.Clear();
using (ExcelPackage ep = new ExcelPackage(fs))
{
// Get all tables E:\Test\Text.xlsx
StringBuilder sb1 = new StringBuilder();
ExcelWorksheets worksheets = ep.Workbook.Worksheets;
for (int i = 1; i <= worksheets.Count; i++)
{
// The current worksheet
ExcelWorksheet sheet = worksheets[i];
// The current worksheet How many? Column
int columCount = sheet.Dimension.End.Column;
// The current worksheet How many? That's ok
int rowCount = sheet.Dimension.End.Row;
// From the first few lines \ Which column starts reading data , It depends on the specific situation
int startRow = 2;
int startColum = 1;
for (int j = startRow; j <= rowCount; j++)
{
sb1.Clear();
for (int k = startColum; k <= columCount; k++)
{
if (!string.IsNullOrEmpty(sheet.Cells[j, k].Text))
{
sb1.Append(sheet.Cells[j, k].Text+" ");
}
}
string str = $" At present Excel Table No {i} Table reading : The first {j} Row data :{sb1.ToString()}\n";
sb.Append(str);
}
}
}
des.text = sb.ToString();
}
}
private void CreateTitle(ExcelWorksheet worksheet)
{
worksheet.Cells[1, 1].Value = " Serial number ";
worksheet.Cells[1, 2].Value = " name ";
worksheet.Cells[1, 3].Value = " Number ";
worksheet.Cells[1, 4].Value = " Company ";
worksheet.Cells[1, 5].Value = " The unit price ";
worksheet.Cells[1, 6].Value = " Total price ";
worksheet.Cells[1, 7].Value = " remarks ";
}
private void CreateContent(ExcelWorksheet worksheet)
{
for (int i = 2; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
worksheet.Cells[i, j].Value = i * j;
}
}
}4、 Effect display

边栏推荐
- Solve the brick stacking problem (DP)
- Why is customer support important to SaaS?
- LeetCode_ Bit operation_ Medium_ 260. Number III that appears only once
- 漂亮的蓝色背景表单输入框样式
- Explain RESNET residual network in detail
- Storage of C language data in memory (1)
- Vivado design single cycle CPU
- [task03: complex query methods - views, subqueries, functions, etc.]
- Speech controlled robot based on ROS (I): realization of basic functions
- Network shell
猜你喜欢
随机推荐
DOS common commands
h5微信射击小游戏源码
产品经理访谈 | 第五代验证码的创新与背景
Raspberry pie 4B parsing PWM
激光slam:LeGO-LOAM---代码编译安装与gazebo测试
Raspberry pie 4B deploy yolov5 Lite using ncnn
微信公众号授权登录后报redirect_uri参数错误的问题
同质化代币与 NFT 结合,如何使治理结构设计更灵活?
超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案
C语言数据 3(1)
字符设备驱动结构
Raspberry connects EC20 for PPP dialing
太空射击第14课: 玩家生命
Configure Windows Server + install MySQL database on the server + Remote Access database
Simple example of C language 1
Practice of real-time push demo of three web messages: long polling, iframe and SSE
太空射击第15课: 道具
太空射击第13课: 爆炸效果
Using typedef in C language to change the name of data type
太空射击第10课: Score (繪畫和文字)





![[task03: complex query methods - views, subqueries, functions, etc.]](/img/83/2cad48016199b079aca0251b7b4ee8.png)



