[.NET] 怎麼把excel的資料存成文字檔

看板Visual_Basic作者 (傷心無話)時間18年前 (2007/08/02 21:48), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
因為用VB處理excel儲存格的速度太慢, 所以我想先存成文字檔處理完 再回頭貼上VB 我查過MSDN, 想說應該是用saveas加上指定fileformat為文字檔就可以成功儲存 但在正式實做的時候, 一直出現錯誤: HRESULT: 0x800A03EC 而且查MSDN HRESULT: 0x800A03EC也查不到什麼資料, google的說法則是眾說紛紜 請問有沒有EXCEL存成文字檔的方法呢? 以下是我的程式碼 Module PubVariables Public xlApp As Microsoft.Office.Interop.Excel.Application = _ New Microsoft.Office.Interop.Excel.Application '開啟一個Excel應用程式 '供所有的form都可以使用 End Module '另一個form裡 '讓使用者決定要開幾個工作表 Private Sub BtnExcel_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles BtnExcel.Click Dim xl2input As Microsoft.Office.Interop.Excel.Workbook xlApp.SheetsInNewWorkbook = CInt(TxtNo.Text) xl2input = CType(xlApp.Workbooks.Add, _ Microsoft.Office.Interop.Excel.Workbook) xlApp.Application.Visible = True End Sub '開完之後待使用者輸入資料完畢之後按下另一個鈕讓VB做之後的處理 Private Sub BtnProceed_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles BtnProceed.Click Dim i As Integer Dim xl2input As Microsoft.Office.Interop.Excel.Workbook Dim xlsheet As Microsoft.Office.Interop.Excel.Worksheet xl2input = CType(xlApp.Workbooks(1), _ Microsoft.Office.Interop.Excel.Workbook) For i = 1 To xlApp.Workbooks.Count '將所有的工作表存成文字檔 xlsheet = CType(xl2input.Worksheets(i), _ Microsoft.Office.Interop.Excel.Worksheet) '在這一行會出現錯誤 xlsheet.SaveAs(xlsheet.Name, _ FileFormat:=_ Microsoft.Office.Interop.Excel.XlFileFormat.xlTextWindows,_ TextCodepage:=True, Local:=True) Next End Sub '另一個函式是開啟已有的檔案進行處理, 也是在存檔時會出錯 Private Sub MnuItmCnvFile_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MnuItmCnvFile.Click Dim xl2cnvt As Microsoft.Office.Interop.Excel.Workbook Dim xlsheet As Microsoft.Office.Interop.Excel.Worksheet Dim i As Integer OpnExistExcel.InitialDirectory = "我的文件" OpnExistExcel.Filter = "Excel 活頁簿 (*.xls)|*.xls" 'Try '開啟檔案 If OpnExistExcel.ShowDialog() = Windows.Forms.DialogResult.OK Then xl2cnvt = xlApp.Application.Workbooks.Open(OpnExistExcel.FileName) '依序存檔 For i = 1 To xl2cnvt.Worksheets.Count xlsheet = CType(xl2cnvt.Worksheets(i), _ Microsoft.Office.Interop.Excel.Worksheet) '在這一步就當機了 xlsheet.SaveAs(xlsheet.Name & ".txt", _ FileFormat:=_ Microsoft.Office.Interop.Excel.XlFileFormat.xlTextWindows, _ TextCodepage:=True, Local:=True) Next End If 'Catch ex As Exception '這邊因為不知道怎麼處理所以先用成註解 'End Try End Sub -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.228.60.124
文章代碼(AID): #16iU2fKP (Visual_Basic)