.net C#Windows应用程序与Excel文件的连接问题 [英] .net C# windows application connection problem with excel file

查看:74
本文介绍了.net C#Windows应用程序与Excel文件的连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好;
我正在尝试将.Net C#Windows程序连接到excel文件,但显示连接无法打开".

以下是代码

Hi All;
I m trying to connect .Net C# windows program to a excel file but it is showing "connection cannot open".

Following is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Printing;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;


namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private OleDbConnection conn; //SqlConnection conn = new SqlConnection();
        private string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\horton\Desktop\TestExcel.xlsx";
            //@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\horton\Desktop;Extended Properties=TestExcel";
        //@"Data Source=HORTON-2A5CB16E\SQLEXPRESS;Initial Catalog=Temp;Integrated Security=SSPI;Max Pool Size=100;Connect Timeout=1000";
       public Form1()
        {
            InitializeComponent();
            conn = new OleDbConnection(conStr);
            conn.Open();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            clr();
            txtRollNumber.Focus();
            label1.Enabled = false;
            label2.Enabled = false;
        }




谢谢&问候
Indrajit




Thanks & regards
Indrajit

推荐答案

尝试此

//使用System.Data.OleDb;

try this

//using System.Data.OleDb;

OleDbConnection Con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://Documents and Settings//Admin//My Documents//Downloads//MyReport2.xls;Extended Properties=Excel 8.0;");

Con.Open();


您忘记了Data Source属性上的excel文件名.

尝试这样的事情.
You forgot your excel file name on your Data Source property.

try something like this.
private OleDbConnection conn;
private string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\horton\Desktop\TextExcel.xls;Extended Properties=Excel 8.0;HDR=Yes;IMEX=1";



祝你好运!



Good luck!


检查数据源属性

Excel文件名不存在
Check your data source property

Excel file name is not there


这篇关于.net C#Windows应用程序与Excel文件的连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