如何从包含长字符的C#中读取Excel列? [英] How to read excel column from C# which contains long characters?

查看:149
本文介绍了如何从包含长字符的C#中读取Excel列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C#中读取Excel文件并将数据填充到DataTable中.我编写了如下代码.

I want to read excel file from C# and fill the data to a DataTable. I have written the code as below.

string strNewPath = Server.MapPath("~/UploadExcel/" + "Action Center_Agent.xls");
 string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        // Create the connection object

        OleDbConnection oledbConn = new OleDbConnection(connString);
        try
        {
            oledbConn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Agent$A9:Z500]", oledbConn);
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            oleda.Fill(ds, "Customer");
            DataTable dtExcel = new DataTable();
            dtExcel = ds.Tables[0];
         }
        catch
        {
        }
        finally
        {
           
            oledbConn.Close();
        }


一切正常.但是问题是excel工作表包含一个包含80个字符的列.它无法读取该列的所有字符.正在读取60个字符,其余字符被跳过.那么,您能帮我如何阅读完整的列名吗?


Everything is working fine. But the problem is that the excel sheet contains a column which contains 80 characters. It is not able to read all the charactes of that colums.It is reading 60 characters and remaining characters are getting skipped . So can u please help me how to read complete column name please ?

推荐答案

A9:Z500]",oledbConn); OleDbDataAdapter oleda = OleDbDataAdapter(); oleda.SelectCommand = cmd; DataSet ds = DataSet(); oleda.Fill(ds," ); DataTable dtExcel = DataTable(); dtExcel = ds.Tables [ 0 ]; } 捕获 { } 最终 { oledbConn.Close(); }
A9:Z500]", oledbConn); OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; DataSet ds = new DataSet(); oleda.Fill(ds, "Customer"); DataTable dtExcel = new DataTable(); dtExcel = ds.Tables[0]; } catch { } finally { oledbConn.Close(); }


一切正常.但是问题是excel工作表包含一个包含80个字符的列.它无法读取该列的所有字符.正在读取60个字符,其余字符被跳过.那么,您能帮我如何阅读完整的列名吗?


Everything is working fine. But the problem is that the excel sheet contains a column which contains 80 characters. It is not able to read all the charactes of that colums.It is reading 60 characters and remaining characters are getting skipped . So can u please help me how to read complete column name please ?


这可能对您有帮助
http://stackoverflow.com/questions/657131/如何使用Excel读取数据的文件 [
this may be helpful for you
http://stackoverflow.com/questions/657131/how-to-read-data-of-an-excel-file-using-c[^]


这篇关于如何从包含长字符的C#中读取Excel列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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