首页 > 解决方案 > 在 Power shell 中创建具有多行和多列的动态 HTML 表

问题描述

参考示例 JSON,我正在尝试创建动态 HTML 表,但是我无法获得具有特定行数据的特定列名。

条件:

脚本:

 $response = get-content <JSONFilePath>
 $dataTable = ($response | ConvertFrom-Json).Tables[0]
 $columncount = $dataTable.Columns.count
  $header = "<table border=1><tr>"
  for($i=0;$i -lt $columncount;$i++){
    $header = $header+"<th>"+$dataTable.Columns[$i].ColumnName+"</th>"
  }
  $header = $header +"</tr>"
      $Lines ="<tr>"
       for($i=0;$i -lt $columncount;$i++)
     {   
      $val=""
     foreach($rowdata in $dataTable.Rows)
     {
        ###Unable to get Specific row data for specific Column name
        $val = $val+"<td>"+$i[$rowdata]+"</td>"
      }
      $Lines = $Lines+ $val +"</tr>"
     }

       $tableval = $header+ $Lines +"</table>"
      Write-Output $tableval 

JSON:

     {
 "Tables": [
{
  "TableName": "Table_0",
  "Columns": [
    {
      "ColumnName": "Name",
      "DataType": "String",
      "ColumnType": "string"
    },
    {
      "ColumnName": "Rating",
      "DataType": "Double",
      "ColumnType": "real"
    }
  ],
  "Rows": [
    [
      "Satya",
      1000.0
    ],
    [
      "Pavan",
      2000.00
    ]
  ]
},
{
  "TableName": "Table_1",
  "Columns": [
    {
      "ColumnName": "Value",
      "DataType": "String",
      "ColumnType": "string"
    }
  ],
  "Rows": [
    [
    ]
         ]
         }
   ]
   }

输出:

              <table>
    <tr>
      <th>Name</th>
   <th>Rating</th> 
   </tr>
    <tr>
     <td>Satya</td>
     <td>1000</td>
   </tr>
  <tr>
     <td>Pavan</td>
     <td>2000</td>
  </tr>
 </table>
  

标签: htmljsonpowershellforeachhtml-table

解决方案


推荐阅读