A few days ago, there was a place in the project where the data of the DataGrid needs to be directly imported into the Excel file for users to download. I found the data online and it seems to be different from the following Code Similar implementation:
Program code: |
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 (); |
The principle is to write the DataGrid information to the HTML output stream in the form of a stream. You can try it yourself. After I installed Windows XP SP2, I went to my colleagues' machine and tried it. The result was a problem. The page of [open, save, and cancel] is displayed each time I run it, A little more, the page of the entire site is closed, and then try again on other machines. Some of the results can be downloaded normally, and some won't work. I have heard of such a problem before, it seems that there is no good solution.
Later, I thought about it. Why don't I directly write the information written to the HTML stream into an Excel file so that the customer can directly download the Excel file? So I slightly modified others' source code, directly generate an Excel file for the customer to download.Source codeAs follows:
Webform4.aspx ---- HTML part:
Program code: |
<% @ Page Language = "C #" codebehind = "webform4.aspx. cs" autoeventwireup = "false" inherits = "webui. webform4" %> <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <HTML> <Head> <Title> webform4 </title> <Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1"> <Meta name = "code_language" content = "C #"> <Meta name = "vs_defaultclientscript" content = "JavaScript"> <Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5"> </Head> <Body ms_positioning = "gridlayout"> <Form ID = "form1" method = "Post" runat = "server"> <Asp: DataGrid id = "datagrid1" runat = "server" width = "100%"> <Itemstyle horizontalalign = "center"> </itemstyle> <Headerstyle horizontalalign = "center"> <Footerstyle horizontalalign = "center"> </footerstyle> <Pagerstyle pagebuttoncount = "15" mode = "numericpages"> </pagerstyle> </ASP: DataGrid> <Asp: button id = "button1" style = "Z-INDEX: 101; left: 32px; position: absolute; top: 152px" runat = "server" TEXT = "button"> </ASP: button> </Form> </Body> </Html> |
There is a DataGrid and a button on the page. The button is used to trigger data to excel, and other background code will not be obtained. The following is the key button event.
Program code: |
Private void button#click (Object sender, system. eventargs E) { Datatable sourcetb = new datatable (); Datacolumn mydatacolumn; Mydatacolumn = new datacolumn (); Mydatacolumn. datatype = system. type. GetType ("system. String "); Mydatacolumn. columnname = "rowindex"; // Sn Sourcetb. Columns. Add (mydatacolumn ); Mydatacolumn = new datacolumn (); Mydatacolumn. datatype = system. type. GetType ("system. String "); Mydatacolumn. columnname = "checkupmanname"; // approver Sourcetb. Columns. Add (mydatacolumn ); Mydatacolumn = new datacolumn (); Mydatacolumn. datatype = system. type. GetType ("system. String "); Mydatacolumn. columnname = "checkupideas"; // approval comments Sourcetb. Columns. Add (mydatacolumn ); Mydatacolumn = new datacolumn (); Mydatacolumn. datatype = system. type. GetType ("system. String "); Mydatacolumn. columnname = "checkupdate"; // approval time Sourcetb. Columns. Add (mydatacolumn ); Mydatacolumn = new datacolumn (); Mydatacolumn. datatype = system. type. GetType ("system. String "); Mydatacolumn. columnname = "checkuprole"; // approval position Sourcetb. Columns. Add (mydatacolumn ); Mydatacolumn = new datacolumn (); Mydatacolumn. datatype = system. type. GetType ("system. String "); Mydatacolumn. columnname = "handletype"; // operation type (1: Submitted | 9: rejected) Sourcetb. Columns. Add (mydatacolumn ); datarow mydatarow; for (INT I = 0; I <30; I ++) {< br> mydatarow = sourcetb. newrow (); mydatarow ["rowindex"] = I. tostring (); mydatarow ["checkupmanname"] = "Zhang San"; mydatarow ["checkupideas"] = "agree "; mydatarow ["checkupdate"] = "2006-03-20"; mydatarow ["checkuprole"] = "Director of Material Department"; sourcetb. rows. add (mydatarow); } // bind data to datagrid1 This. datagrid1.datasource = sourcetb. defaultview; This. datagrid1.databind (); // write the HTML code consisting of datagrid1 into stringwriter This. datagrid1.page. enableviewstate = false; system. io. stringwriter Tw = new system. io. stringwriter (); system. web. UI. htmltextwriter hW = new system. web. UI. htmltextwriter (TW); This. datagrid1.rendercontrol (HW); string htmlinfo = TW. tostring (). trim (); String docfilename = "Approval Information .xls "; String filepathname = request. physicalpath; Filepathname = filepathname. substring (0, filepathname. lastindexof ("\\")); // Obtain the physical address of the Excel file Filepathname = filepathname + "\" + docfilename; System. Io. file. Delete (filepathname ); Filestream FS = new filestream (filepathname, filemode. Create ); Binarywriter bwriter = new binarywriter (FS, system. Text. encoding. getencoding ("gb18030 ")); // Write the DataGrid information to an Excel file Bwriter. Write (htmlinfo ); Bwriter. Close (); FS. Close (); } |
Now, the information is successfully written to the Excel file. As for where the generated Excel file is, you should know it at first glance. If you want to download it, that is, the simple <a href = 'excel file path'> File Download </a>.