Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Im trying a simple example of closed xml (used to create Excel 2007/2010 files) in c#.

my code line is:
C#
var workbook = new XLWorkbook();

Im getting warning on this statement as follows:
HTML
the type or namespace XLWorkbook cannot be found(are you missing using a directive or an assembly reference ?)
I have added ClosedXML.dll,DocumentFormat.OpenXml.dll in bin folder.
My Compelete code is as follows:
C#
using System;
using ClosedXML.Excel;

public partial class Demo01 : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
               var workbook = new XLWorkbook();
               var worksheet = workbook.Worksheets.Add("Sample Sheet");
               worksheet.Cell("A1").Value = "Hello World!";
               workbook.SaveAs("HelloWorld.xlsx");
        }
}

Please help me..!!
Posted
Updated 27-Sep-12 12:01pm
v3
Comments
Sergey Alexandrovich Kryukov 26-Sep-12 12:44pm    
You already know the namespaces, why do you think you need to know another namespace? Why do you think it can help you? It looks like you don't know how assemblies work in principle. You need to learn it before going ahead.
--SA
vision2fly 27-Sep-12 7:22am    
@Sergey Alexandrovich Kryukov, i have just changed the way of declaring object of type workbook and it worked for me. Can u pls explain why there is problem with-
var workbook = new XLWorkbook();
?? Dont .net support var??
Sergey Alexandrovich Kryukov 27-Sep-12 12:23pm    
Var is not really .NET, and it is not a type at all. This is called "type inference": the compiler infer that this line is strictly equivalent to:
XLWorkbook workbook = new XLWorkbook();

Got it? There is nothing to "support". This inference is done before IL code generation, essntially.
--SA
vision2fly 28-Sep-12 2:47am    
Got it sir...!!
Thank u very much..
Sergey Alexandrovich Kryukov 28-Sep-12 13:34pm    
Very good. You are welcome.
Are you going to accept my answer formally as well (green button, solution 1)? -- thanks.
--SA

What do you mean "I have added ClosedXML.dll"? You don't "add DLL" (DLL is no really significant in .NET, no more then a naming convention for some of executable modules, .NET operates with assemblies and modules), you need to reference assembly to build an assembly dependent on ClosedXML or anything else. If this is Visual Studio, you have the "Add Reference" item in your project tree, a part of "Solution Explorer" tree view. If you build in other ways, there are always the options to reference assemblies.

The namespace is ClosedXML, in your case, ClosedXML.Excel. How could you possibly miss the namespace? Please see:
http://closedxml.codeplex.com/SourceControl/changeset/view/76880#1045401[^].

Namespace does not add any modularity or something like that; they only introduce extended "fully-qualified" naming to top-level types. You can use using clause or not using it at all: it cannot change anything in assembly dependencies.

—SA
 
Share this answer
 
Comments
_Amy 31-Jul-14 6:44am    
+5!
Sergey Alexandrovich Kryukov 31-Jul-14 12:06pm    
Thank you, Amy.
—SA
you are missing the reference of the assemby,just add ClosedXML.dllit will works
 
Share this answer
 
Comments
Member 12567289 6-Jun-16 4:43am    
where add it
You're missing a reference to ClosedXML.dll or DocumentFormat.OpenXml.dll

See the requirements section at the top of the documentation page.
 
Share this answer
 
Comments
vision2fly 28-Sep-12 4:35am    
@Member 8697687 Read my post carefully. I have added ClosedXML.dll and DocumentFormat.OpenXml.dll and my problem has been solved. Actually it was baecause of 'var' uased in the declaration of XLWorkbook object.
Sergey Alexandrovich Kryukov 28-Sep-12 13:20pm    
"Added" or "referenced"? What exactly did you do? Is that you who vote 1s, by the way?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900