multiple sheets in one excel file + vb.net
note: import excel dll
Public Sub ExcelReport(ByVal ds As DataSet) Dim k, i, j, l As Integer Me.Cursor = Cursors.WaitCursor Dim Xlapp As Excel.Application Xlapp = Nothing Xlapp = New Excel.Application Dim Xlwb As Excel.Workbook Xlwb = Nothing Dim xlshOne As Excel.Worksheet Dim theExcelDataMinus As System.Text.StringBuilder Xlwb = Xlapp.Workbooks.Add Dim dt As DataTable Dim dtr As DataRow Dim ColCnt, cntCols As Integer If Xlwb.Worksheets.Count >= 1 Then For l = 1 To Xlwb.Sheets.Count - 1 xlshOne = Xlwb.Sheets(l) xlshOne.Delete() Next End If For i = 0 To ds.Tables.Count - 1 Step 1 cntCols = ds.Tables(i).Columns.Count dt = ds.Tables(i) k = 0 If ds.Tables(i).Rows.Count <> 0 Then If ds.Tables(i).Columns.Count = 0 Then xlshOne = Xlwb.Worksheets.Add xlshOne.Name = ds.Tables(i).TableName.Trim() Else xlshOne = Xlwb.Worksheets.Add If (i = 0) Then xlshOne.Name = "GRPS" ElseIf (i = 1) Then xlshOne.Name = "AGINGREPORT" ElseIf (i = 2) Then xlshOne.Name = "SCHEDULE5" ElseIf (i = 3) Then xlshOne.Name = "DIRECT SCHEDULES" ElseIf (i = 4) Then xlshOne.Name = "SCHEDULES" ElseIf (i = 5) Then xlshOne.Name = "PL" ElseIf (i = 6) Then xlshOne.Name = "BS" End If theExcelDataMinus = New System.Text.StringBuilder With xlshOne 'Adding heading to Excel Sheet 'For j = 0 To cntCols - 1 ' theExcelDataMinus.Append(dt.Columns(j).Caption) ' theExcelDataMinus.Append(vbTab) 'Next 'theExcelDataMinus.Append(vbCrLf) For Each dtr In dt.Rows If ds.Tables(i).Rows.Count > 0 Then For ColCnt = 0 To dt.Columns.Count - 1 If dtr(ColCnt) Is DBNull.Value Then theExcelDataMinus.Append("") Else theExcelDataMinus.Append(dtr(ColCnt).ToString) End If theExcelDataMinus.Append(vbTab) Next theExcelDataMinus.Append(vbCrLf) End If k = k + 1 Next Clipboard.SetDataObject(theExcelDataMinus.ToString, False) .Cells.Font.Size = 10 .Cells.Font.Name = "Times New Roman" .Range("A1:N1").Font.Bold = True .Range("A1:AZ1").Font.Bold = True .Range("A1:AZ1").Select() .Cells(1, 1).select() .Paste() .Range("A1:U1").ShrinkToFit = True .Range("T:T").NumberFormat = "@" .Range("U:U").NumberFormat = "@" .Range("v:v").NumberFormat = "@" .Range("AD:AD").NumberFormat = "@" .Columns.AutoFit() theExcelDataMinus = Nothing Application.DoEvents() .Cells(1, 1).select() .Paste() .Columns.AutoFit() Xlapp.Visible = True End With End If End If Next Me.Cursor = Cursors.Default ds = Nothing End Sub
|
No responses found. Be the first to respond and make money from revenue sharing program.
|