我正在微软视觉工作室中编写一个网页,其中有一个显示报告的网格视图。我想要一个按钮,将这个 gridview 导出到 csv 文件。
只要gridview有,我就可以让导出按钮工作
AutoGenerateColumns = "假"
但我需要 AutoGenerateColumns 为真,因为我正在使用此代码动态填充 gridview。
enter code here
Dim gv As GridView = pnlGridViews.FindControl("gv0")
Dim lbl As Label = pnlGridViews.FindControl("lbl0")
gv.Visible = True
Dim sql As String = "select net_id, byu_id, rest_of_name || ' ' || last_name as NAME, acc_balance as HOUSING_AMOUNT, din_balance as DINING_AMOUNT, " & _
"(acc_balance + din_balance) as TOTAL from housing.PEOPLE where acc_balance >0 or din_balance > 0 order by net_id"
Dim result As CoreData
result = QueryDB.GetAllResultsCD(sql)
Dim dt As New DataTable()
'make the columns
dt.Columns.Add("NET_ID", Type.GetType("System.String"))
dt.Columns.Add("BYU_ID", Type.GetType("System.String"))
dt.Columns.Add("NAME", Type.GetType("System.String"))
dt.Columns.Add("HOUSING_AMOUNT", Type.GetType("System.String"))
dt.Columns.Add("DINING_AMOUNT", Type.GetType("System.String"))
dt.Columns.Add("TOTAL", Type.GetType("System.String"))
dt.Columns.Add("FaWi(HS)", Type.GetType("System.String"))
dt.Columns.Add("FaWi(MP)", Type.GetType("System.String"))
dt.Columns.Add("Wint(HS)", Type.GetType("System.String"))
dt.Columns.Add("Wint(MP)", Type.GetType("System.String"))
dt.Columns.Add("SpSu(HS)", Type.GetType("System.String"))
dt.Columns.Add("SpSu(MP)", Type.GetType("System.String"))
dt.Columns.Add("Sprg(HS)", Type.GetType("System.String"))
dt.Columns.Add("Sprg(MP)", Type.GetType("System.String"))
dt.Columns.Add("Sumr(HS)", Type.GetType("System.String"))
dt.Columns.Add("Sumr(MP)", Type.GetType("System.String"))
dt.Columns.Add("AcYr(HS)", Type.GetType("System.String"))
dt.Columns.Add("AcYr(MP)", Type.GetType("System.String"))
dt.Columns.Add("Else", Type.GetType("System.String"))
For Each person In result
dt.Rows.Add()
Dim fawi_hs As String = ""
Dim fawi_mp As String = ""
Dim wint_hs As String = ""
Dim wint_mp As String = ""
Dim spsu_hs As String = ""
Dim spsu_mp As String = ""
Dim spr_hs As String = ""
Dim spr_mp As String = ""
Dim sumr_hs As String = ""
Dim sumr_mp As String = ""
Dim acyr_hs As String = ""
Dim acyr_mp As String = ""
Dim yearly As String = ""
Dim meal_plan As String = ""
Dim bedspace As String = ""
Dim agr_prd As String = ""
Dim net_id As String = person("NET_ID")
Dim byu_id As String = person("BYU_ID")
Dim name As String = person("NAME")
Dim h_amt As String = person("HOUSING_AMOUNT")
Dim d_amt As String = person("DINING_AMOUNT")
Dim total As String = person("TOTAL")
Dim sql2 As String = "select item_type, common_id as byu_id, item_amt,applied_amt,(item_amt - applied_amt) as bal, ACCOUNT_TYPE_SF, item_term from link.ps_item_sf_lnk where " & _
"common_id = '" & byu_id & "' and ACCOUNT_TYPE_SF in ('ACC','DIN','DPH') and due_dt < sysdate and item_amt - applied_amt <> 0"
Dim result2 As CoreData
result2 = QueryDB.GetAllResultsCD(sql2)
If byu_id = "949888717" Then
'Hello
byu_id = byu_id
End If
For Each delinquent In result2
Dim term_id As String = delinquent("ITEM_TERM")
Dim sql_terms As String = "select * from housing.RS_SNG_CES_TERMS where ps_term_id = '" & term_id & "'"
Dim result_term As CoreData
result_term = QueryDB.GetOneResultCD(sql_terms)
Dim agr As String
If Not result_term Is Nothing Then
agr = result_term("AGR_PRD_ID")
Else
agr = "None"
End If
Dim act_type As String = delinquent("ACCOUNT_TYPE_SF")
Dim bal As Decimal = delinquent("BAL")
Dim current_sum As Decimal
If agr.EndsWith("FaWi") And act_type = "ACC" Then
current_sum = getCurrencyDecimal(fawi_hs)
fawi_hs = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("FaWi") And act_type = "DIN" Then
current_sum = getCurrencyDecimal(fawi_mp)
fawi_mp = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("Wint") And act_type = "ACC" Then
current_sum = getCurrencyDecimal(wint_hs)
wint_hs = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("Wint") And act_type = "DIN" Then
current_sum = getCurrencyDecimal(wint_mp)
wint_mp = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("Sumr") And act_type = "ACC" Then
current_sum = getCurrencyDecimal(sumr_hs)
sumr_hs = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("Sumr") And act_type = "DIN" Then
current_sum = getCurrencyDecimal(sumr_mp)
sumr_mp = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("Sprg") And act_type = "ACC" Then
current_sum = getCurrencyDecimal(spr_hs)
spr_hs = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("Sprg") And act_type = "DIN" Then
current_sum = getCurrencyDecimal(spr_mp)
spr_mp = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("SpSu") And act_type = "ACC" Then
current_sum = getCurrencyDecimal(spsu_hs)
spsu_hs = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("SpSu") And act_type = "DIN" Then
current_sum = getCurrencyDecimal(spsu_mp)
spsu_mp = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("AcYr") And act_type = "ACC" Then
current_sum = getCurrencyDecimal(acyr_hs)
acyr_hs = String.Format("{0:$#,##0.00}", bal + current_sum)
ElseIf agr.EndsWith("AcYr") And act_type = "DIN" Then
current_sum = getCurrencyDecimal(acyr_mp)
acyr_mp = String.Format("{0:$#,##0.00}", bal + current_sum)
Else
current_sum = getCurrencyDecimal(yearly)
yearly = String.Format("{0:$#,##0.00}", bal + current_sum)
End If
Next
dt.Rows(dt.Rows.Count - 1)("NET_ID") = net_id
dt.Rows(dt.Rows.Count - 1)("BYU_ID") = byu_id
dt.Rows(dt.Rows.Count - 1)("NAME") = name
If Not h_amt Is Nothing And Not h_amt = "" Then
dt.Rows(dt.Rows.Count - 1)("HOUSING_AMOUNT") = String.Format("`enter code here`{0:$#,##0.00}", Decimal.Parse(h_amt))
End If
If Not d_amt Is Nothing And Not d_amt = "" Then
dt.Rows(dt.Rows.Count - 1)("DINING_AMOUNT") = String.Format("`enter code here`{0:$#,##0.00}", Decimal.Parse(d_amt))
End If
If Not total Is Nothing And Not total = "" Then
dt.Rows(dt.Rows.Count - 1)("TOTAL") = String.Format("{0:$#,##0.00}", `enter code here`Decimal.Parse(total))
End If
dt.Rows(dt.Rows.Count - 1)("FaWi(HS)") = fawi_hs
dt.Rows(dt.Rows.Count - 1)("FaWi(MP)") = fawi_mp
dt.Rows(dt.Rows.Count - 1)("Wint(HS)") = wint_hs
dt.Rows(dt.Rows.Count - 1)("Wint(MP)") = wint_mp
dt.Rows(dt.Rows.Count - 1)("SpSu(HS)") = spsu_hs
dt.Rows(dt.Rows.Count - 1)("SpSu(MP)") = spsu_mp
dt.Rows(dt.Rows.Count - 1)("Sprg(HS)") = spr_hs
dt.Rows(dt.Rows.Count - 1)("Sprg(MP)") = spr_mp
dt.Rows(dt.Rows.Count - 1)("Sumr(HS)") = sumr_hs
dt.Rows(dt.Rows.Count - 1)("Sumr(MP)") = sumr_mp
dt.Rows(dt.Rows.Count - 1)("AcYr(HS)") = acyr_hs
dt.Rows(dt.Rows.Count - 1)("AcYr(MP)") = acyr_mp
dt.Rows(dt.Rows.Count - 1)("Else") = yearly
Next
gv.DataSource = dt
gv.DataBind()
lbl.Text = "<br/>Total Records: " & gv.Rows.Count & "<br/><br/>"
导出按钮的代码如下所示
enter code here
尝试
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", `enter code here`"attachment;filename=GridViewExport.csv")
Response.Charset = ""
Response.ContentType = "application/text"
'gv0.AllowPaging = False
'gv0.DataBind()
Dim exportStr As String = ""
exportStr &= Mha.GridViews.ToCSV(gv0) & Environment.NewLine
'append new line
Response.Output.Write(exportStr)
Response.Flush()
Response.End()
Catch ex As Exception
'do nothing
End Try
关于如何导出此网格视图同时还允许我事先动态填充网格视图的任何想法?