赞
踩
目录
2.创建脚本,引入命名空间
- using UnityEngine;
- using UnityEditor;
- using OfficeOpenXml;
- using System.IO;
- 表格数据
- string filePath = Application.dataPath + "/目标名称.xlsx";//这里是文件路径
-
- //获取Excel文件的信息
- FileInfo fileInfo = new FileInfo(filePath);
- //通过Excel文件信息,打开表格
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
- //using是用来强行资源释放(前括号是打开,后括号是关闭)
- {
-
- }
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
- {
- //取得Excel文件中的第N张表
- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
- }
-
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
- {
- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
- for (int i = 2; i < worksheet.Dimension.End.Row; i++)
- //End.Row获得当前表格的最大行数
- {
- Debug.Log("目标名称:" + worksheet.Cells[i, 1].Value.ToString() + " // 设备名称:" + worksheet.Cells[i, 2].Value.ToString() + " // 地址:" + worksheet.Cells[i, 3].Value.ToString());
- //Cells是个二维数组,第一个参数是读取第几行,第二个参数是读取第几列需要ToString出数据
- }
- }
- using UnityEngine;
- using UnityEditor;
- using OfficeOpenXml;
- using System.IO;
-
- /// <summary> Read文件 </summary>
- public class ReadExcel
- {
-
- [MenuItem("数据持久化/1.Excel/1.ReadExcel")]
- public static void InputExcel()
- {
- string filePath = Application.dataPath + "/00.Excel文件夹/Read.xlsx";
-
- ///获取Excel文件的信息
- FileInfo fileInfo = new FileInfo(filePath);
-
- ///通过Excel文件信息,打开表格
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//using是用来强行资源释放(前括号是打开,后括号是关闭)
- {
- //取得Excel文件中的第N张表
- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
- for (int i = 2; i < worksheet.Dimension.End.Row; i++)//End.Row获得当前表格的最大行数
- {
- Debug.Log("目标名称:" + worksheet.Cells[i, 1].Value.ToString() +
- " // 设备名称:" + worksheet.Cells[i, 2].Value.ToString() +
- " // 地址:" + worksheet.Cells[i, 3].Value.ToString());
- //Cells是个二维数组,第一个参数是读取第几行,第二个参数是读取第几列需要ToString出数据
- }
- }
- }
- }
- 初始表格
- ///通过Excel文件信息,打开表格
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//using是用来强行资源释放(前括号是打开,后括号是关闭)
- {
- //取得Excel文件中的第N张表
- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
- for (int i = 2; i <= 12; i++)
- {
- //直接向每个表格赋值则是写入
- worksheet.Cells[i, 1].Value = "目标名称" + " " + (i - 1);
- worksheet.Cells[i, 2].Value = "设备地址" + " " + (i - 1);
- worksheet.Cells[i, 3].Value = "地址" + " " + (i - 1);
- }
- excelPackage.Save();//写入后保存表格
- }
- using UnityEngine;
- using UnityEditor;
- using OfficeOpenXml;
- using System.IO;
-
- public class WriteExcel
- {
-
- [MenuItem("数据持久化/1.Excel/2.WriteExcel")]
- public static void ChangeExcel()
- {
- string filePath = Application.dataPath + "/00.Excel文件夹/Write.xlsx";
-
- ///获取Excel文件的信息
- FileInfo fileInfo = new FileInfo(filePath);
-
- ///通过Excel文件信息,打开表格
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//using是用来强行资源释放(前括号是打开,后括号是关闭)
- {
- //取得Excel文件中表格叫Sheet1的表格
- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
- for (int i = 2; i <= 12; i++)
- {
- //直接向每个表格赋值则是写入
- worksheet.Cells[i, 1].Value = "目标名称" + " " + (i - 1);
- worksheet.Cells[i, 2].Value = "设备地址" + " " + (i - 1);
- worksheet.Cells[i, 3].Value = "地址" + " " + (i - 1);
- }
- excelPackage.Save();//写入后保存表格
- }
- }
-
- }
- using UnityEngine;
- using UnityEditor;
- using OfficeOpenXml;
- using System.IO;
-
- public class Creat
- {
- [MenuItem("数据持久化/1.Excel/3.CreatExcel")]
- public static void ChangeExcel()
- {
- string filePath = Application.dataPath + "/00.Excel文件夹/Creat.xlsx";
-
- ///获取Excel文件的信息(文件中没有这个文件也不会报错)
- FileInfo fileInfo = new FileInfo(filePath);
-
- ///通过Excel文件信息,打开表格
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))//在这里也没有创建Excel文件
- {
- //在新Excel文件中创建叫Sheet1的表格
- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet1");
- worksheet.Cells[1, 1].Value = "目标名称";
- worksheet.Cells[1, 2].Value = "设备地址";
- worksheet.Cells[1, 3].Value = "地址";
- for (int i = 2; i <= 12; i++)
- {
- //直接向每个表格赋值则是写入
- worksheet.Cells[i, 1].Value = "目标名称" + " " + (i - 1);
- worksheet.Cells[i, 2].Value = "设备地址" + " " + (i - 1);
- worksheet.Cells[i, 3].Value = "地址" + " " + (i - 1);
- }
- excelPackage.Save();//写入后保存表格
- }
- //完成文件创建
- }
- }
- 通过int索引取得Excel文件中的第N张表
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1]- 通过string获得Excel的表格
ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets["Sheet1"];- 获得当前表格数目
int i = excelPackage.Workbook.Worksheets.Count;- 添加一张表格
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet2");- 给表格名称添加颜色(表头颜色换成绿色)
worksheet.TabColor = System.Drawing.Color.ForestGreen;
- 获得表格中最大行和列
int row = worksheet.Dimension.End.Row;//行
int column = worksheet.Dimension.End.Column;//列- 获得第x各格子中的内容
worksheet.Cells[1, 1].Value.ToString();//获得[1,1]位置的内容,可以写入(写入不加ToString)也可以读取
- 获得一个Excel文件
ExcelPackage excelPackage = new ExcelPackage(fileInfo);- 保存修改后的Excel文件
excelPackage.Save();//保存
excelPackage.SaveAs();//另存为
下面的EPPlus的GitHUb:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。