在powershell中可以通过调用com来对excel等office产品进行操控,这里介绍一些excel的一些API

打开、保存、关闭

  1. $xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] #设置excel文件类型
  2. $objExcel = New-Object -ComObject excel.application #创建excel对象
  3. $objExcel.visible = $false #是否可见
  4. $excel.displayAlerts = $false #是否显示对话框
  5. $path = "C:\Users\Administrator\Desktop\test.xls" #待打开文件
  6. $objExcel.workbooks.Add()#创建新的工作簿
  7. $workbook = $objExcel.workbooks.open($path) #获取工作簿对象
  8. $ws=$workbook.WorkSheets.item(1)#获取工作表对象,也可以通过item("Sheet1")
  9. $S2 = $workbook.sheets | where {$_.name -eq "Sheet2"} #获取名为Sheet2的表
  10. $workbook.activate()#激活工作簿
  11. $ws.activate()#激活工作表
  12. $workbook.save()#保存工作簿
  13. $workbook.saveas("C:\Users\Administrator\desktop\save.xlsx")#另存工作簿
  14. $workbook.close()#关闭工作簿
  15. $objExcel.Quit()#退出Excel程序
  16. $workbook.workSheets.item(3).delete() #删除工作表
  17. # 释放进程
  18. $excel = $null
  19. [GC]::Collect()

表格属性

  1. $workbook.author = "Thomas Lee - tfl@psp.co.uk" #工作簿作者
  2. $workbook.title = "Excel and PowerShell rock!"
  3. $workbook.subject = "Demonstrating the Power of PowerShell"

定位区域、单元格

  1. $ws.cells.item(1,2)#行列索引只能为整数
  2. $ws.range("b2")#选择单个单元格
  3. $ws.range("b2","d5")#选择从B2D5的一块区域
  4. $ws.range("1:1").select()#选择第一行
  5. $ws.range("b:d").select()#选择第二列到第四列
  6. $ws.rows.item(3).select()#选择第三行
  7. $ws.columns.item(3).select()#选择第三列
  8. $ws.Columns(1).EntireColumn.Delete() #删除第一列

单元格操作

  1. $ws.cells.item(2,1) = "hello"#设置值
  2. $ws.cells.item(2,1).value2 = "hello"#设置值
  3. $ws.cells.item(2,1).select()#选中单元格
  4. $ws.cells.item(2,1).copy()#复制单元格
  5. $ws.cells.item(2,1).pastespecial(-4163)#粘贴单元格
  6. $r1=$ws.range("b2","d5").find('hello')#区域搜索包含hello的单元格
  7. $row=$r1.row #搜索到的单元格的行
  8. $column=$r1.column #搜索到的单元格的列
  9. $max_line_num = $worksheet_data.Range("A1").CurrentRegion.Rows.Count #返回表格中最后一行的行号

格式

  1. $ws.cells.item(2,1).font.Size = 9#设置字体大小
  2. $ws.cells.item(2,1).Name = "Times New Roman"#设置字体名称
  3. $ws.cells.item(2,1).borders.LineStyle = $LineStyle::xlContinuous#设置单元格边框
  4. $ws.cells.item(2,1).font.bold = "true"#设置加粗
  5. $ws.cells.item(2,1).font.italic = $true#设置倾斜
  6. $ws.cells.item(2,1).NumberFormat = "#,##0.00"#设置数字格式

