C # Build Excel

Source: Internet
Author: User

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

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.