How to – Update Cell value in Excel Spreadsheet using C# (Open XML and EPPlus library)

Sharing a sample code that updates a particular cell’s value in Excel Spreadsheet.

Here we are updating cell B4

First using Open XML SDK

<p>public static void UpdateExcelUsingOpenXMLSDK(string fileName)<br />
{<br />
// Open the document for editing.<br />
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(fileName, true))<br />
{<br />
// Access the main Workbook part, which contains all references.<br />
WorkbookPart workbookPart = spreadSheet.WorkbookPart;<br />
// get sheet by name<br />
Sheet sheet = workbookPart.Workbook.Descendants&lt;Sheet&gt;().Where(s =&gt; s.Name == "Sheet1").FirstOrDefault();</p>
<p>// get worksheetpart by sheet id<br />
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id.Value) as WorksheetPart;</p>
<p>// The SheetData object will contain all the data.<br />
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild();</p>
<p>Cell cell = GetCell(worksheetPart.Worksheet, "B", 4);</p>
<p>cell.CellValue = new CellValue("10");<br />
cell.DataType = new EnumValue&lt;CellValues&gt;(CellValues.Number);</p>
<p>// Save the worksheet.<br />
<p>// for recacluation of formula<br />
spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;<br />
spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;</p>
<p>}<br />
<p>private static Cell GetCell(Worksheet worksheet,<br />
string columnName, uint rowIndex)<br />
{<br />
Row row = GetRow(worksheet, rowIndex);</p>
<p>if (row == null) return null;</p>
<p>var FirstRow = row.Elements&lt;Cell&gt;().Where(c =&gt; string.Compare<br />
(c.CellReference.Value, columnName +<br />
rowIndex, true) == 0).FirstOrDefault();</p>
<p>if (FirstRow == null) return null;</p>
<p>return FirstRow;<br />
<p>private static Row GetRow(Worksheet worksheet , uint rowIndex)<br />
{<br />
Row row = worksheet.GetFirstChild&lt;SheetData&gt;().<br />
Elements&lt;Row&gt;().FirstOrDefault(r =&gt; r.RowIndex == rowIndex);<br />
if (row == null)<br />
{<br />
throw new ArgumentException(String.Format("No row with index {0} found in spreadsheet", rowIndex));<br />
}<br />
return row;<br />

Now the same code using EPPlus

<p>public void UpdateExcelUsingEPPlus(string fileName)<br />
{<br />
FileInfo fileInfo = new FileInfo(fileName);<br />
ExcelPackage p = new ExcelPackage(fileInfo);<br />
ExcelWorksheet myWorksheet = p.Workbook.Worksheets["Sheet1"];<br />
myWorksheet.Cells[4, 2].Value = 10;<br />

Hope it helps..


Sample Code to consume UNData API using JavaScript

Sharing a sample code that can be used to consume UNData API

The below code fetches the population data for country India.

Sample Code

var xmlHttp;
// creatng the xmlHttp object
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
// Calling the web service using post and true means asynchronous call"POST", "", false);

// Setting the request header to let the web service find the soap request we would be sending
xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

xmlHttp.setRequestHeader("SOAPAction", "");

var soapRequest = "<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->xml version='1.0' encoding='utf-8'?>"+
"<soap:Envelope xmlns:soap='' xmlns:xsi='' xmlns:xsd=''>"+
"<GetCompactData xmlns=''>"+
"<message:QueryMessage xsi:schemaLocation='' xmlns:generic='' xmlns:message='' xmlns:query='' xmlns:xsi=''>"+
"<message:Sender id='online-help' />"+
"<query:Dimension id='INDICATOR'>SP_POP_TOTL</query:Dimension>"+
"<query:Dimension id='AGE'>_T</query:Dimension>"+
"<query:Dimension id='SEX'>_T</query:Dimension>"+
"<query:Dimension id='LOCATION'>_T</query:Dimension>" +
"<query:Dimension id='REF_AREA'>DEU</query:Dimension>" +
"<query:Dimension id='SCENARIO'>H</query:Dimension>"+
"</soap:Body>" +

var xmlDoc = jQuery.parseXML(xmlHttp.responseText);
var popValue = xmlDoc.getElementsByTagName('ns1:Obs')['0'].attributes['OBS_VALUE'].nodeValue;
alert("Total Population " + popValue);


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap=""
<Sender id="1B0">
<Name xml:lang="en">United Nations Statistics Division</Name>
<Source xml:lang="en">
United Nations Population Division World Population Prospects</Source>
<ns1:DataSet DATASET_NAME="World Population Prospects"
DATASET_SOURCE="United Nations Population Division"
DATASET_DESC="The United Nations World Population Prospects contains estimates and projections for every country in the world, including estimates and projections of demographic indicators such as birth rates, deaths rates, infant mortality rates and life expectancy." UNDATA_LASTUPDATE="20 Aug 2013" UNDATA_NEXTUPDATE="2015 release" METADATA=""><ns1:Series AGE="_T" FREQ="A" INDICATOR="SP_POP_TOTL" LOCATION="_T" SEX="_T" REF_AREA="DEU" SCENARIO="H"><ns1:Obs TIME_PERIOD="2015" OBS_VALUE="83178.615" UNIT="NUMBER" UNIT_MULT="3" TIME_SPAN="2015" /></ns1:Series></ns1:DataSet></CompactData></GetCompactDataResult></GetCompactDataResponse></soap:Body></soap:Envelope>

Hope it helps.

Dll not getting copied to bin directory in Visual Studio


In one of our projects we were referencing custom assemblies that were part of the same solution. However those assemblies were not getting copied to the bin directory of the project upon building the project.

We had also set property Copy Local as true for those class library projects.

The reason for this was as we were adding those assemblies in GAC.

VS.NET will copy the dll to the bin directory if your system cannot find the dll in the GAC.”

After removing those assemblies from GAC it added those dlls in the bin.

The helpful thread

Hope it helps..

Using EncryptedXml class for encrypting XML file in C#


We had a requirement to encrypt a particular node and its corresponding child elements in one of our xml file. While searching for the best and the simplest way to do so, I came across these wonderful posts which make use of EncrptedXml class.

Hope it helps

.NET Framework missing from Visual Studio 2010 new project dialog box

To resolve this issue, download and install .NET Framework 3.5 sp1.

Or if it is Windows Server 2008 R2,  just enable the .NET Framework 3.5.1 Features using Server Manager or else you will get the following error

“You must use role management tool to install or configure Microsoft .NET Framework 3.5”


Sign an already built assembly


These are the steps to be followed to sign an already built assembly

Open Visual Studio Command prompt.

Generate a KeyFile:

sn -k keyPair.snk

Obtain the MSIL for the provided assembly:

ildasm myAssembly.dll /

Rename/move the original assembly

Create a new assembly from the MSIL output and your assembly

ilasm /dll /key= keyPair.snk


%d bloggers like this: