繁体   English   中英

在 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 应用程序启动但无法打开文件并抛出有关“无法打开文件,可能已移动或无法访问”的错误

我换了

Process.Start("EXCEL.EXE", strFileName)

Process.Start("EXCEL.EXE", """" & strFileName & """")

第一次失败,因为文件名包含空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM