using System.Collections.Generic; using System.Data; using System.IO; using Newtonsoft.Json; using System.Text; using System; using ExcelDataReader; namespace CG.UTility { public class ExcelUtility { /// /// 表格数据集合 /// private DataSet mResultSet; /// /// 构造函数 /// /// Excel file. public ExcelUtility(string excelFile) { FileStream mStream = File.Open(excelFile, FileMode.Open, FileAccess.Read); IExcelDataReader mExcelReader = ExcelReaderFactory.CreateOpenXmlReader(mStream); mResultSet = mExcelReader.AsDataSet(); } /// /// 转换为Json /// string int float double bool /// /// Json文件路径 /// 表头行数 public void ConvertToJson(string jsonPath, Encoding encoding) { //判断Excel文件中是否存在数据表 if (mResultSet.Tables.Count < 1) return; string json = null; Dictionary>> typeDic = new Dictionary>>(); for (int x = 0; x < mResultSet.Tables.Count; x++) { DataTable mSheet = mResultSet.Tables[x]; if (mResultSet.Tables.Count > 1) { List> tabDic = GetTableDic(mSheet); Dictionary>> typeItem = new Dictionary>>(); string[] names = mResultSet.Tables[x].TableName.Split('_'); typeDic.Add(names[0], tabDic); json = JsonConvert.SerializeObject(typeDic, Formatting.Indented); } else { List> tabDic = GetTableDic(mSheet); json = JsonConvert.SerializeObject(tabDic, Formatting.Indented); } } //写入文件 CG.UTility.FileHandle.CheckFile(jsonPath, true); using (FileStream fileStream = new FileStream(jsonPath, FileMode.Create, FileAccess.Write)) { using (TextWriter textWriter = new StreamWriter(fileStream, encoding)) { textWriter.Write(json); } } } private List> GetTableDic(DataTable sheet) { //判断数据表内是否存在数据 if (sheet.Rows.Count < 1) return null; //读取数据表行数和列数 int rowCount = sheet.Rows.Count; int colCount = sheet.Columns.Count; //准备一个列表存储整个表的数据 List> table = new List>(); //读取数据 for (int i = 4; i < rowCount; i++) { //准备一个字典存储每一行的数据 if (string.IsNullOrEmpty(sheet.Rows[i][0].ToString())) continue; Dictionary row = new Dictionary(); for (int j = 0; j < colCount; j++) { //如果此列没有字段名,则跳过 if (string.IsNullOrEmpty(sheet.Rows[3][j].ToString())) continue; //读取第4行数据作为字段名 string field = sheet.Rows[3][j].ToString(); field = field.Trim(); //读取第3行数据作为字段类型 string typestring = sheet.Rows[2][j].ToString(); typestring = typestring.ToLower().Trim(); string valuestr = sheet.Rows[i][j].ToString(); valuestr = valuestr.Trim(); //Key-Value对应 按类型存放 switch (typestring) { case "int": if (valuestr != "") { row[field] = Convert.ToInt32(valuestr); } else { row[field] = 0; } break; case "float": if (valuestr != "") { row[field] = float.Parse(valuestr); } else { row[field] = 0; } break; case "double": if (valuestr != "") { row[field] = Convert.ToDouble(valuestr); } else { row[field] = 0; } break; case "bool": if (valuestr == "0" || valuestr == "fasle" || valuestr == "") { row[field] = false; } else { row[field] = true; } break; default: row[field] = valuestr; break; } } //添加到表数据中 table.Add(row); } return table; } /// /// 转换为Json /// string int float double bool /// /// Json文件路径 /// 表头行数 public void ConvertToCSharp(string CSharpPath, Encoding encoding) { //判断Excel文件中是否存在数据表 if (mResultSet.Tables.Count < 1) return; //默认读取第一个数据表 DataTable mSheet = mResultSet.Tables[0]; string outname = mSheet.TableName; //CSharpPath = CSharpPath + outname + ".cs"; if (File.Exists(CSharpPath)) return; StringBuilder CSharp = new StringBuilder(); CSharp.Append("namespace ZXK.LouDiXvMuNiu" + Environment.NewLine); CSharp.Append("{" + Environment.NewLine); CSharp.Append("public class " + Path.GetFileNameWithoutExtension(CSharpPath) + Environment.NewLine); CSharp.Append("{" + Environment.NewLine); //判断数据表内是否存在数据 if (mSheet.Rows.Count < 1) return; //读取数据表行数和列数 int rowCount = mSheet.Rows.Count; int colCount = mSheet.Columns.Count; for (int i = 0; i < colCount; i++) {//列优先读取,每列读取前四个 //如果此列没有字段名,则跳过 if (mSheet.Rows[3][i] == null || string.IsNullOrEmpty(mSheet.Rows[3][i].ToString())) continue; CSharp.Append("//" + mSheet.Rows[1][i].ToString() + Environment.NewLine); CSharp.Append("public " + mSheet.Rows[2][i].ToString() + " " + mSheet.Rows[3][i].ToString() + ";" + Environment.NewLine); } CSharp.Append("}" + Environment.NewLine); CSharp.Append("}" + Environment.NewLine); //写入文件 CG.UTility.FileHandle.CheckFile(CSharpPath, true); using (FileStream fileStream = new FileStream(CSharpPath, FileMode.Create, FileAccess.Write)) { using (TextWriter textWriter = new StreamWriter(fileStream, encoding)) { textWriter.Write(CSharp.ToString()); } } } } }