|
|
|
Grasshopper
      
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 6,737,
Visits: 11,791
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Grasshopper
      
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 6,737,
Visits: 11,791
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Grasshopper
      
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.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 6,737,
Visits: 11,791
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Grasshopper
      
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 9:33 AM
Points: 14,
Visits: 50
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 6,737,
Visits: 11,791
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 6,737,
Visits: 11,791
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|