|
|
|
ASP.NET C# ขอโค้ดในการ อิมพอร์ต Excel เข้าสู่ Database MS-SSQL ด้วยครับ |
|
|
|
|
|
|
|
ลองฝช้ ADO.NET กับ Excel น่ะครับ ได้เป็น DataTable จากนั้นก็สามารถนำไปใช้อย่างอื่น ๆ ได้ครับ
|
|
|
|
|
Date :
2011-01-11 06:42:31 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
public void Page_Load(object sender, EventArgs e)
{
OleDbConnection objConn = new OleDbConnection();
OleDbDataAdapter dtAdapter = default(OleDbDataAdapter);
DataTable dt = new DataTable();
string strConnString = null;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + Server.MapPath("MyXls/MyExcelDB.xls") + ";Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(strConnString);
objConn.Open();
string strSQL = null;
strSQL = "SELECT * FROM [Sheet1$]";
dtAdapter = new OleDbDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
//*** BindData To DataGrid ***'
this.myDataGrid.DataSource = dt;
this.myDataGrid.DataBind();
dtAdapter = null;
objConn.Close();
objConn = null;
}
ได้เป็น DataTable นำไปใช้ได้เลยครับ
|
|
|
|
|
Date :
2011-08-22 21:31:08 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตามแบบที่พี่วินแนะนำมาครับ มันมีวิธีที่จะให้เริ่ม import ข้อมูลเริ่มจาก row ที่ 4 ไหมครับ
แบบว่า format excel ที่จะใช้มันมี header ปาไป 3 row แล้วครับ
|
|
|
|
|
Date :
2013-02-07 09:03:29 |
By :
Rookie User |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนที่ Loop Insert ให้ใช้ IF เอาครับ
Code (C#)
for(i = 0;i <= dt.Rows.Count; i++)
{
if(i > 4_
{
// Insert
}
}
|
|
|
|
|
Date :
2013-02-07 09:28:39 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าผมใช้ stored procedure เป็นตัว insert มันจะใช้ยังไงพอทราบไหมครับCode (C#)
ConnectDB();
if (this.fileUpload.HasFile)
{
string newFileName;
String fileExtension;
String[] myArr = null;
int countSplit;
string activeDir = "FileUpload";
myArr = fileUpload.FileName.Split('.');
countSplit = myArr.Count();
fileExtension = myArr[countSplit - 1].Trim();
Random random = new Random();
newFileName = myArr[0].Trim() + "_" + dateNow + "_" + timeNow + "." + fileExtension;
//lbResult.Text = newFileName;
if ((fileExtension == "xls") || (fileExtension == "xlsx"))
{
//Delete Temp Data
strSql = "DELETE FROM tbl_iep_temp_2 WHERE user_name=@user_name";
myCommand = new SqlCommand(strSql, myConnection);
parameter = myCommand.Parameters.Add("@user_name", SqlDbType.NVarChar);
parameter.Value = Session["SessionUserName"].ToString();
myCommand.ExecuteNonQuery();
//Save file to folder
this.fileUpload.SaveAs(Server.MapPath(activeDir + "/" + newFileName));
//-----------------------start import file to DB---------------------------------//
String strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + Server.MapPath(activeDir + "/" + newFileName) + ";" +
"Extended Properties=Excel 12.0;";
using (OleDbConnection connection = new OleDbConnection(strConn))
{
connection.Open();
DataTable dt = null;
dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String[] excelSheetNames = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheetNames[i] = row["TABLE_NAME"].ToString();
i++;
}
OleDbCommand command = new OleDbCommand("Select *, '" + Session["SessionUserName"].ToString() + "' AS user_id FROM [" + excelSheetNames[0] + "]", connection);
using (DbDataReader dr = command.ExecuteReader())
{
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(myConnection))
{
//bulkCopy.BatchSize = 5000;
bulkCopy.DestinationTableName = "tbl_iep_temp_2";
bulkCopy.WriteToServer(dr);
}
}
connection.Close();
//--------------- call stored procedure ------------------//
//Create Wave By Store Procedure
myCommand = new SqlCommand("SP_LP_UPLOAD", myConnection); //Procedure Name
myCommand.CommandTimeout = 0;
myCommand.CommandType = CommandType.StoredProcedure; //Set Type to StoredProcedure
myCommand.Parameters.Add("@user_name", SqlDbType.NVarChar, 50).Value = Session["SessionUserName"].ToString();
myCommand.Parameters.Add("@filename", SqlDbType.NVarChar, 255).Value = newFileName;
myCommand.Parameters.Add("@OP_result", SqlDbType.Int).Direction = ParameterDirection.Output;
myCommand.Parameters.Add("@OP_text", SqlDbType.NVarChar, 250).Direction = ParameterDirection.Output;
myCommand.ExecuteNonQuery();
if (myCommand.Parameters["@OP_result"].Value.ToString() == "0")
{
lbResult.Text = "Upload successful...";
lbResult.ForeColor = System.Drawing.Color.Green;
}
else
{
lbResult.ForeColor = System.Drawing.Color.Red;
lbResult.Text = myCommand.Parameters["@OP_text"].Value.ToString();
}
}
}
else
{
lbResult.Text = "Pls use only excel file. (.xls or .xlsx )";
lbResult.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lbResult.Text = "Not found file to upload! pls try again.";
lbResult.ForeColor = System.Drawing.Color.Red;
}
|
|
|
|
|
Date :
2013-02-07 10:04:54 |
By :
Rookie User |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|