设置ExecutionPolicy远程签名
$word=New-Object -ComObject “Word.Application”
$excel = New-Object -ComObject Excel.Application
#$excel.Visible = $true #excel视图可见
$exFile=”D:\onedrive\MINE\脚本\个人\excelToWord\测试\测试题.xlsx”
$doc=$word.documents.add()
#$word.Visible=$true
$selection=$word.Selection
$wb=$excel.Workbooks.Open($exFile)
$ws=$wb.ActiveSheet
$row=2
#$j=1
$DataItem_Num=$ws.Range(“A$row”).Text
while ($DataItem_Num)
{
$DataItem_type=$ws.Range(“D$row”).Text ###d列存放试题类型,比如单选,多选,判断等
#$DataItem_type=””
if ($DataItem_c=$ws.Range(“I$row”).Text)
{
if ($DataItem_d=$ws.Range(“K$row”).Text)
{
$DataItem_Num=$ws.Range(“A$row”).Text
$DataItem_Body=$ws.Range(“B$row”).Text
$DataItem_answer=$ws.Range(“E$row”).Text
$DataItem_a=$ws.Range(“G$row”).Text
$DataItem_b=$ws.Range(“H$row”).Text
$DataItem_c=$ws.Range(“I$row”).Text
$DataItem_d=$ws.Range(“J$row”).Text
$DataItem_e=$ws.Range(“K$row”).Text
$selection.TypeText($DataItem_Num)
$selection.TypeText(“. “)
$selection.TypeText($DataItem_Body)
$selection.TypeParagraph()
$selection.TypeText(“a: “)
$selection.TypeText($DataItem_a)
$selection.TypeParagraph()
$selection.TypeText(“b: “)
$selection.TypeText($DataItem_b)
$selection.TypeParagraph()
$selection.TypeText(“c: “)
$selection.TypeText($DataItem_c)
$selection.TypeParagraph()
$selection.TypeText(“d: “)
$selection.TypeText($DataItem_d)
$selection.TypeParagraph()
$selection.TypeText(“e: “)
$selection.TypeText($DataItem_e)
$selection.TypeParagraph()
$selection.TypeText(“(“+$DataItem_type+”)答案是: “)
$selection.TypeText($DataItem_answer)
$selection.TypeParagraph()
$selection.TypeParagraph()
$row=$row+1
}else{
$DataItem_Num=$ws.Range(“A$row”).Text
$DataItem_Body=$ws.Range(“B$row”).Text
$DataItem_answer=$ws.Range(“E$row”).Text
$DataItem_a=$ws.Range(“G$row”).Text
$DataItem_b=$ws.Range(“H$row”).Text
$DataItem_c=$ws.Range(“I$row”).Text
$DataItem_d=$ws.Range(“J$row”).Text
$selection.TypeText($DataItem_Num)
$selection.TypeText(“. “)
$selection.TypeText($DataItem_Body)
$selection.TypeParagraph()
$selection.TypeText(“a: “)
$selection.TypeText($DataItem_a)
$selection.TypeParagraph()
$selection.TypeText(“b: “)
$selection.TypeText($DataItem_b)
$selection.TypeParagraph()
$selection.TypeText(“c: “)
$selection.TypeText($DataItem_c)
$selection.TypeParagraph()
$selection.TypeText(“d: “)
$selection.TypeText($DataItem_d)
$selection.TypeParagraph()
$selection.TypeText(“(“+$DataItem_type+”)答案是: “)
$selection.TypeText($DataItem_answer)
$selection.TypeParagraph()
$selection.TypeParagraph()
$row=$row+1
}
}else{
$DataItem_Num=$ws.Range(“A$row”).Text
$DataItem_Body=$ws.Range(“B$row”).Text
$DataItem_answer=$ws.Range(“E$row”).Text
$DataItem_a=$ws.Range(“G$row”).Text
$DataItem_b=$ws.Range(“H$row”).Text
$selection.TypeText($DataItem_Num)
$selection.TypeText(“. “)
$selection.TypeText($DataItem_Body)
$selection.TypeParagraph()
$selection.TypeText(“a: “)
$selection.TypeText($DataItem_a)
$selection.TypeParagraph()
$selection.TypeText(“b: “)
$selection.TypeText($DataItem_b)
$selection.TypeParagraph()
$selection.TypeText(“(“+$DataItem_type+”)答案是: “)
$selection.TypeText($DataItem_answer)
$selection.TypeParagraph()
$selection.TypeParagraph()
$row=$row+1
}
#$j=$j+1
}
$doc.SaveAs([ref]”D:\onedrive\MINE\脚本\个人\excelToWord\测试\测试题.docx”)
$doc.Close()
$word.quit()
$wb.Close()
$excel.quit()