V
Vaibhav
Hello
I am trying to export data from asp.net to excel spreadsheet, using office
web components. I am able to export the data to a single excel sheet.
PROBLEM:
I am unable to create mutiple sheets in the created excel file(it always
creates one sheet), also i want to export different data to differnt sheets
in the exported spreadsheet.
TIA
Mentioned below is my code VB.net:
Dim xlSheet As New OWC.SpreadsheetClass
Dim oWBs As OWC.Workbook
Dim oSheet As OWC.Worksheet
Dim oSheet1 As OWC.Worksheet
cnSql = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString1"))
cmdSql = New SqlCommand("Select * From myTable", cnSql)
adSql = New SqlDataAdapter(cmdSql)
adSql.Fill(dsSql)
numCols = dsSql.Tables(0).Columns.Count
oWBs = xlSheet.Workbooks.Item(1)
oWBs.Sheets.Add()
oSheet1 = oWBs.Sheets.Item(1)
oWBs.Sheets.Add()
oSheet = oWBs.Sheets.Item(2)
oSheet.Name = "TestSheet1"
oSheet1.Name = "TestSheet2"
For i = 0 To numCols - 1
oSheet.Cells(iRow, i + 1) = dsSql.Tables(0).Columns(i).ToString
oSheet1.Cells(iRow, i + 1) = dsSql.Tables(0).Columns(i).ToString
Next
If dsSql.Tables(0).Rows.Count > 0 Then
For j = 0 To dsSql.Tables(0).Rows.Count - 1
For i = 0 To numCols - 1
oSheet.Cells(row, i + 1) = dsSql.Tables(0).Rows(j)(i)
oSheet1.Cells(row, i + 1) = dsSql.Tables(0).Rows(j)(i)
Next
row = row + 1
Next
End If
oSheet.Activate()
oSheet1.Activate()
Dim xlFileName As String = DateTime.Now.Ticks.ToString + ".xls"
xlSheet.Export(Server.MapPath(".") + "\\" + xlFileName,
OWC.SheetExportActionEnum.ssExportActionNone,
OWC.SheetExportFormat.ssExportHTML)
Response.ContentType = "application/x-msexcel"
Response.WriteFile(Server.MapPath(".") + "\\" + xlFileName)
RemoveFiles(Server.MapPath("."))
I am trying to export data from asp.net to excel spreadsheet, using office
web components. I am able to export the data to a single excel sheet.
PROBLEM:
I am unable to create mutiple sheets in the created excel file(it always
creates one sheet), also i want to export different data to differnt sheets
in the exported spreadsheet.
TIA
Mentioned below is my code VB.net:
Dim xlSheet As New OWC.SpreadsheetClass
Dim oWBs As OWC.Workbook
Dim oSheet As OWC.Worksheet
Dim oSheet1 As OWC.Worksheet
cnSql = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString1"))
cmdSql = New SqlCommand("Select * From myTable", cnSql)
adSql = New SqlDataAdapter(cmdSql)
adSql.Fill(dsSql)
numCols = dsSql.Tables(0).Columns.Count
oWBs = xlSheet.Workbooks.Item(1)
oWBs.Sheets.Add()
oSheet1 = oWBs.Sheets.Item(1)
oWBs.Sheets.Add()
oSheet = oWBs.Sheets.Item(2)
oSheet.Name = "TestSheet1"
oSheet1.Name = "TestSheet2"
For i = 0 To numCols - 1
oSheet.Cells(iRow, i + 1) = dsSql.Tables(0).Columns(i).ToString
oSheet1.Cells(iRow, i + 1) = dsSql.Tables(0).Columns(i).ToString
Next
If dsSql.Tables(0).Rows.Count > 0 Then
For j = 0 To dsSql.Tables(0).Rows.Count - 1
For i = 0 To numCols - 1
oSheet.Cells(row, i + 1) = dsSql.Tables(0).Rows(j)(i)
oSheet1.Cells(row, i + 1) = dsSql.Tables(0).Rows(j)(i)
Next
row = row + 1
Next
End If
oSheet.Activate()
oSheet1.Activate()
Dim xlFileName As String = DateTime.Now.Ticks.ToString + ".xls"
xlSheet.Export(Server.MapPath(".") + "\\" + xlFileName,
OWC.SheetExportActionEnum.ssExportActionNone,
OWC.SheetExportFormat.ssExportHTML)
Response.ContentType = "application/x-msexcel"
Response.WriteFile(Server.MapPath(".") + "\\" + xlFileName)
RemoveFiles(Server.MapPath("."))