///
Using microsoft.office.core;using microsoft.office.interop.excel;using system.io;using System.Reflection;
<summary>///Export button click events///</summary>//<param name= "sender" ></param>//<param name= "E" & gt;</param> private void Button1_Click (object sender, EventArgs e) {Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application (); try {Excel. SheetsInNewWorkbook = 1; Excel. Workbooks.Add (); Gets the first worksheet Worksheet Wsheet = (Worksheet) of Excel. ACTIVEWORKBOOK.WORKSHEETS[1]; Wsheet. Name = "Customer Information Form"; Set Excel column name to Excel. Cells[1, 1] = "Customer Name"; Excel. Cells[1, 2] = "Social Security Number"; Excel. Cells[1, 3] = "Check-in date"; Excel. Cells[1, 4] = "Deposit"; Excel. Cells[1, 5] = "state"; Excel. Cells[1, 6] = "Room number"; Gets the header row of the cell that is range var titilrow = Excel. Range[excel. Cells[1,1],excel. cells[1,7]] as Range; Set Font Bold TitilRow.Font.Bold = true; Set Font Color TitilRow.Font.ColorIndex = 0; Set background color TitilRow.Interior.ColorIndex = 15; Set border Style TitilRow.Borders.LineStyle = xllinestyle.xlcontinuous; Sets the width of the cell titilrow.columnwidth = 18; int I, j = 0; for (i = 0; i < DgvGuestInfo.Rows.Count; i++) {//loops put DataGridView data into Excel documents for (j = 0; J < 6; J + +) {Excel. Cells[i + 2, j + 1] = Dgvguestinfo.rows[i]. CELLS[J]. Value.tostring (); }}////set the date of birth in format//excel.get_range (Excel. Cells[2, 8], Excel. Cells[i + 2, 8]). NumberFormat = "YYYY-MM-DD"; Set the format of the ID number var identity = Excel. Range[excel. Cells[2, 2], Excel. Cells[i, 2]]. NumberFormatLocal = "0"; Gets the active row and column var activerowandcolums = Excel. Range[excel. Cells[1, 7], Excel. Cells[i, 7]] as Range; Set the active row and column center display activerowandcolums.verticalalignment = Xlvalign.xlvaligncenter; Displays the Excel file contents of Excel. Visible = true; Sets the length of the display System.Threading.Thread.Sleep (5000); Set the name of the saved file to Excel. ActiveWorkbook.SaveAs (environment.currentdirectory + "/customer information Sheet. xls", xlfileformat.xlworkbooknormal); } catch (Exception ex) {throw ex;} finally {//closes the currently active workbook Excel. Activeworkbook.close (); Quit Excel application Excel. Quit (); }} like this, you should be able to pay attention to the code in the finally!
From the
C # operations Excel basic Operations