不通过驱动读取Excel文件

using System;

using System.IO;

using System.Text;

using System.Data;

using System.Reflection;

using System.Diagnostics;

using System.Collections;



namespace ImportDatas

{

    public class ImportData

    {

        #region Excel变量们

        private string templetFile = null;

        private string outputFile = null;

        private object missing = Missing.Value;

        private DateTime beforeTime;

        private DateTime afterTime;

        public string name = null;

        public string code = null;

        Excel.Application app;

        Excel.Workbook workBook;

        Excel.Worksheet worksheet;

        Excel.Range range;

        #endregion



        public ImportData(String filepath,int sheet)

        {

            CreatExcel(filepath,sheet);

        }

        /// <summary>

        /// 将一个已经存在的excel读取,并制定输出路径

        /// </summary>

        /// <param name="templetFilePath">Excel模板文件路径</param>

        /// <param name="outputFilePath">输出Excel文件路径</param>

        private void CreatExcel(string templetFilePath,int sheet)

        {

            if (System.IO.File.Exists(templetFilePath))

            {

                if (System.IO.Path.GetExtension(templetFilePath).ToLower() == ".xls")

                {

                    if (templetFilePath == null)

                    {

                        throw new Exception("Excel模板文件路径不能为空!");

                    }

                    if (!File.Exists(templetFilePath))

                    {

                        throw new Exception("指定路径的Excel模板文件不存在!");

                    }

                    name = System.IO.Path.GetFileName(templetFilePath);

                    code = name.Substring(0, 8);

                    this.templetFile = templetFilePath;

                    //创建Application对象并对其可见

                    beforeTime = DateTime.Now;

                    app = new Excel.ApplicationClass();

                    app.Visible = true;

                    afterTime = DateTime.Now;

                    workBook = app.Workbooks.Open(templetFile, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

                    worksheet = (Excel.Worksheet)workBook.Worksheets[sheet];

                    if (worksheet.UsedRange.Rows.Count == 0)

                    {

                        throw new Exception("文件中没有记录!");

                    }

                }

                else

                {

                    throw new Exception("该文件不是Excel文件!");

                }

            }

            else

            {

                throw new Exception("指定文件不存在!");

            }



        }

        /// <summary>

        /// 返回一个excle中取得的数组

        /// </summary>

        /// <param name="xList">一组行纵标</param>

        /// <returns></returns>

        public ArrayList GetRowsCellValues(ArrayList xList)

        {

            ArrayList retvalist=new ArrayList();

            foreach (string str in xList)

            {

                retvalist.Add(GetrowCellvalue(str, str));

            }

            return retvalist;

        }

        /// <summary>

        /// 添加excle某一个单元格的值

        /// </summary>

        /// <param name="x">行标志</param>

        /// <param name="y">纵标志</param>

        /// <returns></returns>

        public object GetrowCellvalue(string x,string y)

        {

            object retval = "";

            retval = worksheet.get_Range(x,y).Value2;

            return retval;

        }

        /// <summary>

        /// 注销所有对象

        /// </summary>

        public void close()

        {

            workBook.Close(null,null,null);

            app.Workbooks.Close();

            app.Quit();

            if (app != null)

            {

                if (workBook != null)

                {

                    if (worksheet != null)

                    {

                        if (range != null)

                        {

                            System.Runtime.InteropServices.Marshal.ReleaseComObject(range);

                            range = null;

                        }

                        System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);

                        worksheet = null;

                    }

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);

                    workBook = null;

                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                app = null;

            }



            GC.Collect();



            this.KillExcelProcess();

        }



        /// <summary>

		/// 结束Excel进程

		/// </summary>

		public void KillExcelProcess()

		{

			Process[] myProcesses;

			DateTime startTime;

			myProcesses = Process.GetProcessesByName("Excel");



			//得不到Excel进程ID,暂时只能判断进程启动时间

			foreach(Process myProcess in myProcesses)

			{

				startTime = myProcess.StartTime;



				if(startTime > beforeTime && startTime < afterTime)

				{

					myProcess.Kill();

				}

			}

		}

    }

}


自己写的一个小小框架(根据别人的代码改编)

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值