class ExcelDataClass{
[String]$BookFilePath
[System.Object]$AllDataHash
[object]$workbook
[object]$excel
ExcelDataClass([string]$filepath){
$this.BookFilePath = $filepath
$this.excel = New-Object -ComObject Excel.Application
$this.workbook = $this.excel.workbooks.open($filepath)
$SheetList = new-object System.collections.arrayList
$this.workbook.Sheets | foreach-object {
$SheetName = $_.name
$SheetList.add($SheetName)
}
$this.AllDataHash = [ordered]@{}
foreach($SheetName in $SheetList){
$ColHash = [ordered]@{}
$sheet = $this.workbook.worksheets.Item($SheetName)
$lastRow = $sheet.UseDrange.Rows.Count
$lastCol = $sheet.UsedRange.Columns.Count
if($lastCol -eq 0){
continue
}
for($col=1;$col -le $lastCol;$col++){
$RowHash = new-object System.Collections.Hashtable
for($row=1;$row -le $lastRow;$row++){
$val = $sheet.Cells.Item($row,$col).text.trim()
if($val -eq ""){
$val = "-"
}
$RowNum = ([string]$row).PadLeft(3,"0")
$RowHash.$RowNum = $val
}
$ColNum = ([string]$col).PadLeft(4,"0")
$ColHash.$ColNum = $RowHash
}
[void][System.Runtime.InteropServices.Marshal]::FinalReleaseComObject($sheet)
$this.AllDataHash.$SheetName."ブックファイルパス" = $this.BookFilePath
$this.AllDataHash.$SheetName."列数" = $lastCol
$this.AllDataHash.$SheetName."行数" = $lastRow
$this.AllDataHash.$SheetName."データ" = $ColHash
}
$this.quit()
}
[void]ShowAllData(){
$this.AllDataHash.keys.getEnumerator() | sort-object value | foreach-object {
$SheetName = $_
$this.AllDataHash.$SheetName.keys.getEnumerator() | sort-object |foreach-object {
$Col = $_
$this.AllDataHash.$SheetName.$Col.keys.getEnumerator() | sort-object |foreach-object {
$Row = $_
$this.AllDataHash.$SheetName.$Col.$Row | foreach-object {
$v = $_
write-host("Sheet name:[{0}],Col:[{1}],Row:[{2}],Value:[{3}]" -f $SheetName,$Col,$Row,$v)
}
}
}
}
}
[Array]GetSheetList(){
return $this.AllDataHash.keys
}
[String]GetSheetData([string]$sheetName){
write-host("ブック=[{0}]" -f $this.BookFilePath)
write-host("列数={0}" -f $this.AllDataHash.$sheetName."列数")
write-host("行数={0}" -f $this.AllDataHash.$sheetName."行数")
return 0
}
[int]GetSheetLastCol([string]$sheetName){
return $this.AllDataHash.$sheetName."0001".count
}
[int]GetSheetLastRow([string]$sheetName){
return $this.AllDataHash.$sheetName.count
}
[system.collections.ArrayList]GetSheetList(){
$a = new-object system.collections.ArrayList
foreach($sheetName in $this.AllDataHash.keys){
$a.add($sheetName)
}
return $a
}
[void]OutputCsv([string]$csvfile){
}
[void]quit(){
$this.book.Close()
$this.excel.Quit()
if($this.sheet){
[void][System.Runtime.InteropServices.Marshal]::FinalReleaseComObject($this.sheet)
}
[void][System.Runtime.InteropServices.Marshal]::FinalReleaseComObject($this.excel)
$this.excel = $null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.GC]::Collect()
}
}
'''