| $time=get-date |
| |
| $excel = New-Object -ComObject Excel.Application |
| Start-Sleep -Seconds 2 |
| $Excel.Visible = $true |
| if ( [Io.File]::Exists("$Pwd\Lottery.xlsx") ) { $Exist = $true } else { $Exist =$Null } |
| if ( $Exist ) { |
| $workbook = $excel.Workbooks.Open("$Pwd\Lottery.xlsx") |
| $NewestName = $workbook.worksheets.Item(1).name |
| $workbook.worksheets.Application.DisplayAlerts = $false |
| $workbook.worksheets.Item(1).delete() |
| $sheet = $workbook.worksheets.add() |
| $sheet = $workbook.worksheets.Item(1) |
| } else { |
| $NewestName = 2003 |
| $workbook = $excel.Workbooks.add() |
| $sheet = $workbook.worksheets.Item(1) |
| } |
| $borderWeight = "microsoft.office.interop.excel.xlBorderWeight" -as [type] |
| |
| Function ExcelMerge { |
| Param ( [Array]$CSVData, [string]$year ) |
| |
| $workbook.worksheets.Item(1).Name = $year |
| $sheet = $workbook.worksheets.Item($year) |
| |
| $sheet.Rows.HorizontalAlignment = 3 |
| $sheet.Rows.VerticalAlignment = 2 |
| $sheet.Columns.RowHeight = 16 |
| $RangeHeight = $sheet.Range("A1:B1") |
| $RangeHeight.RowHeight = 24 |
| |
| $CELL1 = $sheet.Cells.item(1,3) |
| $CELL2 = $sheet.Cells.item(1,35) |
| $CELLRange = $sheet.Range( $CELL1 , $CELL2 ) |
| $CELLRange.Merge() |
| $CELL1 = $sheet.Cells.item(1,36) |
| $CELL2 = $sheet.Cells.item(1,51) |
| $CELLRange = $sheet.Range( $CELL1 , $CELL2 ) |
| $CELLRange.Merge() |
| |
| $CELL1 = $sheet.Cells.item(1,1) |
| $CELL2 = $sheet.Cells.item(1,51) |
| $CELLRange = $sheet.Range( $CELL1 , $CELL2 ) |
| $CELLRange.Borders.Weight = $borderWeight::xlThin |
| $CELLRange.Font.bold = $true |
| $CELLRange.Font.Size = 13 |
| $CELLRange.Font.ColorIndex = 1 |
| $sheet.cells.item(1,1) = '期号' |
| $sheet.cells.item(1,2) = '开奖日期' |
| $sheet.cells.item(1,3) = '红球' |
| $sheet.cells.item(1,36) = '兰球' |
| |
| $CELL1 = $sheet.Cells.item(2,3) |
| $CELL2 = $sheet.Cells.item($($CSVData.Count + 1),35) |
| $CELLRange = $sheet.Range( $CELL1 , $CELL2 ) |
| $CELLRange.Borders.Weight=$borderWeight::xlHairLine |
| $CELLRange.Font.ColorIndex = 3 |
| $CELLRange.Font.Size = 12 |
| $CELLRange.Columns.ColumnWidth = 2.5 |
| |
| $CELL1 = $sheet.Cells.item(2,36) |
| $CELL2 = $sheet.Cells.item($($CSVData.Count + 1),51) |
| $CELLRange = $sheet.Range( $CELL1 , $CELL2 ) |
| $CELLRange.Borders.Weight=$borderWeight::xlHairLine |
| $CELLRange.Font.ColorIndex = 5 |
| $CELLRange.Font.Size = 12 |
| $CELLRange.Font.bold = $true |
| $CELLRange.Columns.ColumnWidth = 2.5 |
| |
| $CELL1 = $sheet.Cells.item(2,1) |
| $CELL2 = $sheet.Cells.item($($CSVData.Count + 1),2) |
| $CELLRange = $sheet.Range( $CELL1 , $CELL2 ) |
| $CELLRange.Borders.Weight = $borderWeight::xlThin |
| $CELLRange.Font.Size = 12 |
| $CELLRange.Font.ColorIndex = 1 |
| $RangeWidth = $sheet.Range("A1:A2") |
| $RangeWidth.EntireColumn.ColumnWidth = 8 |
| $RangeWidth = $sheet.Range("B1:B2") |
| $RangeWidth.EntireColumn.ColumnWidth = 11 |
| |
| $StartLine = 2 |
| for ( $line =0; $line -lt $CSVData.Count; $line++ ) { |
| $EachCell = @($CSVData[$line].Split(',')) |
| for ( $v=1; $v -le $EachCell.Count; $v++ ) { $sheet.Cells.Item($StartLine,$v) = $EachCell[$($v -1)] } |
| $StartLine++ |
| } |
| if ( [int]$year -ne 2019 ) { $sheet = $workbook.worksheets.add() } |
| } |
| |
| |
| Function DownLotteryData { |
| Param ( [Array]$DATA, [string]$year, [int]$n ) |
| |
| $Ball=$RedBlue=$arr=$array= New-Object "System.Collections.ArrayList" |
| (1..52) |ForEach-Object {$Ball += 'A'} |
| |
| for ( $i = 0; $i -lt $DATA.count; $i++ ) { |
| if ( $n -le 8 ) { |
| $arr += $DATA[$i] |
| if ($n -le 2) { $num = $n } else { $num = [int](([string]($DATA[$i])).TrimStart('0')) + 2 } |
| $Ball[$num] = $DATA[$i] |
| $n++ |
| } else { |
| $arr += $DATA[$i] |
| $num = [int](([string]($DATA[$i])).TrimStart('0')) + 35 |
| $Ball[$num] = $DATA[$i] |
| $RedBlue += ,$Ball |
| $array += ,$arr |
| $arr = $Ball = New-Object "System.Collections.ArrayList" |
| (1..52) |ForEach-Object {$Ball += 'A'} |
| $n = 1 |
| } |
| } |
| $content = $AllBall = New-Object "System.Collections.ArrayList" |
| for ( $i = 0; $i -lt ( $DATA.count /9 ); $i++ ) { |
| $Content += ( $array[$i] -join ' ' ).Replace('.','/') |
| $AllBall += ( $RedBlue[$i] -join ',' ).Replace('.','_').Replace('A',' ') -replace '^(\s+)?,?','' |
| } |
| $Content | Set-Content .\开奖数据\简略数据\$year.txt -enc Default -force |
| ExcelMerge $AllBall $year |
| } |
| |
| new-item .\开奖数据\简略数据 -type Directory -force |
| for ( $year = [int]$NewestName; $year -le 2019; $year++ ) { |
| $url = "https://kjh.55128.cn/ssq-history-$year.htm" |
| Invoke-WebRequest -uri $url -Outfile "$env:temp\Downh.log" |
| $PageData = (( Get-Content "$env:temp\Downh.log" -ReadCount 0 -enc utf8 ) ` |
| -match '^(\s+)?<td>(\d{7}|\d{4}(\.\d{2}){2})</td>$|<li(\s+)?class.*>\d{2}</li>$' ) ` |
| -replace "(\s+)?</?(td)?(li)?((\s+)class='ball.*-24')?>",'' |
| DownLotteryData $PageData $year 1 |
| } |
| Remove-Item "$env:temp\Downh.log" -force |
| |
| if ( $Exist ) { $workbook.Save() } else { $workbook.SaveAs("$Pwd\Lottery.xlsx") } |
| $workbook.Close() |
| $Excel.quit() |
| $excel = $null |
| [GC]::Collect() |
| ([datetime]::Now -$time).totalmilliseconds |
| pauseCOPY |