Private Sub ButExcel3_Click(sender As Object, e As EventArgs) Handles ButExcel3.Click
Try
If XXXGridView.Rows.Count > 0 Then
Using cn As New SqlConnection(ConnectString)
cn.Open()
SetExcel3(cn)
End Using
Else
TycFunction.MsgAlert(Page, "無資料,無法輸出!!")
Exit Sub
End If
SetGridView4()
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=中心單位自主檢查表.xls")
Response.Charset = "big5"
Response.ContentType = "application/vnd.xls"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
GridView4.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString)
Response.End()
Me.Session("dtEX3") = Nothing
Catch ex As Exception
Dim str As String = "系統執行發生錯誤:ButExcel3_Click()" & "<br>"
str += "message:" & ex.Message & "<br>"
str += "sql:" & sql & "<br>"
LabError.Text = str
End Try
End Sub
Private Sub SetGridView4()
Try
Dim gvr As GridViewRow = New GridViewRow(0, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim cell1 As TableCell = New TableCell()
cell1.Text = "<table style='font-size:12pt;font-family:DFKai-sb'><tr><td></td><td>駐點人員簽名:</td><td></td><td></td><td></td><td></td><td></td><td></td><td>中心人員:</td><td></td><td></td><td></td><td></td><td></td><td>第 頁</td><td>共 頁</td></tr></table>"
cell1.ColumnSpan = 16
gvr.Controls.Add(cell1)
GridView4.Controls(0).Controls.AddAt(GridView4.Rows.Count + 2, gvr)
Catch ex As Exception
Dim str As String = "系統執行發生錯誤:SetGridView4()" & "<br>"
str += "message:" & ex.Message & "<br>"
str += "sql:" & sql & "<br>"
LabError.Text = str
End Try
End Sub
Private Sub SetExcel3(ByRef cn As SqlConnection)
Try
Dim myDataView As DataView = Me.Session("dv")
Dim myDataTable As DataTable = myDataView.Table
Dim dt As New DataTable("dtEX3")
dt.Columns.Add("XX", Type.GetType("System.String"))
dt.Columns.Add("XXXXXid", Type.GetType("System.String"))
dt.Columns.Add("XXXXXd", Type.GetType("System.String"))
dt.Columns.Add("XXXXe1", Type.GetType("System.String"))
......
dt.Columns.Add("XXXXXote", Type.GetType("System.String"))
dt.Columns.Add("XXXXXe", Type.GetType("System.String"))
dt.Columns.Add("XXXXXte", Type.GetType("System.String"))
Dim row As DataRow = dt.NewRow()
If myDataTable.Rows.Count > 0 Then
For i As Integer = 0 To myDataTable.Rows.Count - 1
row = dt.NewRow
row("no") = (i + 1).ToString()
row("startid") = myDataTable.Rows(i).Item("startid")
......
SqlDA.Dispose()
SqlDR.Close()
End If
dt.Rows.Add(row)
Next
dt.AcceptChanges()
End If
Me.Session("dtEX3") = dt
GridView4.DataSource = Me.Session("dtEX3")
GridView4.DataBind()
For j As Integer = 0 To GridView4.Rows.Count - 1
Dim Hidstartid As HiddenField = GridView4.Rows(j).FindControl("Hidstartid")
Dim DB As DataTable
sql = "select Typ ...... Result='1' "
SqlDA = New SqlDataAdapter(sql, cn)
sql += " and dt.StartID = @startid order by ch.TypeID"
SqlDA.SelectCommand.Parameters.Add("@startid", SqlDbType.NVarChar)
SqlDA.SelectCommand.Parameters("@startid").Value = Hidstartid.Value
SqlDA.SelectCommand.CommandText = sql
SqlDS = New DataSet 'DataSet資料集物件實體化
SqlDA.Fill(SqlDS) '填充DataSet資料集
DB = SqlDS.Tables(0)
SqlDA.Dispose()
SqlDS.Dispose()
Dim DB2 As DataTable
sql = "selec ......nd CChkResult='1' "
SqlDA = New SqlDataAdapter(sql, cn)
sql += " and dt.StartID = @startid order by ch.TypeID"
SqlDA.SelectCommand.Parameters.Add("@startid", SqlDbType.NVarChar)
SqlDA.SelectCommand.Parameters("@startid").Value = Hidstartid.Value
SqlDA.SelectCommand.CommandText = sql
SqlDS = New DataSet 'DataSet資料集物件實體化
SqlDA.Fill(SqlDS) '填充DataSet資料集
DB2 = SqlDS.Tables(0)
SqlDA.Dispose()
SqlDS.Dispose()
Dim g As Integer = 1
If DB.Rows.Count > 0 And DB2.Rows.Count < DB.Rows.Count Then
g = DB.Rows.Count
End If
If DB2.Rows.Count > 0 And DB2.Rows.Count > DB.Rows.Count Then
g = DB2.Rows.Count
End If
If DB2.Rows.Count > 0 And DB.Rows.Count > 0 And DB2.Rows.Count = DB.Rows.Count Then
g = DB2.Rows.Count
End If
For k As Integer = 0 To DB2.Rows.Count - 1
GridView4.Rows(j).Cells(5).Text += "<tr><td style='border: solid'>" & k + 1 & "</td><td style='border: solid'>" & DB2.Rows(k).Item("XXpeNa") & "</td><td colspan='4' style='border: solid'>" & DB2.Rows(k).Item("STypeNa") & "</td></tr>"
Next
GridView4.Rows(j).Cells(5).Text += "</table>"
GridView4.Rows(j).Cells(5).ColumnSpan = 6
Next
Catch ex As Exception
Dim str As String = "系統執行發生錯誤:SetExcel3()" & "<br>"
str += "message:" & ex.Message & "<br>"
str += "sql:" & sql & "<br>"
LabError.Text = str
End Try
End Sub
Protected Sub GridView4_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView4.RowCreated
If (e.Row.RowType = DataControlRowType.Header) Then
Dim header As String = ""
header = "<table><tr><td></td><td colspan='14' style='text-align: center'><div>XX中心<br>"
header += "XX表</div></td><td></td></tr><tr><td></td><td><span>單位:</span></td>"
header += "<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>"
header += "<td></td><td></td><td><span>日期:</span></td><td></td><td></td><td></td></tr></table>"
Dim headRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
Dim cell1 As New TableHeaderCell
cell1.Text = header
cell1.ColumnSpan = 16
headRow.Cells.Add(cell1)
GridView4.Controls(0).Controls.Add(headRow)
End If
End Sub