asp.net export Excel table method Rollup

Source: Internet
Author: User
Tags foreach format httpcontext return string tostring
Asp.net|excel| export Excel 1, generated by the dataset





public void Createexcel (DataSet ds,string typeid,string FileName)


{


HttpResponse resp;


resp = page.response;


resp. ContentEncoding = System.Text.Encoding.GetEncoding ("GB2312");


resp. Appendheader ("Content-disposition", "attachment;filename=" + filename);


string Colheaders= "", ls_item= "";


int i=0;





//Defines table objects and row pairs, and initializes their values with a dataset


DataTable Dt=ds. Tables[0];


datarow[] Myrow=dt. Select ("");


//typeid== "1" exported to Excel format file, typeid== "2" exported to XML format file


if (typeid== "1")


{


//Get the column headings of the datasheet, each heading is separated by \ t, and the last column heading is followed by a carriage return


for (I=0;i Colheaders+=dt. Columns[i]. Caption.tostring () + "T";


colheaders +=dt. Columns[i]. Caption.tostring () + "\ n";


//Writes the obtained data information to the HTTP output stream


Resp. Write (colheaders);


//row-by-line processing of data


foreach (DataRow row in myrow)


{


///In the current row, the data is obtained by column, the data is separated by \ t, and the carriage return is added \ n


for (i=0;i ls_item +=row[i). ToString () + "\ T";


Ls_item + = Row[i]. ToString () + "\ n";


//Current row data is written to the HTTP output stream and the Ls_item is empty for downlink data


resp. Write (Ls_item);


ls_item= "";


}


}


Else


{


if (typeid== "2")


{


//Exports XML data directly from the dataset and writes to the HTTP output stream


resp. Write (ds. GETXML ());


}


}

Data in the
//write buffer to the HTTP header file


resp. End ();








}











2, using Microsoft's C + + written ActiveX control: http://download.microsoft.com/download/OfficeXPDev/sample/1.0/WIN98MeXP/EN-US/ Dsoframerctl.exe


3, generated by the DataGrid:





public void Toexcel (System.Web.UI.Control ctl)


{


HttpContext.Current.Response.AppendHeader ("Content-disposition", "Attachment;filename=excel.xls");


HttpContext.Current.Response.Charset = "UTF-8";


HttpContext.Current.Response.ContentEncoding =system.text.encoding.default;


HttpContext.Current.Response.ContentType = "Application/ms-excel";//image/jpeg;text/html;image/gif; Vnd.ms-excel/msword


ctl. Page.enableviewstate =false;


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


System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (TW);


ctl. RenderControl (HW);


HttpContext.Current.Response.Write (tw. ToString ());


HttpContext.Current.Response.End ();


}





usage: toexcel (DATAGRID1);








4, this use DataView, the code is very long





public void Outputexcel (DataView dv,string str)


{


//


//TODO: Add constructor logic here


//


//DV for data to be exported to Excel, Str is the title name


GC. Collect ();


Application excel;//= new application ();


int rowindex=4;


int colindex=1;





_workbook XBK;


_worksheet xSt;





excel= New ApplicationClass ();





XBK = Excel. Workbooks.Add (TRUE);





xSt = (_worksheet) xbk.activesheet;





//


//Get title


//


foreach (DataColumn col in DV. Table.columns)


{


colindex++;


Excel. Cells[4,colindex] = Col. ColumnName;


Xst.get_range (Excel. Cells[4,colindex],excel. Cells[4,colindex]). HorizontalAlignment = xlvalign.xlvaligncenter;//format title is centered


}





//


//Get the data in the table


//


foreach (DataRowView row in DV)


{


RowIndex + +;


Colindex = 1;


foreach (DataColumn col in DV. Table.columns)


{


Colindex + +;


if (Col. DataType = = System.Type.GetType ("System.DateTime"))


{


Excel. Cells[rowindex,colindex] = (Convert.todatetime (row[col). ColumnName]. ToString ())). ToString ("Yyyy-mm-dd");


Xst.get_range (Excel. Cells[rowindex,colindex],excel. Cells[rowindex,colindex]). HorizontalAlignment = xlvalign.xlvaligncenter;//The field format of the date type is centered


}


Else


if (Col. DataType = = System.Type.GetType ("System.String"))


{


Excel. Cells[rowindex,colindex] = "'" +row[col. ColumnName]. ToString ();


Xst.get_range (Excel. Cells[rowindex,colindex],excel. Cells[rowindex,colindex]). HorizontalAlignment = xlvalign.xlvaligncenter;//The field format of the character type is centered


}


Else


{


Excel. Cells[rowindex,colindex] = Row[col. ColumnName]. ToString ();


}


}


}


//


//Load an aggregate row


//


int rowsum = rowIndex + 1;


int colsum = 2;


Excel. cells[rowsum,2] = "Total";


xst.get_







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.