2008-08-26 2 views

답변

1

네이티브 XLS 파일이어야합니까? 최선의 방법은 데이터를 CSV 파일로 내보내는 것입니다. CSV 파일은 일반 텍스트이며 합리적으로 생성하기 쉽습니다. CSV는 기본적으로 대부분의 사용자가 Excel에서 열어 차이점을 알지 못합니다.

0

여기서 우리는 많은 앱에서 코드를 사용합니다. 우리는 "내보낼 수없는"열을 정리할 특별한 방법이 있습니다. 또한 헤더가없는 열은 내보낼 수 없지만 필요에 맞게 논리를 조정할 수 있습니다.

편집 : 코드 포맷터는 vb.net을 좋아하지 않습니다. 비주얼 스튜디오에 복사/붙여 넣기를하면 괜찮습니다.


    Public Overloads Shared Function BuildExcel(ByVal gView As System.Web.UI.WebControls.GridView) As String 

      PrepareGridViewForExport(gView) 


      Dim excelDoc As New StringBuilder 

      Dim startExcelXML As String = " " + _ 
       " " + _ 
       " " + _ 
       " " + _ 
       " " + _ 
       " " + _ 
       "  " + _ 
       "  " + _ 
       " " 
      Dim endExcelXML As String = "" 

      Dim rowCount As Int64 = 0 
      Dim sheetCount As Int16 = 1 


      excelDoc.Append(startExcelXML) 
      excelDoc.Append("") 
      excelDoc.Append("") 

      ' write out column headers 
      excelDoc.Append("") 

      For x As Int32 = 0 To gView.Columns.Count - 1 

       'Only write out columns that have column headers. 
       If Not gView.Columns(x).HeaderText = String.Empty Then 
        excelDoc.Append("") 
        excelDoc.Append(gView.Columns(x).HeaderText.ToString) 
        excelDoc.Append("") 
       End If 
      Next 

      excelDoc.Append("") 

      For r As Int32 = 0 To gView.Rows.Count - 1 

       rowCount += rowCount 

       If rowCount = 64000 Then 
        rowCount = 0 
        sheetCount += sheetCount 
        excelDoc.Append("") 
        excelDoc.Append(" ") 
        excelDoc.Append("") 
        excelDoc.Append("") 
       End If 

       excelDoc.Append("") 

       For c As Int32 = 0 To gView.Rows(r).Cells.Count - 1 

        'Don't write out a column without a column header. 

        If Not gView.Columns(c).HeaderText = String.Empty Then 
         Dim XMLstring As String = gView.Rows(r).Cells(c).Text 

         XMLstring = XMLstring.Trim() 
         XMLstring = XMLstring.Replace("&", "&") 
         XMLstring = XMLstring.Replace(">", ">") 
         XMLstring = XMLstring.Replace("" + "") 
         excelDoc.Append(XMLstring) 
         excelDoc.Append("") 
        End If 

       Next 

       excelDoc.Append("") 
      Next 

      excelDoc.Append("") 
      excelDoc.Append(" ") 
      excelDoc.Append(endExcelXML) 



      Return excelDoc.ToString 


     End Function 

     Shared Sub PrepareGridViewForExport(ByVal gview As System.Web.UI.Control) 
      ' Cleans up grid for exporting. Takes links and visual elements and turns them into text. 
      Dim lb As New System.Web.UI.WebControls.LinkButton 
      Dim l As New System.Web.UI.WebControls.Literal 
      Dim name As String = String.Empty 


      For i As Int32 = 0 To gview.Controls.Count - 1 

       If TypeOf gview.Controls(i) Is System.Web.UI.WebControls.LinkButton Then 
        l.Text = CType(gview.Controls(i), System.Web.UI.WebControls.LinkButton).Text 
        gview.Controls.Remove(gview.Controls(i)) 
        gview.Controls.AddAt(i, l) 
       ElseIf TypeOf gview.Controls(i) Is System.Web.UI.WebControls.DropDownList Then 
        l.Text = CType(gview.Controls(i), System.Web.UI.WebControls.DropDownList).SelectedItem.Text 
        gview.Controls.Remove(gview.Controls(i)) 
        gview.Controls.AddAt(i, l) 
       ElseIf TypeOf gview.Controls(i) Is System.Web.UI.WebControls.CheckBox Then 
        l.Text = CType(gview.Controls(i), System.Web.UI.WebControls.CheckBox).Checked.ToString 
        gview.Controls.Remove(gview.Controls(i)) 
        gview.Controls.AddAt(i, l) 
       End If 


       If gview.Controls(i).HasControls() Then 
        PrepareGridViewForExport(gview.Controls(i)) 
       End If 

      Next 
     End Sub 

