如何将一个excel文件中的超链接嵌入到另一个带有powershell

时间:2017-06-29 03:43:48

标签: excel powershell hyperlink embed

大家晚上好,

我有一个问题,我遇到了一些问题,我真的需要一些帮助。我拿了两个csv文件并比较它们并将它们转换为xls。现在,我感到困惑的是,我将如何从一个Excel文档中的第1列第1行中获取超链接,并将它们嵌入到另一个文档第1列Row2中的文本中。

有一种简单的方法吗?我发现以下链接让我有点困惑:https://social.technet.microsoft.com/Forums/scriptcenter/en-US/123d673a-f9a7-4ae6-ae9c-d4ae8ef65015/powershell-excel-how-do-i-create-a-hyperlink-to-a-cell-in-another-sheet-of-the-document?forum=ITCG

感谢您提供的任何指导和帮助。

#Define the file path and sheet name
$FilePath= `enter 
code"C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.csv"
$FilePath2="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls"
$FilePath3="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test4.xls"
$SheetName="Test3"
$SheetName2="HyperLinks"

#Compare two CSV files to look for matches
$CSV1 = import-csv -path 
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test1.csv
$CSV2 = import-csv -path 
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test2.csv
Compare-Object $CSV1 $CSV2 -property ShoppingList -IncludeEqual | where-
object {$_.SideIndicator -eq "=="} 

# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Enable the 'visible' property so the document will open in excel
$objExcel.Visible = $true
$objExcel.DisplayAlerts = $False

# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet "Test3" 
$WorkSheet = $WorkBook.sheets.item($SheetName)

# Delete data from column
[void]$WorkSheet.Cells.Item(1,2).EntireColumn.Delete()

#Auto fit everything so it looks better
$usedRange = $WorkSheet.UsedRange   
$usedRange.EntireColumn.AutoFit() | Out-Null

#Save and convert to XLS
$Workbook.SaveAs("C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls",1)
$Workbook.Saved = $True

#Load 
$excel = New-Object -comobject Excel.Application

$excel.Visible = $True

$workbook = $objExcel.Workbooks.Add()

$workbook.Worksheets.Item($FilePath2).Hyperlinks.Add( `
$workbook.Worksheets.Item($FilePath2).Cells.Item(1,2) , `
"" , $FilePath3, "https://community.spiceworks.com/topic/673034-powers

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

Sub WHT()

    Dim origin      As Worksheet
    Dim destination As Worksheet
    Dim desrow      As Long
    Dim descol      As Long
    Dim descolstart As Long
    Dim origrow     As Long
    Dim origcol     As Long
    Dim rng         As range
    Dim C           As range
    Dim qual        As Integer
    Dim total       As Integer
    Dim descolnext  As Integer

    Set origin = Sheets("1")
    Set destination = Sheets("OFFLIMITS")
    desrow = 8
    descol = 1
    origrow = 17
    origcol = 32
    Set rng = origin.range("AI17:AI117")
    total = WorksheetFunction.Sum(origin.range("AI17:AI117"))
    descolstart = destination.Cells(desrow, Columns.Count).End(xlToLeft).Column
    descolnext = descolstart + 1

    Dim whtRange As range
    Dim whtAmount As range

    Set whtRange = origin.range("AI17:AI117")

    For Each whtAmount In whtRange

        If whtAmount.Value = 30 Then

            destination.Cells(desrow, descolstart).Value = origin.range("C3").Value             'Vendor
            destination.Cells(desrow, descolstart + 1).Value = origin.range("C1").Value         'Transaction Date
            destination.Cells(desrow, descolstart + 2).Value = origin.range("C4").Value         'RefNumber
            destination.Cells(desrow, descolstart + 3).Value = origin.range("C6").Value         'Bill Due
            destination.Cells(desrow, descolstart + 4).Value = origin.range("C5").Value         'Terms
            destination.Cells(desrow, descolstart + 5).Value = origin.range("C7").Value         'Memo

            destination.Cells(desrow, descolstart + 6).Value = origin.Cells(origrow, 26).Value   'WHT Account
            destination.Cells(desrow, descolstart + 7).Value = origin.Cells(origrow, 23).Value   'WHT amount
            destination.Cells(desrow, descolstart + 8).Value = origin.Cells(origrow, 31).Value   'WHT Memo
            'no class for wht                                                                    'WHT class

            destination.Cells(desrow, descolstart + 15).Value = origin.range("C2").Value        'AP Account

            origrow = origrow + 1 'make next row origin
            desrow = desrow + 1 'make next row destination cell

        End If

    Next whtAmount

End Sub

参考:Hyperlinks.Add Method

希望有所帮助