1, a dataset generated 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 =" "", = 0; // defines the table object with the row object, while using DataSet to initialize DataTable DT = DS.TABLES [0]; DATAROW [] Myrow = DT.SELECT (""); // typeID == "1" exports to Excel format file; type ID == "2" exports to XML format file IF (TYPEID == "1") {// acquisition data tables, each title is divided into / t, the last column title is returned to the car (i = 0; i colheaders = dt.columns [i ] .Caption.toString () "/ t"; colheaders = dt.columns [i] .caption.toString () "/ n"; // Write the data information written in the HTTP output stream Resp.Write (COLHEADERS); // progressive processing Data FOREACH (DATAROW ROW in Myrow) {// In the current line, obtain data one by one, data between / t, end, end, end, return, the car, N for (i = 0; I ls_item = row [i] .tostring () "/ t"; ls_item = row [i] .tostring () "/ n"; // Current row data writes an HTTP output stream, and Empty LS_ITEM to down the data resp.write (ls_item); ls_item = "";}} Else {if (typeid == "2") {// export XML data directly from the DataSet and write resp.write in the HTTP output stream;}}}} // The data in the write buffer The http header file is resp.end ();} 2, using Microsoft C written ActiveX control: http://download/officexpdev/sample/download/officexpdev/sample/download/win98mexp/en-us/dsoframerctl.exe3, Generate by 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 (20); httpContext.current. Response.end ();} Usage: TOEXCEL (DataGrid1); 4, this uses DataView, the code is long public void OutputExcel (DataView DV, String Str) {// // Todo: Add Construction Function Logic here // / / / DV is the data to be output to Excel, STR is the title name gc.collect (); Application Excel; // = new application (); int = 1; _Workbook XBK; _Worksheet Xst; Excel = new ApplicationClass (); XBK = Excel.Workbooks.add (true); xst = (_Worksheet) xbk.activesheet; // // get the title // foreach (Datacolumn col in dv.table.columns) {Colindex ; Excel.cells [4, ColIndex] = columnname; xst.get_range (Excel.cells [4, ColIndex], Excel. Cells [4, Colindex]). Horizontalalignment = XLVALIGN.XLVALIGNCENTER; // Set the title format to place the data // // get the data in the table // ForeAach (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 [columnname] .tostring ())). TOSTRING ("
YYYY-MM-DD "); Xst.get_Range (Excel.cells [RowIndex, Colindex], Excel.cells [RowIndex, Colindex]). Horizontalalignment = XLVALIGN.XLVALIGNCENTER; // Setting the date type field format to center alignment} Else IF (col.DataType == system.type.gettype ("system.string")) {excel.cells [rowindex, colIndex] = "'" row [columnname] .tostring (); xst.get_range (Excel) .Cells [RowIndex, Colindex], Excel.cells [RowIndex, Colindex]). Horizontalalignment = XLVALIGN.XLVALIGNCENTER; // Set the field format of the character pattern to the center alignment} else {Excel.cells [RowIndex, ColIndex] = row [colIL .Columnname] .tostring ();}}} // // Int rowsum = rowindex 1; int colsum = 2; Excel.cells [Rowsum, 2] = "Total"; xst.get_range Excel.cells [Rowsum, 2], Excel.cells [Rowsum, 2]). HorizontalaLAlignment = xlhalign.xlhalignCenter; // // Set color // xst.get_range (Excel.cells [Rowsum, COLSUM], Excel.cells [Rowsum, Colindex]). Select (); xst.get_range (Excel.cells [Rowsum, Colsum], Excel.cells [Rowsum, Colindex]). Interior.ColorIndex = 19; // Set to light yellow, There are 56 kinds of // // Total // Excel.cells [2, 2, 2] = Str; // // xst.get_range (Excel.cells [2, 2], Excel.cells [2, 2], Excel.cells [2, 2], Excel.cells [2, 2], Excel.cells [2, 2]). Font.bold = true; xst.get_range (Excel.cells [2, 2], Excel.cells [2, 2]). Font.size = 22; // // Setting the report form is The most suitable for width // xst.get_range (Excel.cells [4, 2], Excel.cells [Rowsum, Colindex]). Select (); xst.get_range (Excel.cells [4, 2], Excel.cells [ROWSUM , Columns.autofit (); // // Setting the title of the entire report to the cross-column center // xst.get_range (Excel.cells [2, 2], Excel.cells [2, Colindex]). Select (); Xst.get_Range (Excel.cells [2, 2], Excel.cells [2, Colindex]). Horizontalalignment = Xlhalign.xlhalignCenterCrossSseLection;
// // Draw border // xst.get_range (Excel.cells [4, 2], Excel.cells [Rowsum, ColIndex]). Borders.LineStyle = 1; Xst.get_Range (Excel.cells [4, 2], Excel.cells [ROWSUM, 2]). Borders [xlbordersindex.xledgeleft] .weight = xlborderweight.xlt = xlborderweight.xlt = xlborderweight.xlt; // Set the left cable plus xst.GET_RANGE (Excel.cells [4, 2], Excel.cells [4, ColIndex ]). Borders [xlbordersindex.xledgetop] .weight = xlborderweight.xlt = xlborderweight.xlt = xlborderweight.xlt = x 设置 设置 上 加 加 x x x 加 加 加 加 加 加.............. xlEdgeRight] .Weight = XlBorderWeight.xlThick; // Sets the right bold line xSt.get_Range (excel.Cells [rowSum, 2], excel.Cells [rowSum, colIndex]) Borders [XlBordersIndex.xlEdgeBottom] .Weight = XlBorderWeight.. XLTHICK; // Set the lower edge plus thick // // Display effect // Excel.visible = true; //xst.export(server.mappath(".mappath(".mappath ("). xls ", SheetExportActionEnum.ssExportActionNone, Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML); xBk.SaveCopyAs (Server.MapPath (".. ") " // " this.xlfile.Text " xls "); ds = NULL; XBK.Close (false, null, null); excel.Quit (); System.Runtime.InteropServices.Marshal.ReleaseComObject (xBk); System.Runtime.InteropServices.Marshal.ReleaseComObject (excel); System.Runtime.InteropServices.Marshal.ReleaseComObject (xSt); xBk = null; excel = NULL; XST = NULL; gc.collect (); string path = server.mappath (this.xlfile.text ". xls"); system.io.fileinfo file = new system.io.fileinfo (path); response. CLEAR (); response.charset = "gb2312"; response.contentencoding = system.text.Encoding.utf8; // Add header information, for "file download / saving as"
The dialog specifies the default file name. Response.addheader ("Content-Disposition", "attachment; filename =" server.urlencode (file.name)); // Add header information, specify the file size, so that the browser can display the download schedule Response.addheader ("Content-length", file.Length.tostring ()); // Specifying the return is a stream that cannot be read by the client, must be downloaded response.contenttype = "Application / MS-Excel"; // Send the file stream to the client response.writefile (file.fullname); // Stop the execution of the page to execute response.end ();
http://blog.9cbs.net/javaer/