2015-10-07 59 views
0

我运行了一个MySQL Query,它给了我一个可以使用的结果集。将dataRow导出到csv

但是,我现在需要将其导出到CSV文件。

如果我运行

$result1 | export-csv "c:\path\to\csv.csv" 

我得到仅在第一单元中的单个条目的文件:

#TYPE System.Int32 

不过应该APX 6000行。

如果我做

$result1 | Get-member 

我得到:

TypeName: System.Int32 

Name  MemberType Definition                   
----  ---------- ----------                   
CompareTo Method  int CompareTo(System.Object value), int CompareTo(int value)       
Equals  Method  bool Equals(System.Object obj), bool Equals(int obj)         
GetHashCode Method  int GetHashCode()                  
GetType  Method  type GetType()                  
GetTypeCode Method  System.TypeCode GetTypeCode()               
ToString Method  string ToString(), string ToString(string format), string ToString(System.IFormatP... 


    TypeName: System.Data.DataRow 

Name    MemberType   Definition               
----    ----------   ----------               
AcceptChanges  Method    System.Void AcceptChanges()           
BeginEdit   Method    System.Void BeginEdit()            
CancelEdit  Method    System.Void CancelEdit()            
ClearErrors  Method    System.Void ClearErrors()           
Delete   Method    System.Void Delete()             
EndEdit   Method    System.Void EndEdit()            
Equals   Method    bool Equals(System.Object obj)          
GetChildRows  Method    System.Data.DataRow[] GetChildRows(string relationName), System.D... 
GetColumnError Method    string GetColumnError(int columnIndex), string GetColumnError(str... 
GetColumnsInError Method    System.Data.DataColumn[] GetColumnsInError()       
GetHashCode  Method    int GetHashCode()             
GetParentRow  Method    System.Data.DataRow GetParentRow(string relationName), System.Dat... 
GetParentRows  Method    System.Data.DataRow[] GetParentRows(string relationName), System.... 
GetType   Method    type GetType()              
HasVersion  Method    bool HasVersion(System.Data.DataRowVersion version)     
IsNull   Method    bool IsNull(int columnIndex), bool IsNull(string columnName), boo... 
RejectChanges  Method    System.Void RejectChanges()           
SetAdded   Method    System.Void SetAdded()            
SetColumnError Method    System.Void SetColumnError(int columnIndex, string error), System... 
SetModified  Method    System.Void SetModified()           
SetParentRow  Method    System.Void SetParentRow(System.Data.DataRow parentRow), System.V... 
ToString   Method    string ToString()             
Item    ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Ite... 
CJ    Property    System.UInt64 CustomerJobId {get;set;}        
End    Property    System.DateTime Endtime {get;set;}         
Name    Property    System.String Engineer {get;set;}         
JN    Property    System.String Jobname {get;set;}          
Start    Property    System.DateTime Starttime {get;set;}  

什么是将此转换为CSV文件的正确方法是什么?


write-host $result1给我类似下面的数据:

CJ   : 
JN   : 
Name   : Mr Smith 
Start   : 
End   : 

CJ   : 987654321 
JN   : 
Name   : Mr Jones 
Starttime  : 29/09/2015 08:00:00 
Endtime  : 29/09/2015 08:30:00 
+2

'$ result1'在控制台中的外观如何?你可以用'export-csv“c:\ path \去除\ csv.csv”-NoTypeInformation“这个定义行,但这不是你真正的问题。 '$ result1'只是听起来像一个数字。不是'export-csv'会使用的对象。 – Matt

+0

欢呼,我已经添加了一个$ result1看起来像 – IGGt

+0

的例子你可以显示加载'$ result1'的代码 –

回答

2

我已经找到一种方法,使这项工作,通过创建一个PSObject,然后转换,为CSV。

$a = 0 
$data = @() 
foreach($res in $result1) 
    { 
     if($a -eq 0) 
      { 
       #Do Nothing 
      } 
     else 
      { 
       $cj = $res.CJ 
       $jn = $res.JN 
       $en = $res.Name 
       $st = $res.Start 
       $et = $res.End 

       #write-host "$cj,$jn,$en,$st,$et" 

       $row = New-Object PSObject 
       $row | Add-Member -MemberType NoteProperty -Name "CJ" -Value $cj -force 
       $row | Add-Member -MemberType NoteProperty -Name "JN" -Value $jn -force 
       $row | Add-Member -MemberType NoteProperty -Name "Name" -Value $en -force 
       $row | Add-Member -MemberType NoteProperty -Name "Start" -Value $st -force 
       $row | Add-Member -MemberType NoteProperty -Name "End" -Value $et -force 

       $data += $row   
      } 
     $a++ 
    } 

$data | Export-Csv "c:\path\to\csv.csv" -NoTypeInformation