Unity中读取和创建Excel文件(支持PC端、Android端)

注意事项:开发前需要导入NPOI 相关DLL库
在这里插入图片描述
DLL库下载链接:https://download.csdn.net/download/U3DCoder/40739654

相关读取和创建代码如下所示:

using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using UnityEngine;
using System.Collections.Generic;

public class ReadExcelManager : Singleton<ReadExcelManager>
{
    
    
    private string filePath;
    private Dictionary<string, string> dataDic=new Dictionary<string, string>();
    /// <summary>
    /// 使用NPOI库读取 Excel
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="sheetName"></param>
    public void ReadExcelWithNPOI(string fileName, string sheetName)
    {
    
    
        dataDic = new Dictionary<string, string>();
        if (Application.platform == RuntimePlatform.Android)
        {
    
    
            filePath = "/storage/emulated/0/" + "/ExcelFiles/" + fileName;
        }
        else
        {
    
    
            filePath = Application.streamingAssetsPath + "/ExcelFiles/" + fileName;
        }

        string extension = Path.GetExtension(filePath);
        ISheet sheet;
        if (extension.Equals(".xlsx"))
        {
    
    
            //excel 2007之后版本
            XSSFWorkbook xSSFWorkbook;
            xSSFWorkbook = new XSSFWorkbook(filePath);
            sheet = xSSFWorkbook.GetSheet(sheetName);
        }
        else
        {
    
    
            //excel 2007之前版本
            HSSFWorkbook hSSFWorkbook;
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
    
    
                hSSFWorkbook = new HSSFWorkbook(fileStream);
                //获取对应 的表
                sheet = hSSFWorkbook.GetSheet(sheetName);
            }
        }

        //获取行数
        int rowLength = sheet.LastRowNum;
        Debug.Log(rowLength);

        //遍历所有行
        for (int i = 0; i <= sheet.LastRowNum; i++)
        {
    
    
            //获取行
            IRow sheet_row = sheet.GetRow(i);
            //当前行为null时,跳过此行
            if (sheet_row == null) continue;
            //Debug.LogError("ID::" + sheet_row.GetCell(0).ToString());
            //Debug.LogError("内容::" + sheet_row.GetCell(1).ToString());
            //遍历行的所有列
            for (int j = 0; j < sheet_row.LastCellNum; j++)
            {
    
    
                Debug.LogError("内容::" + sheet_row.GetCell(j).ToString());
            }
        }

    }

    /// <summary>
    /// 使用NPOI库创建 Excel 版本小于2007
    /// </summary>
    /// <param name="createPpath">创建的文件路径包含文件名称</param>
    void CreateExcelWithNPOI(string createPpath)
    {
    
    
        FileStream MyAddress = new FileStream(createPpath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        HSSFWorkbook MyWorkbook = new HSSFWorkbook();
        //创建表一
        HSSFSheet Sheet01 = (HSSFSheet)MyWorkbook.CreateSheet("表一");
        for (int i = 0; i < 5; i++)
        {
    
    
            //创建行
            HSSFRow row = (HSSFRow)Sheet01.CreateRow(i);
            for (int b = 0; b < 3; b++)
            {
    
    
                //创建列
                HSSFCell cell = (HSSFCell)row.CreateCell(b);
                //添加内容
                cell.SetCellValue("数值" + i * b);
                if (b < 2) continue;
                //创建单元格样式
                cell.CellStyle = MyWorkbook.CreateCellStyle();
                //边界右侧
                cell.CellStyle.BorderRight = BorderStyle.Thin;
                //边框底部
                cell.CellStyle.BorderBottom = BorderStyle.Dashed;
                //下边框颜色
                cell.CellStyle.BottomBorderColor = HSSFColor.Red.Index;

                //创建字体样式
                HSSFFont MyFont = (HSSFFont)MyWorkbook.CreateFont();
                MyFont.FontName = "Tahoma";
                MyFont.FontHeightInPoints = 14;
                MyFont.Color = HSSFColor.Gold.Index;
                MyFont.Boldweight = (short)FontBoldWeight.Bold;
                //设置字体样式
                cell.CellStyle.SetFont(MyFont);
            }
            //创建单元格样式
            row.RowStyle = MyWorkbook.CreateCellStyle();
            //边框底部
            row.RowStyle.BorderBottom = BorderStyle.Double;
        }
        //创建表二
        HSSFSheet Sheet02 = (HSSFSheet)MyWorkbook.CreateSheet("表二");
        for (int i = 0; i < 5; i++)
        {
    
    
            HSSFRow row = (HSSFRow)Sheet02.CreateRow(i);
            for (int b = 0; b < 3; b++)
            {
    
    
                HSSFCell cell = (HSSFCell)row.CreateCell(b);
                cell.SetCellValue("数值" + i * b);
            }
        }
        //写入到文件流
        MyWorkbook.Write(MyAddress);
        //关闭
        MyWorkbook.Close();
        //关闭文件流
        MyAddress.Dispose();
    }
}

参考链接:https://www.cnblogs.com/G993/p/13931766.html

猜你喜欢

转载自blog.csdn.net/U3DCoder/article/details/121290558