| | |
Excel integration with the ASP.net over VB
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 84
Reputation:
Solved Threads: 0
hi,
I am desiging ASP.net website , in one of the forms I need to import data to an excel work book with a specific name but I need to check first is that work bokk already exist so not recreate but only add a sheet to it . here is segment of the code to chekc:
If FileExists(path & doc_ID) Then
MsgBox("already exists ", MsgBoxStyle.MsgBoxSetForeground)
Else
'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
calculation()
oBook.SaveAs(path & doc_ID)
'oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
MsgBox("Done")
Response.Redirect("calculateResults.aspx")
End If
the method FileExists is as follows :
Private Function FileExists(ByVal FileFullPath As String) _
As Boolean
'If Trim(FileFullPath) = "" Then Return False
Dim f As New IO.FileInfo(FileFullPath)
Return f.Exists()
End Function
note the does not contain the operation of adding a sheet but only display msg,
The problem is even if a have a file with the same exact name it never shows the msg box
any body can help, I'll appreciate it
I am desiging ASP.net website , in one of the forms I need to import data to an excel work book with a specific name but I need to check first is that work bokk already exist so not recreate but only add a sheet to it . here is segment of the code to chekc:
If FileExists(path & doc_ID) Then
MsgBox("already exists ", MsgBoxStyle.MsgBoxSetForeground)
Else
'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
calculation()
oBook.SaveAs(path & doc_ID)
'oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
MsgBox("Done")
Response.Redirect("calculateResults.aspx")
End If
the method FileExists is as follows :
Private Function FileExists(ByVal FileFullPath As String) _
As Boolean
'If Trim(FileFullPath) = "" Then Return False
Dim f As New IO.FileInfo(FileFullPath)
Return f.Exists()
End Function
note the does not contain the operation of adding a sheet but only display msg,
The problem is even if a have a file with the same exact name it never shows the msg box
any body can help, I'll appreciate it
there's always something to learn
•
•
Join Date: Jan 2008
Posts: 35
Reputation:
Solved Threads: 5
Hi,
Regarding MsgBox: MsgBox displays a message on the screen; however, the code is on the server, so the message box would show on the server, which is ignored by it so that the it wouldn't be locked down until a user sat down in front of the server and clicked "OK" on the message box.
I would say a better approach would be to make an OleDb connection to the Excel file, from which you can insert sheets, etc. if the Excel file exists.
Use your code for checking if a file exists; if the file exists, connect to the file using an OleDb connection (reference www.connectionStrings.com for help with constructing an appropriate connection string). Here is example code I wrote which worked, adding a sheet named "test" to the file, test.xsl:
Also, make sure that the Excel file is not Read-Only, and that the IUSR_<machine_name> user has read/write access to the file.
~ mellamokb
Regarding MsgBox: MsgBox displays a message on the screen; however, the code is on the server, so the message box would show on the server, which is ignored by it so that the it wouldn't be locked down until a user sat down in front of the server and clicked "OK" on the message box.
I would say a better approach would be to make an OleDb connection to the Excel file, from which you can insert sheets, etc. if the Excel file exists.
Use your code for checking if a file exists; if the file exists, connect to the file using an OleDb connection (reference www.connectionStrings.com for help with constructing an appropriate connection string). Here is example code I wrote which worked, adding a sheet named "test" to the file, test.xsl:
ASP.NET Syntax (Toggle Plain Text)
Dim strExcelFile As String = Server.MapPath("~/test.xls") Dim strExcelFolder As String = strExcelFile.Substring(0, strExcelFile.LastIndexOf("\")) Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFile + ";Extended Properties=Excel 8.0" Dim con As New OleDbConnection(connectionString) con.Open() Dim com As New OleDbCommand("CREATE TABLE [test] ( A INT, B INT )", con) com.ExecuteNonQuery() con.Close()
Also, make sure that the Excel file is not Read-Only, and that the IUSR_<machine_name> user has read/write access to the file.
~ mellamokb
Last edited by mellamokb; Jan 3rd, 2008 at 9:48 pm. Reason: Convert c#.net to vb.net
![]() |
Similar Threads
- New Win32 virus - i dont know what to do (Viruses, Spyware and other Nasties)
- MPTFT.EXE problem (Viruses, Spyware and other Nasties)
- Browser Hijacking - AAARGH! (Viruses, Spyware and other Nasties)
Other Threads in the ASP.NET Forum
- Previous Thread: adding data to sql db
- Next Thread: Variables
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox class click commonfunctions compatible confirmationcodegeneration content contenttype control countryselector courier css database datagrid datagridview datagridviewcheckbox datalist deadlock deployment development dgv dropdownlist dropdownmenu dynamic edit embeddingactivexcontrol expose findcontrol flash flv formatdecimal forms formview gridview homeedition iframe iis javascript jquery list login menu microsoft mono mssql multistepregistration nameisnotdeclared numerical objects order panelmasterpagebuttoncontrols problem ratings rotatepage save schoolproject search security serializesmo.table silverlight smartcard sql sqlserver2005 ssl suse textbox tracking unauthorized validation vb.net video virtualdirectory vista visual-studio visualstudio vs2008 web webarchitecture webdevelopemnt webdevelopment webservice wizard xml youareanotmemberofthedebuggerusers





