namespace Clock.UI.Controllers
{
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using Clock.BLL;
using Clock.Model;
using Newtonsoft.Json;
public class ClockController : Controller
{
UsersClockBLL clockBLL = new UsersClockBLL();
// GET: Clock
public ActionResult Index()
{
return View();
}
public ActionResult Execl()
{
return View();
}
[HttpPost]
public string GetExcel()
{
//獲取所選文件
HttpPostedFileBase getFile = Request.Files["Excel"];
if (getFile != null)
{
//得到所選文件名
string fileName = Server.MapPath("~/Content/") + getFile.FileName;
if (!System.IO.File.Exists(fileName))
getFile.SaveAs(fileName);
//把Excel當作數據源鏈接
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=Excel 12.0;";
//打開Excel
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
//查詢Excel
string sql = "select * from [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(sql, conn);
//初始化適配器
OleDbDataAdapter adapter = new OleDbDataAdapter();
//獲取查出來的Excel表
adapter.SelectCommand = cmd;
//初始化dataset並經過適配器賦值
DataSet ds = new DataSet();
adapter.Fill(ds);
//得到ds的第一個表並添加到dataGridView1顯示
DataTable dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
string sqlStr = string.Format("insert into UsersClock(Department, Name, Num, ClockDate, position) values('{0}','{1}','{2}','{3}','{4}')", dt.Rows[i]["Department"], dt.Rows[i]["Name"], dt.Rows[i]["Num"], dt.Rows[i]["ClockDate"], dt.Rows[i]["position"]);
DBhelper.AddOrDeleteOrUpdate(sqlStr, CommandType.Text);
}
conn.Close();
return "導入成功!";
}
else
{
return "導入失敗!";
}
}
/// <summary>
/// 顯示打卡記錄
/// </summary>
/// <returns></returns>
public ActionResult ShowUsersClock()
{
return View();
}html
/// <summary>
/// 得到全部打卡記錄
/// </summary>
/// <returns></returns>
[HttpGet]
public JsonResult GetUsersClocks()
{
List<UsersClock> clocksList = clockBLL.GetUsersClocks();
return Json(clocksList,JsonRequestBehavior.AllowGet);
}
}
}jquery
@{
Layout = null;
}ajax
<!DOCTYPE html>
<script src="~/Content/jquery-3.1.1.js"></script>
<script>
$(function () {
$.ajax({
url: "/Clock/GetUsersClocks",
type: "get",
success: function (data) {
$(data).each(function (index,data) {
$("#getData").append("<tr><td>" + data.ID + "</td><td>" + data.Department + "</td><td>" + data.Name + "</td><td>" + data.Num + "</td><td>" + data.ClockDate + "</td><td>" + data.position + "</td></tr>");
})
}
})
})
</script>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ShowUsersClock</title>
</head>
<body>
<table id="getData">
<tr>
<td>ID</td>
<td>所屬部門</td>
<td>姓名</td>
<td>打卡號</td>
<td>打卡日期</td>
<td>職位</td>
</tr>
</table>
<div>
</div>
</body>
</html>sql
@{
Layout = null;
}app
<!DOCTYPE html>
<script src="~/Content/jquery-3.1.1.js"></script>
<script>
$(function () {
$("#btnToLead").click(function () {
$.ajax({
url: "/Clock/GetExcel",
contentType: false,
processData: false,
cache: false,
data: new FormData($("#formData")[0]),
type: 'POST',
success: function (data) {
alert(data);
}
})
})
})
</script>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Execl</title>
</head>
<body>
<div>
<form id="formData" method="post" enctype="multipart/form-data">
<input id="File1" type="file" accept=".xls,.xlsx" name="Excel" />
<input id="btnToLead" type="button" value="導入" />
</form>
</div>
</body>
</html>ide