讀取excel檔案並將其中資料轉換成指令碼資料結構

2021-07-27 05:43:40 字數 2078 閱讀 5895

最近寫**測試了在unity中讀取excel配置檔案,將配置中的資料結構自動寫成指令碼中的資料結構。

要寫的excel檔案如下:

角色資訊表

boss資訊表

讀取excel檔案並寫成指令碼的**:

using unityengine;

using system.collections;

using excel;

using system.io;

using system.text;

using system.data;

using system.collections.generic;

using system.globalization;

using system.threading;

using unityeditor;

public class unityeditorselection_test : editor

catch(system.exception e)

if (result.tables.count < 1) //判斷excel檔案中是否存在資料表

datatable one = result.tables[0]; //預設獲取第乙個資料表

if (one.rows.count < 1) //判斷資料表內是否存在資料

//存注釋

list

comments = new list

();//存欄位型別

list

filedstype = new list

();//存欄位名

list

filedsname = new list

(); string text = string.empty; for (int i = 0; i < one.columns.count; ++i) { //要注意這裡獲取各個資料的時候,要和excel檔案的資料編寫順序一致。excel中第一行是注釋,那麼這裡就要先得到注釋 //存欄位注釋 comments.add(one.rows[0][i].tostring()); //存欄位型別 text = one.rows[1][i].tostring().trim(); //以防萬一,去掉空格 if (string.isnullorempty(text)) continue; switch (text) { case "bool": filedstype.add("bool"); break; case "int": filedstype.add("int"); break; case "float": filedstype.add("float"); break; case "string": filedstype.add("string"); break; case "list

": filedstype.add("list

**執行後,會在目錄assets/myscripts中生成乙個新的指令碼dataclass.cs。裡面的內容就是assets/excel目錄中所有的excel檔案對應生成的類。

如下,就是我測試時生成的結果。

當excel表轉換指令碼出錯時(**中執行到return語句了),還是會生成dataclass2.cs這個指令碼,這個時候需要手動將這個檔案刪除了。生成成功是會自動刪除的。

excel.dll

system.data

assetdatabase 

unity 獲取指定資源目錄下的所有檔案

unity3d 操作檔案(儲存,讀取,刪除檔案,刪除目錄)

批量修改unity的asset下資源的檔名

unity excel 檔案讀取和寫入

unity3d 根據指定的assets下的資料夾路徑 返回這個路徑下的所有檔名

unity遍歷所選資料夾中包含某字尾名的檔案路徑

基於檔案操作的電影資料展示,並將其展示在網頁中

import requests當需要建立資料夾時,就要引入此包 import os如果url中的引數包含中文,那麼需要先編碼,否則對方伺服器不識別 引數是中文的必須編碼,requests包會自動編碼 url 由於返回的資料不是網頁,所以沒有必要設定user agent response reques...

讀取檔案並將檔案內容匯入資料庫

很多時候我們需要把乙個檔案按行讀取出來,並把讀出的內容插入到資料庫對應的字段,我們可以寫個方法,規定從檔案第幾行讀取,檔案中不同欄位間的分隔符是什麼,以及插入資料庫中對應表的列數。當然這其中檔案有的字段可能為空,這可能導致我們每行分隔檔案後的字段數和資料庫中的字段數不對應,所以也要進一步的處理。以下...

C 建立Excel檔案並將資料匯出到Excel檔案

工具原料 windows 7,visual studio 2010,microsoft office 2007 建立解決方案 選單 新建 專案 windows窗體應用程式 新增兩個datagridview,乙個textbox,兩個按鈕 如下圖 新增excel資源 c 建立excel檔案,這裡實際上是...