loudixvmuniu/Assets/Third/Excel/ExcelUtility.cs

207 lines
8.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2025-01-02 12:15:45 +08:00
using System.Data;
using System.IO;
using Newtonsoft.Json;
using System.Text;
using System;
using ExcelDataReader;
2025-01-02 12:15:45 +08:00
namespace CG.UTility
{
public class ExcelUtility
{
/// <summary>
/// 表格数据集合
/// </summary>
private DataSet mResultSet;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="excelFile">Excel file.</param>
public ExcelUtility(string excelFile)
{
FileStream mStream = File.Open(excelFile, FileMode.Open, FileAccess.Read);
IExcelDataReader mExcelReader = ExcelReaderFactory.CreateOpenXmlReader(mStream);
mResultSet = mExcelReader.AsDataSet();
}
/// <summary>
/// 转换为Json
/// string int float double bool
/// </summary>
/// <param name="jsonPath">Json文件路径</param>
/// <param name="Header">表头行数</param>
public void ConvertToJson(string jsonPath, Encoding encoding)
{
//判断Excel文件中是否存在数据表
if (mResultSet.Tables.Count < 1) return;
string json = null;
Dictionary<string, List<Dictionary<string, object>>> typeDic = new Dictionary<string, List<Dictionary<string, object>>>();
for (int x = 0; x < mResultSet.Tables.Count; x++)
{
DataTable mSheet = mResultSet.Tables[x];
if (mResultSet.Tables.Count > 1)
{
List<Dictionary<string, object>> tabDic = GetTableDic(mSheet);
Dictionary<string, List<Dictionary<string, object>>> typeItem = new Dictionary<string, List<Dictionary<string, object>>>();
string[] names = mResultSet.Tables[x].TableName.Split('_');
typeDic.Add(names[0], tabDic);
2025-01-02 12:15:45 +08:00
json = JsonConvert.SerializeObject(typeDic, Formatting.Indented);
}
else
{
List<Dictionary<string, object>> 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<Dictionary<string, object>> GetTableDic(DataTable sheet)
{
//判断数据表内是否存在数据
if (sheet.Rows.Count < 1)
return null;
//读取数据表行数和列数
int rowCount = sheet.Rows.Count;
int colCount = sheet.Columns.Count;
//准备一个列表存储整个表的数据
List<Dictionary<string, object>> table = new List<Dictionary<string, object>>();
//读取数据
for (int i = 4; i < rowCount; i++)
{
//准备一个字典存储每一行的数据
if (string.IsNullOrEmpty(sheet.Rows[i][0].ToString())) continue;
Dictionary<string, object> row = new Dictionary<string, object>();
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;
}
/// <summary>
/// 转换为Json
/// string int float double bool
/// </summary>
/// <param name="JsonPath">Json文件路径</param>
/// <param name="Header">表头行数</param>
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);
2025-01-02 12:15:45 +08:00
//判断数据表内是否存在数据
if (mSheet.Rows.Count < 1)
return;
2025-01-02 12:15:45 +08:00
//读取数据表行数和列数
int rowCount = mSheet.Rows.Count;
int colCount = mSheet.Columns.Count;
2025-01-02 12:15:45 +08:00
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);
2025-01-02 12:15:45 +08:00
//写入文件
CG.UTility.FileHandle.CheckFile(CSharpPath, true);
using (FileStream fileStream = new FileStream(CSharpPath, FileMode.Create, FileAccess.Write))
{
using (TextWriter textWriter = new StreamWriter(fileStream, encoding))
2025-01-02 12:15:45 +08:00
{
textWriter.Write(CSharp.ToString());
2025-01-02 12:15:45 +08:00
}
}
2025-01-02 12:15:45 +08:00
}
}
}