How can I extract a particular worksheet in an xlsx spreadsheet as a text file?

Sam Alleman

I need to extract a worksheet called Approval_Logs from a few different xlsx spreadsheets and convert them to text files.

I've done this under linux using gnumeric's ssconvert command, but I haven't found a way to do this in windows, and I'm trying to make a user-friendly powershell script to automate this task.

Thanks!

René Nyffenegger

The following snippet might give you a starting point.

You will probably have to find the correct path of the Interop dll on your hardrive and then adjust the add-type cmdlet.

$dir is the directory from which the workbooks are opened and into which they're written.

$dir = convert-path ~/ZZZ/Excel/Export-CSV/

add-type -path 'C:\Program Files (x86)\Microsoft Office\Office16\DCF\Microsoft.Office.Interop.Excel.dll'

$xls = new-object Microsoft.Office.Interop.Excel.ApplicationClass
$xls.visible       = $true
$xls.displayAlerts = $false

foreach ($wbFile in get-childItem $dir\*.xls*) {
   $wb = $xls.workbooks.open($wbFile.fullName)

   try {
      $sh = $wb.sheets('Approval_Logs')
   }
   catch {
      if ($_.exception.message -match 'Invalid index.') {
         write-host "Expected sheet not found in $($wb.name)"
         $wb.close()
         continue
      }
      throw $_
   }

   $sh.select()

   $csvFile = "${dir}$($wbFile.basename).csv"
   $wb.saveAs($csvFile, 6, $false)
   write-host "$csvFile was saved"

   $wb.close()

}

$xls.quit()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use phpoffice to extract a specific worksheet from an .xlsx file and create a new .xlsx from it?

How can I automatically organize data from a text file in a spreadsheet?

How can I insert an existing Google Worksheet into a Google Spreadsheet?

How can I find the particular file in a Project/Folder in Sublime Text

How can i remove a particular text from a file in java?

How can I scrape an embedded image url from xlsx spreadsheet

Extract Text File data to worksheet

How can I extract the text between two strings in a log file?

How can I extract text before a character or string in a batch file?

How can I extract these characters from a text file?

How can I extract data from text file?

How can I extract a text from a bytes file using python

How can I extract certain portions of all lines in a text file?

How I can extract specific target number from text file

How can I extract a portion of text from all lines of a file?

How can I used XSLT to add alt text to an image file using a spreadsheet?

How to read a xlsx worksheet file in tcl?

How to extract particular paragraph in text file using python?

How can i replace particular characters in a file

How do I extract particular name from a file name in shell

How can I find both identical and similar strings in a particular field in a text file in Linux?

How can I hide a file with a particular name from the sidebar in Sublime Text?

How can I search for and extract a particular value from a dataframe in python?

How can I use generics to extract all values of a particular type?

How can I extract a particular link from a string of HTML in JavaScript?

How can I clip text in in cell in spreadsheet with google apps script?

How can I covert .tsv file to xlsx file using vbscript?

Can I batch update multiple worksheet in a spreadsheet using gspread?

How can I extract text and lists with BeautifulSoup