powershell - 自动扩展行和列
问题描述
我正在使用代码在 datagridview 中打开和查看 XML 文件,但行和列尚未像在 excel 中那样打开。
我一直在互联网上搜索有关如何像在 excel 中一样呈现数据的答案,但一直无法找到如何做到这一点。
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[Void][reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[Void][System.Windows.Forms.Application]::EnableVisualStyles()
#Open File Dialog
Function Get-FileName($initialDirectory){
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "XML (*.xml)| *.xml"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
#Save File Dialog
Function Save-FileName($initialDirectory){
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "XML (*.xml)| *.xml"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
#Create Form Functions
function Create-Form {
# Draw Form
$form1 = New-Object System.Windows.Forms.Form
$form1.ClientSize = New-Object System.Drawing.Size(725,400)
$form1.Text = "XML to DataGrid Editor"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.FormBorderStyle = 5
# Draw DataGrid
$dataGrid1 = New-Object System.Windows.Forms.DataGrid
$dataGrid1.Size = New-Object System.Drawing.Size(700,338)
$dataGrid1.Location = New-Object System.Drawing.Point (13,13)
$dataGrid1.DataBindings.DefaultDataSourceUpdateMode = 0
$dataGrid1.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$dataGrid1.Name = "dataGrid1"
$dataGrid1.DataMember = ""
$dataGrid1.TabIndex = 0
$form1.Controls.Add($dataGrid1)
# Draw Open xml Configuration Button
$button_openxml = New-Object System.Windows.Forms.Button
$button_openxml.Size = New-Object System.Drawing.Size(150,25)
$button_openxml.Location = New-Object System.Drawing.Point(13,365)
$button_openxml.Text = "Open XML Document"
$button_openxml.Add_Click({
$xml_networkschema = Get-FileName
# Bind Data to DataGrid
$ds = New-Object System.Data.Dataset
$ds.ReadXml($xml_networkschema)
$dataGrid1.DataSource = $ds
})
$form1.Controls.Add($button_openxml)
# Save xml Configuration Button
$button_savexml = New-Object System.Windows.Forms.Button
$button_savexml.Size = New-Object System.Drawing.Size(150,25)
$button_savexml.Location = New-Object System.Drawing.Point(170,365)
$button_savexml.Text = "Save XML Document"
$button_savexml.enabled = "false"
$button_savexml.Add_Click({
$dbm_savenetworkschema = Save-FileName
$dataGrid1.DataSource.writexml($dbm_savenetworkschema)
})
$form1.Controls.Add($button_savexml)
$form1.ShowDialog()| Out-Null
}
Create-Form
解决方案
推荐阅读
- javascript - 什么是 Service Worker 的 URL.createObjectURL 的模拟
- php - 使用 simplexml 修改从 rss 文件中提取的数据
- android - 有没有办法发送 IP 摄像机的本地 RTSP 流并从 android 应用程序访问它?
- python - 在Python中逐位比较2个数字(16位)的最有效方法是什么?
- android - Flutter app apk 在手机上无法正常运行,但在模拟器上运行良好
- amazon-web-services - AWS EBS 卷优化停留在 70% 达 8 小时
- sql-server - 如何计算特定单词在列中出现的次数?
- r - R从变量名列表创建变量表
- python - 如何将此 JSON 文件插入 MongoDB 集合?
- linux - 如何确定 Linux 中正确的运行线程数?