1
Answer

Export DB table into .xls working but getting an error !!

Tiger Singh

Tiger Singh

7y
159
1
Dear All,
 
i am working on the below code and it's loading the table's data into excel (.xls) file but while exporting giving an error as attached. when i am converting this code into .csv then the data are moving to next column when it finds the coma values i.e.
Employee_FullName
Marek,John 
 
--Excel 
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in dataTable.Columns)
{
context.Response.Write(column.ColumnName + "\t");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in dataTable.Rows)
{
for (int i = 0; i < dataTable.Columns.Count; i++)
{
context.Response.Write(row[i].ToString() + "\t");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "application/ms-excel";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
context.Response.End();
 
--CSV
 
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in dataTable.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in dataTable.Rows)
{
for (int i = 0; i < dataTable.Columns.Count; i++)
{
context.Response.Write(row[i].ToString() + ",");
//context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "application / ms - excel";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
context.Response.End();
 
 
 
 
 

Attachment: error.zip

Answers (1)
0
alibasha syed
NA 41 24.1k 11y
Hi, I got the solution. The only solution to resolve these kind of issues(extra page issues in rdlc) is .... In the report designer, reduce the width of the table and report body. Keep this relation in mind: report body width must be <= (report width - report margins width). Ideally Report widht and height should be 8.5in and 11 In respectively.
0
alibasha syed
NA 41 24.1k 11y
Then please suggest me the alternate solution for my problem. Please reply as soon as possible.
0
Abhishek Sur
NA 3.4k 53.8k 11y
Hi Alibasha, 

The problem is every subreport control has an implicit KeepTogether property which tries to keep the content of the subreport together inside the page. This has a problem.

Say your subreport 1 tries to keep everything inside the page, but cannot find the space enough to show everything. It then go ahead and keep this page blank and write everything on a new separate page. This is a known bug in SQL server reporting service. So it is recommended to avoid using subreport inside rdl files to support continuous printing.