教大家如何建立一個簡單的巨集(VBA)
這邊主要是要利用Function的方式讓大家可以在Excel上呼叫自已寫的程式
首先,要先把Excel中的開發人員這個頁籤打開
1.選擇檔案>選項
2.接著將開發人員項目勾起來
//以下所有未加AllowAnonymous者,都一定要經過login 成功才可以進入
[Authorize]
public class MainController : Controller
{
//不一定要經過login 成功才可以進入
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
[AllowAnonymous]
[HttpPost]
public ActionResult Login(Log_Xml logxml)
{
//這邊就會進到Log_Xml 做Validate func
if (!ModelState.IsValid)
{
return View();
}
//要讓會員登入成「已驗證」狀態的話
FormsAuthentication.RedirectFromLoginPage(logxml.Account, false);
return null;
}
[AllowAnonymous]
[HttpPost]
public void LogOut()
{
//清除Session中的資料
Session.Abandon();
//登出表單驗證
FormsAuthentication.SignOut();
//導至登入頁
//return RedirectToAction("Login", "Main");
}
}
DECLARE @temp_addr NVARCHAR(100)=NULL
DECLARE @temp_zipcode NVARCHAR(10)
DECLARE @temp_district NVARCHAR(50)
DECLARE @temp_city NVARCHAR(50)
DECLARE @temp_IsPostAddr NVARCHAR(1)
DECLARE @temp_ori NVARCHAR(400)
DECLARE @data_count INT=0
SET @temp_ori= dbo.f_AddressMerge(@mail_addr_dis,@mail_road,@mail_lane,@mail_alley,@mail_number_start,@mail_number_end,@mail_floor_start,@mail_floor_end,@mail_room,@mail_remark,@mail_post_name,@mail_postal_address,@mail_lane_l,@mail_alley_l,@mail_hong,@mail_hong_l,@mail_number_start2)
----宣告一個指標
DECLARE my_cur CURSOR FOR (
--回傳純量函數
SELECT res from f_SplitToTable(@temp_ori,',')
)
--開啟指標
open my_cur
--當指標的狀態為0, 即指標內尚有資料
--如知道資料列中有多少資料,可以用data_count變數來判斷
--WHILE @@fetch_status = 0 AND @data_count<5
WHILE @data_count<5
BEGIN
--By不同列指定給不同的變數
SET @data_count=@data_count+1
IF @data_count='1'
BEGIN
FETCH NEXT FROM my_cur INTO @temp_IsPostAddr--,@temp_zipcode,@temp_city,@temp_district,@temp_addr
END
IF @data_count='2'
BEGIN
FETCH NEXT FROM my_cur INTO @temp_zipcode--@temp_city,@temp_district,@temp_addr
END
IF @data_count='3'
BEGIN
FETCH NEXT FROM my_cur INTO @temp_city--,@temp_district,@temp_addr
END
IF @data_count='4'
BEGIN
FETCH NEXT FROM my_cur INTO @temp_district--,@temp_addr
END
IF @data_count='5'
BEGIN
FETCH NEXT FROM my_cur INTO @temp_addr
END
END
--關閉指標
CLOSE my_cur
--釋放記憶體
DEALLOCATE my_cur
--方式二
DECLARE @temp_addr NVARCHAR(100)=NULL
DECLARE @temp_zipcode NVARCHAR(10)
DECLARE @temp_district NVARCHAR(50)
DECLARE @temp_city NVARCHAR(50)
DECLARE @temp_IsPostAddr NVARCHAR(1)
DECLARE @temp_ori TABLE (IsPostAddr nvarchar(1), zipcode nvarchar(6), city nvarchar(20), district nvarchar(20), result nvarchar(300))
DECLARE @data_count INT
--回傳多重式資料表直函式
INSERT INTO @temp_ori(IsPostAddr,zipcode,city,district,result)
(SELECT * FROM dbo.tf_AddressMerge(@mail_addr_dis,@mail_road,@mail_lane,@mail_alley,@mail_number_start,@mail_number_end,@mail_floor_start,@mail_floor_end,@mail_room,@mail_remark,@mail_post_name,@mail_postal_address,@mail_lane_l,@mail_alley_l,@mail_hong,@mail_hong_l,@mail_number_start2))
SET @data_count=(SELECT COUNT(*) CT FROM @temp_ori)
IF @data_count>0
BEGIN
SET @temp_IsPostAddr =(SELECT TOP 1 IsPostAddr FROM @temp_ori)
SET @temp_zipcode =(SELECT TOP 1 zipcode FROM @temp_ori)
SET @temp_city =(SELECT TOP 1 city FROM @temp_ori)
SET @temp_district =(SELECT TOP 1 district FROM @temp_ori)
SET @temp_addr =(SELECT TOP 1 result FROM @temp_ori)
END