找回密码
 立即注册
查看: 247|回复: 0

[简易教程] Unity 使用 NOPI 操作Excel文件 修改内容 创建图表方案

[复制链接]
发表于 2022-8-10 12:34 | 显示全部楼层 |阅读模式
1.首先你要先导入nopi库文件,这我就不在说nopi下载与导入了。我们直接进入正题。
2.如何向Excel插入数据:第一种就是直接创建文档写入数据不过种方式很繁琐写起来很麻烦。这里提供excel学习地址:NOPI使用手册 - Areas - 博客园 。
第2种就是自己先设计好模板然后查找替换制定的内容:


public class NopiUtils{

   // 这里是我自己写的数据可自行定义
   public static Dictionary<string, string> dicWord = new Dictionary<string, string>();

   /// <summary>
    /// 将类转换为字典存储
    /// </summary>
    /// <param name="t"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static void SetFieldsToDictionary<T>(T t) where T : class
    {
        if (t == null)
            return;
        
        dicWord.Clear();
        Type type = typeof(T);
        int index = -1;
        foreach (var item in type.GetFields())
        {
            string strKey = "$" + item.Name + "$";
            string strValue = item.GetValue(t).ToString();
            dicWord.Add(strKey, strValue);
        }
    }

    /// <summary>
    /// 创建excel文件.xls
    /// </summary>
    /// <param name="tempFile"> 模板文件路径</param>
    /// <param name="saveFile"> 生成新的文件路径</param>
    public static void CreatFileExcel(string tempFile, string saveFile)
    {
        using (FileStream stream = File.OpenRead(tempFile))
        {
            HSSFWorkbook wb = new HSSFWorkbook(stream);
            ISheet sheet = wb.GetSheetAt(0);
            ISheet sheet1 = wb.GetSheetAt(1);
            SetExcelData(sheet);
            //这个是绘制 折线图
            DrawLineChat(sheet1);
            
            using (FileStream fs = File.Create(saveFile))
            {
                wb.Write(fs);
            }
        }
    }

    /// <summary>
    /// 导入Excel数据
    /// </summary>
    private static void SetExcelData(ISheet sheet)
    {
        for (int i = 0; i < sheet.LastRowNum; i++)
        {
            IRow row = sheet.GetRow(i);  //读取当前行数据
            if (row != null)
            {
                for (int j = 0; j < row.LastCellNum; j++) // 遍历列
                {
                    ICell cell = row.GetCell(j);  //当前表格
                    if (cell != null)
                    {
                        string currStr = cell.ToString(); //当前格数据
                        if (!string.IsNullOrEmpty(currStr))
                        {
                            //这里就是替换数据可自定义实现
                            foreach (KeyValuePair<string, string> kvp in dicWord)
                            {
                                if (currStr.Contains(kvp.Key))
                                {
                                    cell.SetCellValue(kvp.Value);
                                }
                            }
                        }
                    }
                }
            }
        }
//这里就完成了 替换制定模板的内容

3.如何生成图表呢 。其实也有2种方式:
(1)第一种:先创建表格然后根据表格绘制图表这个方式也是非常麻烦 而且效果也不好控制这里我发个链接:GitHub - nissl-lab/npoi-examples: To get started with NPOI,  here is all the official examples.
(2)第二种:我们通过l模板的方式可以巧妙地利用Excel强大的透视和图表功能。这个我先说下文件excel类型必须是.xls 我之前用的xlsx 这个类型是有问题的。
首先就是先创建模 在上面的模板里 创建透视图设置好轴和值这样图就出现了(其他类型图也是如此)把模板保存到unity StreamingAssets文件下



sheet1



sheet2


这样的话我们只需要往sheet1里添加数据就可以了。
/// <summary>
/// 绘制折线图
/// </summary>
private static void DrawLineChat(ISheet sheet)
{
        // 写数据自由发挥
        IRow row = sheet.CreateRow(9);
        row.CreateCell(0).SetCellValue(10);
        row.CreateCell(1).SetCellValue(150);
        row.CreateCell(2).SetCellValue(100);
} 其实到这里你会发现sheet1数据写进去了 但是透视图并没有 更新数据 必须自己手动刷新。所以你在制作模板时设置自动刷新 :右击透视表设置。


这样创建出来数据就会刷新啦。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-8 01:22 , Processed in 0.154628 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表