且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的表中有10万条记录,如何通过将1万条记录分成一个excel文件将数据导出到excel。

更新时间:2023-01-28 19:48:13

 cmd =  SqlCommand(  SELECT TOP&行和  * FROM& txtTable.Text&   WHERE& txtfltclmn.Text&   =&  '& txtfltclmnval.Text&  '&  



不是解决方案你的问题,但你有另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - *** [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


您正在寻找的技术是使用SQL寻呼。



CodeProject上有几篇帖子将会用示例解释它 - 这是一个入门搜索...只需过滤它,直到找到一个与之相关的内容

CodeProject Search [ ^ ]

I have 10 Lakh records in my table, How to export the data to excel by splitting into 1 lakh records into one Excel file.

If I am having 10 lakh records if i export the data using ASP.NET it should generate 10 Excel files containing 1 lakh records.

I tried the following code, It is able to download the first 100,000 records after that it is not going back to the loop again, is there any way to download excel file in loop?

What I have tried:

I tried the following code it works for me to export the data into Excel.

<pre>Protected Sub ExportExcel(ByVal sender As Object, ByVal e As EventArgs)
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim cmd As New SqlCommand

        If txtRows.Text <> String.Empty Then
            rows = txtRows.Text
        End If

        If txtTable.Text <> String.Empty Then
            tbl = txtTable.Text
        End If

        'Dim excelfiles As Integer = dt.Rows.Count / 100000

        If txtfltclmn.Text <> String.Empty AndAlso txtfltclmnval.Text <> String.Empty AndAlso txtTable.Text <> String.Empty Then
            cmd = New SqlCommand("SELECT TOP " & rows & " * FROM " & txtTable.Text & " WHERE " & txtfltclmn.Text & "=" & "'" & txtfltclmnval.Text & "'" & " ")
        ElseIf txtfltclmn.Text = String.Empty AndAlso txtfltclmnval.Text = String.Empty AndAlso txtTable.Text <> String.Empty Then
            cmd = New SqlCommand("SELECT TOP " & rows & " * FROM " & txtTable.Text & " ")
        ElseIf txtfltclmn.Text = String.Empty AndAlso txtfltclmnval.Text = String.Empty AndAlso txtTable.Text = String.Empty Then
            cmd = New SqlCommand("SELECT TOP " & rows & " * FROM " & tbl & " ")
        End If
        Dim con As New SqlConnection(constr)
        con.Open()
        'Using cmd As New SqlCommand("SELECT * FROM " & txtTable.Text & " WHERE " & txtfltclmn.Text & "=" & "'" & txtfltclmnval.Text & "'" & " ")
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dt As New DataTable()
                sda.Fill(dt)

                Dim i As Integer = 0
                i = dt.Rows.Count / 100000
                For j = 0 To i
                    Dim dtfind As DataTable = dt.AsEnumerable().Skip(j * 100000).Take(100000).CopyToDataTable()
                    If dtfind.Rows.Count > 0 Then
                        Using wb As New XLWorkbook()
                            wb.Worksheets.Add(dtfind, "TSTRAN")
                            Response.Clear()
                            Response.Buffer = True
                            Response.Charset = ""
                            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                            Response.AddHeader("content-disposition", "attachment;filename=" & tbl & "_" & j & ".xlsx")
                            Using MyMemoryStream As New MemoryStream()
                                wb.SaveAs(MyMemoryStream)
                                MyMemoryStream.WriteTo(Response.OutputStream)
                                Response.Flush()
                                Response.End()
                            End Using
                        End Using
                    End If
                    j = j + 1
                Next
            End Using
        End Using
        
        con.Close()
        'End Using
        'End Using
    End Sub

cmd = New SqlCommand("SELECT TOP " & rows & " * FROM " & txtTable.Text & " WHERE " & txtfltclmn.Text & "=" & "'" & txtfltclmnval.Text & "'" & " ")


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


The technique you are looking for is "Paging" using SQL.

There are several posts here on CodeProject that will explain it with examples - here is a starter search ... just filter it until you find one that you can relate to
CodeProject Search[^]