CSV到现有工作簿中的命名表

时间:2016-04-07 19:42:42

标签: excel vba excel-vba csv powershell

所以我已经有一段时间了,而且我有各种各样的方法(一些VBA,其他的PowerShell)半工作说...

我正在尝试完成的快速概述是每周导入两个CSV(删除旧数据,标题始终保持相同但两张不同)到同一工作簿中的两个特定Excel工作表(Ex sheet1, sheet2,计算表),其中有另一张表,然后计算数据。最后,我想将其导出为PDF格式。

完整说明:

每个星期一将两个查询导出为.CSV,让我们说C:\Users\Me\Desktop\Data1.CSV & C:\Users\Me\Desktop\Data2.CSV

我希望将CSV的数据输入到工作簿C:\Users\Me\Desktop\Calculation.xlsx

中的各自工作表(Data1,Data2)中

Data1看起来像这样:

COUNT   STATUS  OPERATOR    PRODUCT     WEEK
1       CANCEL  BOB         Product 1   10
65      CLEAR   JIM         Product 2   10
20      SEND    BOB         Product 1   10
58      CC      KRIS        Product 4   10
3       CLEAR   BOB         Product 1   10
11      SEND    SMIT        Product 6   10
6       CANCEL  JASON       Product 7   10

Data2看起来像这样:

OPERATOR    CLEARS  SENDS   TOTAL   CR       WEEK
BOB         11      1       12     0.916667  10 
JIM         17      2       19     0.894737  10 
KRIS        9       1       10     0.9       10 
SMITH       22      5       27     0.814815  10 
JASON       25      7       32     0.78125   10

然后,计算表将识别数据并相应处理,然后导出为PDF。接下来的星期一,Windows计时器服务调用一个.bat文件,该文件然后运行此脚本(VBA或PowerShell),该脚本将删除此工作簿中的前几周数据,并从查询中输入新数据。

我非常开放,因为我掌握了哪种语言,我掌握了PowerShell和VBA的基本知识和不足之处。我没有包含我目前拥有的代码,因为我已经将它斩首试图让它在我的需求中工作,因为我已经将各种方法混合在一起研究如何做到这一点。

希望我提供了足够的信息,以便有人能指出我正确的方向......

由于

修改

根据克里斯的说法我已经尝试使用了一些代码,因为我试图根据我的需要修改它而不是最初的代码,这可能会非常混乱:

Get-service bits | Select-Object COUNT, STATUS, OPERATOR, PRODUCT, WEEK | Export-Csv 'C:\Users\Me\Desktop\Data1.csv' -NoTypeInformation
Get-service bits | Select-Object COUNT, STATUS, OPERATOR, PRODUCT, WEEK | Export-Excel 'C:\Users\Me\Desktop\Calculations.xlsx' 

$Results = @()

Import-Excel -Path 'C:\Users\Me\Desktop\Calculations.xlsx' | foreach {
    $Properties = @{
        COUNT = $PSItem.COUNT
        STATUS = $PSItem.STATUS
        OPERATOR = $PSItem.OPERATOR
        PRODUCT = $PSItem.PRODUCT
        WEEK = $PSItem.WEEK
        }
    $Results += New-Object -TypeName psobject -Property $Properties
}
Import-Csv -Path 'C:\Users\Me\Desktop\Data1.csv' | foreach {
    $Properties = @{

          COUNT = $PSItem.COUNT
        STATUS = $PSItem.STATUS
        OPERATOR = $PSItem.OPERATOR
        PRODUCT = $PSItem.PRODUCT
        WEEK = $PSItem.WEEK
        }
    $Results += New-Object -TypeName psobject -Property $Properties
}
$Results | Export-Excel -Path 'C:\Users\Me\Desktop\Calculations.xlsx'

我尝试的另一个例子:

$Excel = New-Object -ComObject Excel.Application

