SQLServerCentral.com / SQL Server 2008 - General / SQL Server 2008 / Access Excel Hyperlinks from SQL / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comSun, 02 Aug 2015 12:41:05 GMT20RE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspx[quote][b]Greg Snidow (1/11/2013)[/b][hr][quote][b]opc.three[/b]The C# code I provided does exactly that[/quote]Ah, my bad then. I'm C# illiterate (and all flavors of C for that matter). Now that you pointed that out, I can kind of see how it works. So I'm using SQL Server 2012 express (my company does not use SQL Server, I just use it for handling my data). Is it possible for me to do things like you have done above? Or do I need a paid version to be able to use CLR's and such?[/quote]No worries. I get that C# is not read as cleanly (or sometimes at all) on this site as T-SQL ;-) I would not recommend using the code I showed in a SQLCLR object. If you [i]must[/i] something like this that deals with hyperlinks in Excel, I would look to put the code in an SSIS package Script Task or Transformation Component, or into a stand-alone C# app, and run it on an application server, i.e. not the server running SQL Server hosting the databases.Fri, 11 Jan 2013 08:56:17 GMTOrlando ColamatteoRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspx[quote][b]opc.three[/b]The C# code I provided does exactly that[/quote]Ah, my bad then. I'm C# illiterate (and all flavors of C for that matter). Now that you pointed that out, I can kind of see how it works. So I'm using SQL Server 2012 express (my company does not use SQL Server, I just use it for handling my data). Is it possible for me to do things like you have done above? Or do I need a paid version to be able to use CLR's and such?Fri, 11 Jan 2013 07:42:21 GMTGreg SnidowRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxHi Guys,I've gone back to the client with regards to it specifically being a sql procedure that needs to do the import.Waiting on their response now before I go any further with this.Thanks for all the responses up till now.Fri, 11 Jan 2013 01:13:30 GMTPhilip-1144230RE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspx[quote][b]Greg Snidow (1/10/2013)[/b][hr][quote][b]Philip-1144230 (1/10/2013)[/b]the other two would require me to manually go through roughly 12000 excel files with a average of 15 worksheets each to extract the hyperlinks[/quote]Philip, it sounds like you have already resolved to make some changes regarding future requests, but that you still have to solve this problem. VBA is very powerful, and from it you can do any kind of file handling. It would be relatively easy to loop through your files, flip through the pages, and expose the links. Granted, it would probably take a little while for the script to run. This would be a good option, *if* the links are *always* in the *exact* same location. If you want to go this route, there are lots of resources out there to help get you started.[/quote]The C# code I provided does exactly that (flips through all cells in a Worksheet). And you are right, it is painfully slow. If you know the column and defend against the possibility of there not being a hyperlink on the cell it's not tough code to write and could perform acceptably if the scope were limited. The question of where to host the code though, that's the big decision on this one in my opinion.Thu, 10 Jan 2013 09:24:12 GMTOrlando ColamatteoRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspx[quote][b]Philip-1144230 (1/10/2013)[/b]the other two would require me to manually go through roughly 12000 excel files with a average of 15 worksheets each to extract the hyperlinks[/quote]Philip, it sounds like you have already resolved to make some changes regarding future requests, but that you still have to solve this problem. VBA is very powerful, and from it you can do any kind of file handling. It would be relatively easy to loop through your files, flip through the pages, and expose the links. Granted, it would probably take a little while for the script to run. This would be a good option, *if* the links are *always* in the *exact* same location. If you want to go this route, there are lots of resources out there to help get you started.Thu, 10 Jan 2013 09:19:13 GMTGreg SnidowRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxAnytime, good luck. If you have a moment to circle back, I would be interested to hear about where you landed, and how you got there. Of course if you hit a snag post back, or make a new thread. Here to help.Thu, 10 Jan 2013 08:13:27 GMTOrlando ColamatteoRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxValuable input is always welcome.Seems I'll have to complete some SQL courses in the near future along with a lot of practical examples before I take on another SQL based project.That said, I'll still have to solve this one.Thanks again for all the help, appreciate it.Thu, 10 Jan 2013 07:55:11 GMTPhilip-1144230RE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspx[quote][b]Philip-1144230 (1/10/2013)[/b][hr]Disregard the previous question, answer is located here:[url]http://social.msdn.microsoft.com/Forums/en/sqlintegrationservices/thread/f863db04-c84e-4283-8e31-565b46f0803d[/url]Would you consider SSIS to be a better practice?[/quote]I would not put much stock in that post. The suggestion that macros are somehow safer than accessing a Worksheet using the Excel object is unfair. They are both dangerous. As I said in my previous post I would not want to host this operation on the server hosting my databases. Steer the requirement provider towards putting this onto an application server and keep it away from and out of SQL Server.Thu, 10 Jan 2013 07:39:36 GMTOrlando ColamatteoRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxSure. I developed the C# code I posted inside an SSIS Script Task. You'll just need to add a reference to Microsoft.Office.Interop.Excel.dll in the Script Task VSTA Project (what opens when you click Edit Script) to get started.That said, I would urge you to challenge the piece of the requirement saying this functionality must be available by calling a stored procedure unless they want to move to SQL 2012 where [u][url=http://msdn.microsoft.com/en-us/library/ff878034.aspx]SSIS packages can be securely executed using T-SQL[/url][/u]. Kicking off an SSIS package in SQL 2008 R2 and below from within a T-SQL stored procedure implies you'll have to enable xp_cmdshell to be able to call dtexec.exe and that is a dealbreaker in my book. The lesser of the evils would be to create a SQLCLR proc marked for EXTERNAL_ACCESS that will call dtexec.exe.Thu, 10 Jan 2013 07:31:42 GMTOrlando ColamatteoRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxDisregard the previous question, answer is located here:[url]http://social.msdn.microsoft.com/Forums/en/sqlintegrationservices/thread/f863db04-c84e-4283-8e31-565b46f0803d[/url]Would you consider SSIS to be a better practice?Thu, 10 Jan 2013 07:30:12 GMTPhilip-1144230RE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxThank you, Appreciate the effort.Taking into consideration what you've just said, it's forcing me to rethink what I am trying to achieve and that's always a good thing. :unsure:It does however enforce my original post. What I am creating now will be used in future to import excel documents into their database and on their demand has to be a sql procedure, which does limit my options considerably.Would I be able to access a cells hyperlink through SSIS?Thanks again.Thu, 10 Jan 2013 07:22:27 GMTPhilip-1144230RE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxNot at all. My response was admittedly a bit Spartan.Office objects are designed to be used on a client system, not a server, and they are notorious for having memory problems and can make any system unstable. This is not to mention that using them would force you to mark your SQLCLR objects UNSAFE, which I never condone. The SQLCLR is most conservatively used to extend the Transact-SQL language (i.e. all objects can be marked SAFE) and do not need to reach anything outside the database engine itself (i.e. no need to access the file system, the internet, network resources, DLLs or assemblies external to what the SQLCLR provides by default etc.). SQLCLR is not an open offer to implement full-scale .NET functionality that would access resources outside the database engine. If you are developing an object and find it needs to be marked EXTERNAL_ACCESS or UNSAFE it is a sign you should go back to the drawing board and try to find a different way. I may hold an overly conservative view on the topic according to some but I prefer to keep servers hosting applications (.NET, Java, etc.) and servers that host SQL Server databases on distinct and separate physical entities (servers) that do not share any resources. Resist the urge to turn SQL Server into an application server; that is what Windows and stand-alone .NET applications (of which SSIS is a type) were designed for.Thu, 10 Jan 2013 07:01:45 GMTOrlando ColamatteoRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxWould you mind elaborating?I have never worked with CLR Procedures, I've only read about them and the possibilities that they provide.I'm quite new to SQL and all it's abilities and would like to gain understanding other than just a plain "how to" or "not quite" if that makes sense to you.Apologies if I am being a nuisance.Thu, 10 Jan 2013 06:33:36 GMTPhilip-1144230RE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspx[quote][b]Philip-1144230 (1/10/2013)[/b][hr]Hey opc.three,Thanks for the reply.This sparked anther thought that I would like to run by you and anyone else who might have more insight into CLR Procedures.Would it be possible to use a CLR Procedure to copy the hyperlink text to another cell for each row and then save the excel file?Regards,Philip[/quote]I would not recommend putting anything to do with Office interop into the SqlClr.Thu, 10 Jan 2013 06:22:39 GMTOrlando ColamatteoRE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxHey opc.three,Thanks for the reply.This sparked anther thought that I would like to run by you and anyone else who might have more insight into CLR Procedures.Would it be possible to use a CLR Procedure to copy the hyperlink text to another cell for each row and then save the excel file?Regards,PhilipThu, 10 Jan 2013 06:02:26 GMTPhilip-1144230RE: Access Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxC# is not an option :crying:Here is a mockup of some code for a Script Task that reads a Worksheet row-by-row and accesses the Hyperlinks collection for each row. I am no Excel expert and this only took me about 15 minutes to mock up. I am sure it could be easily extended to suit your needs:[font="Courier New"][size="2"][color="blue"]using [/color][color="black"]Microsoft.Office.Interop.Excel[/color][color="gray"];[/color][color="green"]//...[/color][color="blue"]public void [/color][color="black"]Main[/color][color="gray"]()[/color][color="black"]{&#160;&#160;&#160;&#160;Microsoft.Office.Interop.Excel.Application excelApp [/color][color="blue"]= new [/color][color="black"]Microsoft.Office.Interop.Excel.Application[/color][color="gray"]();&#160;&#160;&#160;&#160;[/color][color="black"]excelApp.Visible [/color][color="blue"]= true[/color][color="gray"];&#160;&#160;&#160;&#160;[/color][color="black"]Workbook workbook [/color][color="blue"]= [/color][color="black"]excelApp.Workbooks.Open[/color][color="gray"]([/color][color="darkred"]@&quot;C:\@\Book1.xlsx&quot;[/color][color="gray"],&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"], [/color][color="black"]Type.Missing[/color][color="gray"]);&#160;&#160;&#160;&#160;[/color][color="green"]// The key line:&#160;&#160;&#160;&#160;[/color][color="black"]Worksheet worksheet [/color][color="blue"]= [/color][color="gray"]([/color][color="black"]Worksheet[/color][color="gray"])[/color][color="black"]workbook.Worksheets[&quot;Sheet1&quot;][/color][color="gray"];&#160;&#160;&#160;&#160;[/color][color="blue"]foreach[/color][color="gray"]([/color][color="black"]Range range [/color][color="blue"]in [/color][color="black"]worksheet.Rows[/color][color="gray"])&#160;&#160;&#160;&#160;[/color][color="black"]{&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[/color][color="blue"]foreach [/color][color="gray"]([/color][color="black"]Hyperlink hyperlink [/color][color="blue"]in [/color][color="black"]range.Hyperlinks[/color][color="gray"])&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[/color][color="black"]{&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[/color][color="blue"]string [/color][color="black"]hl [/color][color="blue"]= [/color][color="black"]hyperlink.Address[/color][color="gray"];&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[/color][color="black"]}&#160;&#160;&#160;&#160;}&#160;&#160; Dts.TaskResult [/color][color="blue"]= [/color][color="gray"]([/color][color="blue"]int[/color][color="gray"])[/color][color="black"]ScriptResults.Success[/color][color="gray"];[/color][color="black"]}[/color][/size][/font]Thu, 10 Jan 2013 05:15:13 GMTOrlando ColamatteoAccess Excel Hyperlinks from SQLhttp://www.sqlservercentral.com/Forums/Topic1405321-391-1.aspxHi,I have to import excel documents into a SQL table, that part is simple enough and can easily be achieved by using OPENROWSET().The problem I am having is that the client wants to see the hyperlinks they have applied to cell as an extra column in the table:Example:[sheet1]Cell A1: Contains Word "Google" and has a hyperlink that opens "http://www.google.com/"Cell A2: Contains Word "Bing" and has a hyperlink that opens "http://www.bing.com/"I need to insert this in to table "Search Engines"Column 1 (row 1)= 'Google'Column 2 (row 1)= 'http://www.google.com/'Column 1 (row 2)= 'Bing'Column 2 (row 2)= 'http://www.bing.com/'I can find the following possible solutions when I Google the problem:VB Macro to expose the hyperlink in another cell inside the workbook.Manually copy and pasting the hyperlink into new cells.c# code to do the import.The problem with these solutions are:C# is not a option.and the other two would require me to manually go through roughly 12000 excel files with a average of 15 worksheets each to extract the hyperlinks.Is there anyway to get hold of the hyperlink using SQL or for that matter any file I can execute via sqlcmd on the excel workbooks to copy the hyperlinks to another cell without having to manually open and edit each book?Thank in advance.PhilipThu, 10 Jan 2013 03:44:56 GMTPhilip-1144230