A class library that needs to refer to MyXls.SL2.dll;: http://sourceforge.net/projects/myxls/
namespace using Org.in2bits.MyXls;
//Create a tableXlsdocument doc =NewXlsdocument ();//Create a new XLS documentDoc. FileName = DateTime.Now.ToString ("yyyymmddhhmmssms") +". xls";//Excel file nameWorksheet sheet = doc. WORKBOOK.WORKSHEETS.ADD ("Sheet1");//Excel worksheet nameCells cells = sheet. Cells;//all cells in a worksheet//Set Cell widthColumninfo col =NewColumninfo (doc, sheet);//Create a column style objectCol. Columnindexstart =0;//starting column, index starting from 0Col. Columnindexend =7;//end column, indexed starting at 0, this style is used for columns 1th and 2ndCol. Width =7111;//width, byte length, ushort type 0~65535Sheet. Addcolumninfo (COL);//Apply column styles to this worksheet//Set cell styleXF XF = doc. NEWXF ();//cell Style ObjectXf. VerticalAlignment = verticalalignments.centered;//Center VerticallyXf. HorizontalAlignment = horizontalalignments.centered;//Center HorizontallyXf. Pattern =1;//fill style, 0 is a colorless fill, and 1 is a solid color filled with no gapsXf. PatternColor = Colors.green;//Fill BackgroundXf. Font.ColorIndex =5;//font foreground color, unknown valueXf. Font.fontname ="Microsoft Ya-Black";//FontXf. Font.height = -* -;//Font SizeXf. Useborder =true;//using bordersXf. Bottomlinestyle =1;//Border StyleXf. Bottomlinecolor = colors.red;//Border Color//add data to a cellCells. ADD (1,1,"numbering", XF); Cells. ADD (1,2,"Area", XF); Cells. ADD (1,3,"Ref. 2", XF); using(Dbdatacontext con=NewDbdatacontext ()) {List<ChinaStates> cl =con. Chinastates.tolist (); intCount =2; foreach(Chinastates Cinchcl) {cells. ADD (Count,1, C.AREACODE,XF); Cells. ADD (Count,2, C.AREANAME,XF); Cells. ADD (Count,3, C.PARENTAREACODE,XF); Count++; }} doc. Save (Server.MapPath ("file"));//save to local, absolute path required
C # Build Excel