在 excel 文件中查找并替换,然后用 Excel 应用程序打开它
Find and replace in excel file and then open it with Excel app
我想在 xls 文件中进行替换,然后用 Excel 打开它。
我有这个
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strFileName As String
OpenFileDialog1.Title = "Apri file TimeSheet"
OpenFileDialog1.InitialDirectory = "c:"
OpenFileDialog1.Filter = "File Excel (*.xls)|*.xls|All files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If OpenFileDialog1.FileName.Length > 0 Then
strFileName = OpenFileDialog1.FileName
Dim xlapp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim wb As Microsoft.Office.Interop.Excel.Workbook = Nothing
wb = xlapp.Workbooks.Open(strFileName.ToString())
wb.Worksheets(1).Cells.Replace(".", ",")
wb.Save()
wb.Close()
Threading.Thread.Sleep(500)
Process.Start("EXCEL.EXE", strFileName)
End
End If
End Sub
End Class
但是 process.start 失败了*,因为文件似乎已被移动,即使路径和文件名相同。我如何在运行时解决这个问题(意味着没有用户干预)?
* 表示 Excel 应用启动但无法打开文件并抛出有关 "can't open the file, maybe it was moved or it is unaccessible"
的错误
我替换了
Process.Start("EXCEL.EXE", strFileName)
和
Process.Start("EXCEL.EXE", """" & strFileName & """")
第一次失败,因为文件名包含 space.
我想在 xls 文件中进行替换,然后用 Excel 打开它。 我有这个
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strFileName As String
OpenFileDialog1.Title = "Apri file TimeSheet"
OpenFileDialog1.InitialDirectory = "c:"
OpenFileDialog1.Filter = "File Excel (*.xls)|*.xls|All files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If OpenFileDialog1.FileName.Length > 0 Then
strFileName = OpenFileDialog1.FileName
Dim xlapp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim wb As Microsoft.Office.Interop.Excel.Workbook = Nothing
wb = xlapp.Workbooks.Open(strFileName.ToString())
wb.Worksheets(1).Cells.Replace(".", ",")
wb.Save()
wb.Close()
Threading.Thread.Sleep(500)
Process.Start("EXCEL.EXE", strFileName)
End
End If
End Sub
End Class
但是 process.start 失败了*,因为文件似乎已被移动,即使路径和文件名相同。我如何在运行时解决这个问题(意味着没有用户干预)?
* 表示 Excel 应用启动但无法打开文件并抛出有关 "can't open the file, maybe it was moved or it is unaccessible"
的错误我替换了
Process.Start("EXCEL.EXE", strFileName)
和
Process.Start("EXCEL.EXE", """" & strFileName & """")
第一次失败,因为文件名包含 space.