Add hyper link in Excel using EPPlus

Many People has asked me that how they can add hyper link to another sheet through EPPlus. This is bit tricky because ws.Cells[1,1].Hyperlink takes Uri object which is generally used for external references only (file or website).

So after doing bit RnD I have developed a function through which you can easily add hyperlinks within your sheets. Here is the function:-

/// 
/// Adds the hyper link.
/// 
/// "ws">The ws.
/// "source">The source.
/// "destination">The destination.
/// "displayText">The display text.
private static void AddHyperLink(ExcelWorksheet ws, ExcelRange source, ExcelRange destination, string displayText)
{
	source.Formula = "HYPERLINK(\"[\"&MID(CELL(\"filename\"),SEARCH(\"[\",CELL(\"filename\"))+1, SEARCH(\"]\",CELL(\"filename\"))-SEARCH(\"[\",CELL(\"filename\"))-1)&\"]" + destination.FullAddress + "\",\"" + displayText + "\")";
}
Here is how you can call above function to add hyperlinks in your cell:-

ExcelWorksheet ws = CreateSheet(p,"Sample Sheet");
ExcelWorksheet sheet2 = CreateSheet(p, "Sample Sheet 2");
sheet2.Cells[1, 1].Value = "Hyperlink Target";
AddHyperLink(ws, ws.Cells[13, 1], sheet2.Cells[1,1], "Sample Hyperlink");
Hopefully this tip will be useful for you, Happy Coding !!!

Presentations - Introduction to ASP .NET MVC

Last month I was invited at PNSC (Pakistan National Shipping Corporation) to have a session related to ASP .Net MVC 3. It was really a wonder full experience to share the knowledge with other persons. You can find the slides shared during session below:-

Also have a look at some interesting moments:-




Get List of Process with Process Owner/User

Recently I have come to a situation where I want to list all the process in the computer. Solution was quite simple using Process.GetProcess().

Process[] processlist = Process.GetProcesses();
 
foreach (Process theprocess in processlist)
{
 Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}


However I also want to see that process is created with which user. For this after some googling, I found this solution. Hopefully it will be useful for you happy coding !!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Management;
using System.Runtime.InteropServices;
 
namespace ProcessViewer
{
 class Program
 {
  static void Main(string[] args)
  {
   GetProcessInfo();
  }
 
  /// Gets the process info.
  static void GetProcessInfo()
  {
   foreach (var process in Process.GetProcesses())
   {
    string name = process.ProcessName;
    int processId = process.Id;
    string windowTitle = process.MainWindowTitle ?? "N/A";
 
    Console.Out.WriteLine(string.Format("{0,20}|{1,10}|{2,20}|{3,20}",
     name,
     processId,
     GetProcessOwner(processId),
     windowTitle));
   }
   Console.ReadKey();
  }
 
  /// Gets the process owner.
  static string GetProcessOwner(int processId)
  {
   string query = "Select * From Win32_Process Where ProcessID = " + processId;
   ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(query);
   ManagementObjectCollection moCollection = moSearcher.Get();
 
   foreach (ManagementObject mo in moCollection)
   {
    string[] args = new string[] { string.Empty };
    int returnVal = Convert.ToInt32(mo.InvokeMethod("GetOwner", args));
    if (returnVal == 0)
     return args[0];
   }
 
   return "N/A";
  }
 }
}

Article of the Day! jQGrid and MVC 3 with Custom Paging and Custom Search Filters

I am quite pleased to announce that on 25th April 2012, my article jQGrid and MVC 3 with Custom Paging and Custom Search Filters was selected as Article of the Day on forums.asp.net. For details please visit:-


http://www.asp.net/community/articles/


I am among top 125 contributors around the world in forums.asp.net

I am quite pleased to share that I have received All-Star rank in forums.asp.net. See this:-

It was really wonderful experience while posting in forums. It helped me a lot in gaining in-depth knowledge of ASP .Net technologies. I recommend all other developers to participate in such activity.

You can also checkout my profile at forums.asp.net.

Happy coding !!!