+0

코드가 손상된 것으로 생각합니다. 이해가되지 않기 때문입니다. –

0

'기능이 풍부한'는 터치 Brendans보다 간단하지만,하지의이 밖으로 시도 :

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    'Export to excel 
    Response.Clear() 
    Response.Buffer = True 
    Response.ContentType = "application/vnd.ms-excel" 
    Response.Charset = "" 
    Me.EnableViewState = False 
    Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter 
    Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter) 
    Me.ClearControls(gvSearchTerms) 
    gvSearchTerms.RenderControl(oHtmlTextWriter) 
    Response.Write(oStringWriter.ToString) 
    Response.End() 
End Sub 



Private Sub ClearControls(ByVal control As Control) 
    Dim i As Integer = (control.Controls.Count - 1) 
    Do While (i >= 0) 
     ClearControls(control.Controls(i)) 
     i = (i - 1) 
    Loop 
    If Not (TypeOf control Is TableCell) Then 
     If (Not (control.GetType.GetProperty("SelectedItem")) Is Nothing) Then 
      Dim literal As LiteralControl = New LiteralControl 
      control.Parent.Controls.Add(literal) 
      Try 
       literal.Text = CType(control.GetType.GetProperty("SelectedItem").GetValue(control, Nothing), String) 
      Catch ex As System.Exception 

      End Try 
      control.Parent.Controls.Remove(control) 
     ElseIf (Not (control.GetType.GetProperty("Text")) Is Nothing) Then 
      Dim literal As LiteralControl = New LiteralControl 
      control.Parent.Controls.Add(literal) 
      literal.Text = CType(control.GetType.GetProperty("Text").GetValue(control, Nothing), String) 
      control.Parent.Controls.Remove(control) 
     End If 
    End If 
    Return 
End Sub 



Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) 
    Return 
End Sub 
0

당신은 크리스탈을 사용할 수 있습니다 그것은 VS.에 내장되어 있기 때문에 적절한 열이있는 크리스탈 보고서를 미리 정의한 다음 DataGrid 또는 Gridview에 사용할 데이터 소스를 사용할 수 있습니다.

Dim report_source As CrystalDecisions.Web.CrystalReportSource 
report_source.ReportDocument.SetDataSource(dt) 'DT IS A DATATABLE 
report_source.Report.FileName = "test.rpt" 
report_source.ReportDocument.Refresh() 
report_source.ReportDocument.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, "c:\test.xls") 
1

각 데이터 셀의 데이터를 추출하고 Excel 셀에 개별적으로 쓰기 위해 double for 루프를 다시 경고합니다. 대신 2D 객체 배열을 사용하고 데이터 격자를 반복하면 모든 데이터가 저장됩니다. 그런 다음 2D 객체 배열과 동일한 엑셀 범위를 설정할 수 있습니다.

이것은 셀 단위로 Excel을 쓰는 것보다 몇 배 빠른 속도입니다. 나가 수출하기 위하여 단순히 2 시간을 걸리기 위하여 이용 된 저에 종사하고있는 몇몇보고는 분 이하로 삭감되었다.

1

I 설정은 다음의 gridview와 그렇게처럼 .xls 파일로 뱉어하기 위해 HTML 텍스트 작가 객체를 사용 :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    'get the select command of the gridview 
    sqlGridview.SelectCommand = Session("strSql") 
    gvCompaniesExport.DataBind() 
    lblTemp.Text = Session("strSql") 

    'do the export 
    doExport() 

    'close the window 
    Dim closeScript As String = "<script language='javascript'> window.close() </scri" 
    closeScript = closeScript & "pt>" 
    'split the ending script tag across a concatenate to keep it from causing problems 
    'this will write it to the asp.net page and fire it off, closing the window 
    Page.RegisterStartupScript("closeScript", closeScript) 
