Microsoft Office Excel cannot access the file.There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook
bharathreddy...
Member
365 Points
113 Posts
Microsoft Office Excel cannot access the file.There are several possible reasons: • The file name...
Sep 11, 2006 07:58 AM|LINK
Imports System.Data.OleDb
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim connection As New System.Data.OleDb.OleDbConnection
Dim command As New System.Data.OleDb.OleDbCommand
Dim dr As System.Data.OleDb.OleDbDataReader
Dim cmd1 As New System.Data.OleDb.OleDbCommand
Dim strFilename As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load</div><div></div><div> End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
controlstatus(True)
If Not FileUpload1.HasFile Then
Response.Write("upload something!")
Return
End If
strFilename = FileUpload1.PostedFile.FileName
HiddenField1.Value = FileUpload1.PostedFile.FileName
Dim i As Integer
connection.ConnectionString = connectionstr(HiddenField1.Value)
connection.Open()
command.CommandText = "SELECT * FROM [Sheet1$]"
command.CommandType = Data.CommandType.Text
command.Connection = connection
dr = command.ExecuteReader</div><div></div><div> For i = 0 To dr.FieldCount - 1
combo1.Items.Add(dr.GetName(i).ToString)
combo2.Items.Add(dr.GetName(i).ToString)
combo3.Items.Add(dr.GetName(i).ToString)
Next
combo1.SelectedValue = Label1.Text
combo2.SelectedValue = Label2.Text
combo3.SelectedValue = Label3.Text
dr.Close()
command.Dispose()
connection.Close()</div><div></div><div> End Sub</div><div></div><div> Protected Sub combo1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles combo1.SelectedIndexChanged
connection.ConnectionString = connectionstr(HiddenField1.Value)
connection.Open()
command.CommandText = "SELECT * FROM [Sheet1$] "
command.CommandType = Data.CommandType.Text
command.Connection = connection
dr = command.ExecuteReader
ListBox1.Items.Clear()
While dr.Read = True
ListBox1.Items.Add(dr(combo1.SelectedValue).ToString)
End While
dr.Close()
command.Dispose()
connection.Close()
End Sub</div><div></div><div> Protected Sub combo2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles combo2.SelectedIndexChanged
connection.ConnectionString = connectionstr(HiddenField1.Value)
connection.Open()
command.CommandText = "SELECT * FROM [Sheet1$] "
command.CommandType = Data.CommandType.Text
command.Connection = connection
dr = command.ExecuteReader
ListBox2.Items.Clear()
While dr.Read = True
ListBox2.Items.Add(dr(combo2.SelectedValue).ToString)
End While
dr.Close()
command.Dispose()
connection.Close()
End Sub</div><div></div><div> Protected Sub combo3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles combo3.SelectedIndexChanged
connection.ConnectionString = connectionstr(HiddenField1.Value)
connection.Open()
command.CommandText = "SELECT * FROM [Sheet1$]"
command.CommandType = Data.CommandType.Text
command.Connection = connection
dr = command.ExecuteReader
ListBox3.Items.Clear()
While dr.Read = True
ListBox3.Items.Add(dr(combo3.SelectedValue).ToString)
End While
dr.Close()
command.Dispose()
connection.Close()
End Sub</div><div></div><div> Sub controlstatus(ByVal state As Boolean)
combo1.Visible = state
combo2.Visible = state
combo3.Visible = state
Label1.Visible = state
Label2.Visible = state
Label3.Visible = state
ListBox1.Visible = state
ListBox2.Visible = state
ListBox3.Visible = state
End Sub</div><div></div><div> Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
Dim oExcel As Excel.Application 'object for new excel Application
Dim oWB As Excel.Workbook 'object for new excel workbook
Dim oWS As Excel.Worksheet</div><div></div><div> oExcel = New Excel.Application
oWB = oExcel.Workbooks.Add ' Adding work book to excel Application
oWS = oWB.Worksheets("Sheet1") ' selecting sheet from work book
Dim i As Integer ' varisblr for holading loop counter
oWS.Cells(1, 1) = combo1.SelectedItem.Value ' loading first row in first column item in excel sheet to Header which is selected item
oWS.Cells(1, 2) = combo2.SelectedItem.Value ' loading first row in second column item in excel sheet to Header which is selected item
oWS.Cells(1, 3) = combo3.SelectedItem.Value ' loading first row in third column item in excel sheet to Header which is selected item</div><div></div><div> For i = 0 To ListBox1.Items.Count - 1 ' Loop start
oWS.Cells(i + 2, 1) = ListBox1.Items(i).Value ' inserting item in first coumn from Listbox1
Next ' loop end
For i = 0 To ListBox2.Items.Count - 1 ' loop start
oWS.Cells(i + 2, 2) = ListBox2.Items(i).Value ' inserting item in second coumn from Listbox2
Next 'loop end</div><div></div><div> For i = 0 To ListBox3.Items.Count - 1 ' Loop start
oWS.Cells(i + 2, 3) = ListBox3.Items(i).Value 'inserting item in third coumn from Listbox2
Next 'Loop end
Dim strMonth, strDay As String ' Variable for holding month and day
If Now.Month.ToString.Length = 1 Or Now.Day.ToString.Length = 1 Then
strMonth = "0" & Now.Month.ToString
strDay = "0" & Now.Day.ToString
Else
strMonth = Now.Month.ToString
strDay = Now.Day.ToString
End If
' finally saves the document
System.IO.Directory.GetAccessControl(Server.MapPath("Excelsheets\"), System.Security.AccessControl.AccessControlSections.Access)
oWB.SaveAs(Server.MapPath("Excelsheets\") & strMonth & strDay & Now.Year.ToString.Substring(2, 2) & txtClientcode.Text & txtType.Text & ".xls", , , , , , XlSaveAsAccessMode.xlNoChange)
'oWB.SaveAsXMLData("c:/xml.xml") </div><div></div><div> oWB.Close()</div><div></div><div> Catch ex As Exception
Response.Write(ex.Message)
End Try</div><div></div><div> End Sub
'Function for connection string
'Input:Xl filename
'output:connection string
'Author:Vijay</div><div></div><div> Function connectionstr(ByVal filename As String)
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & filename & ";Extended Properties=""Excel 8.0;"""
Return strConn
End Function
'Loading File path to Hidden value
Protected Sub HiddenField1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles HiddenField1.ValueChanged
HiddenField1.Value = FileUpload1.PostedFile.FileName
End Sub
End Class
</div>
stebes
Member
2 Points
3 Posts
Re: Microsoft Office Excel cannot access the file.There are several possible reasons: • The file ...
Sep 30, 2010 07:14 PM|LINK
I have the same problem. Any solution?
malek.safa
Member
79 Points
77 Posts
Re: Microsoft Office Excel cannot access the file.There are several possible reasons: • The file ...
Oct 06, 2010 08:55 AM|LINK
check this link: http://forums.asp.net/t/1609952.aspx
mjc130
Member
2 Points
1 Post
Re: Microsoft Office Excel cannot access the file.There are several possible reasons: • The file ...
May 16, 2012 12:38 AM|LINK
We had a similar issue with opening Excel witha VB routine on a 2008r2 server. Simply creating the c:\windows\syswow64\config\systemprofile\desktop folder resolved this for Office 2007. Alas it does not work for Office 2010, but it may point people in the right direction
Refer to this thread
http://forums.techarena.in/windows-security/1297117.htm
Michael.