How to use C # data to guide Excel

Source: Internet
Author: User

Ability to generate an Excel table from the relevant code, and to control the table style and so on ~

public void Exportexcel ()         {             //creating an instance of Excel              excelapplication Excel = new ExcelApplication ();             try            {                                excel. Visible = false;//does not display Excel files, if True displays Excel files                  excel. Workbooks.Add (Missing.Value);//Add Workbook                  worksheet sheet = (Worksheet) Excel. activesheet;//Get current sheet       &NBSp;         range Range = null;//Creates an empty cell object                  sheet.get_range (sheet. Cells[29, 2], sheet. CELLS[29, 2]). Orientation = microsoft.office.interop.excel.xlorientation.xlvertical;//Set 29 row 2nd cell font centered vertically in cell                  range = Sheet.get_Range ("A1" , Missing.Value);//Get a single cell                  range. RowHeight = 20;           //Setting Line Height                  range. ColumnWidth = 20;         //Setting column widths                  range. Borders.LineStyle = 1;    //setting Cell edgesBox                 range. Font.Bold = true;         //Bold Font                  range. Font.Size = 20;           //Setting font size                  range. Font.ColorIndex =5;//Set Font color 1: Black, 2: White, 3: Red, 4: Green; 5: Blue                           range. Interior.ColorIndex = 6;  //setting cell background color                    range. Interior.ColorIndex = 6;  //setting cell background color                  range. HorizontalAlignment = XlHalign.xlhaligncenter;//setting cells to center horizontally                  range. VerticalAlignment = xlvalign.xlvaligncenter;//Set cell Vertical Center                  range. Value2 = "Set row height and column width";//Set cell value                    range = Sheet.get_range ("B2", "D4");//Get Multiple cells                  range. Merge (Missing.Value);         //Merging Cells                  range. Columns.AutoFit ();            //set column width to auto fit                  range. numberformatlocal = "#,# #0.00";//Set cell format for goodsCurrency format                 // Set cell left border bold                  Range. Borders[xlbordersindex.xledgeleft]. Weight = xlborderweight.xlthick;                 //set cell right border bold                  range. Borders[xlbordersindex.xledgeright]. Weight = xlborderweight.xlthick;                 range. Value2 = "merged cells"; Range = Sheet.get_range ("A1", "C15");//Get Multiple cells                  range. Cells.Borders.LineStyle = 1;//Set full border                   //Page Setup     &nbsP;           sheet. Pagesetup.papersize = XLPAPERSIZE.XLPAPERA4;          //set Page size to a4                 sheet. Pagesetup.orientation = xlpageorientation.xlportrait; Sets the vertical layout                 sheet. Pagesetup.headermargin = 0.0;                          //Setting Header Margins                  sheet. Pagesetup.footermargin = 0.0;                          //Setting footer Margins               &nBsp;  sheet. PageSetup.LeftMargin = Excel. InchesToPoints (0.354330708661417); Set the left margin                 sheet. Pagesetup.rightmargin = Excel. InchesToPoints (0.354330708661417);//Set Right margin                  sheet. Pagesetup.topmargin = Excel. InchesToPoints (0.393700787401575);  //setting Top margin                  Sheet. Pagesetup.bottommargin = Excel. InchesToPoints (0.393700787401575);//Set Bottom margin                  sheet. Pagesetup.centerhorizontally = true;                  //Set Horizontal Center                  //If you want to print files               //  sheet. PrintOut (Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);                  //save file to program run directory                                   Sheet. SaveAs ("D:\\hope.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);                 excel. Activeworkbook.close (false, NULL, NULL); Close Excel file without saving                  messagebox.show ("Build succeeded! ");             }             catch (Exception ex)             {                 messagebox.show (ex. Message);            }         }

How to use C # data for 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.