unity 使用 NPOI 讀取創建Excle


 所需dll          

網盤下載   

鏈接: https://pan.baidu.com/s/12K0eZMt6JGfQlRKYDOJ9dA 提取碼: n5jx 

 

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

public class OperationExcel : MonoBehaviour
{

    
    private void Start()
    {
         ReadExcel_NPOI();
         CreateExcel_NPOI();     
    }
    public Text text;
    string path = @"C://Users/Administrator/Desktop/20200821目錄.xlsx";
    string Cpath = @"C://Users/Administrator/Desktop/2.xlsx";
    //  NPOI  讀取 Excle
    void ReadExcel_NPOI()
    {
        /*
         //excel 2007之前版本
        HSSFWorkbook Mybook;
        using (FileStream fileStream=new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read)) {
            Mybook = new HSSFWorkbook(fileStream);
        }
        */

        //excel 2007之后版本
        XSSFWorkbook Mybook = new XSSFWorkbook(path);
        //獲取表名為  “男性” 的表
        ISheet sheet = Mybook.GetSheet("男性");
        //獲取行數
        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;
            //遍歷行的所有列
            for (int b = 0; b < sheet_row.LastCellNum; b++)
            {
                Debug.Log(sheet_row.GetCell(b).ToString());
                text.text += sheet_row.GetCell(b).ToString()+"--";
            }

        }
    }
  
    //  NPOI  創建 Excle   版本小於2007
    void CreateExcel_NPOI()
    {
        FileStream MyAddress = new FileStream(Cpath, 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();
    }

   

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM