UE4 读写 Excel


UE4中读写Excel需要导出为CSV文件,并且UE4自身提供的一种读写文件的组件:UDataTable。

<先记录后完善>

//--------------------------------------------------------------------------------------------------------------------

1、首先需要创建一张Excel表,并导出为*.csv格式。注意在创建表时第一格不要键入描述,类似:

2、在工程代码中创建一个struct,但要继承自:FTableRowBase:

struct FHxUserInfoTable : public FTableRowBase
{
    GENERATED_USTRUCT_BODY()

public:

    FHxUserInfoTable()
    {}

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

   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时相互关联。

3、

4、

/*
    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(*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(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);
    */
UE4