快速生成Excel

我想我正在努力工作,帮助我…

我正在build立一个分类待办事项列表(不是一个function或任何东西,只是一个快速的,closures的手套列表)。
清单应该像这样组织:

Category1 -----------Item1 -----------Item2 Category2 -----------Item1 (always the same items in each category) -----------Item2 

所以我有2个文本文件,1个与类别和一个项目。

如果这是一个数据库问题,我会做笛卡尔连接,结果将非常接近我后。

我想要做的是采取这两个文件,并喷出一个Excel文件,其中第一列是类别,第二个是项目。

我开始编写一个小小的c#程序来强行这个,但似乎我必须错过一些东西 – 是否有一个macros,或者甚至可以扔在一起的精简的powershell脚本来做到这一点? 它看起来应该是如此简单…

如果你真的只是寻找一个快速和肮脏的解决scheme,你可以快速编写一个VB / C#代码,将两个文件join到一个CSV,并在Excel中打开它们来继续您的任务。 如果您需要更多的操作文件,我build议您阅读这里发布的链接。

使用C#从Excel电子表格读取/写入

问候,安迪。

尝试这个:

  $(foreach($c in cat .\categories.txt) { foreach ($i in cat .\items.txt) { New-Object PSObject -Property @{ Category = $c Item = $i } } }) | Format-Table -GroupBy Category -Property Item Category: category1 Item ---- item1 item2 item3 item4 Category: category2 Item ---- item1 item2 item3 item4 

我不知道C#,但我做了像VB一样。

我所做的只是使用.NET的库,也构buildExcel书籍,我开始录制macros如何我想要它,然后它只是适用于我的VB程序的macros的问题。

这个答案可能不会帮助你,我只是希望它指出你正确的方向,只是让我find一个例子,我会很高兴地分享它

EDTI我发现一个链接的copule,我希望他们工作

MSDN论坛

CodeProject.com

这是一种将其放入Excel的方法。 我认为你需要破折号,而不是重复类别名称。 对?

 $(foreach($c in cat .\categories.txt) { foreach ($i in cat .\items.txt) { New-Object PSObject -Property @{ Category = $c Item = $i } } }) | select Category, Item | Export-Csv -NoTypeInformation $pwd\test.csv; Invoke-Item $pwd\test.csv 

这是自包含的,并显示了一些我发现有用的技术。 你会对Excel的速度感到非常失望,但我的用户喜欢在屏幕上看到漂亮的东西,而不是.csv的。

 Param ( [parameter(Mandatory=$false)] [string]$targetFile = 'D:\cafp1\middleware\restricted\middleware\IIS\PowerShell\Demos\excel\test.xls' ) begin { Set-StrictMode -version Latest function configureSheet { param ( [parameter(Mandatory=$true)] $appExcel, [parameter(Mandatory=$true)] $appWorkbook, [parameter(Mandatory=$true)] $worksheetName ) # Get the appropriate sheet $sheetNumber = 1 $headerSheet = $appWorkbook.WorkSheets.Item($sheetNumber) $headerSheet.Activate() $headerSheet.Name = $worksheetName # Place the intro text $introRowNumber = 1 $introColumnNumber = 1 $headerSheet.Cells.Item($introRowNumber,$introColumnNumber) = "Intro Text" # Freeze panes for easy navigation $freezeCell = "a2" [void]$headerSheet.Range($freezeCell).Select() $headerSheet.application.activewindow.FreezePanes = $true # Configure headers $headerSheet.Cells.Item(2,1) = "Header 1" $headerSheet.Columns.Item(1).ColumnWidth = 15 $headerSheet.Cells.Item(2,2) = "Header 2" $headerSheet.Columns.Item(2).ColumnWidth = 25 $headerTitles = $headerSheet.UsedRange $headerTitles.Interior.ColorIndex = 40 $headerTitles.Font.ColorIndex = 9 $headerTitles.Font.Bold = $True $firstDataRow = 3 [void]$headerSheet.Cells.Item($firstDataRow,1).Activate() $headerSheet } function reportObject { param ( [parameter(Mandatory=$true)] $sheet, [parameter(Mandatory=$true)] $activeRowNumber, [parameter(Mandatory=$true)] $variant ) # I took out a lot of logic to allow complex objects to be reported. This is easy to extend, though. $sheet.Cells.Item($activeRowNumber,1) = $variant.Category $sheet.Cells.Item($activeRowNumber,2) = $variant.Item } $appExcel = New-Object -comObject Excel.Application $appExcel.visible = $true $appExcel.ScreenUpdating = $true $appWorkbook = $appExcel.Workbooks.Add() $originalCalculationState = $appExcel.Calculation $appExcel.Calculation = -4135 # Magic number to disable calculation $appWorkbook.Title = "Title in Properties" $appWorkbook.Subject = "Subject in Properties" $currentSheet = "WorkSheet Name" $sheet = $false } process { $sheet = configureSheet ` -appExcel $appExcel ` -appWorkbook $appWorkbook ` -worksheetName $currentSheet $activeRow = 3 $record = New-Object PSObject add-member -InputObject $record Noteproperty 'Category' "Category1" add-member -InputObject $record Noteproperty 'Item' "" reportObject ` -sheet $sheet ` -activeRowNumber $activeRow ` -variant $record } end { # Save and close this workbook $headerSheet = $appWorkbook.WorkSheets.Item(1) $row = 3 $column = 1 [void]$headerSheet.Cells.Item($row,$column).Select() [void]$appExcel.Selection.AutoFilter() [void]$headerSheet.Cells.Item($row,$column).Activate() $appExcel.Calculation = $originalCalculationState # Excel defaults to My Documents as the home folder $appExcel.DisplayAlerts = $false $appWorkbook.SaveAs($targetFile,1) $appWorkbook.Close() # Do this to get PowerShell Console to really kill the Excel object. # PowerShell ISE will only really kill Excel once it itself is killed. [void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($appExcel) }