Link to home
Start Free TrialLog in
Avatar of Rayne
RayneFlag for United States of America

asked on

daily trick for xls files

To My Respected SQL Server and Scripting Gurus,
I am biting my nailes if this is possible - There is a daily xls file that is system generated. I want it to be unmerged (since it comes that with merged columns) – the goal – using the power of sql server – I want to make it daily task that does this – let me know if this is impossible and I am just dreaming—
Read the xls file and then unmerge all the columns and then import that table into a dest table in sql server – I want to do it via sql server as it is very easy to set it daily- I am open to any suggestion as long as I can automate the daily unmerging of the xls file and then importing that thing into a dest table in sql server
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Can you please provide sample data? http://sscce.org/

Thank you.

Dan
Avatar of Rayne

ASKER

Hello Dan,

i have attached the before  and after look alike worksheet  - please see it

1) Unmerge the entire sheet

2) delete empty columns B & E


3)then, move the summing cell content to the right cell if right cell is empty (see the red fonted text) in its new position

4) move all the relevant period and quarter total headers in one single row
data-This.xlsx
OK, the simplest way I can think of to do this is:
1. Convert the file to csv
2. delete all the headers - first 6 lines
3. delete all the lines that contain "Total" - no point in inserting calculated fields in db

After that, you can simply import the csv into your prepared db (with the same number of columns) or add a header to match db table column names.

I'll try to make today a powershell script that will do 1-3.

Dan
Avatar of Rayne

ASKER

Hello Dan,

thank you for kindness :)
 One thing I updated the data - so that it has the actual entire year set  - just few extra  columns for all months
data-This-updated1.xlsx
Avatar of Rayne

ASKER

Dan,

I am not sure on the "remove totals" thing and if user need it- may be we can have a version that doesn't delete the totals :(
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rayne

ASKER

Hello Dan,

first of all, thank you so much for your gold help.

Just a note: I am getting this error when I ma running in powerShell GUI -
File C:\Users\AYY\AppData\Local\Temp\5deefc25-4262-46d9-849e-31dd4b76c475.ps1 cannot be loaded because the execution of
scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:2
+ . <<<<  'C:\Users\AYY\AppData\Local\Temp\5deefc25-4262-46d9-849e-31dd4b76c475.ps1'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException
As the error says, you should try to run get-help about_signing in Powershell.

Or, if you want to just skip to the good part, run this:
Set-ExecutionPolicy RemoteSigned

Open in new window


Afterwards, right click on your ps1 file and choose "Unblock".

Dan
Avatar of Rayne

ASKER

Hello Dan,

I tried to do this:

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\ThisUser> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft
.PowerShell' is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<<  RemoteSigned
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyComma
   nd

PS C:\Users\ThisUser> Set-ExecutionPolicy RemoteSigned

Open in new window

Avatar of Rayne

ASKER

When I type in Y, it gives me this error
You need to run Powershell as administrator to be able to run set-executionPolicy
Avatar of Rayne

ASKER

i right on the pst file - where do i get the unblock thing?
Avatar of Rayne

ASKER

Hello Dan,

I did changed it via Admin mode. Then I copied your code ( with changed the folders and file name) in to a powershell admin windows and this is what get>>

Note_ i have attached the two files that i got from it
data.csv
mod-data.csv
Avatar of Rayne

