I am generating one Excel report using XML Excel passing via dataset.
Private Function getWorksheets(source As DataSet) As String
Dim dc As DataColumn
Dim sw As StringWriter = New StringWriter()
Dim sww As StringWriter = New StringWriter()
Dim sheetCount As Integer = 0
If (source Is Nothing Or source.Tables.Count = 0) Then
sw.Write("<Worksheet ss:Name=""Sheet1"">" + "<Table>" + System.Environment.NewLine + System.Environment.NewLine + "<Row><Cell><Data ss:Type=""String""></Data></Cell></Row>" + System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
Return sw.ToString()
End If
For Each dt As DataTable In source.Tables
If (dt.Rows.Count = 0) Then
sw.Write("<Worksheet ss:Name=""" + replaceXmlChar(dt.TableName) + """>" + System.Environment.NewLine + System.Environment.NewLine + "<Table>" + System.Environment.NewLine + System.Environment.NewLine + "<Row><Cell ss:StyleID=""s62""><Data ss:Type=""String""></Data></Cell></Row>" + System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
Else
'write each row data
For i As Integer = 0 To dt.Rows.Count - 1
If ((i Mod rowLimit) = 0) Then
'add close tags for previous sheet of the same data table
If ((i / rowLimit) > sheetCount) Then
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
sheetCount = (i / rowLimit)
End If
'sw.Write((System.Environment.NewLine + System.Environment.NewLine + "<Worksheet ss:Name=" + replaceXmlChar(dt.TableName)) + ((((i / rowLimit) = 0) ? """ : "_" + Convert.ToString(i / rowLimit))) + "\">" + System.Environment.NewLine + System.Environment.NewLine + "<Table>");
sw.Write((System.Environment.NewLine & System.Environment.NewLine & "<Worksheet ss:Name=""" & replaceXmlChar(dt.TableName)) & ((If(((i / rowLimit) = 0), "", "_" & Convert.ToString(i / rowLimit)))) & """>" & System.Environment.NewLine & System.Environment.NewLine & "<Table>")
'write column name row
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "<Row>")
For Each dc In dt.Columns
sw.Write(String.Format("<Cell ss:StyleID=""s62""><Data ss:Type=""String"">{0}</Data></Cell>", replaceXmlChar(dc.ColumnName)))
Next
sw.Write("</Row>")
End If
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "<Row>")
For Each dc In dt.Columns
sw.Write(getCell(dc.DataType, dt.Rows(i)(dc.ColumnName)))
Next
sw.Write("</Row>")
Next
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
End If
Next
Return String.Format(sw.ToString())
End Function
it working properly when dataset count 20000 or less.
when dataset contain more than 23000 records.
it debugging properly and finally this function return string statement, thrown an error as
System.OutofMemory Exception. same set of coding concepts also used in c#. but it does not return an error and working properly. can any one. please find the mistake or tell me the correct way.
Finally i resolved my problem in Simple way.
I hope this answer will you.
My scenario i am writing data from dataset using Stringwriter. When my dataset contain more than 20000 records, it thrown an error as "System.outofmeory exception". Because Stringwriter keep your data in virtual memory(RAM).
so, i used Streamwriter instead of Stringwriter.
1st step:
I created XML excel with header initially.
Dim excelTemp As String = getWorkbookTemplate()
Dim fs As IO.FileStream
Dim sw As IO.StreamWriter
fs = New IO.FileStream(filename, IO.FileMode.Create, IO.FileAccess.Write)
sw = New IO.StreamWriter(fs)
sw.Write(excelTemp) -- Here some header for excel files.
sw.Flush()
sw.Close()
fs.Close() -- file closed
getWorksheets(dsInput,filename) -- calling getworksheet method with dataset and filename
fs.Close()
2nd step:
Private Sub getWorksheets(source As DataSet,fname As String)
Dim dc As DataColumn
Dim dr As DataRow
Dim sw As StreamWriter = File.AppendText(fname) -- Here my closed file again re-open and write to file directly using streamwriter Appendtext
Dim sheetCount As Integer = 0
If (source Is Nothing Or source.Tables.Count = 0) Then
sw.Write("<Worksheet ss:Name=""Sheet1"">" + "<Table>" + System.Environment.NewLine + System.Environment.NewLine + "<Row><Cell><Data ss:Type=""String""></Data></Cell></Row>" + System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
End If
For Each dt As DataTable In source.Tables
If (dt.Rows.Count = 0) Then
sw.Write("<Worksheet ss:Name=""" + replaceXmlChar(dt.TableName) + """>" + System.Environment.NewLine + System.Environment.NewLine + "<Table>" + System.Environment.NewLine + System.Environment.NewLine + "<Row><Cell ss:StyleID=""s62""><Data ss:Type=""String""></Data></Cell></Row>" + System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
Else
'write each row data
For i As Integer = 0 To dt.Rows.Count - 1
If ((i Mod rowLimit) = 0) Then
'add close tags for previous sheet of the same data table
If ((i / rowLimit) > sheetCount) Then
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
sheetCount = (i / rowLimit)
End If
sw.Write((System.Environment.NewLine + System.Environment.NewLine + "<Worksheet ss:Name=""" + replaceXmlChar(dt.TableName)) + ((If(((i / rowLimit) = 0), "", "_" + Convert.ToString(i / rowLimit)))) + """>" + System.Environment.NewLine + System.Environment.NewLine + "<Table>")
'write column name row
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "<Row>")
For Each dc In dt.Columns
sw.Write(String.Format("<Cell ss:StyleID=""s62""><Data ss:Type=""String"">{0}</Data></Cell>", replaceXmlChar(dc.ColumnName)))
Next
sw.Write("</Row>")
End If
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "<Row>")
For Each dc In dt.Columns
sw.Write(getCell(dc.DataType, dt.Rows(i)(dc.ColumnName)))
Next
sw.Write("</Row>")
Next
sw.Write(System.Environment.NewLine + System.Environment.NewLine + "</Table>" + System.Environment.NewLine + System.Environment.NewLine + "</Worksheet>")
End If
Next
sw.Write("</Workbook>")
sw.Flush()
sw.Close()
End Sub
Finally get the file from location where you have saved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.