 |
|
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;
public partial class Operation_importfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!(FileUpload1.PostedFile == null))
{
string UlFileName;
int i;
UlFileName = ("MyXls/" + System.IO.Path.GetFileName(FileUpload1.FileName));
// *** Save Images ***'
FileUpload1.PostedFile.SaveAs(Server.MapPath(UlFileName));
// *** Create Excel.Application ***'
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlBook;
Excel.Worksheet xlSheet1;
xlBook = xlApp.WorkbookOpen += (Server.MapPath(UlFileName));
xlBook.Application.Visible = false;
xlSheet1 = xlBook.Worksheets(1);
// *** Create DataTable ***'
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
// *** Column ***'
dt.Columns.Add("PID");
dt.Columns.Add("FName");
dt.Columns.Add("LName");
dt.Columns.Add("OtherName");
dt.Columns.Add("SDay");
dt.Columns.Add("SMonth");
i = 2;
while (!(xlSheet1.Cells.Item[i, 1].Value.Trim() == ""))
{
dr = dt.NewRow;
dr["PID"] = xlSheet1.Cells.Item[i, 1].Value;
dr["FName"] = xlSheet1.Cells.Item[i, 2].Value;
dr["LName"] = xlSheet1.Cells.Item[i, 3].Value;
dr["OtherName"] = xlSheet1.Cells.Item[i, 4].Value;
dr["SDay"] = xlSheet1.Cells.Item[i, 5].Value;
dr["SMonth"] = xlSheet1.Cells.Item[i, 6].Value;
dt.Rows.Add(dr);
i = (i + 1);
}
// *** End DataTable ***'
// *** Insert to Database ***'
SqlConnection objConn;
SqlCommand objCmd;
string strConnString;
string strSQL;
strConnString = ("Provider=System.Data.SqlClient;Data Source=LAPTOP\\MSSQLSERVER2008;Initial Catalog=TEST;Integrated Security=True"
+ (Server.MapPath("database/mydatabase.mdb") + ";"));
objConn = new SqlConnection(strConnString);
objConn.Open();
for (i = 0; (i
<= (dt.Rows.Count - 1)); i++)
{
// *** Insert Record ***'
strSQL = ("INSERT INTO Profile (PID,FName,LName,OtherName,SDay,SMonth) " + (" VALUES (\'"
+ (dt.Rows[i]["PID"] + ("\',\'"
+ (dt.Rows[i]["FName"] + ("\' " + (",\'"
+ (dt.Rows[i]["LName"] + ("\',\'"
+ (dt.Rows[i]["OtherName"] + ("\' " + (",\'"
+ (dt.Rows[i]["SDay"] + ("\',\'"
+ (dt.Rows[i]["SMonth"] + "\') ")))))))))))))));
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
}
this.lblText.Text = "Record Inserted.";
objConn.Close();
objConn = null;
xlApp.Application.Quit();
xlApp.Quit();
xlSheet1 = null;
xlBook = null;
xlApp = null;
}
}
}
รบกวนพี่ๆช่วยดูหน่อยนะคะ ไม่รู้ว่าผิดตรงไหน ขอบคุนมากค่ะ
|
 |
 |
 |
 |
Date :
2010-12-23 01:42:50 |
By :
FFF |
|
 |
 |
 |
 |
|
|
 |