$XLSFile = 'C:\Users\me\Desktop\Calculations.xlxs'
$csvFile = 'C:\Users\me\Desktop\Data\Data1.csv'
$Excel.Visible = $true
$ExcelWorkBook = $Excel.Workbooks.Open($XLSFile)
$ExcelWorkSheet = $ExcelWorkBook.sheets.item('Data')
$ExcelWorkSheet.Activate()
# Go to the first empty row
$LastRow = $ExcelWorkSheet.UsedRange.rows.count + 1
Import-Csv -Path $csvFile | ForEach {

$ExcelWorkSheet.cells.Item($lastRow,1) = $psitem.COUNT
$ExcelWorkSheet.cells.Item($lastRow,2) = $psitem.STATUS
$ExcelWorkSheet.cells.Item($lastRow,3) = $psitem.OPERATOR
$ExcelWorkSheet.cells.Item($lastRow,4) = $psitem.PRODUCT
$ExcelWorkSheet.cells.Item($lastRow,5) = $psitem.WEEK
$LastRow = $LastRow + 1
}

$ExcelWorkBook.Save()
$ExcelWorkBook.Close()
$Excel.Quit()

$path = "C:\Users\me\Desktop\"
$xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as [type] 
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse 
$objExcel = New-Object -ComObject excel.application 
$objExcel.visible = $false 
foreach($wb in $excelFiles) 
{ 
 $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + " Weekending " +(Get-Date).AddDays(-1).ToString('MMM-dd-yyyy') + “.pdf”) 
 $workbook = $objExcel.workbooks.open($wb.fullname, 3) 
 $workbook.Saved = $true 
“saving $filepath” 
 $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath, 1, 2) 
 $objExcel.Workbooks.close() 
} 
$objExcel.Quit() 

最后的例子

$Excel = New-Object -ComObject Excel.Application

$XLSFile = 'C:\Users\me\Desktop\Calculations.xlsx'
$csvFile = 'C:\Users\me\Desktop\Data1.csv'
$Excel.Visible = $true
$ExcelWorkBook = $Excel.Workbooks.Open($XLSFile)
$ExcelWorkBook.worksheets.item("Data1").Delete()
# Create a new worksheet
$ExcelWorkSheet = $ExcelWorkBook.Worksheets.Add()
# Set the name for the worksheet
$ExcelWorkSheet.Name = "Data1"
$ExcelWorkSheet = $ExcelWorkBook.sheets.item('Data1')
$ExcelWorkSheet.Activate()
# Go to the first empty row
Import-Csv -Path $csvFile | ForEach {

$ExcelWorkSheet.cells.Item($lastRow,1) = $psitem.COUNT
$ExcelWorkSheet.cells.Item($lastRow,2) = $psitem.STATUS
$ExcelWorkSheet.cells.Item($lastRow,3) = $psitem.OPERATOR
$ExcelWorkSheet.cells.Item($lastRow,4) = $psitem.PRODUCT
$ExcelWorkSheet.cells.Item($lastRow,5) = $psitem.WEEK
$LastRow = $LastRow + 1
}

$ExcelWorkBook.Save()
$ExcelWorkBook.Close()
$Excel.Quit()

1 个答案:

答案 0 :(得分:0)

使用Excel COM界面一次填充目标单元格非常慢。使用Range.Copy和Sheet.Paste更快:

class RememberUserCheck extends StatefulWidget {
  @override
  RememberUserCheckState createState() => RememberUserCheckState();
}

class RememberUserCheckState extends State<RememberUserCheck> {
  bool rememberUser = false;

  @override
  Widget build(BuildContext context) {
    return Checkbox(
      value: rememberUser,
      onChanged: (bool value) {
        setState(() {
          rememberUser = value;
        });
      },
    );
  }
}

C ++版本示例代码(假设我们已经有“ pSheetCsvDest”,这是我们的.csv数据目标表):

    step 1: "ExcelApp.Workbooks.OpenText" to open data.csv file 
    step 2: "ExcelApp.Windows("data").ActiveSheet" to target the data sheet just opened
    step 3: "ActiveSheet.Range" to target the data area
    step 4: "Range.Copy" to copy the source data to the clipboard 
    step 5: "Sheet.Paste" to move the data to the destination