Free Support Forum - aspose.com

Changing the background color of a cell

Hi,

Please send me the code for changing the background color of a cell with a solid lightgay. As soon as I evaluate this functionality works fine, we will go ahead and purchase the component.

Prakash Billa
Coach


This message was posted using Email2Forum by salman.sarfraz.
Hi,

Thanks for considering Aspose.

Well, the color (LightGray) your want to apply is not present on the Standard Color Palette, so you have to add it first into the palette before setting it as a fill color of the cell. Since MS Excel (Excel97-Excel2003) Color Palette has only 56 colors (0-55 indexed) on it, so if a color is not there, you will have to add the color to the palette replacing any existing color on a specified index position.

Here is a sample code for your need:

//Instantiating a Workbook object
Workbook workbook = new Workbook();
// Add Light Gray color to the palette.
workbook.ChangePalette(System.Drawing.Color.LightGray, 55);
//Obtaining the first worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
//Now setting the foreground color of the "A1" cell to light gray.
worksheet.Cells["A1"].Style.ForegroundColor = Color.LightGray;
//Setting the background pattern of the "A1" cell to solid
worksheet.Cells["A1"].Style.Pattern = BackgroundType.Solid;
//Saving the Excel file
workbook.Save("d:\\test\\fillcolors.xls",FileFormatType.Default);

For further reference, please check the thread:

http://www.aspose.com/COMMUNITY/Forums/thread/74700.aspx

Thank you.

Hi,

Can we insert a new row inside a existing excel file which has already data?

Example.

I have a excel file with 300 rows already populated. I would like to insert a new row at row position (0). I want a new row to be inserted and and all the existing data should be moved down. Is this possible. Seems to be existing worsheet.cells.insertrow(0) is not working.

Thanks,

Prakash

Hi prakash,

Kindly try an overloaded verion of Cells.InsertRange() and utilize its ShiftType parameter.

Thank you.

Hi Amjad,

Thanks for your reply. Can you send me any sample code?.

Prakash

Hi,

Here it is the sample code.

Workbook wb = new Workbook();
wb.Open(@"d:\test\inrbook.xls");
Worksheet sheet1 = wb.Worksheets[0];
CellArea cellarea = new CellArea();
cellarea.StartRow = 0;
cellarea.StartColumn = 0;
cellarea.EndRow = 1;
cellarea.EndColumn = 3;
//Insert range of cells A1:D1 and shifts the cells down.
sheet1.Cells.InsertRange(cellarea,1,ShiftType.Down);
wb.Save(@"d:\test\outrbook.xls");
The inrbook.xls file:
3 2 4 aa nb sf
1 3
The outrbook.xls file:
nb sf
3 2 4 aa
1 3
If you still find some problem, kindly post your sample code with excel file.
Thank you.