Unity写EXCEL

来源:互联网 发布:雪姨王琳撕网络喷子 编辑:程序博客网 时间:2024/06/05 17:04

unity环境:5.34f1

需要用到的dll:EPPlus.dll 下载地址:https://epplus.codeplex.com/downloads/get/1591093

里面有2个版本:1个4.0的、1个2.0的 用2.0的[因为Unity默认的.net框架是3.5]


如图

准备工作:新建Plugins文件夹 把dll拖进去


如图

下面上代码

using UnityEngine;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using OfficeOpenXml;
using Assets.Scripts.CameraControls;


namespace Assets.Scripts.Excel
{
    public static class ExcelHelper
    {
        public static void ExportExcel()
        {
            string outPutDir = Application.dataPath + "\\SaleData.xls";
            FileInfo newFile = new FileInfo(outPutDir);
            if (newFile.Exists)
            {
                newFile.Delete();  // ensures we create a new workbook  
                newFile = new FileInfo(outPutDir);
            }
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("车位售卖数据");
                worksheet.Cells[1, 1].Value = "序号";
                worksheet.Cells[1, 2].Value = "车位编号";
                worksheet.Cells[1, 3].Value = "长(mm)";
                worksheet.Cells[1, 4].Value = "宽(mm)";
                worksheet.Cells[1, 5].Value = "状态";
                int i = 1;
                List<SationNode> sationNodeList = StaticMemory.SastionInfomation.OrderBy(p=>p.No).ToList();
                foreach (SationNode node in sationNodeList)
                {
                    i++;
                    worksheet.Cells[i,1].Value = i.ToString();
                    worksheet.Cells[i, 2].Value = node.No;
                    worksheet.Cells[i, 3].Value = node.SationLong;
                    worksheet.Cells[i, 4].Value = node.SationWidth;
                    string state = "未售";
                    int saleFlag = PlayerPrefs.GetInt(node.Id);
                    switch (saleFlag)
                    {
                        case 0: state = "未售"; break;
                        case 1: state = "预售"; break;
                        case 2: state = "已售"; break;
                    }
                    worksheet.Cells[i, 5].Value = state;                  
                }
                package.Save();
            }           
        }
    }
}

效果