创建四个枚举类型。枚举类型用于告诉 Excel 允许将哪些值填入特定的选项类型。例如,xlLineStyle 枚举用于确定所绘制线条的类型:双线条、虚线等,针对将使用的每个枚举类型创建一个快捷别名。实际上,我们将把代表枚举名称的字符串转换成 [type]

  1. $lineStyle = "microsoft.office.interop.excel.xlLineStyle" -as [type]
  2. $colorIndex = "microsoft.office.interop.excel.xlColorIndex" -as [type]
  3. $borderWeight = "microsoft.office.interop.excel.xlBorderWeight" -as [type]
  4. $chartType = "microsoft.office.interop.excel.xlChartType" -as [type]
  5. for($b = 1 ; $b -le 2 ; $b++)
  6. {
  7. $sheet.cells.item(1,$b).font.bold = $true
  8. $sheet.cells.item(1,$b).borders.LineStyle = $lineStyle::xlDashDot
  9. $sheet.cells.item(1,$b).borders.ColorIndex = $colorIndex::xlColorIndexAutomatic
  10. $sheet.cells.item(1,$b).borders.weight = $borderWeight::xlMedium
  11. }

调整行高

  1. $worksheet.Rows(4).RowHeight = 20
  2. $worksheet.Rows(5).RowHeight = 20

自动调整行高和列宽

  1. $range = $sheet.usedRange #已经有内容的区域,等价于ctrl+a
  2. $range.EntireColumn.AutoFit() | out-null

定义网格线种类

$LineStyle = "microsoft.office.interop.excel.xlLineStyle" -as [type]

指定网格线种类

$sheet.cells.item(2,3).borders.LineStyle = $LineStyle::xlDashDotDot
网线种类:

  • xlContinuous 实线
  • xlDashDot 一点划线
  • xlDashDotDot 二点划线
  • xlDouble 双线
  • xlSlantDashDot 斜线
  • xlDash 虚线
  • xlLineStyleNone 没有线

    定义网线的宽度

    $Weight = "microsoft.office.interop.excel.xlBorderWeight" -as [type]

    指定网格线的宽度

    $sheet.cells.item(2,3).borders.Weight = $Weight::xlMedium
    网线宽度种类:

  • xlHairlin极细

  • xlThin
  • xlMedium
  • xlThick
  1. $worksheet.Range("A1","I1").Merge() #合并单元格
  2. $worksheet.Range("A1","I1").font.bold = $true #字体加粗
  3. $selection1 = $worksheet.Range("A1","I1")
  4. $selection1.HorizontalAlignment = -4108 #居中
  5. $worksheet.Rows(1).RowHeight = 50 #设置行高
  6. $selection1.Font.Size = 14 #字体大小
  7. $selection1.Interior.ColorIndex = 2 #单元格颜色为白色

复制粘贴

  1. $worksheet_data.Range("A1","AD"+$max_line_num).copy() #复制区域
  2. $work_raw_data.Range("A1","AD"+$max_line_num).PasteSpecial(-4163) #粘贴为数值

XlPasteType常量(粘贴的格式)

xlPasteAll -4104 全部(默认值)
xlPasteAllExceptBorders 7 边框除外
xlPasteColumnWidths 8 列宽
xlPasteComments -4144 批注
xlPasteFormats -4122 格式
xlPasteFormulas -4123 公式
xlPasteFormulasAndNumberFormats 11 公式和数字格式
xlPasteValidation 6 有效性验证
xlPasteValues -4163 数值
xlPasteValuesAndNumberFormats 12 值和数字格式

XlPasteSpecialOperation常量(粘贴的操作)

xlPasteSpecialOperationNone -4142 无(默认值)
xlPasteSpecialOperationAdd 2 加
xlPasteSpecialOperationSubtract 3 减
xlPasteSpecialOperationMultiply 4 乘
xlPasteSpecialOperationDivide 5 除

查找替换

  1. $sh_data.Range("G:H").replace("-", "/") #替换

筛选

  1. $range = $ws.UsedRange # 选中所有内容
  2. $range.EntireColumn.AutoFilter() # 开启筛选模式
  3. $range.AutoFilter(1, "A") # 决定要筛选哪列的哪些内容

公式

单元格输入公式

  1. $worksheet.Range("H4").formula = '=SUMIFS(数据源!E:E,数据源!G:G,">=2021/9/1",数据源!G:G,"<=2021/9/30",数据源!U:U,"=")/10000'

