Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 23rd, 2006, 08:25 AM
Authorized User
 
Join Date: Jan 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel cell Formating Using C#

Hi..Friends,

how to format cells patterns,Border color, Font styles,
and Rename Worksheet Name for MS-Excel.
Using C#.

Plz help meee...

Thanks & Regards,
Veeruu...


 
Old April 5th, 2006, 04:52 AM
Registered User
 
Join Date: Apr 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

//Variables
protected Excel.Worksheet feuilleExcel;
protected Excel.Range Cellules;
protected Excel.Range UneCellule;
protected Excel.Range DeuxCellules;
protected ExcelApplication xls;

//Open worksheet #1
this.feuilleExcel = (Excel.Worksheet)xls.__Excel.Worksheets[1];
this.feuilleExcel.Activate();
//get entire cells
this.Cellules = this.feuilleExcel.Cells;

//work on 1 cell
this.UneCellule=(Excel.Range)this.Cellules[XListe,YListe];
//value inserted
this.UneCellule.Value=this.ListeTypesCarte[i];
//cell border
this.UneCellule.BorderAround(Excel.XlLineStyle.xlC ontinuous,Excel.XlBorderWeight.xlThin,Excel.XlColo rIndex.xlColorIndexAutomatic,1);
//font style
this.UneCellule.Font.Bold=true;
//background color
this.UneCellule.Interior.Color=16777164;

//work on 2 cells
//insert values on first cell
this.Cellules[X,1]=rs.GetString(0);
//get cells
this.DeuxCellules = (Excel.Range)this.feuilleExcel.get_Range(this.Cell ules[X,1],this.Cellules[X,2]);
//cell border
this.DeuxCellules.BorderAround(Excel.XlLineStyle.x lContinuous,Excel.XlBorderWeight.xlThin,Excel.XlCo lorIndex.xlColorIndexAutomatic,1);
//background color
this.DeuxCellules.Interior.Color=10092543;
//font style
this.DeuxCellules.Font.Bold=true;

***********************************
EXCEL CLASS
***********************************
using System;
using System.Reflection;

namespace ControlOffice
{
public class ExcelApplication
    {
        public Excel.Application __Excel; // Application est une interface
        public object opt= Missing.Value;
        public string f;

        public ExcelApplication(string fich)
        {
            //
            // TODO : ajoutez ici la logique du constructeur
            //

            __Excel=new Excel.ApplicationClass();
            __Excel.Workbooks.Open(fich,opt,opt,opt,opt,opt,op t,opt,opt,opt,opt,opt, opt);
            f =fich;


        }

        public bool Visible
        {
            get
            {
                return __Excel.Visible;
            }
            set
            {
                __Excel.Visible=value;
            }
        }

        public Excel.Workbook nomClasseur
        {
            get
            {
                return __Excel.ActiveWorkbook;
            }
        }

        public Excel.Worksheet nomFeuil
        {
            get
            {
                return (Excel.Worksheet) __Excel.ActiveWorkbook.ActiveSheet;
            }
        }

        public Excel.Range Cellules
        {
            get
            {

                return nomFeuil.Cells;

            }
        }





        public void quit(bool save)
        {

            //__Excel.ActiveWorkbook.Close(save,opt,opt);
            __Excel.ActiveWorkbook.Close(save,this.opt,this.op t);
            __Excel.Quit();

        }
    }
}

****IMPORTANT*****
Don't forget join Office and Excel references to project


 
Old April 7th, 2006, 01:32 AM
Authorized User
 
Join Date: Jan 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi laure,
Thanks alot for your response.
"If you dont mind.." Can you please mail your application to me.
to finish my Task a bit fast..

Thanks & regards,
veeruuu...

prasad7478@yahoo.com


 
Old March 4th, 2010, 04:42 AM
Registered User
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

for easier Excel manipulation, I recommend this Excel C# component.

Here is a code snippet for all tasks you need:

Code:
var excelFile = new ExcelFile();
var worksheet = excelFile.Worksheets.Add("Worksheet1");
ws.Cells[0, 0].Value = DateTime.Now; // Type is System.DateTime

// Some Font Styles
ws.Cells[0, 0].Style.Font.Color = Color.Blue;
ws.Cells[0, 0].Style.Font.Italic = true;
ws.Cells[0, 0].Style.Font.Name = "Comic Sans MS";
ws.Cells[0, 0].Style.Font.Size = 150;

// Setting border on cell range
var mergedRange = ws.Cells.GetSubrangeAbsolute(0, 0, 10, 5);
var cellStyle = new CellStyle();
cellStyle.Borders.SetBorders(MultipleBorders.Right | MultipleBorders.Top, Color.Black, LineStyle.Thin);
mergedRange.Style = cellStyle;

// Change worksheet name
worksheet.Name = "Another name";

// Saves the excel file.
excelFile.SaveXls("excelFile.xls");
 
Old March 9th, 2010, 01:32 AM
hu_yang
Guest
 
Posts: n/a
Default Excel formatting

go through the following link

http://csharp.net-informations.com/e...rmat-excel.htm

hu.
 
Old September 21st, 2011, 01:56 PM
Registered User
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Formatting a excel cell programtically in asp.net c#

I put this way but there is an syntax error: newSheet.Cells[i + 2, 3] = drugorder.DATE; HorizontalAlignment == ExcelAlignment.xlLeft; Error 2 'System.Windows.Forms.HorizontalAlignment' is a 'type' but is used like a 'variable' C:\ADL\ExcelReader\ExcelReader\Form1.cs 257 68 ExcelReader Error 3 Only assignment, call, increment, decrement, and new object expressions can be used as a statement C:\ADL\ExcelReader\ExcelReader\Form1.cs 257 68 ExcelReader





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get Excel Cell color populate .NET Framework 2.0 2 March 3rd, 2014 04:25 AM
c# and Excel cell values hardboiled C# 0 November 16th, 2007 11:12 AM
cell references excel weazy Excel VBA 0 June 21st, 2006 09:28 AM
Changing Cell-formating in Excel via VBA Gert VB How-To 4 May 10th, 2005 01:56 PM
Unprotect a cell on Excel camf650 VBScript 2 April 14th, 2004 11:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.