Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# excel文件的第一列未在窗口应用程序中读取oledb读取器,excel文件数据未读取_C#_Asp.net_Excel_Oledb - Fatal编程技术网

C# excel文件的第一列未在窗口应用程序中读取oledb读取器,excel文件数据未读取

C# excel文件的第一列未在窗口应用程序中读取oledb读取器,excel文件数据未读取,c#,asp.net,excel,oledb,C#,Asp.net,Excel,Oledb,我面临使用oledb阅读器阅读excel表格的问题,阅读器没有返回第一列,最后显示列标题F14,列已为空。 但当我打开excel工作表,双击标题行边框进行自动调整和自动重新调整大小时,请保存excel并再次读取,然后所有列都会完美返回 Excel表格,我试图阅读它生成使用php应用程序和下载后,我们把我的应用程序从Excel读取数据,但上面的问题来了 我已经做了大量的研发工作,甚至在使用web应用程序生成excel时,我在excel表格中给出了宽度。我的代码是这样的 private bool I

我面临使用oledb阅读器阅读excel表格的问题,阅读器没有返回第一列,最后显示列标题F14,列已为空。 但当我打开excel工作表,双击标题行边框进行自动调整和自动重新调整大小时,请保存excel并再次读取,然后所有列都会完美返回

Excel表格,我试图阅读它生成使用php应用程序和下载后,我们把我的应用程序从Excel读取数据,但上面的问题来了

我已经做了大量的研发工作,甚至在使用web应用程序生成excel时,我在excel表格中给出了宽度。我的代码是这样的

private bool Import_To_Grid(string FilePath, string Extension)
        {
            try
            {
                string conStr = "";
                switch (Extension)
                {
                    case ".xls": //Excel 97-03
                        conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"]
                                 .ConnectionString;
                        break;
                    case ".xlsx": //Excel 07 and above
                        conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"]
                                  .ConnectionString;
                        break;
                }
                conStr = String.Format(conStr, FilePath);
                OleDbConnection connExcel = new OleDbConnection(conStr);
                OleDbCommand cmdExcel = new OleDbCommand();
                OleDbDataAdapter oda = new OleDbDataAdapter();

                cmdExcel.Connection = connExcel;

                //Get the name of First Sheet
                connExcel.Open();
                Exceldt = new DataTable();
                DataTable dtExcelSchema;
                dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                connExcel.Close();

                //Read Data from First Sheet
                connExcel.Open();
                cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
                oda.SelectCommand = cmdExcel;

                oda.Fill(Exceldt);
                connExcel.Close();

                //Bind Data to GridView
                dgv_showexcel.DataSource = Exceldt;
                BindDataToCmbClass(); //binddata to class for filter
                cmb_userclass.SelectedIndex = 0;
                return true;
            }
            catch (Exception ex) { MessageBox.Show("Its Error" + " " + ex.ToString()); return false; }
        }


Connection string
<add name="Excel03ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';" />
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';" />
private bool导入网格(字符串文件路径,字符串扩展名)
{
尝试
{
字符串conStr=“”;
交换机(分机)
{
案例“.xls”://Excel 97-03
conStr=ConfigurationManager.ConnectionString[“Excel03ConString”]
.连接字符串;
打破
案例“.xlsx”://Excel 07及以上版本
conStr=ConfigurationManager.ConnectionString[“Excel07ConString”]
.连接字符串;
打破
}
conStr=String.Format(conStr,FilePath);
OLEDB连接连接EXCEL=新的OLEDB连接(连接);
OleDbCommand cmdExcel=新的OleDbCommand();
OleDbDataAdapter oda=新的OleDbDataAdapter();
cmdExcel.Connection=connExcel;
//获取第一张图纸的名称
Open();
Exceldt=新数据表();
数据表模式;
dtExcelSchema=connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
string SheetName=dtExcelSchema.Rows[0][“TABLE_NAME”]。ToString();
connExcel.Close();
//从第一页读取数据
Open();
cmdExcel.CommandText=“从[”+SheetName+“]”中选择*;
oda.SelectCommand=cmdExcel;
oda.Fill(Exceldt);
connExcel.Close();
//将数据绑定到GridView
dgv_showexcel.DataSource=Exceldt;
BindDataToCmbClass();//将数据绑定到筛选器的类
cmb_userclass.SelectedIndex=0;
返回true;
}
catch(异常ex){MessageBox.Show(“其错误”+“”+ex.ToString());返回false;}
}
连接字符串

我们需要在下载锁定第一个单元格的bcz头或写入模式时,将IMEX=3设置为第一列和第一行的默认位置。

我刚刚解决了同样的问题。也许有人对你有好处

在我的例子中,excel文件在列上包含一个过滤器。因此,在读取文件时,获取名为
\u xlnm\u FilterDatabase
的隐藏表

实际上,我需要一张工作表,所以我正在查看模式表的第一行。但是我得到两张表,其中一张是过滤表,这张表不包含所有列。所以我没有得到第一列

要解决此问题,请循环架构并获取不包含
FilterDatabase
的第一个工作表名称

代码对话:

DataTable excelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = null;
for (int i = 0; i < dtExcelSchema.Rows.Count; i++)
{
    string tableNameOnRow = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString();
    if (!tableNameOnRow.Contains("FilterDatabase"))
    {
       sheetName = tableNameOnRow; 
       break;
    }
 }
 OleDbDataAdapter da = new OleDbDataAdapter();
 DataSet ds = new DataSet(); cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
DataTable excelSchema=conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
字符串sheetName=null;
for(int i=0;i
很紧急,我没有得到任何关于此的信息。如果对此有任何想法,请与我分享。谢谢