码迷,mamicode.com
首页 > 其他好文 > 详细

NPOI Excel

时间:2014-09-17 15:02:02      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   io   os   ar   for   文件   

using UnityEngine;
using System.Collections;
using NPOI;
using Ionic.Zip;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Text;

public class Test : MonoBehaviour
{
    void Start ()
    {
        ExportXls();
    }
    public static void ExportXls()
    {
        StringBuilder sbr = new StringBuilder();
        using (FileStream fs = File.OpenRead(@"D:\UnityProject\ExcelTest\Assets\Resources\Book1.xls"))   //打开myxls.xls文件
        {
            HSSFWorkbook wk = new HSSFWorkbook(fs);   //把xls文件中的数据写入wk中
            for (int i = 0; i < wk.NumberOfSheets; i++)  //NumberOfSheets是myxls.xls中总共的表数
            {
                ISheet sheet = wk.GetSheetAt(i);   //读取当前表数据
                for (int j = 0; j <= sheet.LastRowNum; j++)  //LastRowNum 是当前表的总行数
                {
                    IRow row = sheet.GetRow(j);  //读取当前行数据
                    if (row != null)
                    {
                        
                        sbr.Append("-------------------------------------\r\n"); //读取行与行之间的提示界限
                        for (int k = 0; k <= row.LastCellNum; k++)  //LastCellNum 是当前行的总列数
                        {
                            ICell cell = row.GetCell(k);  //当前表格
                            
                            if (cell != null)
                            {
                                cell.SetCellValue("123123424412");
                                //Debug.Log(cell.ToString());
                                //sbr.Append(cell.ToString());   //获取表格中的数据并转换为字符串类型
                            }
                        }
                    }
                }
            }

            //using (FileStream fsWrite = File.OpenWrite(@"D:\UnityProject\ExcelTest\Assets\Resources\Book1.xls")) //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
            //{
            //    wk.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
            //    Debug.Log("写入完成");
            //}

            using (StreamWriter wr = new StreamWriter(new FileStream(@"D:\UnityProject\ExcelTest\Assets\Resources\Book2.xls", FileMode.CreateNew)))  //把读取xls文件的数据写入myText.txt文件中
            {
                wr.Write(wk);
                wr.Flush();
                Debug.Log("写入");
            }
        }

        

        ////sbr.ToString();

            
    }
}

NPOI.dll 

JsonFx.Json.dll

System.Data.dll

using UnityEngine;
using System.Collections;
using System;
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Collections.Generic;
using JsonFx.Json;
using System.IO;

/// <summary>
/// 获取Excel数据
/// </summary>
public class GetExcelData : MonoBehaviour
{
    public GameDataManager m_GameDataManager;
    public List<Achievement> m_PlayerDatas;
    public Achievement m_PlayerData;
    DataTable dtMyData = new DataTable("MyData");
    string str = "";
    string filepath;

    // Use this for initialization
    void Start()
    {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
        filepath = Application.dataPath + "/" + "AchievementData20140410.xls";

#elif UNITY_IPHONE
            filepath = Application.dataPath +"/Raw"+ "AchievementData20140410.xls";
#elif UNITY_ANDROID   //安卓
            filepath ="jar:file://" + Application.dataPath + "!/assets/"+"AchievementData20140410.xls";
#endif
        //////////////
        ReadXLS(filepath);
    }

    void ReadXLS(string filetoread)
    {
        string con = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq=" + filetoread + ";";
        Debug.Log(con);
        string yourQuery = "SELECT * FROM [Sheet1$]";
        OdbcConnection oCon = new OdbcConnection(con);
        OdbcCommand oCmd = new OdbcCommand(yourQuery, oCon);
        oCon.Open();
        OdbcDataReader rData = oCmd.ExecuteReader();
        dtMyData.Load(rData);
        rData.Close();
        oCon.Close();

        if (dtMyData.Rows.Count > 0)
        {
            m_PlayerDatas = new List<Achievement>();
            Achievement achievement = null;
            
            Debug.Log("列数" + dtMyData.Columns.Count + "行数:" + dtMyData.Rows.Count);
          
            for (int i = 0; i < dtMyData.Rows.Count; i++)
            {
                string cjName = dtMyData.Rows[i][dtMyData.Columns[0].ColumnName].ToString();
                string jl = dtMyData.Rows[i][dtMyData.Columns[1].ColumnName].ToString();
                string level = dtMyData.Rows[i][dtMyData.Columns[2].ColumnName].ToString();
                string cjType = dtMyData.Rows[i][dtMyData.Columns[3].ColumnName].ToString();
                string mbCount = dtMyData.Rows[i][dtMyData.Columns[4].ColumnName].ToString();

                achievement = new Achievement();
                achievement.m_Key = "BLDM";
                achievement.m_AchievementName = cjName;
                achievement.m_Level = int.Parse(level);
                achievement.m_Reward = int.Parse(jl);
                achievement.m_AchievementType = (AchievementType)int.Parse(cjType);
                achievement.m_CurrentCount = 0;
                achievement.m_TargetCount = int.Parse(mbCount); ;
                achievement.m_IsOk = 0;
                achievement.m_IsReceive = 0;
                achievement.m_Markup = i+1;
                m_PlayerDatas.Add(achievement);

                //Debug.Log(" : " + cj + "  奖励:" + dtMyData.Rows[i][dtMyData.Columns[1].ColumnName].ToString() + "  列数 : " + dtMyData.Rows[i][dtMyData.Columns[2].ColumnName].ToString());
            }
            m_GameDataManager.gameDatas = m_PlayerDatas;
            m_GameDataManager.Save();

           string json = JsonWriter.Serialize(m_PlayerDatas);
           FileUtility.CreateFile(json);

        }
    }
}

 

NPOI Excel

标签:android   style   blog   color   io   os   ar   for   文件   

原文地址:http://www.cnblogs.com/123ing/p/3977092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!