We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Grid view not exporting to excel using aspose.cell

Hello

my code is like below

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="true">

<Columns>

<asp:TemplateField></asp:TemplateField>

</Columns>

</asp:GridView>

.cs file

DataTable myTable = new DataTable();

myTable = CreateDataTable1();

DataRow row;

row = myTable.NewRow();

row["id"] = "23";

row["username"] = "shdjs";

row["firstname"] = "firstname";

row["lastname"] = "lastname";

myTable.Rows.Add(row);

gv.DataSource = myTable;

gv.DataBind();

--Now export code

sheet.Cells.ImportGridView(gv, 0, 0, false, false, false);

sheet.AutoFitColumns();

workbook.Save("FootNoteTemplate.xls", FileFormatType.Excel97To2003, SaveType.OpenInExcel, this.Response);

// End response to avoid unneeded html after xls

Response.End();

But when it exports it only shows   in the excel.

Thanks in advance for your help

Thanks

Sourav

Hi,

Well, it works fine. Please try the attached latest version v5.1.3.6.

I have used the following code segments, see the pages source:


//Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable myTable = new DataTable();


myTable.Columns.Add(“id”, typeof(string));
myTable.Columns.Add(“username”, typeof(string));
myTable.Columns.Add(“firstname”, typeof(string));
myTable.Columns.Add(“lastname”, typeof(string));

DataRow row;
row = myTable.NewRow();
row[“id”] = “23”;
row[“username”] = “shdjs”;
row[“firstname”] = “firstname”;
row[“lastname”] = “lastname”;
myTable.Rows.Add(row);
gv.DataSource = myTable;
gv.DataBind();

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

sheet.Cells.ImportGridView(gv, 0, 0, false, false, false);
sheet.AutoFitColumns();

workbook.Save(this.Response, “FootNoteTemplate.xls”,ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
// End response to avoid unneeded html after xls
Response.End();
}

//Html part:



<asp:GridView ID=“gv” runat=“server” AutoGenerateColumns=“true”>



asp:TemplateField</asp:TemplateField>



</asp:GridView>




Thank you.




Hello
Thanks for your quick help. The current version my organization is using 4.8.2.3. So I am not sure whether the version change will be an acceptable option for the authority or not.

Actually the problem why i am using grid view to export is like. the attachment . The problem is line 5 and 6[as marked]. I can't try with data table as it dose not allow duplicate column name.[but the same name repeat will be there]. I tried with adding column to grid view dynamically and then export that grid to excel .

for(int i = 0; i < 4; i++)

{

BoundField boundField = new BoundField();

boundField.HeaderText = i.ToString();

boundField.HtmlEncode = false;

gv.Columns.Add(boundField);

}

sheet.Cells.ImportGridView(gv, 0, 0, false, false, false);
but after export there is blank record for that position.

Please help regarding this scenario

Thanks

Sourav

Hi,

After looking your code segment in your last post, I am afraid, the Dynamic BoundColumns in GridView are not supported. Moreover, we cannot log this feature as it is not possible logically and we cannot get any value that would be added dynamically in the template bound fields for the GridView.
I think you have to import and fill the values into the appropriate cells by yourself manually from the source.

Thanks for your understanding!

Hi,

Thanks again for the help.
Is it possible if i add template column with header text dynamically to any gridview/datagrid and the export that to excel.I mean can a datagrid/gridview with only header text be exported to excel so that the header text appears there.

Can you please help me how to put value in cells of a row. like in 6th row i want to put value programatically

like "Name1 Name1 Name1 Name3 Nam2 Name1" ,this names will be generate at runtime;there is no specifc count.

Thanks

Sourav

Hi,

I am afraid and as I told you earlier, it is not possible to export the dynamically added template columns (headers or details) of the GridView to Excel.

And, I am afraid, we don’t know any better option for this except for manually filling those row cells from the source. If we found some way, we will let you know.

Thank you.

Hi,

Above issue is resolved by using

ImportObjectArray(stringArray, 5, 0, false);

I just shared if some one faces the same issue.

Thanks for your help.

Sourav