C # Build Excel save to server side and download

Source: Internet
Author: User

usingMongodb.bson;usingNewtonsoft.Json.Linq;usingNpoi. HSSF. Usermodel;usingNpoi. Ss. Usermodel;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingZluo.CMember.Entity;usingZluo.CMember.Interface;usingZluo.CMember.Service;usingZluo.CMember.Web.RequestParam;usingZluo.CMember.Web.RequestParam.Account;usingZluo.CMember.Web.RequestParam.Order;usingZluo.common;usingZluo.Common.CMember;usingzluo.sessioncached; Public voidCreateexport () {Try            {                stringFileName ="Errorsongids.xls";//file name                stringFilePath = Path.Combine (Httpruntime.appdomainapppath,"Upload") +"/"+FileName; //1. Detect if a folder exists and create a folder if it does not exist                stringDirectoryName = Path.getdirectoryname (FilePath);if(!directory.exists (directoryname))                {directory.createdirectory (directoryname);} //2. Resolve the cell header, set the Chinese name of the unit headHssfworkbook Workbook =NewHssfworkbook (); // WorkbooksIsheet sheet = workbook. Createsheet ("failed Songs"); //Work TableIRow row = sheet. CreateRow (0); Row. Createcell (0). Setcellvalue ("Original ID"); Row. Createcell (1). Setcellvalue ("Song name"); Row. Createcell (2). Setcellvalue ("Name of singer"); Row. Createcell (3). Setcellvalue ("Reason for failure"); //_songlistservice.geterrorexcel (UID);Bsonarray array =NULL; intRowIndex =1; bsondocument BD =NULL; if(Array! =NULL&& Array. Count >0)                {                     for(inti =0; I < array. Count; i++) {IRow rowtmp=sheet.                        CreateRow (RowIndex); BD=(bsondocument) array[i]; if(BD! =NULL) {Rowtmp.createcell (0). Setcellvalue (Bd. GetValue ("SourceID").                            ToString ()); Rowtmp.createcell (1). Setcellvalue (Bd. GetValue ("Songname").                            ToString ()); Rowtmp.createcell (2). Setcellvalue (Bd. GetValue ("Singername").                            ToString ()); Rowtmp.createcell (3). Setcellvalue (string. IsNullOrEmpty (Bd. GetValue ("Errorreason"). ToString ())?"": Bd. GetValue ("Errorreason").                            ToString ()); RowIndex++; }                    }                }                //4. Generating FilesFileStream file =NewFileStream (FilePath, FileMode.Create); Workbook. Write (file); File.                Close (); Response.appendheader ("content-disposition","Attachment;filename=errorsongids.xls"); Response.ContentType="Application/ms-excel";                Response.WriteFile (FilePath);                Response.Flush ();            Response.End (); }            Catch(Exception ex) {Throwex;} }

C # Build Excel save to server side and download

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.