asp.net build Excel and export download Five implementation methods _ practical Tips

Source: Internet
Author: User
Tags flush rowcount

method One passes the GridView (brief comment: The method is simpler, but only suitable for generating simple Excel, and cannot keep VBA code), the page is not refreshed

Aspx.cs part

Copy Code code as follows:



Using System;


Using System.Collections;


Using System.Configuration;


Using System.Data;


Using System.Web;


Using System.Web.Security;


Using System.Web.UI;


Using System.Web.UI.HtmlControls;


Using System.Web.UI.WebControls;


Using System.Web.UI.WebControls.WebParts;


Using System.Text;





public partial class DataPage_NationDataShow:System.Web.UI.Page


{


Private Data_link Link = new Data_link ();


private String sql;





protected void Page_Load (object sender, EventArgs e)


{


Ajax.Utility.RegisterTypeForAjax (typeof (Datapage_nationdatashow));


}





protected void Btnexcel_click (object sender, EventArgs e)


{


String strexcelname = "Myexcel";


Strexcelname = Strexcelname.replace (@ "/", "");





Data_link Link = new Data_link ();


string strSQL = This.hidParam.Value;


DataSet ds = new DataSet ();


ds = link. D_dataset_return (strSQL);//Get the data you want to put in Excel





Gvexcel.visible = true;


Gvexcel.datasource = null;


Gvexcel.datamember = ds. Tables[0]. TableName;


Gvexcel.datasource = ds. Tables[0];


Gvexcel.databind ();





exportToExcel (this. Page, Gvexcel, strexcelname);


}





protected void Gvexcel_rowdatabound (object sender, GridViewRowEventArgs e)


{ }


public override void Verifyrenderinginserverform (Control control)


{ }





<summary>


Tool method, Excel output (solve garbled problem)


</summary>


<param name= "page" > call page </param>


<param name= "Excel" >excel data </param>


<param name= "filename" > FileName </param>


public void exportToExcel (System.Web.UI.Page Page, GridView Excel, String fileName)


{


Try


{


foreach (GridViewRow row in Excel. Rows)


{


for (int i = 0; i < row. Cells.count; i++)


{


Excel. Headerrow.cells[i]. BackColor = System.Drawing.Color.Yellow;


}


}


Excel. Font.Size = 10;


Excel. Alternatingrowstyle.backcolor = System.Drawing.Color.LightCyan;


Excel. Rowstyle.height = 25;





Page. Response.appendheader ("Content-disposition", "attachment;filename=" + filename);


Page. Response.Charset = "Utf-8";


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


Page. Response.Write ("<meta http-equiv=content-type content=text/html;charset=utf-8>");


Excel. Page.enableviewstate = false;


Excel. Visible = true;


Excel. Headerstyle.reset ();


Excel. Alternatingrowstyle.reset ();





System.IO.StringWriter ostringwriter = new System.IO.StringWriter ();


System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter);


Excel. RenderControl (Ohtmltextwriter);


Page. Response.Write (Ostringwriter.tostring ());


Page. Response.End ();





Excel. DataSource = null;


Excel. Visible = false;


}


catch (Exception e)


{





}


}


}





ASPX section


Copy Code code as follows:

<script type= "Text/javascript" > The
//excel DownLoad
Function Excelexport () {
var hidtext = document.getElementById ("Hidparam");
Hidtext.value = "some params";
document.getElementById ("Exceloutput"). Click ();
}
</script>
<body onload= "Pageinit ()" >
<form id= "Form1" runat= "se RVer ">
<input type=" button "value=" Excel Downloads "style=" width:100px; "onclick=" Excelexport () "id=" Excelbut "/ >
<input id= "Hidparam" type= "text" runat= "server" style= "Display:none;" />
<asp:button runat= "Server" id= "Exceloutput" style= "Display:none" text= "Excel Output" width= "0px" onclick= " Btnexcel_click "usesubmitbehavior= false"/>
<asp:gridview id= "Gvexcel" runat= "Server" height= "95px" Onrowdatabound= "Gvexcel_rowdatabound" visible= "False" ></asp:GridView>
</form>
</body >



In the Aspx.cs code just now,


Copy Code code as follows:

foreach (GridViewRow row in Excel. Rows)
{
for (int i = 0; i < row. Cells.count; i++)
{
Excel. Headerrow.cells[i]. BackColor = System.Drawing.Color.Yellow;
}
}



