无法在asp.net中读取excel [英] unable to read excel in asp.net

查看:79
本文介绍了无法在asp.net中读取excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我做了一个直接从excel上传数据的应用程序,我用oledb连接对象连接到excel但是我无法读取excel 。



我的代码:

Hello everyone,

I making an application which upload data directly from excel, I have connected to excel with oledb connection object but i am unable to read the excel.

My code :

 private string GetConnection()
    {
        string connectionString="";
        string path = fpUpload.FileName;
        string ext = System.IO.Path.GetExtension(path);
        string fileName = System.IO.Path.GetFileName(path);
        if (ext.ToString() == ".xls")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        else if (ext.ToString() == ".xlsx")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }
        else
        {
            Response.Write("<script type=text/javascript language=javascript>alert('Please select proper file')</script>");
        }
        return connectionString;
    }
private void GetData()
    {
        string conn = GetConnection();
        DataTable dt = new DataTable();

        string query = "SELECT * from [Sheet1$]";
        olCn = new OleDbConnection();
        olCn.ConnectionString = conn;
        olCn.Open();
     //   dt = olCn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        olCmd = new OleDbCommand(query, olCn);
        olDa = new OleDbDataAdapter(olCmd );
        ds = new DataSet();
        olDa.Fill(ds);
        UploadData(ds);

    }





虽然excel表中包含Sheet1,但它给了我错误,它不能在excel找到Sheet1。



请帮我这个..



谢谢&此致,

Krunal Panchal



and though the excel sheet contains Sheet1 in it but it gives me error that it could not find Sheet1 in excel .

Please help me regarding this..

Thanks & Regards,
Krunal Panchal

推荐答案

;
olCn = new OleDbConnection();
olCn.ConnectionString = conn;
olCn.Open();
// dt = olCn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
olCmd = new OleDbCommand( query,olCn);
olDa = new OleDbDataAdapter(olCmd);
ds = new DataSet();
olDa.Fill(ds);
UploadData(ds);

}
"; olCn = new OleDbConnection(); olCn.ConnectionString = conn; olCn.Open(); // dt = olCn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); olCmd = new OleDbCommand(query, olCn); olDa = new OleDbDataAdapter(olCmd ); ds = new DataSet(); olDa.Fill(ds); UploadData(ds); }





虽然excel表中包含Sheet1,但它给出了我在excel中找不到Sheet1的错误。



请帮帮我关于这个..



谢谢和问候,

Krunal Panchal



and though the excel sheet contains Sheet1 in it but it gives me error that it could not find Sheet1 in excel .

Please help me regarding this..

Thanks & Regards,
Krunal Panchal


您无法读取Excel文件的一个原因是 IMEX 选项值错误!



默认 IMEX 选项应为 0 (零)。

如果您想将数据导入为文本,设置 IMEX = 1
The only one reason you can't read your Excel files is bad IMEX option value!

Default IMEX option should be 0 (zero).
If you would like to import data as a text, set IMEX = 1.


请检查我的代码:



Please Check my codes:

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + Server.MapPath(" test.xls") + ";" +
                "Extended Properties=Excel 8.0;";       
            OleDbConnection objConn = new OleDbConnection(sConnectionString);
            objConn.Open();

            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * from [Sheet1


这篇关于无法在asp.net中读取excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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