提问人:Gabriele Cozzolino 提问时间:5/6/2020 最后编辑:Gabriele Cozzolino 更新时间:5/7/2020 访问量:260
在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 应用程序启动但无法打开文件并抛出有关“无法打开文件,可能它已被移动或无法访问”的错误
答:
1赞
Gabriele Cozzolino
5/7/2020
#1
我替换了
Process.Start("EXCEL.EXE", strFileName)
跟
Process.Start("EXCEL.EXE", """" & strFileName & """")
第一次失败,因为文件名包含空格。
评论
OpenFileDialog1.ShowDialog()