图表

  1. $ch = $excel.Charts.Add() # 初始化画布
  2. $ch.ChartTitle.Text = 'memory ocuppation' # 图表标题
  3. $ch.chartType = 70 # 图表类型
  4. $ch.setSourceData($sht.Range("A1:B11"), 2) # 数据源
  5. $ch.ApplyDataLabels(3) # 数据标签

chartType常量

xlLine 折线图
xlLineMarkersStacked 堆积数据点折线图
xlLineStacked 堆积折线图
xlPie 饼图
xlPieOfPie 复合饼图
xlPyramidBarStacked 堆积条形棱锥图
xlPyramidCol 三维柱形棱锥图
xlPyramidColClustered 簇状柱形棱锥图
xlPyramidColStacked 堆积柱形棱锥图
xlPyramidColStacked100 百分比堆积柱形棱锥图
xlRadar 雷达图
xlRadarFilled 填充雷达图
xlRadarMarkers 数据点雷达图
xlStockHLC 盘高-盘低-收盘图
xlStockOHLC 开盘-盘高-盘低-收盘图
xlStockVHLC 成交量-盘高-盘低-收盘图
xlStockVOHLC 成交量-开盘-盘高-盘低-收盘图
xlSurface 三维曲面图
xlSurfaceTopView 曲面图(俯视图)
xlSurfaceTopViewWireframe 曲面图(俯视框架图)
xlSurfaceWireframe 三维曲面图(框架图)
xlXYScatter 散点图
xlXYScatterLines 折线散点图
xlXYScatterLinesNoMarkers 无数据点折线散点图
xlXYScatterSmooth 平滑线散点图
xlXYScatterSmoothNoMarkers 无数据点平滑线散点图
xl3DArea 三维面积图
xl3DAreaStacked 三维堆积面积图
xl3DAreaStacked100 百分比堆积面积图
xl3DBarClustered 三维簇状条形图
xl3DBarStacked 三维堆积条形图
xl3DBarStacked100 三维百分比堆积条形图
xl3DColumn 三维柱形图
xl3DColumnClustered 三维簇状柱形图
xl3DColumnStacked 三维堆积柱形图
xl3DColumnStacked100 三维百分比堆积柱形图
xl3DLine 三维折线图
xl3DPie 三维饼图
xl3DPieExploded 分离型三维饼图
xlArea 面积图
xlAreaStacked 堆积面积图
xlAreaStacked100 百分比堆积面积图
xlBarClustered 簇状条形图
xlBarOfPie 复合条饼图
xlBarStacked 堆积条形图
xlBarStacked100 百分比堆积条形图
xlBubble 气泡图
xlBubble3DEffect 三维气泡图
xlColumnClustered 簇状柱形图
xlColumnStacked 堆积柱形图
xlColumnStacked100 百分比堆积柱形图
xlConeBarClustered 簇状条形圆锥图
xlConeBarStacked 堆积条形圆锥图
xlConeBarStacked100 百分比堆积条形圆锥图
xlConeCol 三维柱形圆锥图
xlConeColClustered 簇状柱形圆锥图
xlConeColStacked 堆积柱形圆锥图
xlConeColStacked100 百分比堆积柱形圆锥图
xlCylinderBarClustered 簇状条形圆柱图
xlCylinderBarStacked 堆积条形圆柱图
xlCylinderBarStacked100 百分比堆积条形圆柱图
xlCylinderCol 三维柱形圆柱图
xlCylinderColClustered 簇状柱形圆锥图
xlCylinderColStacked 堆积柱形圆锥图
xlCylinderColStacked100 百分比堆积柱形圆柱图
xlDoughnut 圆环图
xlDoughnutExploded 分离型圆环图
xlLineMarkers 数据点折线图
xlLineMarkersStacked100 百分比堆积数据点折线图
xlLineStacked100 百分比堆积折线图
xlPieExploded 分离型饼图
xlPyramidBarClustered 簇状条形棱锥图
xlPyramidBarStacked100 百分比堆积条形棱锥图