Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Generate Excel files without using Microsoft Excel

, 22 Jun 2011 CPOL
A C# class to create Excel files without requiring Microsoft Excel.
ConsoleApplication1.zip
ConsoleApplication1
ConsoleApplication1
bin
Debug
ConsoleApplication1.exe
ConsoleApplication1.pdb
ConsoleApplication1.vshost.exe
demo.xls
ConsoleApplication1.vbproj.user
My Project
Application.myapp
Settings.settings
obj
Debug
ConsoleApplication1.exe
ConsoleApplication1.pdb
ConsoleApplication1.Resources.resources
ConsoleApplication1.vbproj.GenerateResource.Cache
ConsoleApplication1.vbproj.ResolveComReference.cache
TempPE
ConsoleApplication1.suo
XLSExportDemo.zip
XLSExportDemo
XLSExportDemo
Properties
XLSExportDemo_Update.zip
// C# Excel Writer library v2.0
// by Serhiy Perevoznyk, 2008-2011

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace XLSExportDemo
{
    internal class FormatInfo
    {
        private CellInfo cell;
        private int formatIndex;
        private int fontIndex;

        public FormatInfo(CellInfo cell)
        {
            this.cell = cell;
            if (string.IsNullOrEmpty(cell.Format))
                formatIndex = 0;
            else
                formatIndex = cell.Document.Formats.IndexOf(cell.Format);

            FontInfo fontInfo = new FontInfo(cell.Font, cell.ForeColor);
            fontIndex = cell.Document.Fonts.IndexOf(fontInfo);
            if (fontIndex == -1)
            {
                cell.Document.Fonts.Add(fontInfo);
                fontIndex = cell.Document.Fonts.IndexOf(fontInfo);
            }

            if (fontIndex > 3)
                fontIndex++;

        }

        public override bool Equals(object obj)
        {
            if (obj is FormatInfo)
            {
                FormatInfo info = (FormatInfo)obj;
                return ((this.fontIndex == info.fontIndex) && (this.formatIndex == info.formatIndex)
                    && (this.ForeColor.Index == info.ForeColor.Index)  && (this.BackColor.Index == info.BackColor.Index) 
                    && (this.HorizontalAlignment == info.HorizontalAlignment) );

            }
            else
                return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public ExcelColor BackColor
        {
            get { return cell.BackColor; }
        }

        public ExcelColor ForeColor
        {
            get { return cell.ForeColor; }
        }

        public Font Font
        {
            get { return cell.Font; }
        }

        public string Format
        {
            get { return cell.Format; }
        }

        public Alignment HorizontalAlignment
        {
            get { return cell.Alignment; }
        }

        public int FormatIndex
        {
            get { return formatIndex; }
        }

        public int FontIndex
        {
            get { return fontIndex; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Serhiy Perevoznyk
Architect
Belgium Belgium
No Biography provided
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150123.1 | Last Updated 22 Jun 2011
Article Copyright 2009 by Serhiy Perevoznyk
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid