Click here to Skip to main content
15,889,736 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: How does IDisposable Interface Generate Dispose sub? Pin
Polymorpher10-Dec-09 14:41
Polymorpher10-Dec-09 14:41 
GeneralRe: How does IDisposable Interface Generate Dispose sub? Pin
The Man from U.N.C.L.E.14-Dec-09 0:12
The Man from U.N.C.L.E.14-Dec-09 0:12 
GeneralRe: How does IDisposable Interface Generate Dispose sub? Pin
Polymorpher14-Dec-09 2:06
Polymorpher14-Dec-09 2:06 
QuestionNeed help with the event based async pattern Pin
Jeroen De Dauw10-Dec-09 4:55
Jeroen De Dauw10-Dec-09 4:55 
AnswerRe: Need help with the event based async pattern Pin
Ashfield11-Dec-09 1:21
Ashfield11-Dec-09 1:21 
GeneralRe: Need help with the event based async pattern Pin
Jeroen De Dauw11-Dec-09 3:25
Jeroen De Dauw11-Dec-09 3:25 
GeneralRe: Need help with the event based async pattern Pin
Ashfield11-Dec-09 9:29
Ashfield11-Dec-09 9:29 
QuestionExport to excel [modified] Pin
Uma J10-Dec-09 0:16
Uma J10-Dec-09 0:16 
hi all, i need to export lakhs of records to excel, if i use the following code means, it doesn't throws any error,
select top 50000 * from dbo.CustomerDetail
but instead if i use
select * from dbo.CustomerDetail (it contains more than 10lakhs of records), it throws me an error.... Significant loss of functionality - the workbook contains data in cells outside of the row and column limit. data beyond 256(IV) columns by 65,536 rows will not be saved.....

my need is, if i have a table, it contains 70,000 records, i need to export it to excel, at that time it should automatically split the datas to two worksheets (which means 65000 in sheet1 and remaining 5000 in sheet2)...

code snippet in vb.net:

Imports Microsoft.Office.Interop.Excel
Imports System.Data.SqlClient
Imports System.Threading
Public Class Form1
Dim dtstart As DateTime = DateTime.Now
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strUniqueParamId As String
strUniqueParamId = System.Guid.NewGuid().ToString

Dim xThread As New Thread(AddressOf excelcheck)
xThread.Name = strUniqueParamId
xThread.Start()
MessageBox.Show("Generating")
Timer1.Start()
Thread.Sleep(1700)
'MessageBox.Show(Environment.TickCount.ToString());
End Sub
Sub excelcheck()
' Create the Excel Application object
Dim excelApp As New ApplicationClass()

' Create a new Excel Workbook
Dim excelWorkbook As Workbook = excelApp.Workbooks.Add(Type.Missing)

Dim sheetIndex As Integer = 0
Dim col, row As Integer
Dim excelSheet As Worksheet

Dim sqlcon As New SqlConnection("server=SHRIGI104\SHRIUNO0301;database=Test;uid=sa;pwd=welcome3#;")
sqlcon.Open()
Dim ds As New DataSet
Dim Da As New SqlDataAdapter()
Dim sqlcmd As New SqlCommand
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "SampleExcel"
sqlcmd.Connection = sqlcon
Da.SelectCommand = sqlcmd
Da.Fill(ds)
sqlcon.Close()

' Copy each DataTable as a new Sheet
For Each dt As System.Data.DataTable In ds.Tables
sheetIndex += 1

' Copy the DataTable to an object array
Dim rawData(dt.Rows.Count, dt.Columns.Count - 1) As Object

' Copy the column names to the first row of the object array
For col = 0 To dt.Columns.Count - 1
rawData(0, col) = dt.Columns(col).ColumnName
Next

' Copy the values to the object array
For col = 0 To dt.Columns.Count - 1
For row = 0 To dt.Rows.Count - 1
rawData(row + 1, col) = dt.Rows(row).ItemArray(col)
Next
Next

' Calculate the final column letter
Dim finalColLetter As String = String.Empty
Dim colCharset As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim colCharsetLen As Integer = colCharset.Length

If dt.Columns.Count > colCharsetLen Then
finalColLetter = colCharset.Substring( _
(dt.Columns.Count - 1) \ colCharsetLen - 1, 1)
End If

finalColLetter += colCharset.Substring( _
(dt.Columns.Count - 1) Mod colCharsetLen, 1)

' Create a new Sheet
excelSheet = CType( _
excelWorkbook.Sheets.Add(excelWorkbook.Sheets(sheetIndex), _
Type.Missing, 1, XlSheetType.xlWorksheet), Worksheet)

excelSheet.Name = dt.TableName

' Fast data export to Excel
Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1)
excelSheet.Range(excelRange, Type.Missing).Value2 = rawData

' Mark the first row as BOLD
CType(excelSheet.Rows(1, Type.Missing), Range).Font.Bold = True
excelSheet = Nothing
Next

' Save and Close the Workbook
excelWorkbook.SaveAs("C:\BCP\sss.xls", XlFileFormat.xlWorkbookNormal, Type.Missing, _
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, _
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

excelWorkbook.Close(True, Type.Missing, Type.Missing)

excelWorkbook = Nothing

' Release the Application object
excelApp.Quit()
excelApp = Nothing

' Collect the unreferenced objects
GC.Collect()
GC.WaitForPendingFinalizers()
MessageBox.Show("Genearted Sucessfully")
Timer1.[Stop]()
Dim dtEnd As DateTime = DateTime.Now
Dim ts As TimeSpan = dtEnd - dtstart
MessageBox.Show("Mins:" + ts.Minutes.ToString() + "" + "Secs:" + ts.Seconds.ToString())
End Sub
End Class

Sample Stored Procedure:

USE [Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[SampleExcel]
AS
BEGIN
select top 50000 * from dbo.CustomerDetail
select top 50000 * from dbo.CustomerDetail
select top 50000 * from dbo.CustomerDetail
select top 50000 * from dbo.CustomerDetail
select top 50000 * from dbo.CustomerDetail
select top 50000 * from dbo.CustomerDetail
END
GO

the above sp should be re-written as in single select statement
like "select * from dbo.CustomerDetail"....

pls need the help very soon...

modified on Thursday, December 10, 2009 6:42 AM

AnswerRe: Export to excel Pin
dan!sh 10-Dec-09 1:18
professional dan!sh 10-Dec-09 1:18 
AnswerRe: Export to excel Pin
Ashfield10-Dec-09 1:24
Ashfield10-Dec-09 1:24 
AnswerRe: Export to excel Pin
Dave Kreskowiak10-Dec-09 4:26
mveDave Kreskowiak10-Dec-09 4:26 
AnswerRe: Export to excel Pin
The Man from U.N.C.L.E.11-Dec-09 22:57
The Man from U.N.C.L.E.11-Dec-09 22:57 
QuestionPOP3 Class Pin
joelle@scope9-Dec-09 2:17
joelle@scope9-Dec-09 2:17 
AnswerRe: POP3 Class Pin
Richard MacCutchan9-Dec-09 3:26
mveRichard MacCutchan9-Dec-09 3:26 
QuestionType inference problem [modified/solved] Pin
Gideon Engelberth8-Dec-09 13:50
Gideon Engelberth8-Dec-09 13:50 
Questionhow to use .net framework with c++; Pin
geniuspc8-Dec-09 12:04
geniuspc8-Dec-09 12:04 
AnswerRe: how to use .net framework with c++; Pin
DaveyM698-Dec-09 12:34
professionalDaveyM698-Dec-09 12:34 
AnswerRe: how to use .net framework with c++; Pin
Paul Conrad8-Dec-09 13:28
professionalPaul Conrad8-Dec-09 13:28 
AnswerRe: how to use .net framework with c++; Pin
The Man from U.N.C.L.E.9-Dec-09 7:10
The Man from U.N.C.L.E.9-Dec-09 7:10 
Questionhow to insert image Pin
darkyro8-Dec-09 4:03
darkyro8-Dec-09 4:03 
AnswerRe: how to insert image Pin
Luc Pattyn8-Dec-09 5:10
sitebuilderLuc Pattyn8-Dec-09 5:10 
QuestionDifference between events in c# and vb.net Pin
ddecoy8-Dec-09 1:39
ddecoy8-Dec-09 1:39 
AnswerRe: Difference between events in c# and vb.net Pin
Dave Kreskowiak8-Dec-09 5:22
mveDave Kreskowiak8-Dec-09 5:22 
AnswerRe: Difference between events in c# and vb.net Pin
The Man from U.N.C.L.E.8-Dec-09 8:17
The Man from U.N.C.L.E.8-Dec-09 8:17 
GeneralRe: Difference between events in c# and vb.net Pin
ddecoy9-Dec-09 2:42
ddecoy9-Dec-09 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.