do u3d editor xlsx batch generate CSV
The xlsx Editor is a powerful office Excel
in other words, whether it is a program or art only need to maintain this Excel, and then a key batch conversion to CSV file! Packaging doesn't pack Excel, and it doesn't have to be in the game! then come to Unity3d's editor menu.
These four files are required under the Editor folder of Unity:
Reference to: Window Http://www.codeproject.com/Articles/246772/Convert-xlsx-xls-to-csv program.
There is a good way to use CSV in the game on GitHub:Https://github.com/cunkai/Unity-CSV-To-C-Sharp
Unity csv to C # files eliminates the steps to parse CSV and saves game load time.
someone would ask? Why not use Excel's xlsx files directly, because the same content file size is much larger than CSV.
Look at the code: the relevant path can be changed according to the project!
Using unityengine;using system.data;using system.io;using excel;using unityeditor;///<summary>///XLSL to CS./// </summary>public class Xlsltocsv:editorwindow {static string foldername = Application.datapath;static DataSet Res Ult = new DataSet (); [MenuItem ("Games/excel to CSV &% #c")]//to access excelstatic void Menu_click () {String chosen_file = Application.datapath + "/editor" + "/test.xlsx"; The file path of the xlsx for Excel Getexceldata (chosen_file);} static void Getexceldata (string file) {if (file. EndsWith (". xlsx")) {//Reading from a binary Excel file (format; *.xlsx) FileStream stream = File.Open (file, FileMode.Open, FileAccess.Read); Iexceldatareader excelreader = Excelreaderfactory.createopenxmlreader (stream); result = Excelreader.asdataset (); Excelreader.close ();} if (file. EndsWith (". xls")) {//Reading from a binary Excel file (' 97-2003 format; *.xls) FileStream stream = File.Open (file, FileMode . Open, FileAccess.Read); Iexceldatareader Excelreader = Excelreaderfactory.createbinaryreadER (stream); result = Excelreader.asdataset (); Excelreader.close ();} list<string> items = new list<string> (); Get the table name for all tables for (int i = 0; I < result. Tables.count; i++) {//items. ADD (result. Tables[i]. Tablename.tostring ()); Convertocsv (i, result. Tables[i]. Tablename.tostring ());}} static void Convertocsv (int index, string oldfilename) {//sheets in Excel file becomes tables in Dataset//result. Tables[0]. Tablename.tostring (); To get sheet name (table name) string a = ""; int row_no = 0;while (Row_no < result. Tables[index]. Rows.Count) {for (int i = 0; I < result. Tables[index]. Columns.count; i++) {A + = result. Tables[index]. Rows[row_no][i]. ToString () + ",";} Row_no++;a + = "\ n";} String output = foldername + "/editor" + "\ \" + Oldfilename + ". csv"; File path to save StreamWriter csv = new StreamWriter (@output, false); CSV. Write (a); CSV. Close ();D ebug. Log ("File converted Succussfully"); return;}}
In fact, this is also a problem, planning is rarely used unity, they can not always open unity after configuring XLSX and then click on the menu to convert it.
So let's talk about the powerful Python implementation: reference Https://github.com/amengren/xls2csv
is also the use of third-party extensions.
Convert the XLS xlsx format file to CSV (', ' delimited to ' \\n ' line terminator (and of course it can be set to another!). ))
Need to install XLRD module HTTPS://PYPI.PYTHON.ORG/PYPI/XLRD (Installation instructions at the end of this article!) )
How to use:
Place the XLS file and the current module in the same folder, execute the script, and automatically generate the CSV in the current file
xls2csv.py
# encoding:utf-8import Os,sys,inspect,reimport xdrlib,xlrdreload (SYS) sys.setdefaultencoding ("Utf-8") #分割符C_SPACE = " , "#结束符C_END =" \ n "#获取脚本文件的当前路径def cur_file_dir (): Path = Os.path.realpath (sys.path[0]) print path if OS.PATH.ISFI Le (path): print "exe" PATH = os.path.dirname (path) return Os.path.abspath (path) else:print "File" Caller_file = Inspect.stack () [1][1] Return Os.path.abspath (Os.path.dirname (caller_file)) #搜索指定文件夹下面的文件 ( Default current directory) def Find_file_by_pattern (pattern= '. * ', base= ".", Circle=true): "" Find the given folder under All ' Re_file = Re.compile (PA Ttern) If base = = ".": base = Cur_file_dir () print "Start Search Folder:", base final_file_list = [] Cur_list = OS.L Istdir (base) for item in Cur_list: # Print Item if Item = = ". SVN": Continue Full_path = Os.path.join (base, item) if Full_path.startswith ("~"): Continue if Full_path.endswit H (". xlsx") or FULL_PATH.ENDSWITh (". xls"): print "in:" + full_path bfile = os.path.isfile (item) if Os.path.isfile (full_p ATH): If Re_file.search (Full_path): Final_file_list.append (Full_path) Else: Final_file_list + = Find_file_by_pattern (pattern, Full_path) "Returns the file List" ' Return final_file_list# open ex Celdef open_excel (file= ' File.xls '): Try:data = xlrd.open_workbook (file) return data except Exception , E:print str (e) #根据索引获取Excel表格中的数据 parameter: File:excel file path, Colnameindex: Index of row of table header column name, By_index: Index of Table def Excel_table_byindex (file= ' File.xls ', Colnameindex=0, by_index=0): data = open_excel (file) Table = Data.sheets () [by_index] nrows = TA Ble.nrows #行数 ncols = table.ncols #列数 rowlist = [] # print heads ' Start reading Data ' for rownum in range (Colnamein Dex, nrows): RowData = table.row_values (rownum) if rowdata:collist = [] for i in rang E (ncols): Collist.append (Rowdata[i]) rowlist.append (collist) return rowlist# save CSV file def savatocsv (_file, _list, _path) : filename = "" content = "" #生成文件内容 for Collist in _list:for I in range (len (collist)): v = Collist[i] Vstr = "" # Print K,v if isinstance (V, float) or isinstance (v, int): VSTR = str (int (v)) else:# Elif isinstance (V, str): Vstr = v If i > 0: Content = content + C_Space content = content + Vstr content = content + c_end #生成文件后缀 F Name = Os.path.splitext (_file) filename = fname[0] + ". csv" #写文件 if Len (filename) >0 and len (content) >0: # filename = _path + "/" + filename print "out:" + filename file_object = open (filename, ' W ') fil E_object.write (content) File_object.close () def main (): FileList = Find_file_by_pattern () If Len (filelist) ; 0:path = "" # if not Os.path.isdir (path): # os.makedirs (path) #遍历文件生成csv for file in FileList: DataList = Excel_table_byindex (file, 0) If Len (DataList) >0:savatocsv (File, DataList, Path) Else:print "did not find any Excel skill files! "If __name__==" __main__ ": Main ()
Python environment variable configuration: In the "path" line, add Python installed under the win path can be, I python installed in E:\Python27, so in the later, add the path.
PS: Remember, the path is separated directly by a semicolon ";"
PS: Install Xlrd-0.9.3.tar. Since I did not install Python on the system disk, I would like to manually copy the scripts and xlrd to the Python system installation path (some merge folders). PS: It is recommended that the installation of Python be installed in the system tray.
??
Make U3d editor xlsx batch generate CSV and Python batch turn