2009-09-15 8 views
1

이러한 루틴 (vb.net)을 사용하면 셀에 템플릿 컨트롤이있는 경우에도 CSV에 대한 Gridview를 덤프 할 수 있습니다. 그것은 작동하지만, 나는 그것에 흥분하지 않습니다.리팩토링 : Gridview CSV 파일로 내보내기

개선해야 할 점과 그 이유는 무엇입니까?

Private Shared Function CsvFormatted(ByVal t As String) As String 
    If t.Contains(",") Then 
     t = """" + t + """" 
    End If 
    Return t.Replace("\ ", "") 

End Function 

Private Shared Function GetCellText(ByVal cell As DataControlFieldCell) As String 
    If cell.Controls.Count = 0 Then 
     Return CsvFormatted(cell.Text) 
    Else 
     For Each current In cell.Controls 
      If TypeOf current Is Label Then 
       Return CsvFormatted(TryCast(current, Label).Text) 
      ElseIf TypeOf current Is TextBox Then 
       Return CsvFormatted(TryCast(current, TextBox).Text) 
      ElseIf TypeOf current Is LinkButton Then 
       Return CsvFormatted(TryCast(current, LinkButton).Text) 
      ElseIf TypeOf current Is ImageButton Then 
       Return CsvFormatted(TryCast(current, ImageButton).AlternateText) 
      ElseIf TypeOf current Is HyperLink Then 
       Return CsvFormatted(TryCast(current, HyperLink).Text) 
      ElseIf TypeOf current Is DropDownList Then 
       Return CsvFormatted(TryCast(current, DropDownList).SelectedItem.Text) 
      ElseIf TypeOf current Is CheckBox Then 
       Return CsvFormatted(If(TryCast(current, CheckBox).Checked, "True", "False")) 
      End If 
     Next 
    End If 
    Return "" 
End Function 

Public Shared Sub ExportGridViewToCSV(ByVal grid As GridView, ByVal fileName As String) 
    HttpContext.Current.Response.Clear() 
    HttpContext.Current.Response.Buffer = True 
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName) 
    HttpContext.Current.Response.Charset = "" 
    HttpContext.Current.Response.ContentType = "application/text" 
    Dim sb As New StringBuilder() 
    For k As Integer = 0 To grid.Columns.Count - 1 
     grid.Columns(k).Visible = True 
     'add separator 
     sb.Append(grid.Columns(k).HeaderText + ","c) 
    Next 
    'append new line 
    sb.Append(vbCr & vbLf) 
    For i As Integer = 0 To grid.Rows.Count - 1 
     For k As Integer = 0 To grid.Columns.Count - 1 
      grid.Columns(k).Visible = True 
      'add separator 
      sb.Append(GetCellText(grid.Rows(i).Cells(k)) + ","c) 
     Next 
     'append new line 
     sb.Append(vbCr & vbLf) 
    Next 
    HttpContext.Current.Response.Output.Write(sb.ToString()) 
    HttpContext.Current.Response.Flush() 
    HttpContext.Current.Response.End() 
End Sub 
+0

btw 어떻게 SO 편집기에서 Return t.Replace (" ", "")를 인식 할 수 있습니까? – BenB

답변

1
  • 당신은 비 분리 공백 이외의 다른 HTML 리터럴을 언 이스케이프 걱정해야 하는가? HttpUtility.HtmlDecode을 사용할 수 있습니다.
  • CsvFormatted 루틴은 null 입력 문자열에 대해 guard이 될 수 있습니다. 안전하지 않으므로 비용이 거의 들지 않습니다.
  • Turkey test을 전달해야합니까? 일부 국가에서는 세미콜론을 CSV 구분 기호로 사용합니다. 10 진수 구분 기호로 점이나 쉼표를 고려해야 할 수도 있습니다.
  • CSV 문자열을 HttpContext 응답을 발행하는 것과는 별도의 기능으로 분리 할 수 ​​있습니다.
  • vbCr & vbLf 대신 vbCrLf를 사용할 수 있습니다.

내 주요 조언 : 좋은 단위 테스트를 작성하는 코드는 다음을 잊지 패스 있는지 확인하고 몇 가지 더 많은 기능을 구현하기 위해 이동합니다. 코드는 꽤 잘 캡슐화되어 있기 때문에 나중에 필요할 때 리팩토링 할 수 있습니다.

+0

큰 충고, 고마워. 칠면조 테스트는 매우 흥미 롭습니다 ... – BenB