This section adds a style to the table header.

Sometimes in order to facilitate browsing, you need to add a style to the cross row, the simple point can be as follows:

Copy Code code as follows:

Excel. Alternatingrowstyle.backcolor = System.Drawing.Color.LightCyan;



But a closer look will show that it has changed the entire line of styles, including those that are not used in the following columns.

The solution is yes, but more cumbersome, is to modify the style of each cell.

Copy Code code as follows:

int rowcount = Excel. Rows.Count;
int colcount = Excel. HeaderRow.Cells.Count;

for (int i = 0; i < rowcount; i++)
{
for (int j=0;j<colcount; j + +)
{
Excel. Rows[i]. CELLS[J]. BackColor = System.Drawing.Color.LightCyan;
}
}



Method TwoThe page is not refreshed through the DataGrid (basically the same as method)

Aspx.cs part

Copy Code code as follows:



public override void Verifyrenderinginserverform (Control control)


{}





&lt;summary&gt;


エクセル Output イベント


&lt;/summary&gt;


&lt;param name= "Sender" &gt;&lt;/param&gt;


&lt;param name= "E" &gt;&lt;/param&gt;


private void Excelbut_click (object sender, System.EventArgs e)


{


DataGrid dgexcel = new DataGrid ();





Try


{


DataSet ds = Getexceldata (this. Pageparams.value);/output データを obtain する





if (ds. Tables[0]. ROWS.COUNT&GT;0)


{


エクセルへデータを input する


String Execlname= "Myexcel";


Encoding Encodingtype=system.text.encoding.utf8;


Dgexcel.datamember=ds. Tables[0]. TableName;


Dgexcel.datasource=ds. Tables[0];





Response.Buffer = true;


Response.Charset = "Utf-8";


Response.appendheader ("Content-disposition", "attachment;filename=" + execlname+ ". xls");


response.contentencoding = Encodingtype;


Response.ContentType = "Application/ms-excel";


StringWriter ostringwriter = new StringWriter ();


HtmlTextWriter ohtmltextwriter = new HtmlTextWriter (Ostringwriter);


Dgexcel.databind ();


Dgexcel.visible = true;


Dgexcel.rendercontrol (Ohtmltextwriter);


Response.Write (Ostringwriter.tostring ());


Response.Flush ();


Response.close ();


Dgexcel.datasource = null;


Dgexcel.visible = false;


}


Else


{


Response.Write ("&lt;script&gt;alert (' xxxxxx ') &lt;/script&gt;");


}


}


catch (Exception ex)


{


Response.Write ("&lt;script&gt;alert (' oooooo ') &lt;/script&gt;");


}


}





ASPX section


Copy Code code as follows:



&lt;head runat= "Server" &gt;


&lt;script type= "Text/javascript" &gt;


Excel DownLoad


function Excelexport () {


var hidtext = document.getElementById ("Hidparam");


Hidtext.value = "some params";


document.getElementById ("Exceloutput"). Click ();


}


&lt;/script&gt;


&lt;/head&gt;


&lt;body onload= "Pageinit ()" &gt;


&lt;form id= "Form1" runat= "Server" &gt;


&lt;input type= "button" value= "Excel Downloads" style= "width:100px" onclick= "Excelexport ()" id= "Excelbut" name= "excelbut"/ &gt;


&lt;input id= "Hidparam" type= "text" runat= "server" style= "Display:none;" Name= "Hidparam"/&gt;


&lt;asp:button runat= "Server" id= "Exceloutput" style= "Display:none" text= "Excel Output" width= "0px" onclick= "Btnexcel_" Click "Usesubmitbehavior= false"/&gt;


&lt;asp:datagrid id= "Gvexcel" visible= "False" runat= "Server" style= "z-index:107"; Position:absolute; top:72px; left:520px "


Width= "80px" height= "40px" &gt;&lt;/asp:datagrid&gt;


&lt;/form&gt;


&lt;/body&gt;





Method ThreeExcel in XML format (you can set rich styles and can have multiple sheet, but you need a template.) Fast, but the generated file is large and the VBA code cannot be preserved.

Concrete method: The template is saved as A.xml in XML document form. You can then refer to the contents of the A.xml

Copy Code code as follows:



