生成HTML片段的Powershell

生成HTML片段的Powershell,html,excel,powershell,dashboard,Html,Excel,Powershell,Dashboard,我目前正在尝试编写一个Powershell脚本,该脚本将读取Excel文件并生成HTML片段。我正在使用它为仪表板生成HTML,其思想是,无论由于嵌套而在电子表格上放置了什么新的div,脚本都不必更改。我真的不知道从哪里开始。我的原始脚本与下面的脚本类似,但在文件中放入新列或div时必须对其进行编辑,这就是为什么我不能使用CSV文件的原因 new-item registration.html -type file add-content registration.html "<html&g

我目前正在尝试编写一个Powershell脚本,该脚本将读取Excel文件并生成HTML片段。我正在使用它为仪表板生成HTML,其思想是,无论由于嵌套而在电子表格上放置了什么新的div,脚本都不必更改。我真的不知道从哪里开始。我的原始脚本与下面的脚本类似,但在文件中放入新列或div时必须对其进行编辑,这就是为什么我不能使用CSV文件的原因

new-item registration.html -type file
add-content registration.html "<html><head></head><body><div id='registration'>"
new-item bill.html -type file
add-content bill.html "<html><head></head><body><div id='bill'>"
new-item financial.html -type file
add-content financial.html "<html><head></head><body><div id='financial'>"
$csv=(import-csv elements.csv)
foreach($row in $csv){
$name=(get-date).ticks
$registration= $row.registration
$bill= $row.bill
$financial= $row.financial
add-content registration.html "<%="
add-content registration.html $registration
add-content registration.html "%>"
add-content bill.html "<%="
add-content bill.html $bill
add-content bill.html "%>"
add-content financial.html "<%="
add-content financial.html $financial
add-content financial.html "%>"
}
add-content registration.html "</div></body></html>"
add-content bill.html "</div></body></html>"
add-content financial.html "</div></body></html>"
newitem registration.html-类型文件
添加内容注册.html“”
new item bill.html-类型文件
添加内容bill.html“”
new item financial.html-类型文件
添加内容financial.html“”
$csv=(导入csv元素.csv)
foreach($csv中的行){
$name=(获取日期).ticks
$registration=$row.registration
$bill=$row.bill
$financial=$row.financial
添加内容注册.html“”
添加内容bill.html“”
添加内容financial.html“”
}
添加内容注册.html“”
添加内容bill.html“”
添加内容financial.html“”

我依赖于很多东西,基本上你需要找出每个文件的逻辑并相应地生成内容。根据您已有的信息,我可以建议一种较短的方法:

add-content registration.html "<html><head></head><body><div id='registration'>"
add-content bill.html "<html><head></head><body><div id='bill'>"
add-content financial.html "<html><head></head><body><div id='financial'>"

import-csv elements.csv | foreach{
    add-content registration.html ('<%={0}%>' -f $_.registration)
    add-content bill.html ('<%={0}%>' -f $_.bill)
    add-content financial.html ('<%={0}%>' -f $_.financial)
}

add-content registration.html,bill.html,financial.html "</div></body></html>"
add content registration.html“”
添加内容bill.html“”
添加内容financial.html“”
导入csv元素.csv | foreach{
添加content registration.html(“”-f$\注册)
添加content bill.html(“”-f$\u.bill)
添加content financial.html(“”-f$\uux.financial)
}
添加content registration.html、bill.html、financial.html“”
你可以更概括上面的内容,我没有测试它,但我认为它应该可以工作

'registration','bill','financial' | ForEach-Object {

    $page = $_

    add-content "$page.html" "<html><head></head><body><div id='$page'>"

    import-csv elements.csv | foreach{
        add-content "$page.html" ('<%={0}%>' -f $_."$page")
    }

    add-content "$page.html" "</div></body></html>"
}
每个对象的“注册”、“票据”、“财务”{
$page=$_
添加内容“$page.html”“”
导入csv元素.csv | foreach{
添加内容“$page.html”('-f$\'$page”)
}
添加内容“$page.html”“”
}

除非我把你的代码放错地方,否则它不会工作。其思想是根据csv文件中的项目数生成html片段。每个项都有自己的div。它们在foreach循环中移动div创建行。