Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
0
Rayne
Asked:
Rayne
  • 16
  • 8
1 Solution
 
Dan CraciunIT ConsultantCommented:
Can you please provide sample data? http://sscce.org/

Thank you.

Dan
0
 
RayneAuthor Commented:
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
0
 
Dan CraciunIT ConsultantCommented:
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
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
RayneAuthor Commented:
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
0
 
RayneAuthor Commented:
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 :(
0
 
Dan CraciunIT ConsultantCommented:
Use the following script:
$inputXlsx= "X:\your\path\data-This-updated1.xlsx"
$workFile = "X:\your\path\data.csv"
$outputFile = "X:\your\path\mod_data.csv"
$output = @()                                            # initialize hash output

if(Test-Path $workFile) {Remove-Item $workFile -Force}  # delete work file, if present

$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open($inputXlsx)           # open the original xlsx
$sheet = $workbook.Worksheets.Item(1)                   # select the first sheet, just in case
$workbook.SaveAs($workFile, 6)                          # save as csv
$workbook.Close($false)                                 # close the file


#remove first 4 lines from file - unneeded headers
(Get-Content $workFile | Select-Object -Skip 4) | Set-Content $outputFile

# imports the generated csv, then constructs the output object
Import-Csv -path $outputFile -Header "District+item", h2, "City+item", "account+item", h5, "Q1 P1", "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 create 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 %

#output the final csv
$output | Export-Csv -Path $outputFile -Force -NoTypeInformation

Open in new window


Replace paths as needed.

The script will generate a file (mod_data.csv) ready to be imported into an SQL table.

You will need to create the database and the table to contain this, along with any other data you might need (I suspect you will need the date, at least).

The script removes all calculated rows/columns.
It's bad form to insert a calculated field into SQL, because:
1. you can simply use SELECT SUM(fields) to get the sum
2. if you alter/remove a field that was used to obtain the calculated field, that field loses meaning.

HTH,
Dan
0
 
RayneAuthor Commented:
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
0
 
Dan CraciunIT ConsultantCommented:
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
0
 
RayneAuthor Commented:
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

0
 
RayneAuthor Commented:
When I type in Y, it gives me this error
0
 
Dan CraciunIT ConsultantCommented:
You need to run Powershell as administrator to be able to run set-executionPolicy
0
 
RayneAuthor Commented:
i right on the pst file - where do i get the unblock thing?
0
 
RayneAuthor Commented:
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
0
 
RayneAuthor Commented:
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

0
 
RayneAuthor Commented:
the mod file is empty
0
 
Dan CraciunIT ConsultantCommented:
What operating system are you using?

Looks like the [Ordered] cast is not available in your powershell version.
0
 
RayneAuthor Commented:
Dan

I am Windows 7
0
 
RayneAuthor Commented:
wind 7 enterprice
0
 
RayneAuthor Commented:
64 bit
using.png
0
 
RayneAuthor Commented:
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...
0
 
Dan CraciunIT ConsultantCommented:
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
0
 
RayneAuthor Commented:
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
0
 
Dan CraciunIT ConsultantCommented:
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
0
 
RayneAuthor Commented:
Thank you Dan :) and yes I want to be in THE future
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 16
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now