Free Support Forum - aspose.com

How to select a single row

Hi,

Am using aspose cell for creating excel from dataset.I want to change the font and formatting of first row i.e column name so I had made use of range but it change the formatting of entire rows.Here is the code...

Aspose.Cells.License licenseWord = new Aspose.Cells.License();

licenseWord.SetLicense(serverPath);

            Workbook workbook = new Workbook();</p><p>
            workbook.Worksheets.Clear();
            </p><p>if (ds != null && ds.Tables.Count > 0)
            </p><p>{
                foreach (DataTable dts in ds.Tables)

{ Worksheet worksheet = workbook.Worksheets.Add(dts.TableName);

Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;

                    </p><p>cells = worksheet.Cells;
                    
                    </p><p>
                    Aspose.Cells.Range range = worksheet.Cells.CreateRange("A1", "B1");
                    
                    </p><p>range.Name = "Range1";
                   </p><p> Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];

                    </p><p>style.Font.IsBold = true;

StyleFlag styleFlag = new StyleFlag();

styleFlag.All = true;

range.ApplyStyle(style, styleFlag);

worksheet.Cells.ImportDataTable(dts, true, "A1");

                }

workbook.Save("C:\\exceltest\\font.xlsx");}

Hi,


Please change the line of your code i.e.:
worksheet.Cells.ImportDataTable(dts, true, “A1”);
to:
worksheet.Cells.ImportDataTable(dts, true, 0,0,false);

it will work fine as I tested with a sample case. Here is my complete code that works fine and only the first row (A1:B1) is bold. The detail rows are rendered with normal font and not bold.

Sample code:

DataSet ds = new DataSet();
DataTable dt = new DataTable(“Table”);
dt.Columns.Add(new DataColumn(“Title”));
dt.Columns.Add(new DataColumn(“Client”));

DataRow drow = dt.NewRow();
drow[0] = “Item 1”;
drow[1] = “Client 1”;
dt.Rows.Add(drow);

drow = dt.NewRow();
drow[0] = “Item 2”;
drow[1] = “ABC Client”;
dt.Rows.Add(drow);
ds.Tables.Add(dt);

Workbook workbook = new Workbook();

workbook.Worksheets.Clear();

foreach (DataTable dts in ds.Tables)
{
Worksheet worksheet = workbook.Worksheets.Add(dts.TableName);
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
cells = worksheet.Cells;
Aspose.Cells.Range range = worksheet.Cells.CreateRange(“A1”, “B1”);
range.Name = “Range1”;
Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
style.Font.IsBold = true;
StyleFlag styleFlag = new StyleFlag();
styleFlag.All = true;
range.ApplyStyle(style, styleFlag);
worksheet.Cells.ImportDataTable(dts, true, 0,0,false);
}

workbook.Save(“e:\test2\font.xlsx”);


Note: All the rows are bold (issue) for your original sample code–> This is due the fact that and for your information, by default, when you use ImportDataTable(), it first inserts blank rows (based on the number of records in the datatable) then inputs data to it. So, you have to use the other suitable overload of ImportDataTable() method with insertRows Boolean parameter to set to false so that it should not insert blank rows rather directly insert the rows to the cells.

Thank you.