ASKER

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\WINDOWS\system32> $inputXlsx= "C:\Users\ThisUser\Pictures\test\data-This-updated1.xlsx"
PS C:\WINDOWS\system32> $workFile = "C:\Users\ThisUser\Pictures\test\data.csv"
PS C:\WINDOWS\system32> $outputFile = "C:\Users\ThisUser\Pictures\test\mod_data.csv"
PS C:\WINDOWS\system32> $output = @()                                            # initialize hash output
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> if(Test-Path $workFile) {Remove-Item $workFile -Force}  # delete work file, if present
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> $excel = New-Object -ComObject Excel.Application
PS C:\WINDOWS\system32> $workbook = $excel.Workbooks.Open($inputXlsx)           # open the original xlsx
PS C:\WINDOWS\system32> $sheet = $workbook.Worksheets.Item(1)                   # select the first sheet, just in case
PS C:\WINDOWS\system32> $workbook.SaveAs($workFile, 6)                          # save as csv
PS C:\WINDOWS\system32> $workbook.Close($false)                                 # close the file
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> #remove first 4 lines from file - unneeded headers
PS C:\WINDOWS\system32> (Get-Content $workFile | Select-Object -Skip 4) | Set-Content $outputFile
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> # imports the generated csv, then constructs the output object
PS C:\WINDOWS\system32> Import-Csv -path $outputFile -Header "District+item", h2, "City+item", "account+item", h5, "Q1 P
1", "Q1 P2", "Q1 P3", "Q1 total", "Q2 P4", "Q2 P5", "Q2 P6", "Q2 total", "Q3 P7", "Q3 P8", "Q3 P9", "Q3 total", "Q4 P10"
, "Q4 P11", "Q4 P12", "Q4 total" |
>>     %{ if (!($_."City+item" -like '*Total*')){
>>         $output += [psCustomObject] [Ordered] @{
>>             "District+item" = $_."District+item";
>>             "City+item"     = $_."City+item";
>>             "account+item"  = $_."account+item";
>>             "Q1 P1"         = $_."Q1 P1" -replace "\$", "" -replace ",", "" -replace " ", "";   # delete "$", "," and
 " " from numbers
>>             "Q1 P2"         = $_."Q1 P2" -replace "\$", "" -replace ",", "" -replace " ", "";   # probably should cre
ate a function
>>             "Q1 P3"         = $_."Q1 P3" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q2 P4"         = $_."Q2 P4" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q2 P5"         = $_."Q2 P5" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q2 P6"         = $_."Q2 P6" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q3 P7"         = $_."Q3 P7" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q3 P8"         = $_."Q3 P8" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q3 P9"         = $_."Q3 P9" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q4 P10"         = $_."Q4 P10" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q4 P11"         = $_."Q4 P11" -replace "\$", "" -replace ",", "" -replace " ", "";
>>             "Q4 P12"         = $_."Q4 P12" -replace "\$", "" -replace ",", "" -replace " ", ""
>>             } # end @
>>         } #end if
>>       } # end %
>>
Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
At line:3 char:46
+         $output += [psCustomObject] [Ordered] <<<<  @{
    + CategoryInfo          : InvalidOperation: (Ordered:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

PS C:\WINDOWS\system32> #output the final csv
PS C:\WINDOWS\system32> $output | Export-Csv -Path $outputFile -Force -NoTypeInformation
PS C:\WINDOWS\system32>

Open in new window

Avatar of Rayne

ASKER

the mod file is empty
What operating system are you using?

Looks like the [Ordered] cast is not available in your powershell version.
Avatar of Rayne

ASKER

Dan

I am Windows 7
Avatar of Rayne

ASKER

wind 7 enterprice
Avatar of Rayne

ASKER

64 bit
using.png
Avatar of Rayne

ASKER

Hello Dan,

I greatly appreciate the level of handwork you put in. Thank you. Meanwhile the issue is still there. Should I open a second question for that - whichever  - I will do it - please suggest...
You need Powershell 3 for the [Ordered] cast to work.
While I can modify the script so it does not need that construct, it's a good idea for you anyway to upgrade to Powershell 3.

Get the WMF 3 from here: http://www.microsoft.com/en-us/download/details.aspx?id=34595

Quick notes:
-  on line 19 you might need to modify the 4 to 6 - number of header lines in your file. Let me know if that can change, so I put it as a variable
- the script will convert to csv whatever sheet you saved as active in your excel file. If you have multiple sheets, let me know so I can modify it to select the right sheet before export.

HTH,
Dan
Avatar of Rayne

ASKER

Hello Dan,

I can't thank you enough - this is not just a brilliant life saver but  might also tempt me to get in powershell direction than a simple but less powerful vbscript for all things.

This change might ultimately be a life changer for me. You have now shown me the torch -  thank you Sire :) for opening my eyes to the world of scripting
You can use vbscript for the same end result, but I find Powershell to be faster (for me) to write and it is the recommended MS way for scripting.
I don't think shell scripting and vb scripting will go away anytime soon, but Powershell is the future.

Glad I could help.

Dan
Avatar of Rayne

ASKER

Thank you Dan :) and yes I want to be in THE future