简体   繁体   中英

reading empty Rows from Excel ( *.xlsx )

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;

And after the code compiled i see that the rowCount = 1048574 and colCount = 17 , but in the file the rows filled with data = 9000 and columns = 14

How to read those only and what the changes will be in the code because I got out of memory Exception ...

I strongly suspect that this has something to do with the XLSX file being used. The following code, which is based on your sample with a couple of changes to which variables were being looked at, provides the expected results with a newly created Excel document:

using System;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Windows.Forms;

namespace TestApplication
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog ofd = new OpenFileDialog
            {
                Title = "Select file",
                Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*",
                FilterIndex = 1,
                RestoreDirectory = true
            };

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string query = "SELECT * FROM [Sheet1$]";
                OleDbConnection conn = new OleDbConnection
                {
                    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 = ds.Tables[0];

                var message = string.Format("Row Count: {0}{1}Column Count: {2}", data.Rows.Count, Environment.NewLine, data.Rows[0].ItemArray.Count());
                MessageBox.Show(message);
            }
        }
    }
}

With an empty document the above code will crash but does return a row count of 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM