Free Support Forum - aspose.com

Applying style to entire row

Hi,

I am having difficulties applying style to the entire row in aspose.cells. I want to apply bold to bold the title and the first row/header of the table. However, it is applying the style to my contents or some of it on both the header and the content. Below are the codes and results.

Thanks,
Wynn

Codes:

Aspose.Cells.Workbook excelExportedData;
Aspose.Cells.Worksheets wrkshtExportedData;
Aspose.Cells.Worksheet shtExportedData;
Aspose.Cells.Cells cellsExportedData;
int intRowIndex = 1;

wrkshtExportedData = excelExportedData.Worksheets;
shtExportedData = wrkshtExportedData[intSheetIndex];
cellsExportedData = shtExportedData.Cells;

Aspose.Cells.Style style = excelExportedData.Styles[excelExportedData.Styles.Add()];
style.Font.IsBold = true;
style.Font.Size = 10;

shtExportedData.Cells["A" + intRowIndex.ToString()].PutValue("Weekly Data");
cellsExportedData.Rows[intRowIndex].Style = style;
intRowIndex = cellsExportedData.MaxDataRow + 1;
cellsExportedData.InsertRow(intRowIndex);
//start inserting data
cellsExportedData.ImportDataTable(dsResult.Tables["tb_WeeklyData"], true, intRowIndex, 0);
if(dsWeeklyOQASI.Tables["tb_WeeklyData"].Rows.Count == 0)
{
intRowIndex = cellsExportedData.MaxDataRow + 2;
shtExportedData.Cells["A" + intRowIndex.ToString()].PutValue("No records found");
cellsExportedData.Merge(cellsExportedData.MaxDataRow, 0, 1, 3);
}

Results:

Weekly Data
MONTH/WORKWEEK WW 6'2006 WW 7'2006 WW 8'2006 WW 9'2006 WW 10'2006 WW 11'2006
Total Lot Inspected 27 16 30 31 50 16
Total Qty Submitted 9768 6172 5193 7470 50000 9228
Total Qty Inspected 1212 639 765 896 50000 788

Please try Row.ApplyStyle method.

Hi,

Thanks for considering Aspose.

Well you may use Rows.Count and adjust your code like this:

Aspose.Cells.Workbook excelExportedData;
Aspose.Cells.Worksheets wrkshtExportedData;
Aspose.Cells.Worksheet shtExportedData;
Aspose.Cells.Cells cellsExportedData;
int intRowIndex = 1;

wrkshtExportedData = excelExportedData.Worksheets;
shtExportedData = wrkshtExportedData[intSheetIndex];
cellsExportedData = shtExportedData.Cells;

Aspose.Cells.Style style = excelExportedData.Styles[excelExportedData.Styles.Add()];
style.Font.IsBold = true;
style.Font.Size = 10;

shtExportedData.Cells["A" + intRowIndex.ToString()].PutValue("Weekly Data");
cellsExportedData.Rows[intRowIndex].Style = style;
intRowIndex = cellsExportedData.Rows.Count + 1;
cellsExportedData.InsertRow(intRowIndex);
//start inserting data
cellsExportedData.ImportDataTable(dsResult.Tables["tb_WeeklyData"], true, intRowIndex, 0);
if(dsWeeklyOQASI.Tables["tb_WeeklyData"].Rows.Count == 0)
{
intRowIndex = cellsExportedData.MaxDataRow + 2;
shtExportedData.Cells["A" + intRowIndex.ToString()].PutValue("No records found");
cellsExportedData.Merge(cellsExportedData.MaxDataRow, 0, 1, 3);
}

Results:

Weekly Data

CustomerID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin 12209 Germany 030-0074321 030-0076545
ANATR Ana Trujillo Emparedados y helados Ana Trujillo Owner Avda. de la Constitución 2222 México D.F. 05021 Mexico (5) 555-4729 (5) 555-3745
ANTON Antonio Moreno Taquería Antonio Moreno Owner Mataderos 2312 México D.F. 05023 Mexico (5) 555-3932
AROUT Around the Horn Thomas Hardy Sales Representative 120 Hanover Sq. London WA1 1DP UK (171) 555-7788 (171) 555-6750
BERGS Berglunds snabbköp Christina Berglund Order Administrator Berguvsvägen 8 Luleå S-958 22 Sweden 0921-12 34 65 0921-12 34 67
BLAUS Blauer See Delikatessen Hanna Moos Sales Representative Forsterstr. 57 Mannheim 68306 Germany 0621-08460 0621-08924

If you have further queries, feel free to contact us,

Regards

Amjad Sahi

Aspose Nanjing Team

Hi,

Is there any difference if I use intStartRow = cellsExportedData.Rows.Count + 1? My data goes hay wire if I use that.

The parameter of Columns is of data type byte?
workbook.Worksheets[0].Cells.Columns[1].ApplyStyle(style, flag);
In the end, I work around by using:
shtExportedData.Cells["A" + intStartRow.ToString()].Style = style;

In addition, when I try the following code:
shtExportedData.Cells.Rows[intRowIndex].ApplyStyle(style, flag);
the style is not applied to the first column. Below is how it looks like:

Weekly OQA Data

MONTH/WORKWEEK WW 26'2006 WW 27'2006 WW 28'2006 WW 29'2006 WW 35'2006 WW 37'2006
Total Lot Inspected 27 16 30 31 50 16
Total Lot Rejected 2 1 4 3 5 1

MONTH/WORKWEEK is not highlited.

Thanks,
Wynn

Hi Wynn,

It's really strange. Could you please post a simple console project to show your problem? Thank you very much.

Ignore the style part. Somehow it works when I use
shtExportedData.Cells.Rows[intStartRow].Style = style;

However, I got another question. How do I find out the name of the cell? For example I want to align the text from column X, row X to Column y, Row y

Aspose.Cells.Style ContentStyle = null;
int index = excelExportedData.Styles.Add();
ContentStyle = excelExportedData.Styles[index];
ContentStyle.Name = "Custom_Style1";
ContentStyle = excelExportedData.Styles["Custom_Style1"];
ContentStyle.HorizontalAlignment = TextAlignmentType.Right;

int intTotalColumns = cellsExportedData.Columns.Count;
Range range = cellsExportedData.CreateRange("B" + intStartRow.ToString(), intTotalColumns.ToString() + intStartRow.ToString());
range.Style = excelExportedData.Styles["Custom_Style1"];

It gives me error saying my cell name is wrong.

Thanks,
Wynn


Please try to use CellsHelper.CellIndexToName method.