C#打开Excel文件

C#打开Excel文件

方法一

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
//下面这三个名称空间需要引用
using MSword = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.IO;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
			string path2 = System.Environment.CurrentDirectory;
            /C#中”\“是转bai义符,所以当你写"c:\dos"时,C#会把”\d“当du成转义符,所以加上@,C#就会认成"c:\dos"。
            string ex = @"\1.xls";
            string fileName = path2 + ex;
			//新建一个应用程序EXC1
            MSword.Application EXC1 = new MSword.Application();
            EXC1.Visible = true;//设置EXC1打开后可见
            MSword.Workbooks wbs = EXC1.Workbooks;
            MSword._Workbook wb = wbs.Add(fileName);//打开并显示EXCEL文件
            
            //wb.Close();//关闭文档
            //wbs.Close();//关闭工作簿
            //EXC1.Quit();//关闭EXCEL应用程序
            //释放EXCEL应用程序的进程
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(EXC1);

        }
    }
}

方法二

using System.Data.OleDb;//OleDbConnection引用
using System.Data;//DataSet引用

public static DataSet Excel(string filePath)//filePath工作薄路径
        {
            try
            {
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";
                OleDbConnection OleConn = new OleDbConnection(strConn);
                OleConn.Open();
                string sql = "select * from [Sheet1$]";
                
                OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                DataSet OleDsExcel = new DataSet();
                OleDaExcel.Fill(OleDsExcel, "Sheet1");
                OleConn.Close();
                return OleDsExcel;
            }
            catch (Exception err)
            {
                Console.WriteLine("数据绑定Excel失败!失败原因:" + err.Message, "提示信息");
                return null;
            }
        }
//方法二调用
		string filePath = "E:/1.xls";
		var OleDsExcel = Excel(filePath);
        System.Data.DataTable tb1 = new System.Data.DataTable();
        if (OleDsExcel.Tables.Count > 0) tb1 = OleDsExcel.Tables[0];

方法三


//excelPath 工作薄路径
//stCell 起始行
//edCell 结束行 
using Microsoft.Office.Interop.Excel;//Workbook引用
using System.Reflection;//Missing引用
using System.Runtime.InteropServices;//Marshal引用
public static object[,] GetExcel(string excelPath, string stCell, string edCell)//获取Excel数据
        {

            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Workbook workBook = null;
            object oMissiong = Missing.Value;
            try
            {
                workBook = app.Workbooks.Open(excelPath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong,
                    oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
                if (workBook == null)
                    return null;

                Worksheet workSheet = (Worksheet)workBook.Worksheets.Item[1];
                //使用下述语句可以从头读取到最后,按需使用
                //var maxN = workSheet.Range[startCell].End[XlDirection.xlDown].Row;
                return workSheet.Range[stCell + ":" + edCell].Value2;
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                //COM组件方式调用完记得释放资源
                if (workBook != null)
                {
                    workBook.Close(false, oMissiong, oMissiong);
                    Marshal.ReleaseComObject(workBook);
                    app.Workbooks.Close();
                    app.Quit();
                    Marshal.ReleaseComObject(app);
                }
            }

        }
//方法三调用
		string filePath = "E:/1.xls";
        var rng=GetExcelRangeData(filePath,"1","1");
        Console.WriteLine(rng.GetValue(1, 1));
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值