net and C#. I need to write a program to browse and read an excel and then parse it in specified format and finally insert into sql server database.
I have used oledb to read excel and I created DataTable from excel. Now I'm having a trouble to parse it in required format. Here is the link for the picture of what is excel input and what is expected format to insert into database. Input and expected output format
Right now I'm doing with simple data in future I need to do in for large excel data around (3000 columns) to parse into some 250000 records. Please also give me advise in terms of performance wise. Right now I'm using oledb is it fine or do I need to use anything else.
Here is my sample code c# code file
OleDbConnection Econ;
SqlConnection con;
string constr, Query, sqlconn;
protected void Page_Load(object sender, EventArgs e)
{
}
// excel connection
private void ExcelConn(string FilePath)
{
constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath);
Econ = new OleDbConnection(constr);
}
// sql connection
private void connection()
{
sqlconn = ConfigurationManager.ConnectionStrings["SqlCom"].ConnectionString;
con = new SqlConnection(sqlconn);
}
// read data from excel and creating a datatable
private void ExcelToDataTable(string FilePath)
{
ExcelConn("C:\\Users\\username\\Desktop\\EmpEx.xlsx");
Query = string.Format("Select * FROM [Sheet1$]");
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
Econ.Open();
OleDbDataAdapter oda = new OleDbDataAdapter(Ecom);
DataTable dtExcel = new DataTable();
Econ.Close();
oda.Fill(dtExcel);
// DataTable parseTable = ParseDataTable(dtExcel);
//connection();
// printing data table
foreach (DataRow dataRow in dtExcel.Rows)
{
foreach (var item in dataRow.ItemArray)
{
Response.Write(item);
}
}
Response.Write("<br> Colums: " + dtExcel.Columns.Count.ToString() + "<br>");
Response.Write("Rows: " + dtExcel.Rows.Count.ToString() + "<br>");
//print on screen
foreach(DataRow row in dtExcel.Rows)
{
foreach(DataColumn col in dtExcel.Columns)
{
Label1.Text = Label1.Text + row[col].ToString() + "\t";
}
}
}
// Method to make data table in specified format
public DataTable ParseDataTable(DataTable dtExcel)
{
var dt = new DataTable("sourceData");
dt.Columns.Add(new DataColumn("id", typeof(String)));
dt.Columns.Add(new DataColumn("name", typeof(String)));
dt.Columns.Add(new DataColumn("variable", typeof(String)));
dt.Columns.Add(new DataColumn("year", typeof(String)));
dt.Columns.Add(new DataColumn("value", typeof(String)));
// NOT GETTING TO PARSE In specified format
/**** NEED HELP HERE *****/
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
ExcelToDataTable(CurrentFilePath);
}
Please help me how can I achieve this. How can I parse input excel data in specified format as mentioned in the attached picture in the link (screenshot). Please suggest me any way to fix my problem.