Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Access Excel Hyperlinks from SQL Expand / Collapse
Author
Message
Posted Thursday, January 10, 2013 3:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:33 AM
Points: 14, Visits: 50
Hi,

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.
Philip

Post #1405321
Posted Thursday, January 10, 2013 5:15 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, July 26, 2014 8:57 AM
Points: 7,081, Visits: 12,575
C# is not an option

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:

using Microsoft.Office.Interop.Excel;

//...

public void Main()
{
    Microsoft.Office.Interop.Excel.Application excelApp
= new Microsoft.Office.Interop.Excel.Application();
    
excelApp.Visible = true;

    
Workbook workbook = excelApp.Workbooks.Open(@"C:\@\Book1.xlsx",
        
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    
// The key line:
    
Worksheet worksheet = (Worksheet)workbook.Worksheets["Sheet1"];

    
foreach(Range range in worksheet.Rows)
    
{
        
foreach (Hyperlink hyperlink in range.Hyperlinks)
        
{
            
string hl = hyperlink.Address;
        
}
    }

   Dts.TaskResult
= (int)ScriptResults.Success;
}







__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1405355
Posted Thursday, January 10, 2013 6:02 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:33 AM
Points: 14, Visits: 50
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


Post #1405370
Posted Thursday, January 10, 2013 6:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, July 26, 2014 8:57 AM
Points: 7,081, Visits: 12,575
Philip-1144230 (1/10/2013)
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



I would not recommend putting anything to do with Office interop into the SqlClr.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1405379
Posted Thursday, January 10, 2013 6:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:33 AM
Points: 14, Visits: 50
Would 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.
Post #1405385
Posted Thursday, January 10, 2013 7:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, July 26, 2014 8:57 AM
Points: 7,081, Visits: 12,575
Not 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.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1405408
Posted Thursday, January 10, 2013 7:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:33 AM
Points: 14, Visits: 50
Thank 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.

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.


Post #1405432
Posted Thursday, January 10, 2013 7:30 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 10, 2013 9:33 AM
Points: 14, Visits: 50
Disregard the previous question, answer is located here:
http://social.msdn.microsoft.com/Forums/en/sqlintegrationservices/thread/f863db04-c84e-4283-8e31-565b46f0803d

Would you consider SSIS to be a better practice?
Post #1405438
Posted Thursday, January 10, 2013 7:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, July 26, 2014 8:57 AM
Points: 7,081, Visits: 12,575
Sure. 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 SSIS packages can be securely executed using T-SQL. 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.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1405442
Posted Thursday, January 10, 2013 7:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, July 26, 2014 8:57 AM
Points: 7,081, Visits: 12,575
Philip-1144230 (1/10/2013)
Disregard the previous question, answer is located here:
http://social.msdn.microsoft.com/Forums/en/sqlintegrationservices/thread/f863db04-c84e-4283-8e31-565b46f0803d

Would you consider SSIS to be a better practice?

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.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1405456
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse