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