
''' <summary>
''' 出力系ユーティリティクラス
''' </summary>
''' <remarks></remarks>
Public Class MyExportUtility

    ' ''' <summary>
    ' ''' DataSetをCSVとしてHttpResponseに出力
    ' ''' </summary>
    'Public Shared Sub ExportCsv(ByVal ds As DataSet, ByVal fileName As String, ByVal response As HttpResponse)
    '    response.Clear()
    '    response.ContentType = "text/csv"
    '    response.ContentEncoding = Encoding.GetEncoding("Shift_JIS")
    '    response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)

    '    Dim sb As New StringBuilder()

    '    For Each dt As DataTable In ds.Tables
    '        ' ヘッダー行
    '        For i As Integer = 0 To dt.Columns.Count - 1
    '            sb.Append("""" & dt.Columns(i).ColumnName.Replace("""", """""") & """")
    '            If i < dt.Columns.Count - 1 Then sb.Append(",")
    '        Next
    '        sb.AppendLine()

    '        ' データ行
    '        For Each dr As DataRow In dt.Rows
    '            For i As Integer = 0 To dt.Columns.Count - 1
    '                Dim value As String = dr(i).ToString()
    '                value = value.Replace("""", """""")
    '                sb.Append("""" & value & """")
    '                If i < dt.Columns.Count - 1 Then sb.Append(",")
    '            Next
    '            sb.AppendLine()
    '        Next
    '    Next

    '    response.Write(sb.ToString())
    '    response.End()
    'End Sub

    ''' <summary>
    ''' DataTableを指定された列だけでCSV出力
    ''' </summary>
    ''' <param name="dt">対象DataTable</param>
    ''' <param name="columns">出力する列名の配列 (例: {"a","b","c"})</param>
    ''' <param name="fileName">出力するCSVファイル名</param>
    ''' <param name="response">HttpResponse</param>
    Public Shared Sub ExportCsv(ByVal dt As DataTable, ByVal columns As String(), _
                                ByVal fileName As String, ByVal response As HttpResponse)

        response.Clear()
        response.ContentType = "text/csv"
        response.ContentEncoding = Encoding.GetEncoding("Shift_JIS") ' Excel向け
        response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)

        Dim sb As New StringBuilder()

        ' --- ヘッダー行 ---
        For i As Integer = 0 To columns.Length - 1
            sb.Append("""" & columns(i).Replace("""", """""") & """")
            If i < columns.Length - 1 Then sb.Append(",")
        Next
        sb.AppendLine()

        ' --- データ行 ---
        For Each dr As DataRow In dt.Rows
            For i As Integer = 0 To columns.Length - 1
                Dim colName As String = columns(i)
                Dim value As String = ""
                If dt.Columns.Contains(colName) AndAlso Not dr.IsNull(colName) Then
                    value = dr(colName).ToString().Trim()
                End If
                value = value.Replace("""", """""")
                sb.Append("""" & value & """")
                If i < columns.Length - 1 Then sb.Append(",")
            Next
            sb.AppendLine()
        Next

        response.Write(sb.ToString())
        response.End()
    End Sub

End Class
