美文网首页
Unity/C# 修改保存 Excel

Unity/C# 修改保存 Excel

作者: unlockc | 来源:发表于2021-11-26 17:21 被阅读0次
    1. 下载库文件放到Unity/C#工程里。链接: https://pan.baidu.com/s/1kg4J1OGpMDz_PI0v5wqfDQ 提取码: phqi 复制这段内容后打开百度网盘手机App,操作更方便哦
    2. 按需调用以下任意一个API都可以保存对应数据到excel
      注意:excel文件扩展名一定要是.xlsx
    using OfficeOpenXml;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    
    public class Wirte2ExcelUtil
    {
        public void WriteValue2Excel<T>(string path, int raw, int col, T val) 
        {
            throw new NotImplementedException("public void WriteValue2Excel<T>(int raw, int col, T val)");
            List<List<T>> matrixVals = new List<List<T>>(1);
            var list = new List<T>(1);
            list.Add(val);
            matrixVals.Add(list);
            WriteMatrixValues2Excel<T>(path, raw, col, matrixVals);
        }
    
        public void WriteHorizonValues2Excel<T>(string path, int raw, int col, List<T> vals)
        {
            throw new NotImplementedException("public void WriteHorizonValues2Excel<T>(int rawStart, int rawEnd, int col, List<T> vals)");
            List<List<T>> matrixVals = new List<List<T>>(1);
            matrixVals.Add(vals);
            WriteMatrixValues2Excel<T>(path, raw, col, matrixVals);
        }
    
        public void WriteVerticalValues2Excel<T>(string path, int raw, int col, List<T> vals)
        {
            throw new NotImplementedException("public void WriteVerticalValues2Excel<T>(int rawStart, int rawEnd, int col, List<T> vals)");
            List<List<T>> matrixVals = new List<List<T>>();
            foreach (var item in vals)
            {
                var list = new List<T>(1);
                list.Add(item);
                matrixVals.Add(list);
            }
            WriteMatrixValues2Excel<T>(path, raw, col, matrixVals);
        }
    
    
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">Excel文件路径</param>
        /// <param name="raw">从1开始</param>
        /// <param name="col">从1开始</param>
        /// <param name="matrixVals">外层是Vertical 数量,里层是Horizon 数量</param>
        public static void WriteMatrixValues2Excel<T>(string path, int raw, int col, List<List<T>> matrixVals)
        {
            FileInfo xlsxFile = new FileInfo(path);
    
            if (xlsxFile.Exists)
            {
                //通过ExcelPackage打开文件
                using (ExcelPackage package = new ExcelPackage(xlsxFile))
                {
                    //修改excel的第一个sheet,下标从1开始
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
    
                    for (int v = 0; v < matrixVals.Count; v++)//v:外层是Vertical
                    {
                        var vals = matrixVals[v];
                        for (int h = 0; h < vals.Count; h++)//h:里层是Horizon
                        {
                            worksheet.Cells[raw + v, col + h].Value = vals[h];
                        }
                    }
                    try
                    {
                        package.Save();
                        Debug.Log("<color=yellow>WriteToExcel Success</color>");
                    }
                    catch (Exception)
                    {
                        Debug.LogError("请先关闭:" + path);
                        throw;
                    }
    
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity/C# 修改保存 Excel

          本文链接:https://www.haomeiwen.com/subject/cmrxxrtx.html