Hi,
got another issue. i am exporting the contents of the datagrid to an excel sheet. THe code works fine . but it does not work if i have not already created an empty Excel file with the same name. only if there is an existing Excel File it works.
the code is shown below.
I need the code to work even if i dont have any excel file of that name in that folder.
got another issue. i am exporting the contents of the datagrid to an excel sheet. THe code works fine . but it does not work if i have not already created an empty Excel file with the same name. only if there is an existing Excel File it works.
the code is shown below.
VB.NET:
Private Sub ExportToExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToExcel.Click
'Waiting Cursor
Cursor = Cursors.WaitCursor
Dim DataTables As DataTable
Dim Rowcount As Integer
Dim iCount As Integer
'Setting up a new Excel application
Dim oExcel As Microsoft.Office.Interop.Excel.Application
'setting up a new Excel workbook
Dim oExcelWorkBook As Microsoft.Office.Interop.Excel.Workbook
'Setting up a new Worksheet
Dim oWorksheetSheet As Microsoft.Office.Interop.Excel.Worksheet
'Create a datatable before you export to excel
DataTables = InfyRetailDataSet.Tables(3)
Try
'set the object to nothing so that is not open
oExcel = Nothing
oExcelWorkBook = Nothing
oWorksheetSheet = Nothing
'Create a new instance of Excel
oExcel = New Microsoft.Office.Interop.Excel.Application
'Add a new workbook
oExcelWorkBook = oExcel.Workbooks.Add
'Work with the first worksheet
oWorksheetSheet = oExcelWorkBook.Worksheets(1)
With oWorksheetSheet
For iCount = 1 To 6
.Cells(1, iCount).font.bold = True
.Cells(iCount, 1).Font.Name = "Arial"
.Cells(1, iCount).interior.colorindex = 24
Next
.Cells(1, 1) = "Item Code"
.Cells(1, 2) = "Item Description"
.Cells(1, 3) = "Item Category"
.Cells(1, 4) = "Item Price"
.Cells(1, 5) = "Discount"
.Cells(1, 6) = "Units In Stock"
iCount = 2
For Rowcount = 0 To DataTables.Rows.Count - 1
If iCount > DataTables.Rows.Count + 1 Then
Exit For
End If
.Cells(iCount, 1).Font.Name = "Arial"
.Cells(iCount, 2).Font.Name = "Arial"
.Cells(iCount, 3).Font.Name = "Arial"
.Cells(iCount, 4).Font.Name = "Arial"
.Cells(iCount, 5).Font.Name = "Arial"
.Cells(iCount, 6).Font.Name = "Arial"
.Cells(iCount, 1) = DataTables.Rows(Rowcount).Item(0)
.Cells(iCount, 2) = DataTables.Rows(Rowcount).Item(1)
.Cells(iCount, 3) = DataTables.Rows(Rowcount).Item(2)
.Cells(iCount, 4) = DataTables.Rows(Rowcount).Item(3)
.Cells(iCount, 5) = DataTables.Rows(Rowcount).Item(4)
.Cells(iCount, 6) = DataTables.Rows(Rowcount).Item(5)
iCount = iCount + 1
Next
.Cells.EntireRow.AutoFit()
.Cells.EntireColumn.AutoFit()
.Cells.EntireRow.ApplyOutlineStyles()
.Cells.EntireColumn.ApplyOutlineStyles()
oExcel.SaveWorkspace("C:\Documents and Settings\John_A\Desktop\Item Details.xlsx")
MsgBox("The File has been save in the path :C:\Documents and Settings\John_A\Desktop\Item Details.xls ", MsgBoxStyle.Information, "Export To Excel")
oExcel.Visible = True
Cursor = Cursors.Arrow
Exit Sub
End With
Catch ex As Exception
MessageBox.Show("Source [" & ex.Source & "] Description [" & ex.Message & "]")
Exit Sub
End Try
'Again set the objects to nothing
oExcel = Nothing
oExcelWorkBook = Nothing
oExcel.Quit()
oExcel = Nothing
Exit Sub
End Sub
I need the code to work even if i dont have any excel file of that name in that folder.