2009年12月7日 星期一

Gridview 設定 不斷行、欄位隱藏、設定欄寬

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Or e.Row.RowType = DataControlRowType.Header Then
'不斷行設定
For i As Integer = 0 To e.Row.Cells.Count - 1
e.Row.Cells(i).Attributes.Add("style", "word-break :keep-all ; word-wrap:keep-all")
Next
'欄位隱藏
e.Row.Cells(1).Visible = False
'設定欄寬
e.Row.Cells(0).Attributes.Add("style", "width:20px")
e.Row.Cells(2).Attributes.Add("style", "width:30px")
e.Row.Cells(3).Attributes.Add("style", "width:90px")
e.Row.Cells(4).Attributes.Add("style", "width:90px")
e.Row.Cells(5).Attributes.Add("style", "width:70px")
e.Row.Cells(6).Attributes.Add("style", "width:150px")
End If
End Sub

2009年11月28日 星期六

ASP.NET 產生 匯出 Excel 到Server端

假使要從Client產在一個excel檔到Server端
必須在web.config加上一個有權限執行excel的使用者
如:
<system.web>
<identity impersonate="true" userName ="Administrator" password ="123123"/>
</system.web>

參考自:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;306022

Try
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add

For i As Integer = 0 To ds.Tables.Count - 1
oSheet = oBook.Worksheets.Add
'合併欄位
oSheet.Range("A1", "I1").Merge()
oSheet.Range("J1", "K1").Merge()
'文字置中
oSheet.Range("A1", "I1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
oSheet.Range("J1", "K1").HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft
oSheet.cells(1, 1).Value = "自製工具個人分戶帳"
oSheet.cells(1, 10).Value = "資料日期: " & DateTime.Now.ToShortDateString
oSheet.cells(1, 1).Font.Bold = True
'加入外框線
oSheet.Range("A2", "K" & ds.Tables(i).Rows.Count + 2).Borders.LineStyle = 1
'強制轉型 不加這行的話如果字串是0002 匯到excel會變成2
oSheet.Cells.NumberFormatLocal = "@"
For j As Integer = 0 To ds.Tables(i).Rows.Count - 1
'Add data to cells of the first worksheet in the new workbook.
oSheet.Name = ds.Tables(i).Rows(j).Item(1)
'oSheet.Range("A1").Value = "Last Name"
oSheet.cells(2, 1).Value = "項次"
oSheet.cells(2, 2).Value = "借用人"
oSheet.cells(2, 3).Value = "借用人帳號"
oSheet.cells(j + 3, 1).value = RTrim(ds.Tables(i).Rows(j).Item(0))
oSheet.cells(j + 3, 2).value = RTrim(ds.Tables(i).Rows(j).Item(1))
oSheet.cells(j + 3, 3).value = RTrim(ds.Tables(i).Rows(j).Item(2))
'自動調整欄寬
oSheet.Columns("A:Z").EntireColumn.AutoFit()
Next
Next
'Save the Workbook and quit Excel.
'如果檔案已存在 則自動刪除
If System.IO.File.Exists("C:\" & filename & "borrow.xls") = True Then
System.IO.File.Delete("C:\" & filename & "borrow.xls")
End If
oBook.SaveAs("C:\" & filename & "borrow.xls")

oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

'KillExcel()
Catch ex As Exception

End Try

如果要強制刪除Excel的程序可用以下語法
不過要注意執行後會強制關掉畫面上所有的excel
Private Sub KillExcel()
Dim myProcesses() As Diagnostics.Process
Dim myProcess As Diagnostics.Process
' Returns array containing all instances of "EXCEL".
On Error Resume Next
myProcesses = Diagnostics.Process.GetProcessesByName("EXCEL")
For Each myProcess In myProcesses
myProcess.Kill()
Next
On Error GoTo 0
myProcesses = Nothing
myProcess = Nothing
End Sub

DataTable 加入流水編號方式

Dim dt as new DataTable
Dim column As New DataColumn("項次", Type.GetType("System.Int32"))
With column
.AutoIncrement = True
.AutoIncrementSeed = 1
.AutoIncrementStep = 1
End With
dt.Columns.Add(column)

2009年11月23日 星期一

輸出畫面為千分位 加逗號

SELECT convert(nvarchar,cast('12345.999' as money),1)

2009年11月17日 星期二

如何使 ASP.NET 元件 MENU 能夠更漂亮 CSS

套用以下基本CSS,再依個人審美觀做適當的修改
<asp:Menu ID="Menu1" runat="server" CssClass="menu_Main" DynamicHorizontalOffset="2"
Orientation="Horizontal" StaticSubMenuIndent="10px" Width="296px">
<StaticMenuStyle CssClass ="menu_StaticMenuStyle" />
<StaticMenuItemStyle  CssClass="menu_StaticMenuItemStyle " />
<StaticHoverStyle CssClass="menu_StaticHoverStyle" />
<StaticSelectedStyle CssClass="menu_StaticSelectedStyle"/>
<DynamicMenuStyle CssClass="menu_DynamicMenuStyle" BackColor="#F8F8D4" />
<DynamicSelectedStyle CssClass="menu_DynamicSelectedStyle"  />
<DynamicHoverStyle  CssClass ="menu_DynamicHoverStyle"/>
<Items>
</Items>
</asp:Menu>
/*menu style*/
.menu_Main{
height:30px;
}
.menu_StaticMenuStyle{
}
.menu_StaticMenuItemStyle {
width:70px;
font-size:15px;
/*HorizontalPadding="5px" VerticalPadding="2px"*/
}
.menu_StaticHoverStyle{
color:#FFFFFF;
background-color:#668BB2 ;
width:60px;
}
.menu_StaticSelectedStyle{
}
.menu_DynamicMenuStyle{
font-size:15px;
border-right: #B1B266 thin solid;
border-top: #B1B266 thin solid;
border-left: #B1B266 thin solid;
border-bottom: #B1B266 thin solid ;
z-index: 100;
width:100px;
}
.menu_DynamicMenuItemStyle{
width:200px;
/*HorizontalPadding="5px" VerticalPadding="2px"*/
}
.menu_DynamicSelectedStyle{
}
.menu_DynamicHoverStyle{
color:#FFFFFF;
background-color:#B1B266 ;
width:100px;
}

Gridview 套用 CSS

使用Visual Studio 直接拉gridview到畫面, 如果沒有再套上個css的話,真的是醜到暴, 這邊簡單做了一個css套上gridview,畫面好看多了
<asp:GridView ID="GridView1" runat="server" Width="800px" CssClass="fixedheader">
<RowStyle CssClass="gv_ItemCSSR" />
<HeaderStyle CssClass="gv_HeaderCSSM" />
<FooterStyle CssClass="gv_FooterCSS" />
<SelectedRowStyle CssClass ="gv_SelectedRowCSS" />
<Columns>
<asp:TemplateField HeaderText="選取" ShowHeader="False">
<ItemTemplate>
<asp:CheckBox ID="Chk" runat="server" Checked="True" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
/* gridview style*/
.fixedheader
{
left: 0px;
position: relative;
top: 0px;
font-size: 12px;
}
/* header 漸層*/
.gv_HeaderCSSN{
font-size: 14px;
height:20px;
font-weight :100; /* 不設定粗體的話 標題字會太粗 */
background-image: url(../Image_File/gv_bk01.gif);
}
/* header 條紋*/
.gv_HeaderCSSM{
font-size: 14px;
height:20px;
font-weight :100;
background-image: url(../Image_File/gv_bk02.gif);
}
/* header 深藍 字白字*/
.gv_HeaderCSSB
{
font-size: 14px;
height:20px;
font-weight :100;
color:#FFFFFF;
background-image: url(../Image_File/gv_bk03.gif);
}
.gv_ItemCSSG{
background-color: #EEFCF5;
height:15px;
font-size:9pt;
color:#000000;
}
.gv_ItemCSSB{
background-color: #EEEEFC;
height:15px;
font-size:9pt;
color:#000000;
}
.gv_ItemCSSR{
background-color: #FCEEEE;
height:15px;
font-size:9pt;
color:#000000;
}
.gv_SelectedRowCSS
{
background-color:#80FF80;
}
.gv_FooterCSS{
background-color: #1FBBBD;
color: white;
height:20px;
font-size:9pt;
}

jQuery plugin: Autocomplete

為了實現類似搜尋引擊自動完成的功能, 上網找有沒有現成的套件可以使用, http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ jQuery提供不少好用的套件, 不過產生了一個問題, 如果我畫面上使用UpdatePanel則會沒有反應, 上網查了一下,原來是這個套件不支援非同步動作, 必須下載新版的jQuery並使用live綁定 原始:
$("#ctl00_ContentPlaceHolder1_TextBox8").autocomplete('Return_B.aspx?q=a',
{ 
delay: 200,
highlight: false,
multiple: true,
multipleSeparator: " ",
scroll: true,
scrollHeight: 300
});
改成:
$("#ctl00_ContentPlaceHolder1_TextBox8").live("keypress",function(){
$(this).autocomplete('Return_B.aspx?q=a',
{
delay: 200,
highlight: false,
multiple: true,
multipleSeparator: " ",
scroll: true,
scrollHeight: 300,
});
});
使用live()綁定之後雖然自動完成有反應, 不過每次自動搜尋出結果後, 又回來初始的搜尋結果, 只好暫時先把這個畫面的UpdatePanel拿掉, 再找辦法解決