读取Excel

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Excel;
using System.Data;
using UnityEngine.UI;


public class TestExcel : MonoBehaviour {


// Use this for initialization
void Start () {


        FileStream stream = File.Open(Application.dataPath + "/Book.xlsx", FileMode.Open, FileAccess.Read);


        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);


        DataSet result = excelReader.AsDataSet();


        int columns = result.Tables[1].Columns.Count;
        int rows = result.Tables[1].Rows.Count;


        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                string nvalue = result.Tables[1].Rows[i][j].ToString();
                if (nvalue!="")
                {
                    print(nvalue);
                }
            }
        }
        excelReader.Close();


}

// Update is called once per frame
void Update () {

}


    //public void CreateExcel()
    //{
    //    HSSFWorkbook wk = new HSSFWorkbook();
    //}




    //
    /*
    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);


    //1. Reading from a binary Excel file ('97-2003 format; *.xls)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
    //...
    //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    //...
    //3. DataSet - The result of each spreadsheet will be created in the result.Tables
    DataSet result = excelReader.AsDataSet();
    //...
    //4. DataSet - Create column names from first row
    excelReader.IsFirstRowAsColumnNames = true;
    DataSet result = excelReader.AsDataSet();


    //5. Data Reader methods
    while (excelReader.Read())
    {
   //excelReader.GetInt32(0);
    }


    //6. Free resources (IExcelDataReader is IDisposable)
    excelReader.Close();
    */




}
博主设置当前文章不允许评论。

没有更多推荐了,返回首页