Formatting Column

Hi,

I'm trying out this Aspose.Cells for .NET and one of the important things I need to do is formatting the column. I want to format the columns based from the DataType of my DataTable before I put values into the cells. The reason is so that end-user can add data at the end of the filled rows and the column format would remain the same. for instance I like the column A as Text and when user will key in 01234 - the leading zero will not be truncated. My source code is below... please help.

Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets[0];

wb.Styles.Add();
Style coltext = wb.Styles[0];
coltext.Number = 49;
StyleFlag flagstyle = new StyleFlag();
flagstyle.HorizontalAlignment = true;
flagstyle.VerticalAlignment = true;
flagstyle.ShrinkToFit = true;
flagstyle.Borders = true;
flagstyle.FontColor = true;

int colcnt = 0;
int rowcnt = 0;
int totalrows = tbl.Rows.Count;

foreach (DataColumn col in tbl.Columns)
{
ws.Cells[rowcnt, colcnt].PutValue(col.ColumnName);
switch (col.DataType.ToString())
{
case "System.String":
Column celcol = ws.Cells.Columns[colcnt];
celcol.ApplyStyle(coltext, flagstyle);
break;
}
colcnt += 1;
}

Hi,

Well, you need to first define your desired style attributes and then specify/set the corresponding styleflag attributes to make it work, e.g. you may see the code below, I have added a few lines of code to your style related code (you may change it accordingly):

Style coltext = wb.Styles[wb.Styles.Add()];

coltext.Number = 49;

coltext.HorizontalAlignment = TextAlignmentType.Left;

coltext.VerticalAlignment = TextAlignmentType.Bottom;

coltext.ShrinkToFit = true;

coltext.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Medium;

coltext.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;

coltext.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Medium;

coltext.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Medium;

coltext.Font.Color = Color.Green;

StyleFlag flagstyle = new StyleFlag();

flagstyle.NumberFormat = true;

flagstyle.HorizontalAlignment = true;

flagstyle.VerticalAlignment = true;

flagstyle.ShrinkToFit = true;

flagstyle.Borders = true;

flagstyle.FontColor = true;

For reference, please see the document: http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/formatting-rows-columns.html

Thank you.