找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 16342|回复: 0

[原创代码] powershell生成Excel表格,自动填充,可以设置颜色

[复制链接]
发表于 2020-1-4 23:03:39 | 显示全部楼层 |阅读模式
如题, powershell修改Excel,设置颜色,内容, 等。不知道跟vba相比,优缺点如何,有待高人解答。

  1. $processes=Get-Process
  2. $excel = New-Object -ComObject Excel.Application
  3. $excel.Visible = $true
  4. $workbook = $excel.Workbooks.add()
  5. $sheet = $workbook.worksheets.Item(1)
  6. #$workbook.Worksheets.item(3).delete()
  7. $workbook.Worksheets.item(2).delete()
  8. $workbook.Worksheets.item(1).name="Processes"
  9. $sheet = $workbook.WorkSheets.Item("Processes")
  10. $x = 2

  11. $lineStyle = "microsoft.office.interop.excel.xlLineStyle" -as [type]
  12. $colorIndex = "microsoft.office.interop.excel.xlColorIndex" -as [type]
  13. $borderWeight = "microsoft.office.interop.excel.xlBorderWeight" -as [type]
  14. $chartType = "microsoft.office.interop.excel.xlChartType" -as [type]

  15. for($b = 1 ; $b -le 2 ; $b++)
  16. {
  17. $sheet.cells.item(1,$b).font.bold = $true
  18. $sheet.cells.item(1,$b).borders.LineStyle = $lineStyle::xlDashDot
  19. $sheet.cells.item(1,$b).borders.ColorIndex = $colorIndex::xlColorIndexAutomatic
  20. # $sheet.cells.item(1,$b).borders.Weight = $borderWeight::xlMedium
  21. }

  22. $sheet.cells.item(1,1) = "Name of Process"
  23. $sheet.cells.item(1,2) = "Working Set Size"

  24. foreach($process in $processes)
  25. {
  26. $sheet.cells.item($x, 1) = $process.name
  27. $sheet.cells.item($x, 1).font.ColorIndex = 52 #只有这个起作用,
  28. # $sheet.cells.item($x, 1).font.Color = 40
  29.     #$sheet.cells.item($x, 1).interior.color = 22
  30.   $sheet.cells.item($x, 1).interior.colorindex = 20#只有这个起作用,
  31. $sheet.cells.item($x,2) = $process.workingSet
  32. $x++
  33. if ($x -gt 10) {
  34. break
  35. }
  36. } #end foreach

  37. $range = $sheet.usedRange

  38. $MergeCells = $sheet.Range("A2:A5")
  39. $MergeCells.Select()
  40. $MergeCells.MergeCells = $true


  41. $range.EntireColumn.AutoFit() | out-null

  42. $ma = $sheet.range("A2").mergeArea()
  43. write-host $ma.count()
  44. write-host "merge aaa= " $sheet.cells.item(2,2).text -ForegroundColor red

  45. for ($i=0;$i -lt $ma.count();$i++) {
  46.    write-host "merge aera for A2= " $sheet.cells.item(2+$i,2).text -ForegroundColor yellow
  47. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 04:48 , Processed in 0.015656 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表