Click here to Skip to main content
15,867,885 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to export datas from my data access layer
but nothing happens no error and not exported. it is kinda hard to know whats wrong because there's no error

What I have tried:

C#
<pre>if (dt.Rows.Count > 0){
          dgv.DataSource = dt;
         dgv.DataBind();

         Response.ClearContent();
         Response.AppendHeader("content-disposition","attachement;filename=transaction.xls");
         Response.ContentType = "application/excel";
         StringWriter stringWritter = new StringWriter();
         HtmlTextWriter htmlTextWritter = new HtmlTextWriter(stringWritter);

        dgv.RenderControl(htmlTextWritter);
        Response.Write(stringWritter.ToString());

        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.SuppressContent = true;
        HttpContext.Current.ApplicationInstance.CompleteRequest();
}

also trried this
C#
Html32TextWriter osHtmlTextWritter = new Html32TextWriter(osStringWritter);
       DataTable dt = new DataTable();
       DataGrid dgv = new DataGrid();

       Response.Clear();
       Response.Buffer = false;
       Response.ContentType = "application/vnd.ms-excel";
       Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "TransactionReport"));
       Response.Charset = "";

       dgv.DataSource = dt;
       dgv.DataBind();

       dgv.RenderControl(osHtmlTextWritter);
       Response.Write("Report Datw:" + DateTime.Now);
       Response.Write(osStringWritter.ToString());
       //Response.End();
       HttpContext.Current.Response.Flush();
       HttpContext.Current.Response.SuppressContent = true;
       HttpContext.Current.ApplicationInstance.CompleteRequest();

      dgv = null;
      osStringWritter = null;
      osHtmlTextWritter = null;

but still nothing happens no error.
also there's data in my gridView
Posted
Updated 23-Nov-22 19:23pm
Comments
F-ES Sitecore 19-Aug-19 6:12am    
You're not exporting anything to Excel, you're just sending html to the client. If the client is a browser and the user has Excel installed then the browser will get Excel to try and interpret the data as a spreadsheet, but if the client is anything else this won't work.

Also try removing the SuppressContent line.
Von Justine Napalang 19-Aug-19 18:50pm    
sorry i did not understand what you mean im kinda new in asp.net

"No error" just means that nothing fatal to the app was spotted, it doesn't mean that the app in any way does what you expected it to!

Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
And we can't help you here: it needs your code running, with your data, possibly on your system in order to see what exactly is going on - and we have no access at all to any of those.
 
Share this answer
 
You're not generating an Excel workbook. Your code is writing HTML to the response stream with a MIME type of "application/excel". That's not a valid Excel workbook.

Use an SDK to generate an actual valid Excel workbook, like the OpenXML SDK, or the ClosedXML SDK, or similar, to generate the workbook file and send that instead.
 
Share this answer
 
Comments
Von Justine Napalang 19-Aug-19 18:56pm    
sorry i dont understand what you mean im kinda new in programming
Dave Kreskowiak 19-Aug-19 19:07pm    
I don't knoew where you got this code from, but this:
StringWriter stringWritter = new StringWriter();
HtmlTextWriter htmlTextWritter = new HtmlTextWriter(stringWritter);
dgv.RenderControl(htmlTextWritter);
Response.Write(stringWritter.ToString());

does NOT generate a validate Excel file or anything else that Excel understands.
Von Justine Napalang 19-Aug-19 19:18pm    
my code looks like this now
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename = trans.xls"));
HttpContext.Current.Response.Charset = "";

dgv.DataSource = dt;
dgv.DataBind();
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hsw = new HtmlTextWriter(sw))
{

dgv.RenderControl(hsw);
HttpContext.Current.Response.Output.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}

but still nothings happens. can you guid me with this? im stuck in this for 3 days
Dave Kreskowiak 19-Aug-19 19:22pm    
Not even close. If you have to call RenderControl, you're not generating Excel file content at all. You're STILL just dumping HTML back to the stream and the client browser and Excel is not going to know what to do with it.

It's going to take a LOT more code to generate a proper Excel file.

Click on the first link in this list: https://www.google.com/search?source=hp&ei=6y5bXcrFFJGJggev66vQBg&q=OpenXML+sdk&oq=OpenXML+sdk&gs_l=psy-ab.3..0l10.498.14221..16198...10.0..0.129.1845.9j9......0....1..gws-wiz.....10..35i39j0i131j0i10.4G7vwttdk9k&ved=0ahUKEwiK57nFiZDkAhWRhOAKHa_1CmoQ4dUDCAc&uact=5
Von Justine Napalang 19-Aug-19 19:24pm    
why youtube tutorials code look like this and it work
to download or upload a file you need to add a full postback . please add postbacktrigger instead of asyncpostback trigger and check if that is worked or not.
 
Share this answer
 
v2
Comments
Richard Deeming 24-Nov-22 8:33am    
There is nothing in the question, or any comments to the existing solutions, to suggest that the OP is using an UpdatePanel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900