using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml;
public static class ExcelBulkImport
{
public static DataTable GetDataTableFromExcel(string path, bool hasHeader = true)
{
//DataTable dt = ExcelUtility.nike.lyrewing.com.ExcelBulkImport.GetDataTableFromExcel("path");
////dataGridView1.DataSource = dt.DefaultView;
//ExcelUtility.nike.lyrewing.com.ExcelBulkImport.ImportDataFromDataTable2Sql(dt, "Data Source=.;Initial Catalog=Db_laienTrans;User ID=sa;Password=123");
using (var pck = new OfficeOpenXml.ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets.First();
DataTable tbl = new DataTable();
//foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
//{
// tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
//}
/*setting column*/
tbl.Columns.Add("ID", Type.GetType("System.Guid"));
tbl.Columns.Add("CN Launch Date", Type.GetType("System.DateTime"));
tbl.Columns.Add("HK Launch Date", Type.GetType("System.DateTime"));
tbl.Columns.Add("TMALL Launch Date", Type.GetType("System.DateTime"));
tbl.Columns.Add("ProductCode", Type.GetType("System.String"));
tbl.Columns.Add("Sytle", Type.GetType("System.String"));
tbl.Columns.Add("Product Name", Type.GetType("System.String"));
tbl.Columns.Add("Category", Type.GetType("System.String"));
tbl.Columns.Add("Prod Type", Type.GetType("System.String"));
tbl.Columns.Add("Gender Group", Type.GetType("System.String"));
tbl.Columns.Add("Key Product (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("CN InStock Key Item (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("HK InStock Key Item (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("TMALL InStock Key Item (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("Hard Launch (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("Top Style (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("QS (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("Lab (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("CN SLP (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("HK SLP (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("TMALL SLP (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("CN Planning Qty", Type.GetType("System.Int16"));
tbl.Columns.Add("HK Planning Qty", Type.GetType("System.Int16"));
tbl.Columns.Add("TMALL Planning Qty", Type.GetType("System.Int16"));
tbl.Columns.Add("CN CN-Copy Status (Y/N) - Verified", Type.GetType("System.String"));
tbl.Columns.Add("CN EN-Copy Status (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("CN CN-Copy Status (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("CN CN-Copy Comments", Type.GetType("System.String"));
tbl.Columns.Add("Product Copy back from SDL", Type.GetType("System.DateTime"));
tbl.Columns.Add("CN Digital Inventory On-hand Qty", Type.GetType("System.Int16"));
tbl.Columns.Add("Sample Batch", Type.GetType("System.DateTime"));
tbl.Columns.Add("LW Linguistic Review", Type.GetType("System.DateTime"));
tbl.Columns.Add("LW Specs Review", Type.GetType("System.DateTime"));
tbl.Columns.Add("Review Request to Category", Type.GetType("System.DateTime"));
tbl.Columns.Add("Review back from Category", Type.GetType("System.DateTime"));
tbl.Columns.Add("Prodigy Update", Type.GetType("System.DateTime"));
tbl.Columns.Add("HK EN-Copy Status (Y/N) - Verified", Type.GetType("System.String"));
tbl.Columns.Add("HK TC-Copy Status (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("HK EN-Copy Comments", Type.GetType("System.String"));
tbl.Columns.Add("HK TC-Copy Comments", Type.GetType("System.String"));
tbl.Columns.Add("HK EN-Copy Download", Type.GetType("System.DateTime"));
tbl.Columns.Add("Task Type", Type.GetType("System.String"));
tbl.Columns.Add("Task Submission", Type.GetType("System.DateTime"));
tbl.Columns.Add("Word Count", Type.GetType("System.Int16"));
tbl.Columns.Add("New Word Count", Type.GetType("System.Int16"));
tbl.Columns.Add("Linguistic Review Submission", Type.GetType("System.DateTime"));
tbl.Columns.Add("Specs Review Submission", Type.GetType("System.DateTime"));
tbl.Columns.Add("HK Review Request to Category", Type.GetType("System.DateTime"));
tbl.Columns.Add("HK Review back from Category", Type.GetType("System.DateTime"));
tbl.Columns.Add("Warehouse Shooting", Type.GetType("System.DateTime"));
tbl.Columns.Add("HK EN-Copy", Type.GetType("System.DateTime"));
tbl.Columns.Add("HK TC-Copy", Type.GetType("System.DateTime"));
tbl.Columns.Add("Copy Writing-Send Request", Type.GetType("System.DateTime"));
tbl.Columns.Add("TMALL CN-Copy Status (Y/N)", Type.GetType("System.String"));
tbl.Columns.Add("TMALL CN-Copy Comments", Type.GetType("System.String"));
tbl.Columns.Add("Copy Writing", Type.GetType("System.DateTime"));
tbl.Columns.Add("Tmall Review Request to Category", Type.GetType("System.DateTime"));
tbl.Columns.Add("Tmall Review back from Category", Type.GetType("System.DateTime"));
/*end setting column*/
var startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row1 = tbl.Rows.Add();
row1[0] = Guid.NewGuid();
foreach (var cell in wsRow)
{
/*convert null value in datetime kind column in Excel*/
DateTime d2;
if (tbl.Columns[cell.Start.Column].DataType == Type.GetType("System.DateTime"))
{
if (DateTime.TryParse(cell.Text, out d2))
{
row1[cell.Start.Column] = d2;
}
else
{
row1[cell.Start.Column] = DBNull.Value;
}
}
else
{ row1[cell.Start.Column] = cell.Text; }
}
}
return tbl;
}
}
public static void ImportDataFromDataTable2Sql(DataTable SourceTable, string connectionstring)
{
//Open a connection with destination database;
using (SqlConnection connection = new SqlConnection(connectionstring))
{
connection.Open();
//Open bulkcopy connection.
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(connection))
{
//Set destination table name
//to table previously created.
bulkcopy.DestinationTableName = "NikeDTCDailyReport";
bulkcopy.WriteToServer(SourceTable);
connection.Close();
}
}
}
}
}
//public void ImportDataFromExcel(string excelFilePath)
//{
// //declare variables - edit these based on your particular situation
// string ssqltable = "NikeDTCDailyReport";
// // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different
// string myexceldataquery = "select [CN Launch Date],[HK Launch Date],[TMALL Launch Date] ,[ProductCode] ,[Style] float,[Product Name] ,[Category] ,[Prod Type] ,[Gender Group] ,[Key Product (Y/N)] ,[CN InStock Key Item (Y/N)] ,[HK InStock Key Item (Y/N)] ,[TMALL InStock Key Item (Y/N)] ,[Hard Launch (Y/N)] ,[Top Style (Y/N)] ,[QS (Y/N)] ,[Lab (Y/N)] ,[CN SLP (Y/N)] ,[HK SLP (Y/N)] ,[TMALL SLP (Y/N)] ,[CN Planning Qty] ,[HK Planning Qty] ,[TMALL Planning Qty] ,[CN CN-Copy Status (Y/N) - Verified] ,[CN EN-Copy Status (Y/N)] ,[CN CN-Copy Status (Y/N)] ,[CN CN-Copy Comments] ,[Product Copy back from SDL] ,[CN Digital Inventory On-hand Qty] ,[Sample Batch] ,[LW Linguistic Review] ,[LW Specs Review] ,[Review Request to Category] ,[Review back from Category] ,[Prodigy Update] ,[HK EN-Copy Status (Y/N) - Verified] ,[HK TC-Copy Status (Y/N)] ,[HK EN-Copy Comments] ,[HK TC-Copy Comments] ,[HK EN-Copy Download] ,[Task Type] ,[Task Submission] ,[Word Count] ,[New Word Count] ,[Linguistic Review Submission] ,[Specs Review Submission] ,[HK Review Request to Category] ,[HK Review back from Category] ,[Warehouse Shooting] ,[HK EN-Copy] ,[HK TC-Copy] ,[Copy Writing-Send Request] ,[TMALL CN-Copy Status (Y/N)] ,[TMALL CN-Copy Comments] ,[Copy Writing] ,[Tmall Review Request to Category] ,[Tmall Review back from Category] from [Sheet1$]";
// try
// {
// //create our connection strings
// string sexcelconnectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath +
// ";extended properties=" + ""Excel 12.0,HDR=Yes"";
// string ssqlconnectionstring = "Data Source=.;Initial Catalog=Demo;Integrated Security=True";
// //execute a query to erase any previous data from our destination table
// string sclearsql = "delete from " + ssqltable;
// SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
// SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
// sqlconn.Open();
// sqlcmd.ExecuteNonQuery();
// sqlconn.Close();
// //series of commands to bulk copy data from the excel file into our sql table
// OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
// OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
// oledbconn.Open();
// OleDbDataReader dr = oledbcmd.ExecuteReader();
// SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
// bulkcopy.DestinationTableName = ssqltable;
// while (dr.Read())
// {
// bulkcopy.WriteToServer(dr);
// }
// dr.Close();
// oledbconn.Close();
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
//}