Unity中Excel导出CSV文件
【配置表】Unity表管理_读取CSV文件(二)
功能
1、批量Excel转CSV
2、指定导出Sheet(注释|导出文件名)
3、可指定导出开始行(默认3)
4、导出区分服务器与客户端
注意:导表使用python写的,数值类型默认导出为浮点型,处理需注意。【配置表】Unity表管理_读取CSV文件(二)
- 目前支持数据 : 表中写法
int
short
byte
float
double
bool
string
enum : 枚举字符
Vector3 : 3,3,3
Vector2 : 2,2
int[] : , 分割
float[] :,分割
Vector3[] : ,分割Vector3数据 |分割元素(例:0.3,0.3,0.3|3,3,3)
Vector2[] : ,分割Vector3数据 |分割元素(例:0.2,0.2|2,2)
Dictionary<int, int> : ,分割键值对 |分割元素(例:1,-1|2,-2)
Dictionary<int, float> : ,分割键值对 |分割元素(例:1,0.1|2,0.2)
使用方法
1、设置导出Excel表
Excel设置导出Sheet格式为:注释|导出文件名。
将Excel放到“Tools\Config\Excel”文件夹中。
2、填写导出数据
- 由于在 python 中设置的导出行为:3 所以只会导出第三行以下数据(包括第三行)
- 当前第三行为“字段名”行,必须与C#数据结构中的字段名所对应(如果未对应则使用C#字段顺序对应表中数据顺序)。
- “字段名”行之下必须是数据。(“字段名”行可以在python中指定,“字段名”行之上属于注释)。
- 数据填写方式上面有。
3、Unity导出方法
设置导出位置在: EditorToolsExtension.cs
private static void ExcelToCSV() {
//导出位置
string new_path = Application.dataPath + "/Resources/csv";
string src_path = Environment.CurrentDirectory + "/Tools/Config/OutCSV";
EditorUtility.DisplayProgressBar("Excel", "导出 Excel", 0.5f);
string logInfo = ProcessUtils.ProcessCommand(Environment.CurrentDirectory + "/Tools/ExcelToCSV/ExportCSV.bat");
if (logInfo.Contains("all operation finish successful")) {
Debug.Log(logInfo);
DelectDir(new_path);
CopyDir(src_path, new_path);
EditorUtility.ClearProgressBar();
EditorUtility.DisplayDialog("success", "excel 导出成功", "ok");
AssetDatabase.Refresh();
}
EditorUtility.ClearProgressBar();
}
Unity 中使用 Ctrl+E 导出。
4、批量导出
运行 ExportCSV.bat 批量导出。(Excel位置:Tools\Config\Excel 导出位置: Tools\Config\OutCSV)
5、设置导出开始行
设置导出开始行位置 excel_to_csv.py -> START_ROW (文件位置:Tools\ExcelToCSV)
Python代码
import xlrd
import csv
import os
#Tools path
root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
#Config path
config_path = os.path.join(root_path,"Config")
#Excel path
excel_path = os.path.join(config_path,"Excel")
#OutCSV path
outCSV_path = os.path.join(config_path,"OutCSV")
#识别后缀
EXPORT_EXT = [
".xlsx",
".xls",
]
#Table 开始行
START_ROW = 3
def make_dir():
if not os.path.isdir(config_path):
os.mkdir(config_path)
if not os.path.isdir(config_path):
os.mkdir(config_path)
if not os.path.isdir(excel_path):
os.mkdir(excel_path)
if not os.path.isdir(outCSV_path):
os.mkdir(outCSV_path)
file_list = os.listdir(outCSV_path)
for _file in file_list:
os.remove(os.path.join(outCSV_path,_file))
def get_excel_list():
#获取Excel文件列表
excel_file_list = []
file_list = os.listdir(excel_path)
for file_name in file_list:
if os.path.splitext(file_name)[1] in EXPORT_EXT and not file_name.startswith("~"):
excel_file_list.append(file_name)
return excel_file_list
def read_excel(excel_name):
#读取Excel文件中的页
workbook = xlrd.open_workbook(os.path.join(excel_path,excel_name))
for sheet in workbook.sheets():
s_index = sheet.name.find('|')
if s_index == -1: continue
csv_name = sheet.name[s_index + 1 :]
table_to_csv(csv_name,sheet)
print("export "+csv_name+" successful!")
def table_to_csv(csv_file_name,table):
#生成csv文件
with open(os.path.join(outCSV_path,csv_file_name)+".csv", 'w', encoding='utf-8',newline='') as f
write = csv.writer(f)
#从第n行数据开始读
for rows_read in range(START_ROW - 1,table.nrows):
write.writerow(table.row_values(rows_read))
if __name__ == '__main__':
make_dir()
excel_list = get_excel_list()
for file_name in excel_list:
read_excel(file_name)
print("all operation finish successful")
exit(0)
完整工程
MacOS使用C#调用Python报错ModuleNotFoundError
gitee:https://gitee.com/horooo/UnityExcelToCSV