Help with EPPlus.dll and AHK Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Help with EPPlus.dll and AHK

10 Jul 2018, 11:48

TEMPLATING:
(Expanding a little on the June 26, 2018 post above).
You can load an existing .XLSX file and modify it, saving the result as XLSX.
Here is a basic script that loads a spreadsheet (XLSX), adds text to a new row in the worksheet, and saves it, again as XLSX.

Code: Select all

/*
EPPlus - TEMPLATING - Part One
This very simple script shows how to add rows to
existing XLSX files and save the result.
Along with tmplinshi's invaluable help, 
the script at https://stackoverflow.com/questions/9571581/epplus-how-to-use-a-template
was helpful as well(see the VB example by JamesCBaird).
The FileInfo() function is by tmplinshi.
EPPlus.dll needs to be in your script directory.
You'll also need "App 2.xlsx" (MUST NOT start blank. Needs at least one row to exist.)
Run the script several times to verify rows have been added.
*/


lib := Clr_LoadLibrary("mscorlib")

infile := FileInfo("App 2.xlsx")
outfile := infile
; modify the above if you want
; a different file to result.

asm := Clr_LoadLibrary(".\EPPlus.dll")
pck := Clr_CreateObject(asm, "OfficeOpenXml.ExcelPackage", infile)
ws := pck.Workbook.Worksheets.(1)
;grab the first worksheet
ws.Name := "AppTest"
;name or rename it.
nr := ws.Dimension.End.Row + 1
; 'nr' is be concatenated with the column
; letter to get the cell address.
ws.Cells.("A"nr).Value := "Hello World!"
ws.Cells.("A"nr).Style.Font.Size := 24
ws.Cells.("A"nr).Style.Font.Color.SetColor(0xFF0000)
ws.Cells.("A"nr).AutoFitColumns()

pck.SaveAs(outfile)


FileInfo(filename)
{
	static lib := Clr_LoadLibrary("mscorlib")
	return Clr_CreateObject(lib, "System.IO.FileInfo", filename)
}
Regards,
burque505
elmo
Posts: 113
Joined: 09 Oct 2013, 09:08

Re: Help with EPPlus.dll and AHK

21 Jul 2018, 18:41

@burque505 and @tmplinshi,

This looks really interesting, thank you so much for posting your findings.

pck.Workbook.Worksheets.(1) makes perfect sense for a single worksheet.

Do you know how to return a worksheet collection ?

I tried variations on that to create a collection to use in a for loop but everything I tried returns error regarding _NewEnum.

Thank you in advance for your time and attention.
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Help with EPPlus.dll and AHK

21 Jul 2018, 18:50

Hi elmo, I've been struggling with that very issue today as well, for a little project I'm working on to convert XLSX to CSV without Excel. Please let me know if you find a solution, and I'll certainly do the same!
Regards,
burque505
P.S. here's a teaser:
xlsx2csv.gif
xlsx2csv.gif (227.74 KiB) Viewed 4280 times
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Help with EPPlus.dll and AHK

21 Jul 2018, 22:50

@elmo

Code: Select all

Loop, % pck.Workbook.Worksheets.Count
{
	; ws := pck.Workbook.Worksheets.Item(A_Index)
	ws := pck.Workbook.Worksheets.(A_Index)
	MsgBox, % ws.ToString() ; Worksheet Name
}
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Help with EPPlus.dll and AHK

22 Jul 2018, 07:33

:bravo: tmplinshi to the rescue!!!!!
Thank you, my friend.
Regards,
burque505
elmo
Posts: 113
Joined: 09 Oct 2013, 09:08

Re: Help with EPPlus.dll and AHK

22 Jul 2018, 10:03

@tmplinshi and @burque505

Ah, makes good sense and so appreciate the timely response.

I found something similar last night at https://stackoverflow.com/a/48151113

Will test and report back on results here.

While I have you on the line @tmplinshi; may I trouble you for the source of that method?

I am still perusing the EPPlus github and did not see the ".Count" or ".ToString()"; I must be looking in the wrong place.

Again, thank you for your help.
elmo
Posts: 113
Joined: 09 Oct 2013, 09:08

Re: Help with EPPlus.dll and AHK

22 Jul 2018, 11:40

@tmplinshi

Your solution works perfectly.

Also works with ws.Name in place of ws.ToString() for WorkSheet name.

Thank you.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Help with EPPlus.dll and AHK

22 Jul 2018, 12:08

I am using dotPeek to view the methods. (Screenshot)

Didn't know ws.Name, thanks.
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Help with EPPlus.dll and AHK

22 Jul 2018, 12:27

I've been using dnspy after seeing a tip somewhere from qwerty12.
In the hope it'll help, here's some barebones code for opening an Excel workbook and converting it to CSV (I'm working on a GUI, almost done with the barebones version). As you can see, it really doesn't take much code at all for a simple conversion.

Code: Select all

#Persistent

global infile
global outfile
global row := 0
global col := 0
global tempStr := ""
global delim := ","
global sheetnum

FileSelectFile, infile, , %A_ScriptDir%, , XLSX (*.xlsx)
FileSelectFile, outfile, S8, %A_ScriptDir%, ,CSV - (*.csv) or other. No default extension!

;~ InputBox, infile, Choose XLSX, Choose workbook
;~ InputBox, outfile, File Out, File out w/extension

delim := ","

asm := Clr_LoadLibrary(".\EPPlus.dll")
pck := Clr_CreateObject(asm, "OfficeOpenXml.ExcelPackage", FileInfo(infile))
global sheetcount := pck.Workbook.Worksheets.Count
global ws := pck.Workbook.Worksheets.(1)
msgbox % ws.Name

global cn := ws.Dimension.End.Column
;msgbox End column is %cn%   		;uncomment for debug
global rn := ws.Dimension.End.Row
;msgbox End row is %cn%				;uncomment for debug


loop, %rn%
{
	tempStr := ""
	row +=1
	proccol()
}

proccol()
{
	Loop, %cn% ; cn = number of columns
	{
		col += 1 ; started at 0, so first is 1
		If (col == cn) ; i.e. if this is the last column
			tempStr .= ws.GetValue(row, col) ; no delimiter
		Else ; otherwise we want the delimiter
		{
			tempStr .= ws.GetValue(row, col)
			tempStr := tempStr . delim
			;msgbox %tempStr% ; uncomment to see how the string is processed
		}
	}
	col := 0
	If (row < rn)
		{
		FileAppend, %tempStr%`r`n, %outfile%
		}
	else
		{
		FileAppend, %tempStr%, %outfile% ; last row? no delimiter.
		}
}
msgbox Done

ExitApp

FileInfo(filename)
{
	static lib := Clr_LoadLibrary("mscorlib")
	return Clr_CreateObject(lib, "System.IO.FileInfo", filename)
}

Escape::ExitApp
Next is something to compete with xlsx2csv.py :)
Thanks, @elmo and @tmplinshi.
Regards,
burque505
elmo
Posts: 113
Joined: 09 Oct 2013, 09:08

Re: Help with EPPlus.dll and AHK

22 Jul 2018, 16:06

@tmplinshi and @burque505

Appreciate insight on dotPeek and dnspy; will try those out. Looks like your xlsx2csv is coming along nicely.

Unfortunately, I have discovered there were older .xls files in my target WorkBook collection which EPPlus can not interpret because no XML. Guess I'll have to stick with COM for those :cry:

Thanks.
Last edited by elmo on 09 Oct 2018, 10:54, edited 1 time in total.
Invitro
Posts: 12
Joined: 09 Oct 2018, 08:51

Re: Help with EPPlus.dll and AHK

09 Oct 2018, 10:46

