86 lines
2.9 KiB
C#
86 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using AutoConfig;
|
|
using ICSharpCode.SharpZipLib.Zip;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.XSSF.UserModel;
|
|
using UnityEngine;
|
|
|
|
public class ExcelWrite
|
|
{
|
|
private static Dictionary<string, CfgMixKeyData> _cfgMixKeyDataList = new();
|
|
public static Dictionary<string, CfgMixKeyData> CfgMixKeyDataList => _cfgMixKeyDataList;
|
|
|
|
public static string StringConfigPath = Application.dataPath + "/../../Tool/Luban/" + "Datas/"+ "StringConfig.xlsx";
|
|
public static string StringConfigPath2 = Application.dataPath + "/../../Tool/Luban/" + "Datas/"+ "StringConfig2.xlsx";
|
|
|
|
public static IWorkbook _GetWorkBook(string excelPath, FileStream fs)
|
|
{
|
|
if (fs == null)
|
|
{
|
|
Debug.LogError("无法打开文件{0},跳过处理:" + excelPath);
|
|
return null;
|
|
}
|
|
|
|
var isXlsx = excelPath.EndsWith(".xlsx", System.StringComparison.CurrentCultureIgnoreCase);
|
|
if (isXlsx)
|
|
{
|
|
IWorkbook workBook = new XSSFWorkbook(fs);
|
|
return workBook;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static bool WriteToStringCfg()
|
|
{
|
|
try
|
|
{
|
|
var fs = new FileStream(StringConfigPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
|
|
var workbook = _GetWorkBook(StringConfigPath, fs);
|
|
if (workbook == null)
|
|
{
|
|
DebugUtil.LogError($"{StringConfigPath} 不存在,请检查传入路径");
|
|
return false;
|
|
}
|
|
|
|
var fs2 = new FileStream(StringConfigPath2, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
|
|
var zipStream = new ZipOutputStream(fs2);
|
|
workbook.SetActiveSheet(0);
|
|
var sheetTest = workbook.GetSheetAt(0);
|
|
var rowTest = sheetTest.GetRow(0);
|
|
var cellT = rowTest.GetCell(0);
|
|
cellT.SetCellValue("Test000");
|
|
|
|
workbook.Write(zipStream);
|
|
|
|
fs.Close();
|
|
return true;
|
|
// var fs2 = new FileStream(StringConfigPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
|
|
// foreach (var kv in _cfgMixKeyDataList)
|
|
// {
|
|
// var data = kv.Value;
|
|
// var sheet = workbook.GetSheetAt(data.sheetIndex);
|
|
// var row = sheet.GetRow(data.rowIndex);
|
|
// var cell = row.GetCell(data.lineIndex);
|
|
// cell.SetCellValue(data.value);
|
|
// }
|
|
// workbook.Write(fs2);
|
|
// fs.Close();
|
|
// fs2.Close();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
public static bool CheckWrite2Config()
|
|
{
|
|
return WriteToStringCfg();
|
|
}
|
|
} |