Unity 读取word、excel文件(NPOI)

        

1. 下载 NPIO相关dll

2. 读 Word (.docx) 文件示例:

using System;  
using System.IO;  
using UnityEngine;  
using NPOI.XWPF.UserModel; // for .docx  
using NPOI.SS.UserModel;   // if you need additional support  
using System.Text; // for StringBuilder  

public class WordReader : MonoBehaviour  
{  
    public string filePath; // Word file path  

    void Start()  
    {  
        if (!string.IsNullOrEmpty(filePath))  
        {  
            ReadWordFile(filePath);  
        }  
        else  
        {  
            Debug.LogError("Please set the path to the Word file.");  
        }  
    }  

    void ReadWordFile(string path)  
    {  
        try  
        {  
            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))  
            {  
                // Open the Word document  
                XWPFDocument doc = new XWPFDocument(stream);  
                StringBuilder content = new StringBuilder();  

                // Read paragraphs  
                foreach (var para in doc.paragraphs)  
                {  
                    content.AppendLine(para.Text);  
                }  

                // Output content to Console  
                Debug.Log(content.ToString());  
            }  
        }  
        catch (Exception e)  
        {  
            Debug.LogError("Error reading Word file: " + e.Message);  
        }  
    }  
}

注意:① 在word打开时,读取会报错;② 只能读取每行文本,段落貌似无法读取。 

3. 读Excel 文件(.xlsx)文件示例:

using System;  
using System.IO;  
using UnityEngine;  
using NPOI.SS.UserModel;  
using NPOI.XSSF.UserModel;  

public class ExcelReader : MonoBehaviour  
{  
    public string filePath; // Excel file path  

    void Start()  
    {  
        if (!string.IsNullOrEmpty(filePath))  
        {  
            ReadExcelFile(filePath);  
        }  
        else  
        {  
            Debug.LogError("Please set the path to the Excel file.");  
        }  
    }  

    void ReadExcelFile(string path)  
    {  
        try  
        {  
            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))  
            {  
                // Open the Excel workbook  
                IWorkbook workbook = new XSSFWorkbook(stream);  
                ISheet sheet = workbook.GetSheetAt(0); // 读取第一个工作表  

                StringBuilder content = new StringBuilder();  

                // 遍历行和列  
                for (int row = 0; row <= sheet.LastRowNum; row++)  
                {  
                    IRow currentRow = sheet.GetRow(row);  
                    if (currentRow != null) // 确保行不为空  
                    {  
                        for (int col = 0; col < currentRow.LastCellNum; col++)  
                        {  
                            ICell cell = currentRow.GetCell(col);  
                            if (cell != null)  
                            {  
                                content.Append(cell.ToString() + "\t"); // 使用制表符分隔单元格  
                            }  
                        }  
                        content.AppendLine(); // 行结束换行  
                    }  
                }  

                // 输出内容到 Console  
                Debug.Log(content.ToString());  
            }  
        }  
        catch (Exception e)  
        {  
            Debug.LogError("Error reading Excel file: " + e.Message);  
        }  
    }  
}

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值