read excel data line by line with c# .net

后端 未结 4 458
不思量自难忘°
不思量自难忘° 2020-12-30 09:00

Does anyone know how can I read an excel file line by line in c#.

I found this code which will return the data from excel and display a grindview in c#. However, I j

相关标签:
4条回答
  • 2020-12-30 09:05

    Since Excel works with ranges you should first get the range of cells you would want to read. After that you can now browse through them using a for loop. You can see an example below:

        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\myexcel.xlsx");
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;
    
        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;
    
        for (int i = 1; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {
                MessageBox.Show(xlRange.Cells[i, j].Value2.ToString());
            }
        }
    

    A more detailed explanation on this code block can be found here.

    0 讨论(0)
  • 2020-12-30 09:10

    you can use OleDbDataReader as below

    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
    
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();
    
        while (reader.Read())
        {
            var val1= reader[0].ToString();
        }
        reader.Close();
    }
    
    0 讨论(0)
  • 2020-12-30 09:20

    You must try this

            string connectionString = "";
            string strFileType = "Type";
            string path = @"C:\Users\UserName\Downloads\";
            string filename = "filename.xls";
            if (fielname.Contains(.xls))
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + filename + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (fielname.Contains(.xlsx)
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + filename + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
    
    
            string query = "SELECT * FROM [SheetName$]";
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(query, connection);
    
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();
    
                var lines = new List<string>();
                while (reader.Read())
                {
                    var fieldCount = reader.FieldCount;
    
                    var fieldIncrementor = 1;
                    var fields = new List<string>();
                    while (fieldCount >= fieldIncrementor)
                    {
                        fields.Add(reader[fieldIncrementor - 1].ToString());
                        fieldIncrementor++;
                    }
    
                    lines.Add(string.Join("\t", fields));
                }
                reader.Close();
            }
    
    0 讨论(0)
  • 2020-12-30 09:27

    I tried the solution with OleDbConnection but it didn't work because I didn't have something installed. Then I found this solution here and it worked like a charm:

    https://www.codeproject.com/Tips/801032/Csharp-How-To-Read-xlsx-Excel-File-With-Lines-of

    There you can download a small file Excel.dll, add it to your project and browse the excel files cell by cell.

    0 讨论(0)
提交回复
热议问题