 |
| 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
|
|
|
|

March 23rd, 2006, 08:25 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
|
|

April 5th, 2006, 04:52 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
//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
|
|

April 7th, 2006, 01:32 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
[email protected]
|
|

March 4th, 2010, 04:42 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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");
|
|

March 9th, 2010, 01:32 AM
|
|
|
Excel formatting
|
|

September 21st, 2011, 01:56 PM
|
|
Registered User
|
|
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |