read excel data line by line with c# .net - c#

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 just was wandering how to possibly read the data line by line on the server side instead?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace site
{
public partial class pgTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnImport_Click(object sender, EventArgs e)
{
string connString = "";
string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
string path = fileuploadExcel.PostedFile.FileName;
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT [username],[age],[phone] FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
da.Dispose();
conn.Close();
conn.Dispose();
}
}
}

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.

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();
}

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();
}

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.

A simple way to convert data table to enumerable of an object is using Json methods.
Per example, with simple method, using only dotnet libraries, you can convert data table to list of especific objects.
using System.Data;
private static IEnumerable<T> ConvertDataTable<T>(DataTable dataTable)
{
if (dataTable is null ||
!dataTable.AsEnumerable().Any())
{
return Enumerable.Empty<T>();
}
var data = dataTable.Rows.OfType<DataRow>()
.Select(row => dataTable.Columns.OfType<DataColumn>()
.ToDictionary(col => col.ColumnName, c => row[c]));
var jsonTextObject = System.Text.Json.JsonSerializer.Serialize(data);
return System.Text.Json.JsonSerializer.Deserialize<IEnumerable<T>>(jsonTextObject)
?? Enumerable.Empty<T>();
}

Related

Importing excel file into SQL with different schema (asp.net C#)

I'm building a program where the user imports an excel file to a database (SQLServer)... But I Do not want to specify the columns name cause it makes my program very limit, to those column names....
I don't know how to work with datatable and rows very well, but I think its the only way right? (im a newbie, sorry)
Here's the code Guys:
rotected void Upload_Click(object sender, EventArgs e)
{
string sSheetName;
DataTable dtTablesList = new DataTable();
string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
string filename = Path.GetFileName(filepath);
FileUpload1.SaveAs(excelPath);
string ext = Path.GetExtension(filename);
String strConnection = #"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
dtTablesList = excelConnection.GetSchema("Tables");
if (dtTablesList.Rows.Count > 0)
{
sSheetName = dtTablesList.Rows[0]["TABLE_NAME"].ToString();
for (int j = 0; j < dtTablesList.Rows.Count; j++)
{
for (int i = 0; i < dtTablesList.Columns.Count; i++)
{
Debug.Write(dtTablesList.Columns[i].ColumnName + " ");
Debug.WriteLine(dtTablesList.Rows[j].ItemArray[i]);
}
}
Debug.WriteLine(dtTablesList.Rows.Count);
foreach (DataRow dataRow in dtTablesList.Rows)
{
foreach (var item in dataRow.ItemArray)
{
Debug.WriteLine(item);
}
}
OleDbCommand cmd = new OleDbCommand("Select * from [" + sSheetName + "]", excelConnection);
cmd.ExecuteNonQuery();
}
I am not sure what exactly you are looking for.
In the past I have used a component called EPPlus.
I used this solution to turn a worksheet into a datatable.
See here: Excel to DataTable using EPPlus - excel locked for editing
Edit:
When you have the datatable of your excel worksheet you can do something like this:
using (SqlConnection con = new SqlConnection("connectionstring"))
{
try
{
con.Open();
foreach (DataRow dr in worksheetTable.Rows)
{
using (SqlCommand myCommand = new SqlCommand("insert into myTable (Products, column2) values (#prod, #col2)", con))
{
myCommand.CommandType = CommandType.Text;
myCommand.Parameters.AddWithValue("prod", dr[0]);
myCommand.Parameters.AddWithValue("col2", dr[1]);
int result = myCommand.ExecuteNonQuery();
}
}
}
catch (SqlException ex)
{
//handle errors here
}
catch (Exception ex)
{
//handle errors here
}
finally
{
con.Close();
}
}
(Code might not be perfect, I did not test it)
It depends a lot of the structure of your excel and database.
I hope this helps you.

How do I add a key to the app.config file

I am new to automation and I am trying to get data from an Excel sheet using OLEDB, I get an errror saying "OLEDB Exception : Invalid argument" in C#
So, One of the answers to a similar question suggest that I add a key to the app.config file for my test data. Now I am not familiar with the .config files
So, I need help with the key or any other solutions to my problem will do
Here is my code:
{
class ExcelDataAccess
{
public static string TestDataFileConnection()
{
var fileName =
ConfigurationManager.AppSettings[#"Path\DataSet.xlsx"];
var con = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source = {0}; Extended Properties='Excel 12.0 Xml;HDR=YES;'", fileName);
return con;
}
public static UserData GetTestData(string keyName)
{
using (var connection = new
OleDbConnection(TestDataFileConnection()))
{
connection.Open();
var query = string.Format("select * from [DataSet$] where
key='{0}'", keyName);
var value = connection.Query<UserData>
(query).FirstOrDefault();
connection.Close();
return value;
}
}
I use ExcelDataReader - https://github.com/ExcelDataReader/ExcelDataReader
using ExcelDataReader;
using System.Collections.Generic;
using System.IO;
namespace Test.Data.Excel
{
public class Reader
{
public static IEnumerable<object[]> Read(string filePath)
{
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var count = reader.FieldCount;
do
{
var list = new List<object>();
while (reader.Read())
{
list.Add(reader.GetString(0));
}
yield return list.ToArray();
} while (reader.NextResult());
}
}
}
}
}
Try with the following code to import data from Excel sheet.
"openFileDialog1_FileOk" is a Button
Private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string filePath = openFileDialog1.FileName;
string extension = Path.GetExtension(filePath);
string header = rbHeaderYes.Checked ? "YES" : "NO";
string conStr, sheetName;
conStr = string.Empty;
switch (extension)
{
case ".xls": //Excel 97-03
conStr = string.Format(Excel03ConString, filePath, header);
break;
case ".xlsx": //Excel 07
conStr = string.Format(Excel07ConString, filePath, header);
break;
}
//Get the name of the First Sheet.
using (OleDbConnection con = new OleDbConnection(conStr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
con.Close();
}
}
//Read Data from the First Sheet.
using (OleDbConnection con = new OleDbConnection(conStr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandText = "SELECT * From [" + sheetName + "]";
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
sampleDatatable = dt;
con.Close();
dataGridView1.DataSource = dt;
//dataGridView2.DataSource = Headers;
}
}
}
}

syntax error at or near pgsl?

I am developing an asp.net web application and I would like to import a fichier excel to postgresql. I'm using this code but it gives me this error. Can you help me?
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1056: Unexpected character '$'
Line 74: quer = "INSERT INTO poi (num_route, pk, geom)
select num_route,pk_debut from [Sheet1$]";
Line 75: NpgsqlCommand cm = new NpgsqlCommand(quer, cnx); L
ine 76: reader = cm.ExecuteReader();
using System;
using System.Configuration;
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Text;
using System.Xml;
using Npgsql;
using System.Collections.Specialized;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "Please select an excel file first";
lblMessage.Visible = false;
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string query = null;
string quer = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
else
{
lblMessage.Text = "Seuls les fichiers Excel autorisés";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
return;
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
query = "SELECT num_route,pk_debut,pk_fin FROM [Sheet1$]";
NpgsqlConnection cnx = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=****;");
NpgsqlDataReader reader;
cnx.Open();
quer = "INSERT INTO poi (num_route, pk) select num_route,pk_debut from "Sheet1$"";
NpgsqlCommand cm = new NpgsqlCommand(quer, cnx);
reader = cm.ExecuteReader();
//Create the connection object
conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
lblMessage.Text = "Les données récupérées avec succès! Total de lignes:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "S'il vous plaît sélectionner un fichier excel";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
}
I would suggest trying something like this
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
var Builder = new StringBuilder("INSERT INTO poi (num_route, pk) select num_route,pk_debut VALUES ");
bool first = true;
using (var conn = new OleDbConnection(connString))
{
if (conn.State == ConnectionState.Closed) conn.Open();
using (var cmd = new OleDbCommand("SELECT num_route,pk_debut,pk_fin FROM [Sheet1$]", conn))
using (var da = cmd.ExecuteReader())
{
while (da.Read())
{
if (!first) Builder.Append(",");
Builder.Append("(");
Console.WriteLine(da[0]);
Builder.Append(",");
Console.WriteLine(da[1]);
Builder.Append(",");
Console.WriteLine(da[2]);
Builder.Append(")");
first = false;
}
}
}
using (NpgsqlConnection cnx = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=****;"))
{
cnx.Open();
var quer = Builder.ToString();
using (NpgsqlCommand cm = new NpgsqlCommand(quer, cnx))
cm.ExecuteNonQuery();
}

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.

Using OleDB to read excel file in c#?

I am building a program to read excel file into dataGridView.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace pro1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string sConnecStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\\Copy_of_Acute_HCV_2008.xls" + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conObj = new OleDbConnection(sConnecStr);
conObj.Open();
OleDbCommand sqlCommand = new OleDbCommand("SELECT * FROM [Sheet1$]",conObj);
OleDbDataAdapter adaObj = new OleDbDataAdapter();
adaObj.SelectCommand = sqlCommand;
DataSet setObj = new DataSet();
adaObj.Fill(setObj);
conObj.Close();
dataGridView1.DataSource = setObj.Tables[0];
dataGridView1.Refresh();
}
}
}
The program runs fine when i use a small excel file but when i use a big excel file it gives me this error
An unhandled exception of type
'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
Additional information: 'Sheet1$' is
not a valid name. Make sure that it
does not include invalid characters or
punctuation and that it is not too
long.
thanks
edit: i always use .xls files not .xlsx
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed) conn.Open();
string SpreadSheetName = "";
DataTable ExcelSheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
SpreadSheetName = ExcelSheets.Rows[0]["TABLE_NAME"].ToString();
query = "SELECT * FROM [" + SpreadSheetName + "]";
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "tab1");
}
}
}

Categories

Resources