cancel
Showing results for 
Search instead for 
Did you mean: 

How to export data to excel?

01-23-2008 12:12 PM
442 views 6 comments
0 Likes
SAP Managed Tags
Subscribe

Hi Experts,

In a .Net application, Response.ContentType can be set to excel to export data from a table to excel. How to do the same in SAP, as it doesn't accept this command? All I have to do is export data in DataTable to an Excel document. Could anyone suggest me some workaround or snippet in this regard ASAP.

Thank you in advance,

Madhesh.

0 Likes

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Likes

any news about this problem ?

thx

Former Member
0 Likes

Hello Madhesh,

I posting code below.I am using this on button click event.

Check it whether it helps you or not.

/* ************************************************************************************************* */

if (dsDataSet.Tables.Count > ZERO)

{

string sTableStart = @"<HTML><BODY><TABLE Border=1>";

string sTableEnd = @"</TABLE></BODY></HTML>";

string sTHead = "<TR>";

StringBuilder sTableData = new StringBuilder();

foreach (DataColumn col in dsDataSet.Tables[0].Columns)

{

sTHead += @"<TH>" + col.ColumnName + @"</TH>";

}

sTHead += @"</TR>";

foreach (DataRow row in dsDataSet.Tables[0].Rows)

{

sTableData.Append(@"<TR>");

for (int i = 0; i < dsDataSet.Tables[0].Columns.Count; i++)

{

sTableData.Append(@"<TD>" + row<i>.ToString() + @"</TD>");

}

sTableData.Append(@"</TR>");

}

string sTable = sTableStart + sTHead + sTableData.ToString() + sTableEnd;

System.IO.StreamWriter oExcelWriter = System.IO.File.CreateText("c:/excelfile.xls");

oExcelWriter.WriteLine(sTable);

oExcelWriter.Close();

/* ************************************************************************************************* */

If above code snip help then award Points

Regards

Sunil Pawar

Former Member
0 Likes

Hi Sunil,

The code u have provided is to save in an excel document. The User may not have write permissions to local drives. In that case I have to just open it as an Excel file and not save it. Can you provide alternative for the same.

Thanks,

Madhesh.

rima-sirich
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi Madhesh,

Try this, paste following code to the Button click event:

Response.ContentType = "application/vnd.ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

DataGrid1.RenderControl(htw);

Response.Write(sw.ToString());

Regards,

Rima.

Former Member
0 Likes

Hi Rima,

I'm asking about exporting data from SAP Table to Excel. I've already done it in .net using Respone.ContentType. I want to do the same in SAP Portal.

Regards,

Madhesh

Edited by: Madhesh P on Jan 28, 2008 12:09 PM

rima-sirich
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi Madhesh,

Actually, I tested the posted piece of code in SAP Portal Component developed with PDK for .NET. It works fine when you use Datagrid control. When you use Table control from SAP Netweaver toolbox it works too, but it exports rows only from the first Table's page.

Regards,

Rima.