I tried the example. I downloaded the 7zip folder and unpacked it. I tried the example AHK, but I am getting the following error message: Error: Call to nonexistent function.
Specifically: Clr_LoadLibrary
2018-10-09 17_45_18-EPPlus Test.png
(12.55 KiB) Downloaded 76 times
The Path to the EPPlus.dll is corret and Net Framework is installed.
Could someone help me? :)
With best regards,
Alex
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Help with EPPlus.dll and AHK

09 Oct 2018, 11:26

@Invitro, it seems to me like you don't have CLR.ahk in your path anywhere, which might be at fault. I'm attaching a copy just in case. Otherwise it looks like your code should work. Let me know.
You can put CLR.ahk in the same folder and add

Code: Select all

#Include CLR.ahk
at the top.
Regards,
burque505
CLR.ahk
(5.35 KiB) Downloaded 97 times
Invitro
Posts: 12
Joined: 09 Oct 2018, 08:51

Re: Help with EPPlus.dll and AHK

09 Oct 2018, 11:40

:bravo: IT WORKED!!! Thank you so much :)
Invitro
Posts: 12
Joined: 09 Oct 2018, 08:51

Re: Help with EPPlus.dll and AHK

10 Oct 2018, 13:59

According to the web it is not possible with EPPlus to save a XLSX as PDF.

I have managed to create a pdf from an Excel sheet via com, but I have to open the Excel Sheet first. Would it also be possible to print/save an Excel sheet directly as a PDF without opening it?


For these examples to work, I have to open the Excel File:

Code: Select all

excel := Active("Excel.Application")
excel.Worksheets("sheet1").ExportAsFixedFormat(0,TargetFolder,0,False,False,1,1,False) 
return

Code: Select all

Loop, c:\intel\test.xlsx   
{
	excel := ComObjGet(A_LoopFileFullPath)  
	excel.Worksheets("sheet1").ExportAsFixedFormat(0,TargetFolder,0,False,False,1,1,False) 
 }
 return
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Help with EPPlus.dll and AHK

10 Oct 2018, 14:20

@Invitro, excellent question. That hadn't occurred to me, and I don't have an answer. I will look into it.
Regards,
burque505
Edit: Just this CodeProject link and this solution which requires a paid component so far.
Very interesting problem.
Invitro
Posts: 12
Joined: 09 Oct 2018, 08:51

Re: Help with EPPlus.dll and AHK

10 Oct 2018, 14:55

That would be so great! I am fairly new to AHK and scripting and at my wit's end ;-)
I don't know if this is interesting for you, but I am using Windows 10, Office 2016 and "Microsoft Print to PDF" is installed. Not a very neat solution, but at some point I was thinking I would at least be able to use the Microsoft Printer. However that was a dead end, too.
Last edited by Invitro on 10 Oct 2018, 15:02, edited 1 time in total.
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Help with EPPlus.dll and AHK

10 Oct 2018, 15:00

Hi, I believe I am close to a solution. It may take me a day or two, and I know it will require downloading some free tools. But I'm almost sure it can be done. Fingers crossed. Thanks for bringing this up, by the way!
Regards,
burque505
Invitro
Posts: 12
Joined: 09 Oct 2018, 08:51

Re: Help with EPPlus.dll and AHK

10 Oct 2018, 15:03

Thanks for helping me! You already made my day! :-)
Invitro
Posts: 12
Joined: 09 Oct 2018, 08:51

Re: Help with EPPlus.dll and AHK

27 Oct 2018, 04:27

burque505 wrote:
10 Oct 2018, 14:20
@Invitro, excellent question. That hadn't occurred to me, and I don't have an answer. I will look into it.
Regards,
burque505
Edit: Just this CodeProject link and this solution which requires a paid component so far.
Very interesting problem.

I tried the second solution with the shareware converter, because the limitation is no problem for my little project. I only need one page to convert to pdf. However I have to install it. I hoped it would work without an installation. Does someone know an similar solution? I wasn't able to find anything.

With best regards,
Alex

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 302 guests