2016-11-06 172 views
0

我已經將我的excel文件作爲資源添加到了我的vb項目中,並使用了「嵌入式資源」選項。文件「StoredInformation.xlsx」的名稱。 'Build Action'被設置爲'Embedded Resource'。在Resources中讀取和寫入Excel文件

編輯二 - 但我再次卡住了。看來,如果當我運行我的程序

Dim sPath As String 

    sPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) 

    For i = 1 To 50 
     Threading.Thread.Sleep(100) 
     Application.DoEvents() 

    Next 

    If My.Computer.FileSystem.FileExists(sPath & "\Housing\Stored Information.xlsx") Then 
     Dim APP As New Excel.Application 
     workbook = APP.Workbooks.Open(sPath & "\Housing\Stored Information.xlsx") 
     worksheet = workbook.Worksheets("Sheet1") 
     APP.Visible = False 
     MessageBox.Show("File Opened!" & Environment.NewLine & "Path: " & sPath & "\Housing\Stored Information.xlsx") 
    Else 
     My.Computer.FileSystem.WriteAllBytes(sPath & "\Housing\Stored Information.xlsx", My.Resources.StoredInformation, True) 
     Dim APP As New Excel.Application 
     workbook = APP.Workbooks.Open(sPath & "\Housing\Stored Information.xlsx") 
     worksheet = workbook.Worksheets("Sheet1") 
     APP.Visible = False 
     MessageBox.Show("File Created!" & Environment.NewLine & "Path: " & sPath & "\Housing\Stored Information.xlsx") 
    End If 

這不是我的創建文件夾「住有所居」或我的文件「存儲Information.xlsx」 可能有人請看看,並告訴我什麼IM我的代碼甚而不工作做錯了?

編輯 - 我發現它不能創建新的目錄,因爲保護級別,所以當我改變目的地到桌面時,它創建了Excel文件,但它需要修復。

有人能告訴我如何創建一個新的文件夾,並添加一個文件,而不會損壞Excel文件?

編輯 - 修改了代碼,它仍然沒有創造我的文檔的新文件夾,並添加「存儲Information.xlsx」文件

回答

0

Unraveling the confusion about Embedded Resources - 發現這個線程

解釋如何嵌入你使用你的程序,以及如何訪問它的資源。這deffinetly幫我

編輯:答案

Dim sPath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Housing") 

    Dim Fpath As String = sPath & "\Stored Information.xlsx" 

    IO.Directory.CreateDirectory(sPath) ' If location already exists it will not do anything 

    If My.Computer.FileSystem.FileExists(Fpath) = False Then 
     My.Computer.FileSystem.WriteAllBytes(Fpath, My.Resources.StoredInformation, True) ' Don't want to append data (although that would not happen in this instance) so True is used for that. 
    End If 

    Dim APP As New Excel.Application 
    workbook = APP.Workbooks.Open(Fpath) 
    worksheet = workbook.Worksheets("Sheet1") 
    APP.Visible = False 
    MessageBox.Show("File Opened!" & Environment.NewLine & "Path: " & Fpath) 
0

用於讀取和寫入到Excel文件我會去了解一下這位前輩發佈後,我發現列出了與excel文件交互的一些庫和資源。

Excel Library

More Excel Libraries

+0

當時一個有益的參考,但答案都爲C#,即時尋找Vb.net碼 –

+0

還沒什麼即時尋找 –