[Last Call] Learn how to a build a cloud-first strategyRegister Now


create uri to file in current working directory

Posted on 2014-08-12
Medium Priority
Last Modified: 2016-06-10
I’m trying to write a c# program using EPPlus (http://epplus.codeplex.com/) that generates an Excel worksheet with thumbnails of images and links to the full-sized image.  The images reside in the same folder as the executable and the resulting spreadsheet. It is all working OK with the following:
int a = 1;
Image fullSize = Image.FromFile(@"Screenshot.jpg");
Image.GetThumbnailImageAbort myCallback = new
Image thumb = fullSize.GetThumbnailImage(100, 71, myCallback, IntPtr.Zero);

var picture = worksheet.Drawings.AddPicture(a.ToString(), thumb);
picture.SetPosition(a-1 , 0, 6-1, 0);

worksheet.Cells[a, 6].Hyperlink = new Uri("file://c:/Screenshot.jpg");

Open in new window

What I want to do is make the whole thing location independent. As it is, the paths to the full-size jpgs are hard-coded. I would like them to be located wherever the current working folder is.

I have tried all sorts of things, and so far have been unable to get the .net Uri format to agree with Excel.

I did some digging and generated a sample spreadsheet in Excel and added a uri to a cell pointing to a jpg in the current folder. I then opened the .xls file with 7-zip (who knew you could do this?) and saw this snippet in sheet1.xml:
<hyperlinks><hyperlink ref="F2" r:id="rId1"/></hyperlinks>

Open in new window

In sheet1.xml.rels, I found this:
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="Screenshot3.jpg" TargetMode="External"/>

Open in new window

Moving the xls file to the desktop and opening it with Excel and clicking the hyperlink produces a “cannot open the specified file” error. Moving the jpg file to the desktop and then clicking the hyperlink works fine.

So, it appears that Excel’s file format allows for hyperlinks to a relative uri. The question is, how do I generate such a uri and hyperlink programmatically?

I'm stuck at something that really ought to be easy. The hacker in me says I should just script up something that disassembles the .xlsx file (I had no idea until today that you could do that with 7-zip), make the necessary changes and then zip it back up. But that's absolutely nuts, I think.

There's GOT to be a better way,
Question by:1_21gigawatts
  • 2
LVL 24

Expert Comment

ID: 40256980

Author Comment

ID: 40256989
causes this error:
  Invalid URI: A Dos path must be rooted, for example, 'c:\'.
LVL 24

Accepted Solution

SunBow earned 2000 total points
ID: 40257041


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Viewers will learn the basics of using filtering and sorting in Excel 2013.
Viewers will learn a basic relationship technique in Power Pivot for Excel 2013.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question