(2022年兑现承诺,已更新最新代码,经历过一个无比失败的项目后,碰巧搞起excel,所以更新一下,天啊2019年到2022年3年间我居然都没做过excel??那我这3年都干了啥??)
这个包最大问题是不能加载CSV
也就是只能用.xlsx,不过问题不大
“对于那些说效能,喜欢搞二进制,搞到整个项目鸡飞狗跳的同学请绕路”
这里没有唯一答案,有的只是代码分享(也不是完整100%合你用的代码,请带着发散,批判思维阅读一下代码)
如你还不信邪,请看我另一个博客对这个Excel。dll处理库的详细介绍
看这么多API,下面这张图,类似的图,都看腻了。
本人还不是传统程序员,很多API都懒得看,很多程序员比我勤快,
然而网上一堆EPPlus的例子都是Create,并没有其他例子,
导致功能死活实现不了,实在没办法,从头看起。。。。。
使用EPPlus的好处坏处,如下:
*本身项目又excel导入工具,但还是缺从Unity导出
*EPPlus在美术的电脑(稍微不同的Unity Editor环境),引入有问题导致整个Editor无法编译
*EPPlus其实只支持xml,也就是2007之后的格式(微软在2007Excel + Document整个office套件都转成hxml格式了, 网上传说说4.4后有可能支持2003)
*EPPlus的API是挺工整的,例如Row(1) 和Cell[1,1],比很多旧的Office api工整很多,和国人习惯很相同,索引从1开始
新增
public static void PrintToExcel(string outputPath, List<List<string>> data)
{
//加上文件夹不存在的判断
if (!Directory.Exists(Path.GetDirectoryName(outputPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
}
//强制转为xlsx后缀
if (Path.GetFileNameWithoutExtension(outputPath) != ".xlsx")
{
outputPath = Path.GetDirectoryName(outputPath) + "/" + Path.GetFileNameWithoutExtension(outputPath) + ".xlsx";
}
if (data.Count == 0) return;
_datas = data;
EditorUtility.DisplayProgressBar("ExcelEditorUtil", "正在写入 Excel 文件...", 0.85f);
var newFile = new FileInfo(outputPath);
if (newFile.Exists)
{
newFile.Delete();
}
using (var package = new ExcelPackage(newFile))
{
//CreateWorksheet(package.Workbook.Worksheets);
CreateAndFillWorksheet(package.Workbook.Worksheets);
package.Save();
}
//AssetBundleFilesAnalyze.Clear();
EditorUtility.ClearProgressBar();
}
/// <summary>
/// 前面2行留空做title,从第3行开始填充数据
/// </summary>
/// <param name="wss"></param>
/// <param name="typeName"></param>
static void CreateAndFillWorksheet(ExcelWorksheets wss, string typeName = "")
{
string titleName = typeName + " 列表";
ExcelWorksheet ws = wss.Add(titleName);
int colIndex = 1;
if (_columns == null)
{
foreach (var col in _datas[0])
{
ws.Cells[2, colIndex].Value = "列 " + colIndex;
colIndex++;
}
}
else {
foreach (var col in _columns) {
ws.Cells[1, colIndex].Value = col.Type;
ws.Cells[2, colIndex].Value = col.Name;
colIndex++;
}
}
using (var range = ws.Cells[2, 1, 2, _datas.Count])
{
// 字体样式
range.Style.Font.Bold = true;
// 背景颜色
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
//range.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#DDEBF7"));
// 开启自动筛选
range.AutoFilter = true;
}
int cursor = 0;
foreach (var row in _datas)
{
colIndex = 1;
foreach (var v in row)
{
ws.Cells[3 + cursor, colIndex++].Value = v;
}
//第4次修复
cursor++;
}
}
调用方法
List<string> rows = new List<string>();
//一个个数据插入
string path = Application.dataPath + "/../PlayableData.xlsx";
PrintToExcel(path, new List<List<string>>() { rows });
修改(Read)
网上并没有使用EpPlus读取Excel代码,没想到,这是目前国内唯一,第一个写下这个写法的吧
(下面代码,在更新此博客后,又双叒修改了3次才改对,现在这版应该是逻辑完整的。。。)
public static void ReadExcel2ScriptObj() {
string path = Application.dataPath + "/../PlayableData.xlsx";
FileInfo existFile = new FileInfo(path);
using (var package = new ExcelPackage(existFile))
{
//Debug.Log(package.Workbook.Worksheets.Count);
var rows = Dump( package.Workbook.Worksheets[1]);
}
}
/// <summary>
/// EpPlus并没有直接访问数据Count的方法(没有面向对象);
/// 使用这个改良的读取的方法,限制条件是:第一行不能跳空
/// (一般第一行是唯一ID,所以能满足这个条件)
/// </summary>
/// <param name="ws"></param>
/// <returns></returns>
public static List<List<string>> Dump(ExcelWorksheet ws) {
Debug.Log("go du读到行数:"+ ws.Tables.Count);
//TODO:没做安全性(空数据)验证
//for (int i = 1; i < 100; i++)
//{
// if (ws.Cells[i, 1].Value == null)
// break;
// Debug.Log(ws.Cells[i, 1].Value);
//}
//TODO:没做安全性(空数据)验证,显示100列
int columnCount = 0;
int cursor = 1;
while (cursor<=100 && ws.Cells[1, cursor].Value != null)
{
cursor++;
columnCount++;
}
cursor = 1;
List<List<string>> rows = new List<List<string>>();
while (ws.Cells[cursor, 1].Value!=null)
{
List<string> row = new List<string>();
for (int i = 1; i <= columnCount; i++)
{
row.Add(ws.Cells[cursor, i].Value.ToString());
}
rows.Add(row);
cursor++;
}
return rows ;
}