Monday, 12 May 2014

Create Excel(.XLS and .XLSX) file from C# using excellibrary in window application


                                                                                                                                        Previous....
                                                                                                                                              Next..

Here i will explain how to Create Excel(.XLS and .XLSX) file from C# using excellibrary in window application.

Step(1): Form
Drag and down a button from tool box and give it name a "Create Excel File".

Step(2): Add excellibrary file in application
Before working for creating excel file we have to add a Excel File throw this we can access Excel properties.
To add excel file search on google As excellibrary and Open it 1st link.

Open this First link then you will see a tab for download. From here you download "excellibrary.dll" .

And extract this file into your application.

Step(3): Code
Double click on button and write this code. For this we have to add 2 namespace.
using ExcelLibrary.CompoundDocumentFormat;
using ExcelLibrary.SpreadSheet;
Before this you add reference of  "excellibrary.dll" into your application for this right click on reference and "add reference" and go to browser and select .dll file.




For writing code you can copy code from this link and use this code for practice.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ExcelLibrary.CompoundDocumentFormat;
using ExcelLibrary.SpreadSheet;

using System.Text;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
private void Button1_click(object sender, EventArgs e)
{
//create new xls file
string file = "newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[0, 1] = new Cell((short)1);
worksheet.Cells[2, 0] = new Cell(9999999);
worksheet.Cells[3, 3] = new Cell((decimal)3.45);
worksheet.Cells[2, 2] = new Cell("Text string");
worksheet.Cells[2, 4] = new Cell("Second string");
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
worksheet.Cells.ColumnWidth[0, 1] = 3000;
workbook.Worksheets.Add(worksheet);
workbook.Save(file);
// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];

}


Step(3): Run
Now Run your application and you will see excel file like this.
                                                                                                                                        Previous....
                                                                                                                                              Next..

2 comments:

  1. Thanks a lot. I solved an issue in my project with your code.

    ReplyDelete
  2. This seems to run on office 2007 but i use 2013

    ReplyDelete

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...