using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;//编辑器的命名空间
using System.IO;//文件流
using Excel;//读取Excel
using System.Data;
public static class MyEditor
{
[MenuItem("我的工具/excel转成txt")]
public static void ExportExcelToTxt()
{
//_Excel文件夹路径
string assetPath = Application.dataPath + "/_Excel";
//获得Excel文件夹中的Excel文件
string[] files = Directory.GetFiles(assetPath, "*.xlsx");
for (int i = 0; i < files.Length; i++)
{
files[i] = files[i].Replace('\\', '/');
//通过文件流读取文件
using (FileStream fs = File.Open(files[i], FileMode.Open, FileAccess.Read))
{
//文件流转成excel对象
var excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
//获得excel数据
DataSet dataset = excelDataReader.AsDataSet();
//读取excel第一张表
DataTable table = dataset.Tables[0];
//将表中内容 读取后存储到对于的txt文件
readTableToTxt(files[i], table);
}
}
//刷新编辑器
AssetDatabase.Refresh();
}
private static void readTableToTxt(string filePath,DataTable table)
{
//获取文件名(不要文件后缀,生成与之名子相同的txt文件)
string fileName = Path.GetFileNameWithoutExtension(filePath);
//txt文件存贮的路径
string path = Application.dataPath + "/Resources/Data/" + fileName + ".txt";
//判断Resources/Data文件夹中是否已经存在了对于的txt文件,如果是则删除
if(File.Exists(path))
{
File.Delete(path);
}
//文件流创建Txt文件
using (FileStream fs=new FileStream(path,FileMode.Create))
{
using (StreamWriter sw=new StreamWriter (fs))
{
for (int row = 0; row < table.Rows.Count; row++)
{
DataRow dataRow = table.Rows[row];
string str = "";
for (int col = 0; col < table.Columns.Count; col++)
{
string val = dataRow[col].ToString();
//每一项tab分隔
str = str + val + "\t";
}
//写入
sw.Write(str);
//如果不是最后一行,换行
if(row!=table.Rows.Count-1)
{
sw.WriteLine();
}
}
}
}
}
}