问题..在Excel中读取空行 [英] Problem .. .Reading Empty Rows in Excel

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

问题描述

你好

当我使用这段代码从excel阅读时

hello
While i am reading from excel using this code "

OpenFileDialog ofd= new OpenFileDialog();
ofd.Title = "Select file";
ofd.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
ofd.FilterIndex = 1;
ofd.RestoreDirectory = true;

     if (ofImport.ShowDialog() == DialogResult.OK)
        {
           string path = System.IO.Path.GetFullPath(ofImport.FileName);
           string query = "SELECT * FROM [Sheet6$]";
           OleDbConnection conn = new OleDbConnection();
           conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ofd.FileName + ";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
           OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
                    var ds= new DataSet();
                    adapter.Fill(ds);
                    DataTable data = dsz.Tables[0];
                    datagridview1.DataSource = data
                   // to get row count
                   int rowCount = dg_Un_TIA.Rows.Count;
                  // Get the no. of columns in the first row.
            int colCount = dg_Un_TIA.Rows[0].Cells.Count;



并且在编译代码之后我看到rowCount = 1048574和colCount = 17但是在文件中,行中填充的数据= 9000,列= 14,如何只读取那些内容,而更改将在代码中,因为我没有内存执行...


and after the code compiled i see that the rowCount = 1048574 and the colCount=17 but in the file the rows filled with data = 9000 and the columns = 14 how to read those only and whta the changes will be in the code because i got out of memory Exeption ...

推荐答案

;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ ofd.FileName + ;扩展属性=+\Excel 12.0 Xml; HDR = YES; IMEX = 1 \;
OleDbDataAdapter adapter = new OleDbDataAdapter(query,conn);
var ds = new DataSet();
adapter.Fill(ds);
DataTable data = dsz.Tables [0];
datagridview1.DataSource = data
//获取行数
int rowCount = dg_Un_TIA.Rows.Count;
//获取号码第一行中的列。
int colCount = dg_Un_TIA.Rows [0] .Cells.Count;
"; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ofd.FileName + ";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\""; OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn); var ds= new DataSet(); adapter.Fill(ds); DataTable data = dsz.Tables[0]; datagridview1.DataSource = data // to get row count int rowCount = dg_Un_TIA.Rows.Count; // Get the no. of columns in the first row. int colCount = dg_Un_TIA.Rows[0].Cells.Count;



并且在编译代码之后我看到rowCount = 1048574和colCount = 17但是在文件中,行中填充的数据= 9000,列= 14,如何仅读取这些内容,以及更改将在代码中,因为我的内存不足Exeption ...


and after the code compiled i see that the rowCount = 1048574 and the colCount=17 but in the file the rows filled with data = 9000 and the columns = 14 how to read those only and whta the changes will be in the code because i got out of memory Exeption ...


只需修改您的查询,如:



just modify your query like as :

string query ="SELECT  * From [" + SheetName + "] where [Columnname1]<>NULL AND [Columnname2]<>''"


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

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