UE4 讀寫 Excel

UE4中讀寫Excel須要導出爲CSV文件,而且UE4自身提供的一種讀寫文件的組件:UDataTable。spa

<先記錄後完善>code

//--------------------------------------------------------------------------------------------------------------------blog

一、首先須要建立一張Excel表,並導出爲*.csv格式。注意在建立表時第一格不要鍵入描述,相似:繼承

 

 

 

二、在工程代碼中建立一個struct,但要繼承自:FTableRowBase:it

struct FHxUserInfoTable : public FTableRowBase
{
    GENERATED_USTRUCT_BODY()

public:

    FHxUserInfoTable()
    {}

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HxUserInfo)
    FString Level;class

   UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HxUserInfo)
   int32 LvStar;

   UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HxUserInfo)
   int32 TotalStar;

   UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HxUserInfo)
   int32 Score;

   UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HxUserInfo)
   float Time;

};

TODO:該結構體中的變量名字必須與Excel表中的表頭名字一致,以便在藍圖中在Import csv時相互關聯。變量

三、csv

四、bug

/*
    FString GoID = TEXT("1338888888");
    static const FString ContextString(TEXT("GENERAL"));
    static const FString FileName(TEXT("F:\\UE4\\Project_422\\VRDesk2.0_Ctrl\\VRDeskII\\Content\\HxUserInfoData.csv"));
    FHxUserInfoTable* UserTable = HxUserTable->FindRow<FHxUserInfoTable>(*GoID, ContextString);

    //
    GoID = TEXT("1883333333");
    FHxUserInfoTable TemTable;
    TemTable.Level = TEXT("3-2");
    TemTable.LvStar = 3;
    TemTable.TotalStar = 20;
    TemTable.Score = 100;
    TemTable.Time = 12.f;

    HxUserTable->AddRow(*GoID, TemTable);

    for (const auto& Value : HxUserTable->GetRowMap())
    {
        FName Title = Value.Key;
        UserTable = HxUserTable->FindRow<FHxUserInfoTable>(Title, ContextString);
        if (UserTable)
        {
            FString Tem = FString::Printf(TEXT("%s, %s, %d, %d, %d, %f"), *Title, *UserTable->Level, UserTable->LvStar, UserTable->TotalStar, UserTable->Score, UserTable->Time);
            GEngine->AddOnScreenDebugMessage(
                GEngine->ScreenMessages.Num() + 1,
                6.0f,
                FColor::Green, Tem
            );
        }
    }

    // exprot data to csv
    FFileHelper::SaveStringToFile(HxUserTable->GetTableAsCSV(EDataTableExportFlags::UsePrettyPropertyNames | EDataTableExportFlags::UsePrettyEnumNames), *FileName);
    */
相關文章
相關標籤/搜索