当前位置:网站首页>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

边栏推荐
- Redis review summary
- DSACTF7月re
- How can Plato obtain premium income through elephant swap in a bear market?
- LVS deployment Dr cluster
- 长轮询,iframe和sse三种web消息实时推送demo实践
- Use of DDR3 (axi4) in Xilinx vivado (5) board test
- DHCP.DNS.NFS
- Vivado design single cycle CPU
- Solve the problem of adding the least number of parentheses (interval DP)
- Nocturnal simulator settings agent cannot be saved
猜你喜欢
随机推荐
Raspberry pie 4B ffmpeg RTMP streaming
Speech controlled robot based on ROS (I): realization of basic functions
[task01: getting familiar with database and SQL]
七种轮询介绍(后附实践链接)
[task03: complex query methods - views, subqueries, functions, etc.]
C language - data storage
83. (cesium home) how the cesium example works
C# 委托 delegate 的理解
LeetCode_位运算_中等_260.只出现一次的数字 III
产品经理访谈 | 第五代验证码的创新与背景
Introduction to seven kinds of polling (practice link attached)
Vivado designs PC and ram
Raspberry pie 4B deploy yolov5 Lite using ncnn
Solve the cookie splitting problem (DP)
h5微信射击小游戏源码
长轮询,iframe和sse三种web消息实时推送demo实践
Linxu 【基本指令】
Data mining (data preprocessing) -- Notes
3D激光SLAM:LeGO-LOAM论文解读---简介部分
Related concepts of multitasking programming





![Linxu [basic instructions]](/img/94/98d7b2cb4a72c6437a9f604ec5da9d.png)
