| |

VerySource

 Forgot password?
 Register
Search
View: 631|Reply: 5

C # how to import data from datagride to excel

[Copy link]

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-29 18:20:01
| Show all posts |Read mode
Using C # as a website, I want to import the data in the datagride of the client into Excel by clicking the button. How can I achieve this?
Reply

Use magic Report

0

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-3 14:00:02
| Show all posts
/// <summary>
/// export to Excel
/// </ summary>
/// <param name = "sender"> </ param>
/// <param name = "e"> </ param>
private void Button1_Click (object sender, System.EventArgs e)
{
Response.Clear ();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader ("Content-Disposition", "attachment; filename = FileName.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding ("GB2312"); // Set the output stream to Simplified Chinese
Response.ContentType = "application / ms-excel"; // Set the output file type to an excel file.
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo ("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter (myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter (oStringWriter);
this.DataGrid1.RenderControl (oHtmlTextWriter);
Response.Write (oStringWriter.ToString ());
Response.End ();
}
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-3-3 16:30:01
| Show all posts
private void HtmlToExcel (string FileType, string FileName)
        {
            Response.Clear ();
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding ("utf-8");
            Response.AppendHeader ("Content-Disposition", "attachment; filename =" + Server.UrlEncode (FileName));
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter ();
            HtmlTextWriter hw = new HtmlTextWriter (tw);
            divtest.RenderControl (hw);
            Response.Write (tw.ToString ());
            Response.End ();
}



// Then call this function in the button:
HtmlToExcel ("application / ms-excel", "Test.xls"); // where "Test.xls" is the file name of EXCEL
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-3-3 18:45:01
| Show all posts
Hehe, I forgot to tell the landlord to change the "divtest" in the code I posted to the DataGrid name.
Reply

Use magic Report

0

Threads

31

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-3-5 23:15:02
| Show all posts
The easiest way is to take datagird1.innerHTML (or outerHTML) from the page, saveas into an xsl file, the format data is all there, and excel can parse the html
Reply

Use magic Report

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-4-14 13:30:02
| Show all posts
Thank you very much.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list