smwhotjay的专栏

c/c++ socket win32/linux 服务端

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;
 
        }




    }
}


以上

阅读更多
个人分类: c#
上一篇c# socket流转对象
下一篇emule编译过程
博主设置当前文章不允许评论。

C# windowform 由Excel导入数据

2009年04月10日 10KB 下载

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

关闭
关闭