Skip to main content

Posts

C#: Merging Excel cells with NPOI HSSFWorkbook

In this post we’ll see how to merge the two or more cell with each other while creating the excel sheet using NPOI . Mentioned below is code to merge multiple cells, in this example we are merging first cell to fifth cell of first row (you can adjust row or cell range by passing particular parameters in CellRangeAddress). //Created new Workbook var hwb = new NPOI.HSSF.UserModel. HSSFWorkbook (); //Create worksheet with name. var sheet = hwb.CreateSheet( "new sheet" ); //Create row and cell. var row = sheet.CreateRow(0); var cell = row.CreateCell(0); ; //Set text inside cell cell.SetCellValue( "This is Merged cell" ); cell.CellStyle.WrapText = true ; //define cell range address // parameters: -> first row to last and first cell to last cell var cra = new NPOI.SS.Util. CellRangeAddress (0, 0, 0, 4); //Add merged region to sheet. sheet.AddMergedRegion(cra); Hope this solution helps you J

How to fix 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine error

While creating OleDB Connection through Visual Studio (to connect with Access DB or import data from excel or CSV), you may get the error read as: 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. Solutions: There are several possible solutions for this error; you can try any one of mentioned below solution: ·          Try to install MicrosoftAccess Database Engine 2010 Redistributable. ·          If that doesn’t solve your problem try to install 2007Office System Driver: Data Connectivity Components. ·          Or you can try by changing Solution Platform from "Any CPU" to "x86". For this right click on the Solution File(in Solution Explorer) -> Configuration Manager -> Active Platform Drop down -> If x86 is already there then select that, else Click on New -> Select x86 from the new platform dropdown -> Compi...

Singleton Design Pattern and different ways to implement it in C#

The singleton pattern is one of the best-known patterns in software engineering. In a singleton pattern, we ensure that the class has only one instance and we provide a simple and global point of access to this object. This is useful when we require exactly one object of a class to perform our operations. Singleton classes don't allow any parameters to be specified when creating the instance because a second request for an instance but with a different parameter could be problematic. In this article, I'll provide you with a real-time example, a Comparison of Singleton with Static class, a UML class diagram of Singleton, and then the implementation of Singleton in various ways. Real-time example: The practical implementation of the Singleton design pattern can be seen in various applications used by us. Take the example of Microsoft word, there is only one instance of the word application active at a time even though the user opened multiple documents at a time. And all the req...

No exports were found that match the constraint: ContractName Microsoft.VisualStudio

Today morning when I opened one of my projects in Visual Studio 2012, I got mentioned below error: “No exports were found that match the constraint: ContractName Microsoft.VisualStudio.Utilities.IContentTypeRegistryService RequiredTypeIdentity Microsoft.VisualStudio.Utilities.IContentTypeRegistryServicePlease correct before proceeding. (You might rename the current web.config and add a new one).” Reason: It seems error occur when the .NET 4.5 framework is updated via recent windows 7 updates. Solution: I resolved this issue by removing the files inside the Visual Studio Component Model Cache folder at this specific location for Visual Studio 2012. C:\Users\{username}\AppData\Local\Microsoft\VisualStudio\11.0\ComponentModelCache If you facing this issue with Visual Studio 2010 then the relevant location is: C:\Users\{username}\AppData\Local\Microsoft\VisualStudio\10.0\ComponentModelCache Or if you facing this issue with Visual Studio 2013 then the relevant lo...

System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException: Operation is not valid due to the current state of the object.

You might have sometime faced the below exception while attempting to update an entity property: System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException: Operation is not valid due to the current state of the object. Reason: After looking into the issue I found that this error occurs when an attempt is made to change a foreign key when the entity is already loaded. In this case, I was trying to assign a value to the foreign key property, and an exception is thrown as foreign key fields and association properties don’t match, when changes are submitted. In such a scenario there are two values, one in the foreign key field or the one on the other side of the relationship and it doesn’t know which value is correct, and as a result exception is thrown. Solution: To avoid the exception the best way to update the relationship is by changing the association property and not the foreign keys. And then it will automatically keep the foreign keys in sync when you assign the association prop...

C#: Understand about IEnumerable vs. IQueryable vs. ICollection vs. IList

In this article we’ll understand about the interfaces ( IEnumerable, IQueryable, ICollection and IList) available for holding and querying the data. IEnumerable:   ·          IEnumerable exists in System.Collections Namespace. ·          IEnumerable is most generic item of all and a core interface which is used to iterate over collection of specified type. ·          IEnumerable provides Enumerator for accessing collection. ·          IEnumerable is forward only collection likes LinkedList. It doesn’t move between items or backward, i.e. one can't get at fifth item without passing first four items. ·          It is read-only collection and it doesn't support add or remove items. ·          IEnumerable is best to query data from ...