github地址
由于xls版本采用旧格式,读取的数据会导致部分异常,仅支持xlsx后缀格式(office 2007 及以后版本)
安装插件后,把xlsx文件放入工程,并且右键属性,在自定义工具中输入,ToCS(注意大小写)
在弹出对话框中,找到插件目录,输入(注意最后有下划线),并刷新,如果刷新出有数据则为正确路径。
关闭,右键运行自定义工具,即可生成代码。
第一列为ID字段,默认仅支持int,类型,所以填0,(仅关心ID>0)的列,任何小于1的数据都被忽略。
ID不可重复,如果重复,会被后面的相同ID覆盖。
bin/ExcelToCS.vsix 直接进行安装。
安装后,提示框,请指向 /bin/Addin/ 目录, 带最后一个斜杠
在Excel同目录下,会生成以下文件:
nameMap.txt 名称映射
typeMap.txt 类型映射
xxx.cs 代码文件
ConfigHelper.cs 配置公用代码,可自行修改
xxx.xml 对应的xml文件
第一列示例数据,可定义当前类型,
空,默认为 string
0,定义为 int
0.1 定义为 float
可在 typeMap.txt 中强行定义
#为忽略当前行
#针对某个表的特定可进行
#表翻译.字段
#Name|type
ID|int
字段名称|枚举
#字段名称|enum
#怪物表中的五行字段|强制为枚举类型
MonsterTable.五行|Enum_TheFiveLine
枚举,通过寻找当前同表枚举表进行搜索,如果搜索到,则进行匹配。
如果是其他表,则无法自动匹配,需要手动指定类型。
nameMap.txt
#srcurl|http://fanyi.baidu.com/v2transapi?from=zh&to=en&query={src}
#这是百度翻译接口
#dstmath|"dst":"(?([A-Z0-9a-z\s]+))"
#这是结果获取
#xmlout|./
#这是xml输出路径,默认可以不用去改
枚举表格为默认名称 Sheet2 或手动修改成 @@config
默认第一个表格的字段与 配置表格字段相同,则为配置表格生成枚举类型
示例:
表格1 Sheet1
ID | 名字 | 头像 | 形象 | 战斗头像 | 品质 | 五行 |
---|---|---|---|---|---|---|
0 | str | str | str | str | 白 | 金 |
1 | 东方不败 | ZD_TX_dongfangbubai | 蓝 | 金 | ||
2 | 令狐冲 | ZD_TX_dongfangbubai | 蓝 | 金 | ||
3 | 慕容复 | ZD_TX_dongfangbubai | 紫 | 金 | ||
4 | 段誉 | ZD_TX_dongfangbubai | 紫 | 金 | ||
5 | 郭靖 | ZD_TX_dongfangbubai | 橙 | 金 | ||
6 | 小龙女 | ZD_TX_dongfangbubai | 橙 | 金 |
表格2 Sheet2
五行 | 品质 |
---|---|
金 | 白 |
木 | 蓝 |
水 | 紫 |
火 | 橙 |
土 |
生成
//Code Gen By GeneratorSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
//Start Run:
//CodeBase file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/Extensions/0kdo2r54.zzi/GeneratorSample.dll
//wszInputFilePath:C:\Users\fj\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\英雄表.xlsx
//wszDefaultNamespace:ConsoleApplication1
//BasePath:C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\0kdo2r54.zzi
//Code Gen By ExcelCodeBuilder V1.3 ...
//Addin path E:\VSAddIn\Addin\CsvCodeGen.exe
using System;
using System.Collections.Generic;
using System.Xml;
namespace ConsoleApplication1 {
///
/// 五行
///
public enum Enum_TheFiveLine{
///
/// 金
///
Gold,
///
/// 木
///
Wood,
///
/// 水
///
Water,
///
/// 火
///
Fire,
///
/// 土
///
Soil,
}
///
/// 品质
///
public enum Enum_Quality{
///
/// 白
///
White,
///
/// 蓝
///
Blue,
///
/// 紫
///
Purple,
///
/// 橙
///
Orange,
}
}
//eunms Gen By ExcelCodeBuilder ...
//xmlout C:\Users\fj\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\\
namespace ConsoleApplication1
{
///
/// 英雄表
///
[Serializable]
//[Config(Index = 0)]
public partial class HeroWatch
#if MA
:IMemberAccessor
#endif
{
///
/// ID
///
//ConfigValue _ID ;
int _ID;
///
/// ID
///
[ConfigItemMap("ID")]
public int ID {
get{
return _ID;
}
set {
_ID = value;
}
}
///
/// 名字
///
//ConfigValue _Name ;
String _Name;
///
/// 名字
///
[ConfigItemMap("名字")]
public String Name {
get{
return _Name;
}
set {
_Name = value;
}
}
///
/// 头像
///
//ConfigValue _HeadPortrait ;
String _HeadPortrait;
///
/// 头像
///
[ConfigItemMap("头像")]
public String HeadPortrait {
get{
return _HeadPortrait;
}
set {
_HeadPortrait = value;
}
}
///
/// 形象
///
//ConfigValue _Image ;
String _Image;
///
/// 形象
///
[ConfigItemMap("形象")]
public String Image {
get{
return _Image;
}
set {
_Image = value;
}
}
///
/// 战斗头像
///
//ConfigValue _BattleAvatar ;
String _BattleAvatar;
///
/// 战斗头像
///
[ConfigItemMap("战斗头像")]
public String BattleAvatar {
get{
return _BattleAvatar;
}
set {
_BattleAvatar = value;
}
}
///
/// 品质
///
//ConfigValue _Quality ;
String _Quality;
///
/// 品质
///
[ConfigItemMap("品质")]
public String Quality {
get{
return _Quality;
}
set {
_Quality = value;
}
}
///
/// 五行
///
//ConfigValue _TheFiveLine ;
Enum_TheFiveLine _TheFiveLine;
///
/// 五行
///
[ConfigItemMap("五行")]
public Enum_TheFiveLine TheFiveLine {
get{
return _TheFiveLine;
}
set {
_TheFiveLine = value;
}
}
///
/// 兵力
///
//ConfigValue _Troops ;
Int32 _Troops;
///
/// 兵力
///
[ConfigItemMap("兵力")]
public Int32 Troops {
get{
return _Troops;
}
set {
_Troops = value;
}
}
///
/// 攻击
///
//ConfigValue _Attack ;
Int32 _Attack;
///
/// 攻击
///
[ConfigItemMap("攻击")]
public Int32 Attack {
get{
return _Attack;
}
set {
_Attack = value;
}
}
///
/// 防御
///
//ConfigValue _Defense ;
Int32 _Defense;
///
/// 防御
///
[ConfigItemMap("防御")]
public Int32 Defense {
get{
return _Defense;
}
set {
_Defense = value;
}
}
///
/// 智力
///
//ConfigValue _Intelligence ;
Int32 _Intelligence;
///
/// 智力
///
[ConfigItemMap("智力")]
public Int32 Intelligence {
get{
return _Intelligence;
}
set {
_Intelligence = value;
}
}
///
/// 速度
///
//ConfigValue _Speed ;
Int32 _Speed;
///
/// 速度
///
[ConfigItemMap("速度")]
public Int32 Speed {
get{
return _Speed;
}
set {
_Speed = value;
}
}
///
/// 暴击
///
//ConfigValue _Crit ;
String _Crit;
///
/// 暴击
///
[ConfigItemMap("暴击")]
public String Crit {
get{
return _Crit;
}
set {
_Crit = value;
}
}
///
/// 韧性
///
//ConfigValue _Toughness ;
String _Toughness;
///
/// 韧性
///
[ConfigItemMap("韧性")]
public String Toughness {
get{
return _Toughness;
}
set {
_Toughness = value;
}
}
///
/// 兵力成长
///
//ConfigValue _TroopGrowth ;
Int32 _TroopGrowth;
///
/// 兵力成长
///
[ConfigItemMap("兵力成长")]
public Int32 TroopGrowth {
get{
return _TroopGrowth;
}
set {
_TroopGrowth = value;
}
}
///
/// 攻击成长
///
//ConfigValue _AttackGrowth ;
Single _AttackGrowth;
///
/// 攻击成长
///
[ConfigItemMap("攻击成长")]
public Single AttackGrowth {
get{
return _AttackGrowth;
}
set {
_AttackGrowth = value;
}
}
///
/// 防御成长
///
//ConfigValue _DefenseGrowth ;
Single _DefenseGrowth;
///
/// 防御成长
///
[ConfigItemMap("防御成长")]
public Single DefenseGrowth {
get{
return _DefenseGrowth;
}
set {
_DefenseGrowth = value;
}
}
///
/// 智力成长
///
//ConfigValue _IntellectualGrowth ;
Single _IntellectualGrowth;
///
/// 智力成长
///
[ConfigItemMap("智力成长")]
public Single IntellectualGrowth {
get{
return _IntellectualGrowth;
}
set {
_IntellectualGrowth = value;
}
}
///
/// 速度成长
///
//ConfigValue _SpeedGrowth ;
Single _SpeedGrowth;
///
/// 速度成长
///
[ConfigItemMap("速度成长")]
public Single SpeedGrowth {
get{
return _SpeedGrowth;
}
set {
_SpeedGrowth = value;
}
}
///
/// 战斗技能
///
//ConfigValue _CombatSkills ;
Int32 _CombatSkills;
///
/// 战斗技能
///
[ConfigItemMap("战斗技能")]
public Int32 CombatSkills {
get{
return _CombatSkills;
}
set {
_CombatSkills = value;
}
}
///
/// 内政技能1
///
//ConfigValue _HomeSkills1 ;
Int32 _HomeSkills1;
///
/// 内政技能1
///
[ConfigItemMap("内政技能1")]
public Int32 HomeSkills1 {
get{
return _HomeSkills1;
}
set {
_HomeSkills1 = value;
}
}
///
/// 内政技能2
///
//ConfigValue _HomeSkills2 ;
Int32 _HomeSkills2;
///
/// 内政技能2
///
[ConfigItemMap("内政技能2")]
public Int32 HomeSkills2 {
get{
return _HomeSkills2;
}
set {
_HomeSkills2 = value;
}
}
#if !RELEASE_CLIENT
string xmldata = null;
public override string ToString()
{
return xmldata;
}
#endif
///
/// ID
///
//public string xmlcrc { get;set; }
public bool CheckId()
{
if (ID > 0)
{
return true;
}
return false;
}
public int GetId()
{
return ConfigHelper.ConvertTo<int>(ID);
}
public void Parse(XmlNode xmlnode)
{
#if !RELEASE_CLIENT
xmldata = xmlnode.InnerXml;
#endif
//xmlcrc = xmlnode.InnerXml.GetHashCode().ToString();
foreach(XmlNode node in xmlnode)
{
var localnode = node;
switch(node.Name)
{
case "ID":
this._ID = ConfigHelper.ConvertTo<int>(localnode.InnerText);
break;
case "名字":
this._Name = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "头像":
this._HeadPortrait = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "形象":
this._Image = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "战斗头像":
this._BattleAvatar = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "品质":
this._Quality = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "五行":
this._TheFiveLine = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "兵力":
this._Troops = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "攻击":
this._Attack = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "防御":
this._Defense = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "智力":
this._Intelligence = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "速度":
this._Speed = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "暴击":
this._Crit = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "韧性":
this._Toughness = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "兵力成长":
this._TroopGrowth = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "攻击成长":
this._AttackGrowth = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "防御成长":
this._DefenseGrowth = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "智力成长":
this._IntellectualGrowth = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "速度成长":
this._SpeedGrowth = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "战斗技能":
this._CombatSkills = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "内政技能1":
this._HomeSkills1 = ConfigHelper.ConvertTo(localnode.InnerText);
break;
case "内政技能2":
this._HomeSkills2 = ConfigHelper.ConvertTo(localnode.InnerText);
break;
}
}
}
public object GetValue(object instance, string memberName)
{
if (memberName == "ID")
{
return ID;
}
if (memberName == "Name")
{
return Name;
}
if (memberName == "HeadPortrait")
{
return HeadPortrait;
}
if (memberName == "Image")
{
return Image;
}
if (memberName == "BattleAvatar")
{
return BattleAvatar;
}
if (memberName == "Quality")
{
return Quality;
}
if (memberName == "TheFiveLine")
{
return TheFiveLine;
}
if (memberName == "Troops")
{
return Troops;
}
if (memberName == "Attack")
{
return Attack;
}
if (memberName == "Defense")
{
return Defense;
}
if (memberName == "Intelligence")
{
return Intelligence;
}
if (memberName == "Speed")
{
return Speed;
}
if (memberName == "Crit")
{
return Crit;
}
if (memberName == "Toughness")
{
return Toughness;
}
if (memberName == "TroopGrowth")
{
return TroopGrowth;
}
if (memberName == "AttackGrowth")
{
return AttackGrowth;
}
if (memberName == "DefenseGrowth")
{
return DefenseGrowth;
}
if (memberName == "IntellectualGrowth")
{
return IntellectualGrowth;
}
if (memberName == "SpeedGrowth")
{
return SpeedGrowth;
}
if (memberName == "CombatSkills")
{
return CombatSkills;
}
if (memberName == "HomeSkills1")
{
return HomeSkills1;
}
if (memberName == "HomeSkills2")
{
return HomeSkills2;
}
return null;
}
public void SetValue(object instance, string memberName, object newValue)
{
var obj = instance as HeroWatch;
if (memberName == "ID")
{
obj.ID = (int)newValue;
}
if (memberName == "Name")
{
obj.Name = (String)newValue;
}
if (memberName == "HeadPortrait")
{
obj.HeadPortrait = (String)newValue;
}
if (memberName == "Image")
{
obj.Image = (String)newValue;
}
if (memberName == "BattleAvatar")
{
obj.BattleAvatar = (String)newValue;
}
if (memberName == "Quality")
{
obj.Quality = (String)newValue;
}
if (memberName == "TheFiveLine")
{
obj.TheFiveLine = (Enum_TheFiveLine)newValue;
}
if (memberName == "Troops")
{
obj.Troops = (Int32)newValue;
}
if (memberName == "Attack")
{
obj.Attack = (Int32)newValue;
}
if (memberName == "Defense")
{
obj.Defense = (Int32)newValue;
}
if (memberName == "Intelligence")
{
obj.Intelligence = (Int32)newValue;
}
if (memberName == "Speed")
{
obj.Speed = (Int32)newValue;
}
if (memberName == "Crit")
{
obj.Crit = (String)newValue;
}
if (memberName == "Toughness")
{
obj.Toughness = (String)newValue;
}
if (memberName == "TroopGrowth")
{
obj.TroopGrowth = (Int32)newValue;
}
if (memberName == "AttackGrowth")
{
obj.AttackGrowth = (Single)newValue;
}
if (memberName == "DefenseGrowth")
{
obj.DefenseGrowth = (Single)newValue;
}
if (memberName == "IntellectualGrowth")
{
obj.IntellectualGrowth = (Single)newValue;
}
if (memberName == "SpeedGrowth")
{
obj.SpeedGrowth = (Single)newValue;
}
if (memberName == "CombatSkills")
{
obj.CombatSkills = (Int32)newValue;
}
if (memberName == "HomeSkills1")
{
obj.HomeSkills1 = (Int32)newValue;
}
if (memberName == "HomeSkills2")
{
obj.HomeSkills2 = (Int32)newValue;
}
}
}
///
/// 英雄表
///
public partial class HeroWatchConfigs
{
public readonly static string path = ConfigHelper.GetXmlPath + "HeroWatch.xml";
private static HeroWatchConfigs _Configs;
public static HeroWatchConfigs Configs
{
get
{
if (_Configs == null)
{
var config = Datas;
}
return _Configs;
}
}
public static DateTime LastRead { get ; private set;}
static Dictionary<int, HeroWatch> DatasMap { get; set; }
public HeroWatch this[int key]
{
get { return DatasMap[key]; }
}
public static List Datas
{
get
{
if (_Configs == null)
{
ConvertInit();
var cfgs = new HeroWatchConfigs();
cfgs.Init(null);
LastRead = DateTime.Now;
_Configs = cfgs;
}
return Configs.DataList;
}
}
public static void ConvertInit(){
ExctionClass.RegEnumConvert(o =>
{
var str = o.ToString();
switch (str)
{
case "金":
return Enum_TheFiveLine.Gold;
case "木":
return Enum_TheFiveLine.Wood;
case "水":
return Enum_TheFiveLine.Water;
case "火":
return Enum_TheFiveLine.Fire;
case "土":
return Enum_TheFiveLine.Soil;
}
return Enum_TheFiveLine.Gold;
});
ExctionClass.RegEnumConvertBack(o =>
{
var str = (Enum_TheFiveLine)o;
switch (str)
{
case Enum_TheFiveLine.Gold:
return "金";
case Enum_TheFiveLine.Wood:
return "木";
case Enum_TheFiveLine.Water:
return "水";
case Enum_TheFiveLine.Fire:
return "火";
case Enum_TheFiveLine.Soil:
return "土";
}
return "";
});
ExctionClass.RegEnumConvert(o =>
{
var str = o.ToString();
switch (str)
{
case "白":
return Enum_Quality.White;
case "蓝":
return Enum_Quality.Blue;
case "紫":
return Enum_Quality.Purple;
case "橙":
return Enum_Quality.Orange;
}
return Enum_Quality.White;
});
ExctionClass.RegEnumConvertBack(o =>
{
var str = (Enum_Quality)o;
switch (str)
{
case Enum_Quality.White:
return "白";
case Enum_Quality.Blue:
return "蓝";
case Enum_Quality.Purple:
return "紫";
case Enum_Quality.Orange:
return "橙";
}
return "";
});
}
public static void ReLoad(){
_Configs = null;
//var x = Datas;
}
private List _dateList = new List();
public List DataList
{
get { return _dateList; }
set { _dateList = value; }
}
public void Init(string inputxml)
{
if (string.IsNullOrEmpty(inputxml))
{
inputxml = ConfigHelper.GetXml(path, this.Init);
}
if ( string.IsNullOrEmpty( inputxml) || inputxml.StartsWith("<"))
{
DataList.Clear();
XmlDocument doc = new XmlDocument();
XmlReader reader = null;
if (inputxml != null)
{
doc.LoadXml(inputxml);
}
else
{
if (System.IO.File.Exists(path))
{
System.IO.StreamReader file = null;
try
{
file = System.IO.File.OpenText(path);
inputxml = file.ReadToEnd();
doc.LoadXml(inputxml);
}
finally
{
if (file != null)
{
file.Close();
file.Dispose();
}
}
}
else
{
return;
}
}
XmlNode xn = doc.ChildNodes[1];
foreach (XmlNode node in xn.ChildNodes)
{
try{
var item = new HeroWatch();
item.Parse(node);
if(item.CheckId()){
DataList.Add(item);
}
}
catch (Exception e)
{
ConfigHelper.Log("Lost:" + node.InnerXml + " " + e.ToString());
}
}
}
else
{
#if !SV
DataList = ConfigHelper.ToObject>(inputxml);
#endif
}
DatasMap= new Dictionary<int, HeroWatch>();
foreach (var item in DataList)
{
DatasMap[item.GetId()] = item;
}
}
}
}
//User Plug:CsvCodeGen.exe Type:CsvCodeGen.ExcelCodeBuilder