private void Excelbut_click (object sender, System.EventArgs e)


{


DataSet ds = new DataSet ();


String excelfilename = "";


DataRow dr=[------Column name information-------];


Try


{


Excelfilename = Path.Combine (Request.physicalapplicationpath + "/excelfile", "Myexcel.xls");





Get the data that Excel needs


DS = [------obtain data-------];


if (ds. Tables.count = 0)


{


Response.Write ("&lt;script type= ' text/javascript ' &gt;alert (' no data ');&lt;/script&gt;");


Return


}


int sheetnum = ds. TABLES.COUNT/2;


StreamWriter writer = new StreamWriter (Excelfilename, false);





The information in front of the styles label is equivalent to ' header information ' and does not need to be changed


Writer. WriteLine ("&lt;?xml version=\" 1.0\ "?&gt;");


Writer. WriteLine ("&lt;?mso-application progid=\" excel.sheet\ "?&gt;");


Writer. WriteLine ("&lt;workbook xmlns=\" urn:schemas-microsoft-com:office:spreadsheet\ ")";


Writer. WriteLine ("xmlns:o=\" urn:schemas-microsoft-com:office:office\ "");


Writer. WriteLine ("xmlns:x=\" urn:schemas-microsoft-com:office:excel\ "");


Writer. WriteLine ("xmlns:ss=\" urn:schemas-microsoft-com:office:spreadsheet\ "");


Writer. WriteLine ("xmlns:html=\" http://www.w3.org/tr/rec-html40\ "&gt;");


Writer. WriteLine ("&lt;documentproperties xmlns=\" urn:schemas-microsoft-com:office:office\ "&gt;");


Writer. WriteLine ("&lt;lastauthor&gt;automated generator example&lt;/lastauthor&gt;");


Writer. WriteLine (String. Format ("&lt;Created&gt;{0}T{1}Z&lt;/Created&gt;", DateTime.Now.ToString ("Yyyy-mm-dd"), DateTime.Now.ToString ("HH : Mm:ss "));


Writer. WriteLine ("&lt;Company&gt;51aspx.com&lt;/Company&gt;");


Writer. WriteLine ("&lt;Version&gt;11.6408&lt;/Version&gt;");


Writer. WriteLine ("&lt;/DocumentProperties&gt;");


Writer. WriteLine ("&lt;excelworkbook xmlns=\" urn:schemas-microsoft-com:office:excel\ "&gt;");


Writer. WriteLine ("&lt;WindowHeight&gt;6195&lt;/WindowHeight&gt;");


Writer. WriteLine ("&lt;WindowWidth&gt;18495&lt;/WindowWidth&gt;");


Writer. WriteLine ("&lt;WindowTopX&gt;525&lt;/WindowTopX&gt;");


Writer. WriteLine ("&lt;WindowTopY&gt;4260&lt;/WindowTopY&gt;");


Writer. WriteLine ("&lt;AcceptLabelsInFormulas/&gt;");


Writer. WriteLine ("&lt;ProtectStructure&gt;True&lt;/ProtectStructure&gt;");


Writer. WriteLine ("&lt;ProtectWindows&gt;False&lt;/ProtectWindows&gt;");


Writer. WriteLine ("&lt;/ExcelWorkbook&gt;");





Classes that correspond to style in HTML through Ss:id and Ss:name


Writer. WriteLine ("&lt;Styles&gt;");


Writer. WriteLine ("&lt;style ss:id= ' Default ' ss:name= ' Normal ' &gt;");


Writer. WriteLine ("&lt;alignment ss:vertical= ' Bottom '/&gt;");


Writer. WriteLine ("&lt;Borders/&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;NumberFormat/&gt;");


Writer. WriteLine ("&lt;Protection/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S23 ' &gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ' ' 11 '");


Writer. WriteLine ("ss:color= ' #FF0000 '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S24 ' &gt;");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S25 ' &gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S26 ' &gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ' ' 11 '");


Writer. WriteLine ("ss:color= ' #FF0000 '/&gt;");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S27 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;interior ss:color= ' #CCFFFF ' ss:pattern= ' Solid '/&gt;");


Writer. WriteLine ("&lt;Protection/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S28 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;interior ss:color= ' #CCFFFF ' ss:pattern= ' Solid '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' s29 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;interior ss:color= ' #CCFFFF ' ss:pattern= ' Solid '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S30 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;interior ss:color= ' #CCFFFF ' ss:pattern= ' Solid '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S31 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;interior ss:color= ' #CCFFFF ' ss:pattern= ' Solid '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S32 ' &gt;");


Writer. WriteLine ("&lt;alignment ss:vertical= ' Bottom ' ss:wraptext= ' 1 '/&gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;interior ss:color= ' #CCFFFF ' ss:pattern= ' Solid '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S33 ' &gt;");


Writer. WriteLine ("&lt;alignment ss:vertical= ' Bottom ' ss:wraptext= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' s34 ' &gt;");


Writer. WriteLine ("&lt;alignment ss:vertical= ' Bottom ' ss:wraptext= ' 1 '/&gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S35 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S36 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;interior ss:color= ' #FFFF99 ' ss:pattern= ' Solid '/&gt;");


Writer. WriteLine ("&lt;protection ss:protected= ' 0 '/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' s37 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' s38 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;style ss:id= ' S39 ' &gt;");


Writer. WriteLine ("&lt;Borders&gt;");


Writer. WriteLine ("&lt;border ss:position= ' Bottom ' ss:linestyle= ' continuous '" ss:weight= ' 1 '/&gt; ");


Writer. WriteLine ("&lt;border ss:position= ' left ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' right ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;border ss:position= ' top ' ss:linestyle= ' continuous ' ss:weight= ' 1 '/&gt;");


Writer. WriteLine ("&lt;/Borders&gt;");


Writer. WriteLine ("&lt;font ss:fontname= ' Ms pゴシック ' x:charset= ' 128 ' x:family= ' modern ' ss:size= ')"/&gt; ");


Writer. WriteLine ("&lt;Interior/&gt;");


Writer. WriteLine ("&lt;/Style&gt;");


Writer. WriteLine ("&lt;/Styles&gt;");





Sheet operation


Writer. WriteLine ("&lt;worksheet ss:name= '" + dr["Msg030"] + "' &gt;");


Writer. WriteLine ("&lt;table ss:expandedcolumncount= ' 9 ' ss:expandedrowcount= ' 3000 ' x:fullcolumns= ' 1 ' x:fullrows= ' 1 ' ss: Styleid= ' S33 ' ss:defaultcolumnwidth= ' ss:defaultrowheight= ' 13.5 ' &gt; ');


Writer. WriteLine ("&lt;column ss:styleid= ' s34 ' ss:autofitwidth= ' 0 ' ss:width= ' 110.25 '/&gt; ')";


Writer. WriteLine ("&lt;column ss:styleid= ' s34 ' ss:autofitwidth= ' 0 ' ss:width= ' 33.75 '/&gt; ')";


Writer. WriteLine ("&lt;column ss:styleid= ' s34 ' ss:autofitwidth= ' 0 ' ss:width= ' 118.5 '/&gt; ')";


Writer. WriteLine ("&lt;column ss:styleid= ' s34 ' ss:autofitwidth= ' 0 ' ss:width= ' 159.75 '/&gt; ')";


Writer. WriteLine ("&lt;column ss:styleid= ' s34 ' ss:autofitwidth= ' 0 ' ss:width= ' 147.75 ' ss:span= ' 4 '/&gt; ')";





for (int num = sheetNum-1; num &gt;= 0; num = num-1)


{


There are 3 lines of blank lines in the exam.


if (num!= sheetNum-1)


{


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


}





Column Name


Writer. WriteLine ("&lt;row ss:height= ' 40.5 ' &gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s32 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg002"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' s32 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg003"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' s32 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg004"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;/Row&gt;");





Detailed data


for (int i = 0; I &lt; ds. tables[2 * num]. Rows.Count; i++)


{


Writer. WriteLine ("&lt;row ss:height= ' &gt;");


Writer. WriteLine ("&lt;cell&gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num]. rows[i]["Evallevel1name"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell&gt;&lt;data ss:type= ' number ' &gt;" + ds.) tables[2 * num]. rows[i]["Evallevel2code"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell&gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num]. rows[i]["Evallevel2name"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


}


}


Writer. WriteLine ("&lt;/Table&gt;");


Writer. WriteLine ("&lt;/Worksheet&gt;");





Other sheet operations


for (int num = sheetNum-1; num &gt;= 0; num--)


{


Writer. WriteLine ("&lt;worksheet ss:name=") + ds. tables[2 * num]. ROWS[0][1]. ToString () + "' ss:protected= ' 1 ' &gt;");


Writer. WriteLine ("&lt;table ss:expandedcolumncount= ' ss:expandedrowcount= ' 30000 ' x:fullcolumns= ' 1 ' x:fullrows= ' 1 ' ss: Styleid= ' S25 ' ss:defaultcolumnwidth= ' ss:defaultrowheight= ' 13.5 ' &gt; ');


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' S26 ' &gt;&lt;phonetictext xmlns= ' Urn:schemas-microsoft-com:office:excel '" &gt; カキキイロテンスウツ&lt;/phonetictext&gt;&lt;data ss:type= ' String ' &gt; ' + dr[' Msg031 ' + ' &lt;/Data&gt;&lt;/Cell&gt; ');


Writer. WriteLine ("&lt;cell ss:index= ' ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' S26 ' &gt;&lt;phonetictext xmlns= ' Urn:schemas-microsoft-com:office:excel '" &gt; カキキイロテンスウツ&lt;/phonetictext&gt;&lt;data ss:type= ' String ' &gt; ' + dr[' Msg032 ' + ' &lt;/Data&gt;&lt;/Cell&gt; ');


Writer. WriteLine ("&lt;cell ss:index= ' ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' Default '/&gt;");





Writer. WriteLine ("&lt;/Row&gt;");





Column Name


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s27 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg001"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' s28 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg002"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' s28 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg003"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;/Row&gt;");


int sheetn = ds. tables[2 * num]. Rows.Count;





Detailed data


for (int i = 0; i &lt; SHEETN; i++)


{


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' number ' &gt;" + ds.) tables[2 * num]. rows[i]["Evallevel1code"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num]. rows[i]["Evallevel1name"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' number ' &gt;" + ds.) tables[2 * num]. rows[i]["Evallevel2code"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell&gt;&lt;data ss:type= ' String ' &gt; &lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


}


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;/Row&gt;");





Column Name


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s29 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg011"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' S30 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' S31 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s29 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg012"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' S31 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s29 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg013"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' S30 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' S30 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' S30 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' S31 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s29 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg014"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' S31 '/&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s29 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg015"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' S31 '/&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s28 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg016"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' s28 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg017"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' s28 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg018"] + "&lt;/Data&gt;&lt;/Cell&gt;" );


Writer. WriteLine ("&lt;cell ss:styleid= ' s28 ' &gt;&lt;data ss:type= ' String ' &gt;" + dr["Msg019"] + "&lt;/Data&gt;&lt;/Cell&gt;" );





Writer. WriteLine ("&lt;/Row&gt;");


int Datelen = ds. tables[2 * num + 1]. Rows.Count;


String employeemanagement = "";





for (int i = 0; i &lt; Datelen; i++)


{


Writer. WriteLine ("&lt;Row&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s37 ' &gt;&lt;data ss:type= ' number ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["Employeemanagementid"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


if (! Employeemanagement.equals (ds. tables[2 * num + 1]. rows[i]["Employeemanagementid"]. ToString ()))


{


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' number ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["Employeecode"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["EmployeeName"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["BefEmployeeName2"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["EmployeeName2"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s38 ' &gt;&lt;data ss:type= ' number ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["Evallevel1code"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["Evallevel1name"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Employeemanagement = ds. tables[2 * num + 1]. rows[i]["Employeemanagementid"]. ToString ();


}


Else


{


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s38 ' &gt;&lt;data ss:type= ' String ' &gt;&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;&lt;/Data&gt;&lt;/Cell&gt;");


}


Writer. WriteLine ("&lt;cell ss:styleid= ' s39 ' &gt;&lt;data ss:type= ' number ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["Evallevel2code"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;cell ss:styleid= ' s35 ' &gt;&lt;data ss:type= ' String ' &gt;" + ds.) tables[2 * num + 1]. rows[i]["Evallevel2name"]. ToString () + "&lt;/Data&gt;&lt;/Cell&gt;");


Writer. WriteLine ("&lt;/Row&gt;");


}


Writer. WriteLine ("&lt;/Table&gt;");


Writer. WriteLine ("&lt;/Worksheet&gt;");


}





Writer. WriteLine ("&lt;/Workbook&gt;");


Writer. Close ();





FileDownload (Excelfilename);


}


catch (System.Exception ex)


{





}


Finally


{


Response.End ();


}


}


public void FileDownload (Fullfilename)


{


FileInfo DownloadFile = new FileInfo (fullfilename);


Response.Clear ();


Response.ClearHeaders ();


Response.Buffer = true;


Response.appendheader ("Content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode ( System.Text.Encoding.UTF8.GetBytes (MyFile ". xls"));


Response.ContentType = "Application/ms-excel;charset=utf-8";//downloadfile.fullnameoctet-stream


response.contentencoding = System.Text.Encoding.GetEncoding ("Shift-jis");


Response.appendheader ("Content-length", DownloadFile.Length.ToString ());


Response.WriteFile (Fullfilename);


Response.Flush ();





if (file.exists (fullfilename))


{


File.delete (Fullfilename);


}


}





method FourUse Microsoft's COM component to manipulate Excel. Although it is convenient to manipulate cells and keep the VBA code, It is equivalent to opening an Excel process directly.

This COM component was prepared for WinForm, but there was a lot of support on the web side, such as the inability to close an Excel object with the methods in the component.

You can only close the Excel process by forcing it (there are a number of problems, such as closing the currently open process for this Excel, or closing the Excel process for other users). It's not recommended here.

method Five in the way of streaming, the content in table format to excel in the data benefit is to generate rich and complex format Excel, page no refresh

ASPX section

Copy Code code as follows:

<asp:button id= "Hidexport" onclick= "Hidexport_click ()" runat= "Server" ></asp:Button>



Aspx.cs part


Copy Code code as follows:



The content is very understanding, just as a table to spell the string can be


private String Getexcelcontent ()


{


StringBuilder sb = new StringBuilder ();





Sb. Append ("&lt;table bordercolor= ' black ' border= ' 1 ' &gt;");


Sb. Append ("&lt;thead&gt;&lt;tr&gt;&lt;th colspan= ' 2 ' bgcolor= ' #ccfefe ' &gt; Title &lt;/th&gt;&lt;/tr&gt;");


Sb. Append ("&lt;tr&gt;&lt;th bgcolor= ' #ccfefe ' &gt; Number &lt;/th&gt;&lt;th bgcolor= ' #ccfefe ' &gt; name &lt;/th&gt;&lt;/tr&gt; &lt;/thead&gt; ");


Sb. Append ("&lt;tbody&gt;");


Sb. Append ("&lt;tr class= ' firsttr ' &gt;&lt;td bgcolor= ' #FF99CC ' &gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;");


Sb. Append ("&lt;tr class= ' secondtr ' &gt;&lt;td&gt;&lt;/td&gt;&lt;td bgcolor= ' lightskyblue ' &gt;&lt;/td&gt;&lt;/tr&gt;") );


Sb. Append ("&lt;/tbody&gt;&lt;/table&gt;");


Return SB. ToString ();


}





private void Hidexport_click (object sender, System.EventArgs e)


{


String content = Getexcelcontent ();


String css = ". Firsttr td{color:blue;width:100px;}. SECONDTR td{color:blue;width:100px;} ";


string filename = "Test.xls";





Commontool.exporttoexcel (filename, content, CSS);


}





Tool Class Commontool


Copy Code code as follows:

public class Commontool
{
<summary>
In the form of a stream, you can set a rich and complex style
</summary>
<param name= "Content" >excel contents (table format) </param>
<param name= "filename" > FileName </param>
<param name= "Csstext" > Style content </param>
public static void exportToExcel (string filename, string content,string csstext)
{
var res = HttpContext.Current.Response;
Content = String.Format ("<style type= ' text/css ' >{0}</style>{1}", csstext,content);

Res. Clear ();
Res. Buffer = true;
Res. Charset = "UTF-8";
Res. AddHeader ("Content-disposition", "attachment; Filename= "+ filename);
Res. ContentEncoding = System.Text.Encoding.GetEncoding ("UTF-8");
Res. ContentType = "Application/ms-excel;charset=utf-8";
Res. Write (content);
Res. Flush ();
Res. End ();
}
}



This method is more flexible and can be added through selectors, which is pretty good. The disadvantage is that you need to convert the data into strings.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.