DataGrid Header Text not written in Imported Excel File

I am using ASP.NET 2.0 DataGrid. when I try to Import data from DataGrid to Excel file using

"ImportDataGridAsString" method, the DataGrid Header text is missing in the Excel file. Here is my code:

Workbook workbook = new Workbook();

Worksheet sheet = workbook.Worksheets[0];

sheet.Cells.ImportDataGridAsString(DataGrid1, 0, 0, false);

sheet.AutoFitColumns();

workbook.Save("ExportData.xls", FileFormatType.Default, SaveType.OpenInExcel, Response);

whats wrong in this code and how can I do this?

Mamun

Hi Mamun,

Thanks for considering Aspose,

You mean Column Headers, Well, When you use Cells.ImportDataGrid() or Cells.ImportDataGridAsString() method, it does not extract column headers to Excel file. If you could use an overloaded Cells.ImportDataTable() method which can import column names (using its parameter i.e., isFieldNameShown to set it to "true:) to excel worksheet.

Thank you.

Need to import data from DataGrid not from DataTable.

If possible pPlease give some example code.

Thanks in advance...

Hi,

If your data grid is based on some datatable, you may try the following code for your need:

worksheet.Cells.ImportDataTable((DataTable)DataGrid1.DataSource, true, "A1");

Thank you.

Thanks a lot. Its now working. Now I need to show the Imported header in Bold Font and in a different color. How can i do this?

Hi,

Well you may use Style.Font.IsBold and Style.Font.Color properties to set header cells bold and apply color after importing data into the sheet.

E.g.,

.
.
sheet.Cells.ImportDataTable((DataTable)DataGrid1.DataSource, true, "A1"); 
//Get the worksheet cells.
Cells cells = sheet.Cells;
//Set the font color of the header cell.
cells["A1"].Style.Font.Color = Color.Red; 
//Set the font text bold.
cells["A1"].Style.Font.IsBold = true;
.......

Similarly, using the above code you may apply font attributes to other header cells like B1, C1, D1 etc.

For further reference, please check:

Dealing with Font Settings

Colors and Palette

Thank you.

Thanks for your quick reply. I need to apply Fill Color in the Header area and Bold the Header text. Header Text is work fine but I cant able to apply Fill color bcoz there is nothing abt Fill Color. I try with BackGroundColor but it dosent work. Whats the procedure sir?

Thanks in advance

Mamun

Hi Mamun,

Thanks for considering Aspose.

Please try Style.ForegroundColor property with solid Style.Pattern.

E.g.,

//Get the worksheet cells.
Cells cells = sheet.Cells;
//Set the font color of the header cell.
cells["A1"].Style.Font.Color = Color.Red;
//Set the font text bold.
cells["A1"].Style.Font.IsBold = true;
//Apply the fill color of the cell.
cells["A1"].Style.ForegroundColor = Color.Yellow;
cells["A1"].Style.Pattern = BackgroundType.Solid;

For further ref, please check:

<A href="</A></P> <P>Thank you.<BR></P> <P> </P>