powershell 合并多个文件的图片

未用到参数,只是把几个excel中的图片合并到同一个excel中,每3个一行,一个sheet存9张图片,多余的放在后面的sheet中。

涉及到excel中的copy与paste,sheet的顺序,以及使用记录存储在数据库中。
$T=Get-Date -Format  yyyyMMddhhmm
$Curent=(dir .).Parent.FullName[0]
$MyInvocation.MyCommand.Path #"D:\RPA_Project\Project\8.ISDP_合并图片"# Split-Path -Parent $MyInvocation.MyCommand.Definition
$SourceFile=$Curent+"\Sourcefile"
$TargetFile=$Curent+"\10percent_L1_"+ $T +".xlsx"
$TemplateFile=$Curent+"\Template\10percent_L1.xlsx"
$Logfile =$Curent+"\output.log"
 

Function LogWrite
{
   Param ([string]$logstring)
   $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
   Add-content $Logfile -value "$Stamp $logstring"
}

function ConnectMySQL([string]$user,[string]$pass,[string]$MySQLHost,[string]$database) {
 
  [void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")
 
  $connStr = "server=" + $MySQLHost + ";port=3306;uid=" + $user + ";pwd=" + $pass + ";database="+$database+";Pooling=FALSE"
  $conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)

  try{
      $conn.Open()
      $cmd = New-Object MySql.Data.MySqlClient.MySqlCommand("USE $database", $conn)
      $cmd.Dispose()
    }
    catch{}

  return $conn
 
}
 
function WriteMySQLQuery($conn, [string]$query) {
 
  $command = $conn.CreateCommand()
  $command.CommandText = $query
  $RowsInserted = $command.ExecuteNonQuery()
  $command.Dispose()
  if ($RowsInserted) {
    return $RowInserted
  } else {
    return $false
  }
}

$MySqlCon = ConnectMySQL -user dbUserDev -pass xxx -MySQLHost xxxx -database sre


LogWrite “---------------------”
LogWrite $Curent
#--------------------------------------------------------------------
$Fiels=Dir $SourceFile -filter *.xlsx -recurse

#--------------------------------------------------------------------
$Application = New-Object -ComObject "Excel.Application";
$Application.displayAlerts=$false;
$Application.visible= $false;

#--------------------------------------------------------------------
$Workbook = $Application.Workbooks.open($TemplateFile) 

$SheetCount=[Math]::Ceiling($Fiels.Count/3)
for($R=$SheetCount ;$R -ge 1 ;$R--)
{

    $Worksheet = $Workbook.WorkSheets.item(1) 
    $worksheet.activate()
    $range = $WorkSheet.Range(“A1:T1”).EntireColumn
    $range.Copy()  | out-null

    $Workbook.worksheets.add() | out-null
    $Worksheet = $Workbook.Worksheets.item(1)
    $worksheet.activate()
    $Range = $Worksheet.Range(“A1”)
    $Worksheet.Paste($Range)
    $worksheet.Range("A1:D1").rowheight =47

    $worksheet.Range("B4:D6").columnwidth=43
    $worksheet.Range("B4:D6").rowheight =159
    
    $worksheet.Range("B8").rowheight =110

    $worksheet.Range("A1:T8").font.Name = "等线"
    $worksheet.Range("A1:T8").font.FontStyle = "Regular"
    #$worksheet.Range("A1:T8").font.Size = 16

    #[void]$worksheet.Range("B4:D6").Columns.AutoFit()
    #[void]$worksheet.Range("B4:D6").Rows.AutoFit()

    $Worksheet.Name = "Photo L1 Splitter 10%(P.10." + $R + ")"

}

$workbook.worksheets.item("Photo L1 Splitter 10%(P.10)").Delete()

[int]$Rows=1
[int]$Sheets=1
$Fiels | ForEach-Object {
    $info =[system.diagnostics.fileversioninfo]::GetVersionInfo($_.FullName);

    $Workbook1=$Application.Workbooks.open($info.FileName);
    $worksheet1=$Workbook1.worksheets.item(1);
    $worksheet1.activate()
    $range1 = $worksheet1.Range("B4:D4")
    $range1.Copy()  | out-null

    $Sheets=[Math]::Ceiling($Rows/3)
   
    $Worksheet2 = $Workbook.Worksheets.item($Sheets)
    $worksheet2.activate()
    [int]$rr=[Math]::Ceiling($Rows%3);
    if( $rr -eq 1)
    {
        $rr=4
    }
    if( $rr -eq 2)
    {
        $rr=5
    }
    if( $rr -eq 0)
    {
        $rr=6
    }

    $rrr="B"+ $rr +":D"+$rr
    $Range = $Worksheet2.Range($rrr)
    $Worksheet2.Paste($Range)

    $Workbook1.close() 
    $worksheet1 = $Null
    $Workbook1 = $Null

    $Rows++
    LogWrite $info.FileName
    $Rows = WriteMySQLQuery $MySqlCon 'insert table xxx()vaules()'
}

$workbook.SaveAs($TargetFile) 

$Workbook.close()
$worksheet = $Null
$Worksheet2 = $Null

$Workbook = $Null


$Application = $null
        
#Stop-Process -Name excel
LogWrite “---------------------”
$MySqlCon.close()

[System.GC]::Collect()

你可能感兴趣的:(我在华为做RPA)