Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a DLL - basic question

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi there,

I have a website written in ASP classic that presents data from a SQL database. I would now like to export the data as Excel and after some research have found that using EP Plus and .NET is one of the better ways of doing this.

I am totally new to this and so far I have:

1. Downloaded and installed Visual Web Developer 2010 Express on my PC
2. Created a folder on the IIS server and converted it to an application
3. Installed .NET framework 4 on server and configured application to use this.
4. Added a line to my web.config file <customErrors mode="Off"/>
5. Copied EPPLUS.DLL to the folder on the IIS server where my project is.

Some sample code has been supplied on the EP Plus website:

Code:
private void DumpExcel(DataTable tbl)
        {
            using (ExcelPackage pck = new ExcelPackage())
            {
                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

                //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                ws.Cells["A1"].LoadFromDataTable(tbl, true);

                //Format the header for column 1-3
                using (ExcelRange rng = ws.Cells["A1:C1"])
                {
                    rng.Style.Font.Bold = true;
                    rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                    rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue
                    rng.Style.Font.Color.SetColor(Color.White);
                }

                //Example how to Format Column 1 as numeric 
                using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
                {
                    col.Style.Numberformat.Format = "#,##0.00";
                    col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                }

                //Write it back to the client
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
                Response.BinaryWrite(pck.GetAsByteArray());
            }
        }

So my questions for now are...

1. I want to add a button to my default.aspx folder that runs the above code. How do I 'tell' default.aspx about the epplus.dll file?

2. Do I need to make a default.aspx.cs file and stick the code in there? I can see there is a default.aspx.vb file so I think somehow I have selected the wrong language for the website?

Thanks very much

Ed Mozley
 
Yes, you picked the wrong language. You can't simply create a .cs file and make it work.

To bring in the DLL: In Visual Web Developer, open the project. Right-click on it, then Add References. Select the DLL. You will also need a using statement in the .cs file to bring the name space in.

Craig Berntson
MCSD, Visual C# MVP,
 
Hi Craig,

Thanks for this - I have started again and made sure that I selected c# as the language for the website.

I have also added the reference to the DLL - it has now appeared under the bin folder in the solution explorer.

I've had a look at the default.aspx.cs file and at the start I can see some code which I think are the using statements you mention:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

On their FAQ ( it says the following under requirements:

"This library utilize the System.IO.Packaging namespace"

Does this mean I should add...

Code:
using System.IO.Packaging namespace

...to the code above?

Thanks a lot

Ed
 
Update

After a bit more digging around I found I had to add a WindowsBase assembly reference. This meant that when I typed the line:

Code:
using System.IO.Packaging;

I no longer get an error. I must admit at this stage I don't know what a namespace or an assembly reference is but nevertheless shall keep going...!

Now that I've done those two things what's next please?

Thanks again

Ed
 
Update - the website stopped working and after much digging around I found this article:


It told me that an application cannot be in the same application pool as another application that is using a different version of .NET.

I don't know what an application pool is but I made one called DOTNET4 and set my application to use that.

That got the page loading again but I still had lots of errors and after much googling and fiddling around found I had to change my code to include the following:

Code:
using System.IO.Packaging;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Data;
using System.Drawing;

This EP Plus seems very popular but it is REALLY badly documented and I don't understand why there is no working example out there!

The page now runs without any errors but nothing actually happens - what am I doing wrong?

My main chunk of code which is at the top is inside the public partial class - not quite sure how to call it though!

Thanks

Ed
 
Hi; Don't panic. Spend some time for videos on youtube for beginners. This will save a lot of time and arrange recent stuff you learned. How to add reference, button event, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top