Java - exekucia PS

Programovacie jazyky, rady, poradňa...
AllGoneDead
Medium Star
Medium Star
Používateľov profilový obrázok
Príspevky: 364
Registrovaný: 27 aug 2012, 9:03
Bydlisko: košice

Java - exekucia PS

Príspevok od používateľa AllGoneDead »

Ahojte,
s PS velmi nerobim a potreboval by som spustit jeden script z Javy. Avsak vysledok je iny ako ked to spustam priamo z cli.

Script mi konvertuje xlsx na samostatne html subory podla sheetov + to popresuva a necha len potrebne data
Ak to spustam z Javy, tak
  • img to vobec nepremenuje na zaklade worksheetu
  • do vysledneho htmlka to neprekopiruje css. Rovnako ale volam aj replace pre script tag, co funguje ok

script vyzera takto

Kód: Vybrať všetko

param([string]$xlsxPath,[string]$htmlPath, [string]$logPath)
$rf = $htmlPath + '\*'
rm -r -fo $rf
$timestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
start-transcript -path "$logPath\xlsxThHml_$timestamp.log" ;
$xlExcelHTML = 44
function Get-ScriptDirectory { Split-Path $MyInvocation.ScriptName }
$filePath = Get-ChildItem $xlsxPath -Filter *.xlsx
foreach($f in $filePath){
	Write-Host "Processing :" $f.FullName
	$Excel = New-Object -ComObject "Excel.Application"
	$Excel.Visible = $false	
	$WorkBook = $Excel.Workbooks.Open($xlsxPath + '\' + $f)	
	if ($WorkBook.Worksheets.Count -gt 0) { 		
		$tempFolder = $htmlPath + '\temp\' + [System.IO.Path]::GetFileNameWithoutExtension($f.fullname)
		md -Path $tempFolder	
		$FileFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook 
		foreach($Worksheet in $WorkBook.Worksheets) {
			$tempHtml = $tempFolder + '\' + $Worksheet.Name + '.html'
			$Worksheet.Copy()
			$Excel.ActiveWorkbook.SaveAs($tempHtml, $xlExcelHTML)
			$Excel.ActiveWorkbook.Close			
			$htmlFolder = $htmlPath + '\html\' + [System.IO.Path]::GetFileNameWithoutExtension($f.fullname) + '\' + $Worksheet.Name
			New-Item $htmlFolder -ItemType Directory
			$tempData = $tempFolder + '\' + $Worksheet.Name + '_files'
			$tempFile = $tempData + '\sheet001.html' 
			$html =  $htmlFolder + '\' + $Worksheet.Name + '.html'			
			Get-ChildItem -Path $tempData -exclude *.xml, *.html, *.css | Copy-Item -Destination $htmlFolder
			(Get-Content -raw $tempFile) -replace '<script[\S\s]*script>', '' | Set-Content ($html)
			$cssFile = $tempData + '\stylesheet.css' 
			$cssF = Get-Content -Path $cssFile
			$cssStyle = '<style>' + $cssF			
			(Get-Content -raw $html) -replace '<style>', $cssStyle | Set-Content ($html)
			$imgPrefix = $Worksheet.Name + '_image'
			Get-ChildItem $htmlFolder | Rename-Item -NewName { $_.Name -replace 'image', $imgPrefix }
			Add-Content -Path .\DateTimeFile1.log, .\DateTimeFile2.log -Value (Get-Date) -PassThru
		}
	}
	$Excel.Quit()
	[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
	Remove-Variable Excel
}
Exekucia z javy

Kód: Vybrať všetko

String pscli = "Powershell.exe -executionpolicy remotesigned -File " + XLSX_TO_HTML_PS_SCRIPT + " -xlsxPath "
				+ SOURCE_PATH + " -htmlPath " + HTML_PATH + " -logPath " + XLSX_LOG;
Runtime runtime = Runtime.getRuntime();
		Process proc;
		try {
			System.out.println("start PS script");
			proc = runtime.exec(pscli);
			.......


Tato cas sa mi zda, ze sposobuje problemy / nefunguje ako ma

Kód: Vybrať všetko

			$cssFile = $tempData + '\stylesheet.css'
			$cssF = Get-Content -Path $cssFile
			$cssStyle = '<style>' + $cssF			
			(Get-Content -raw $html) -replace '<style>', $cssStyle | Set-Content ($html)
			$imgPrefix = $Worksheet.Name + '_image'
			Get-ChildItem $htmlFolder | Rename-Item -NewName { $_.Name -replace 'image', $imgPrefix }
}
harrison314
Hardcore addict
Hardcore addict
Používateľov profilový obrázok
Príspevky: 8215
Registrovaný: 27 máj 2009, 20:42
Bydlisko: Bratislava
Kontaktovať používateľa:

Re: Java - exekucia PS

Príspevok od používateľa harrison314 »

Prve co pri takychto veciam skusam, tak sa pozeram ci cesty v skripte su take ako maju byt. Skus si ich zalogovat.
Mam podozrenie, ze za to moze to, ze spajas cesty cez +, skus to radsej takto:
Z

Kód: Vybrať všetko

$cssFile = $tempData + '\stylesheet.css'
Na

Kód: Vybrať všetko

$cssFile = Join-Path $tempData  'stylesheet.css'
Vyhnes sa problemom s lomitkami.
Napísať odpoveď