c#加载Excel

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public class ExcelHelper
    {
        private Excel._Application excelApp;
        private string fileName = string.Empty;
        public Excel.WorkbookClass wbclass;
        public ExcelHelper(string _filename)
        {
            excelApp = new Excel.Application();
            object objOpt = System.Reflection.Missing.Value;
            wbclass = (Excel.WorkbookClass)excelApp.Workbooks.Open(_filename, objOpt, false, objOpt, objOpt, objOpt, true, objOpt, objOpt, true, objOpt, objOpt, objOpt, objOpt, objOpt);
        }
        /**/
        /// <summary>
        /// 所有sheet的名称列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetSheetNames()
        {
            List<string> list = new List<string>();
            Excel.Sheets sheets = wbclass.Worksheets;
            string sheetNams = string.Empty;
            foreach (Excel.Worksheet sheet in sheets)
            {
                list.Add(sheet.Name);
            }
            return list;
        }
        public Excel.Worksheet GetWorksheetByName(string name)
        {
            Excel.Worksheet sheet = null;
            Excel.Sheets sheets = wbclass.Worksheets;
            foreach (Excel.Worksheet s in sheets)
            {
                if (s.Name == name)
                {
                    sheet = s;
                    break;
                }
            }
            return sheet;
        }
        /**/
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sheetName">sheet名称</param>
        /// <returns></returns>
        public Array GetContent(string sheetName)
        {
            Excel.Worksheet sheet = GetWorksheetByName(sheetName);
            //获取A1 到AM24范围的单元格
            Excel.Range rang = sheet.get_Range("A1", "AM24");
            //读一个单元格内容
            //sheet.get_Range("A1", Type.Missing);
            //不为空的区域,列,行数目
            //   int l = sheet.UsedRange.Columns.Count;
            // int w = sheet.UsedRange.Rows.Count;
            //  object[,] dell = sheet.UsedRange.get_Value(Missing.Value) as object[,];
            System.Array values = (Array)rang.Cells.Value2;
            return values;
        }

        public void Close()
        {
            excelApp.Quit();
            excelApp = null;
        }

    }
}

 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{

    public struct stWorldMapCfg
    {
        public int MapId;//地图id
        public string MapName;//地图名
    }

    /************************************************************************/
    /* 
     * 
     * 使用例子
      static void test_WorldMapCfg()
        {
            WorldMapCfg maplist = new WorldMapCfg("c:\\WorldMapCfg.xls");

            Console.WriteLine(maplist.GetWorldMapCfg(7).MapName);

        }
     * 
     */
    /************************************************************************/

    public class WorldMapCfg
    {
        public WorldMapCfg(string _filename)
        {
            LoadWorldMapCfg(_filename);
        }


        public List<stWorldMapCfg> m_list;

        static int ToInt(object o)
        {
            if (o is int)
                return (int)o;
            else if (o is short)
                return (int)(short)o;
            else if (o is byte)
                return (int)(byte)o;
            else if (o is long)
                return (int)(long)o;
            else if (o is double)
                return (int)(double)o;
            else if (o is float)
                return (int)(float)o;
            else if (o is decimal)
                return (int)(decimal)o;
            else if (o is uint)
                return (int)(uint)o;
            else if (o is ushort)
                return (int)(ushort)o;
            else if (o is ulong)
                return (int)(ulong)o;
            else if (o is sbyte)
                return (int)(sbyte)o;
            else
                return (int)double.Parse(o.ToString());
        }

        public stWorldMapCfg GetWorldMapCfg(int MapId)
        {

            foreach (stWorldMapCfg map in m_list)
            {
                if (map.MapId == MapId)
                {
                    return map;
                }
            }

            stWorldMapCfg ret = new stWorldMapCfg();
            ret.MapName = "找不到";
            ret.MapId = -1;
            return ret;
        }

        public int MapNameToID(string Mapname)
        {

            foreach (stWorldMapCfg map in m_list)
            {

                if ( map.MapName == Mapname )
                {
                    return map.MapId;
                }
            }

 
            return -1;
        }

        private void  LoadWorldMapCfg(string path)
        {
            m_list = new List<stWorldMapCfg>();

            stWorldMapCfg map = new stWorldMapCfg();

            ExcelHelper h = new ExcelHelper(path);

            Excel.Worksheet sheet = h.GetWorksheetByName("WorldMapCfg");


            for (int row = 3; row <= sheet.UsedRange.Rows.Count; row++)
            {
                Excel.Range rng2 = (Excel.Range)sheet.Cells[row, 1];

                Excel.Range rng3 = (Excel.Range)sheet.Cells[row, 2];

                try
                {
                    map.MapId = ToInt(rng2.Value2);
                    map.MapName = (string)rng3.Value2;
                    m_list.Add(map);
                }
                catch (System.Exception e)
                {
                    //Console.WriteLine(row + " ex");
                }

            }

            h.Close();
            h = null;
 
        }




    }
}


以上

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值