End Sub 
Public Sub doExport() 
    Response.AddHeader("content-disposition", "attachment;filename=IndianaCompanies.xls") 
    Response.ContentType = "application/vnd.ms-excel" 
    Response.Charset = "" 
    Me.EnableViewState = False 
    Dim objStrWriter As New System.IO.StringWriter 
    Dim objHtmlTextWriter As New System.Web.UI.HtmlTextWriter(objStrWriter) 
    'Get the gridview HTML from the control 
    gvCompaniesExport.RenderControl(objHtmlTextWriter) 
    'writes the dg info 
    Response.Write(objStrWriter.ToString()) 
    Response.End() 
End Sub 
1

나는이 모든 시간을 사용

public static class GridViewExtensions 
    { 
     public static void ExportToExcel(this GridView gridView, string fileName, IEnumerable<string> excludeColumnNames) 
     { 
      //Prepare Response 
      HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.AddHeader("content-disposition", 
       string.Format("attachment; filename={0}", fileName)); 
      HttpContext.Current.Response.ContentType = "application/ms-excel"; 



      using (StringWriter sw = new StringWriter()) 
      { 
       using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
       { 
        // Create a table to contain the grid 
        Table table = new Table(); 

        // include the gridline settings 
        table.GridLines = gridView.GridLines; 

        // add the header row to the table 
        if (gridView.HeaderRow != null) 
        { 
         PrepareControlForExport(gridView.HeaderRow); 
         table.Rows.Add(gridView.HeaderRow); 
        } 

        // add each of the data rows to the table 
        foreach (GridViewRow row in gridView.Rows) 
        { 
         PrepareControlForExport(row); 
         table.Rows.Add(row); 
        } 

        // add the footer row to the table 
        if (gridView.FooterRow != null) 
        { 
         PrepareControlForExport(gridView.FooterRow); 
         table.Rows.Add(gridView.FooterRow); 
        } 

        // Remove unwanted columns (header text listed in removeColumnList arraylist) 
        foreach (DataControlField column in gridView.Columns) 
        { 
         if (excludeColumnNames != null && excludeColumnNames.Contains(column.HeaderText)) 
         { 
          column.Visible = false; 
         } 
        } 

        // render the table into the htmlwriter 
        table.RenderControl(htw); 

        // render the htmlwriter into the response 
        HttpContext.Current.Response.Write(sw.ToString()); 
        HttpContext.Current.Response.End(); 
       } 
      } 
     } 

     /// <summary> 
     /// Replace any of the contained controls with literals 
     /// </summary> 
     /// <param name="control"></param> 
     private static void PrepareControlForExport(Control control) 
     { 
      for (int i = 0; i < control.Controls.Count; i++) 
      { 
       Control current = control.Controls[i]; 

       if (current is LinkButton) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 
       } 
       else if (current is ImageButton) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 
       } 
       else if (current is HyperLink) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 
       } 
       else if (current is DropDownList) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 
       } 
       else if (current is CheckBox) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 
       } 

       if (current.HasControls()) 
       { 
        PrepareControlForExport(current); 
       } 
      } 
     } 
    } 
0

먼저 가져 오기 COM 라이브러리의 Microsoft Excel 개체를

샘플 코드 : 나는 특정 위치에 엑셀 만들기의 SaveFileDialog를 사용

Public Sub exportOfficePCandWorkstation(ByRef mainForm As Form1, ByVal Location As String, ByVal WorksheetName As String) 
     Dim xlApp As New Excel.Application 
     Dim xlWorkBook As Excel.Workbook 
     Dim xlWorkSheet As Excel.Worksheet 
     Dim misValue As Object = System.Reflection.Missing.Value 
     Dim Header(23) As String 
     Dim HeaderCell(23) As String 
     Header = {"No.", "PC Name", "User", "E-mail", "Department/Location", "CPU Model", "CPU Processor", "CPU Speed", "CPU HDD#1", "CPU HDD#2", "CPU Memory", "CPU OS", "CPU Asset Tag", "CPU MAC Address", "Monitor 1 Model", "Monitor Serial Number", "Monitor2 Model", "Monitor2 Serial Number", "Office", "Wi-LAN", "KVM Switch", "Attachment", "Remarks", "Date and Time"} 
     HeaderCell = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X"} 
     xlWorkBook = xlApp.Workbooks.Add 
     xlWorkSheet = xlWorkBook.Sheets("Sheet1") 
     xlWorkSheet.Name = WorksheetName 
     xlApp.Visible = True 
     xlWorkSheet.Application.ActiveWindow.SplitRow = 1 
     xlWorkSheet.Application.ActiveWindow.SplitColumn = 3 
     xlWorkSheet.Application.ActiveWindow.FreezePanes = True 
     With xlWorkSheet 
      For count As Integer = 0 To 23 
       .Range(HeaderCell(count) & 1).Value = Header(count) 
      Next 
      With .Range("A1:X1") 
       .Interior.Color = 1 
       With .Font 
        .Size = 16 
        .ColorIndex = 2 
        .Name = "Times New Roman" 
       End With 
      End With 
      For i = 0 To mainForm.DataGridView1.RowCount - 1 
       For j = 0 To mainForm.DataGridView1.ColumnCount - 1 
        If mainForm.DataGridView1(j, i).Value.ToString = "System.Byte[]" Then 
         xlWorkSheet.Cells(i + 2, j + 2) = "Attached" 
        Else 
         xlWorkSheet.Cells(i + 2, j + 2) = mainForm.DataGridView1(j, i).Value.ToString() 
        End If 
       Next 
       .Range("A" & i + 2).Value = (i + 1).ToString 
      Next 
      With .Range("A:Z") 
       .EntireColumn.AutoFit() 
      End With 
      With .Range("B2:X" & mainForm.DataGridView1.RowCount + 1) 
       .HorizontalAlignment = Excel.XlVAlign.xlVAlignJustify 
      End With 
      With .Range("A1:A" & mainForm.DataGridView1.RowCount + 1) 
       .HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter 
      End With 
      '-----------------------------------Insert Border Lines-------------------------------------- 
      With .Range("A1:X" & mainForm.DataGridView1.RowCount + 1) 
       With .Borders(Excel.XlBordersIndex.xlEdgeLeft) 
        .LineStyle = Excel.XlLineStyle.xlDouble 
        .ColorIndex = 0 
        .TintAndShade = 0 
        .Weight = Excel.XlBorderWeight.xlThin 
       End With 
       With .Borders(Excel.XlBordersIndex.xlEdgeTop) 
        .LineStyle = Excel.XlLineStyle.xlContinuous 
        .ColorIndex = 0 
        .TintAndShade = 0 
        .Weight = Excel.XlBorderWeight.xlThin 
       End With 
       With .Borders(Excel.XlBordersIndex.xlEdgeBottom) 
        .LineStyle = Excel.XlLineStyle.xlContinuous 
        .ColorIndex = 0 
        .TintAndShade = 0 
        .Weight = Excel.XlBorderWeight.xlThin 
       End With 
       With .Borders(Excel.XlBordersIndex.xlEdgeRight) 
        .LineStyle = Excel.XlLineStyle.xlContinuous 
        .ColorIndex = 0 
        .TintAndShade = 0 
        .Weight = Excel.XlBorderWeight.xlThin 
       End With 
       With .Borders(Excel.XlBordersIndex.xlInsideVertical) 
        .LineStyle = Excel.XlLineStyle.xlContinuous 
        .ColorIndex = 0 
        .TintAndShade = 0 
        .Weight = Excel.XlBorderWeight.xlThin 
       End With 
       With .Borders(Excel.XlBordersIndex.xlInsideHorizontal) 
        .LineStyle = Excel.XlLineStyle.xlContinuous 
        .ColorIndex = 0 
        .TintAndShade = 0 
        .Weight = Excel.XlBorderWeight.xlThin 
       End With 
      End With 
     End With 
      xlWorkSheet.SaveAs(Location) 
      xlWorkBook.Close() 
     xlApp.Quit() 
     MsgBox("Export Record successful", MsgBoxStyle.Information, "Export to Excel") 
End Sub 

관련 문제