First, you need to add an EXCEl reference and search for it on the Internet to find the add method. Note that you should keep the EXCEl version on the server and then add using Microsoft in cs. office. interop. excel; using Microsoft. office. core; using System. reflection; using System. IO; there may be something else. Watch the VS prompt and add it on the Web. add the user name and password for Computer login in config, this is to give the program permission to call the excel dll. The following is the method to add a DataTable to export the EXCEl file. Pay attention to Chinese garbled characters in the webpage encoding. solve this problem by yourself. Haha, the following is a small example used in my project. Let's take a look at it. [csharp] /// <summary> /// export Excel /// </summary> /// <param name = "dt"> DataTable to be exported </param> publ Ic void ExportToExcel (System. data. dataTable dt) {if (dt = null) return; Microsoft. office. interop. excel. application xlApp = new Microsoft. office. interop. excel. application (); if (xlApp = null) {// lblMsg. text = "an Excel object cannot be created. Excel may not be installed on your computer"; lblMsg. text = GetLocalResourceObject ("noexcel "). toString (); return;} Microsoft. office. interop. excel. workbooks workbooks = xlApp. workbooks; Microsoft. office. Interop. excel. workbook workbook = workbooks. add (Microsoft. office. interop. excel. xlWBATemplate. xlWBATWorksheet); Microsoft. office. interop. excel. worksheet worksheet = (Microsoft. office. interop. excel. worksheet) workbook. worksheets [1]; // get sheet1 Microsoft. office. interop. excel. range range = null; long totalCount = dt. rows. count; long rowRead = 0; float percent = 0; // header range = worksheet. get_Range ("A1 ", "H1"); range. merge (Missing. value); range. font. bold = true; range. font. size = 16; range. value2 = "jinruntian raw material warehouse receiving (report) Form"; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignCenter; // 2nd rows worksheet. cells [2, 1] = "supplier:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [2, 1]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microso Ft. office. interop. excel. xlHAlign. xlHAlignRight; range = worksheet. get_Range ("B2", "D2"); range. merge (Missing. value); range. font. size = 10; range. value2 = DropDownList2.Text; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); worksheet. cells [2, 5] = "Date:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [2, 5]; r Ange. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignCenter; worksheet. cells [2, 6] = add_timetb.Text; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [2, 6]; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); worksheet. cell S [2, 7] = "Ticket No.:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [2, 7]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignCenter; worksheet. cells [2, 8] = ins_idtb.Text; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [2, 8]; range. font. size = 10; range. horizontalAlignment = M Icrosoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); // 3rd rows worksheet. cells [3, 1] = "Contract No.:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [3, 1]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignRight; range = worksheet. get_Range ("B3", "D3"); range. merge (Missing. value ); Range. font. size = 10; range. value2 = c_id_lb.Text; // The Excel cell value is range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); // 4th rows worksheet. cells [4, 1] = "Order No.:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [4, 1]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. XlHAlign. xlHAlignRight; range = worksheet. get_Range ("B4", "D4"); range. merge (Missing. value); range. font. size = 10; range. value2 = dingdan_numtb.Text; // The Excel cell value is range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); // 5th rows worksheet. cells [5, 1] = "warehouse receiving details:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [5, 1]; rang E. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; // write the title for (int I = 0; I <dt. columns. count; I ++) {worksheet. cells [6, I + 1] = dt. columns [I]. columnName; range = (Microsoft. office. interop. excel. range) worksheet. cells [6, I + 1]; // range. interior. colorIndex = 15; // the background color range. font. bold = true; // Bold range. horizontalAlignment = M Icrosoft. office. interop. excel. xlHAlign. xlHAlignCenter; // center // Add a border range. borderAround (Microsoft. office. interop. excel. xlLineStyle. xlContinuous, Microsoft. office. interop. excel. xlBorderWeight. xlThin, Microsoft. office. interop. excel. xlColorIndex. xlColorIndexAutomatic, null); // range. columnWidth = 4.63; // set the column width // range. entireColumn. autoFit (); // automatically adjust the column width // r1.EntireRow. autoFit (); // automatically adjust the Row Height} // write content for (int r = 0; R <dt. rows. count; r ++) {for (int I = 0; I <dt. columns. count; I ++) {worksheet. cells [r + 7, I + 1] = dt. rows [r] [I]; range = (Microsoft. office. interop. excel. range) worksheet. cells [r + 7, I + 1]; range. font. size = 10; // The font Size range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignCenter; // center // Add a border range. borderAround (Microsoft. office. interop. excel. xlLineStyle. xlContinuous, Micro Soft. office. interop. excel. xlBorderWeight. xlThin, Microsoft. office. interop. excel. xlColorIndex. xlColorIndexAutomatic, null); range. entireColumn. autoFit (); // automatically adjust the column width} rowRead ++; percent = (float) (100 * rowRead)/totalCount; System. windows. forms. application. doEvents () ;}for (int I = 0; I <8; I ++) {range = (Microsoft. office. interop. excel. range) worksheet. cells [(7 + totalCount), I + 1]; range. font. S Ize = 10; // font size // Add the border range. borderAround (Microsoft. office. interop. excel. xlLineStyle. xlContinuous, Microsoft. office. interop. excel. xlBorderWeight. xlThin, Microsoft. office. interop. excel. xlColorIndex. xlColorIndexAutomatic, null) ;}for (int I = 0; I <8; I ++) {range = (Microsoft. office. interop. excel. range) worksheet. cells [(8 + totalCount), I + 1]; range. font. size = 10; // font Size // Add the border range. borderAround (M Icrosoft. office. interop. excel. xlLineStyle. xlContinuous, Microsoft. office. interop. excel. xlBorderWeight. xlThin, Microsoft. office. interop. excel. xlColorIndex. xlColorIndexAutomatic, null);} // 7th + totalCount row worksheet. cells [(8 + totalCount), 1] = "Total (RMB/RMB):"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(8 + totalCount), 1]; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); worksheet. cells [(8 + totalCount), 2] = heji_lb.Text; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(8 + totalCount), 2]; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); worksheet. cells [(8 + totalCou Nt), 3] = "tax invoice:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(8 + totalCount), 3]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignCenter; worksheet. cells [(8 + totalCount), 4] = shuilvTB. text; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(8 + totalCount), 4]; range. Font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); // 9th + totalCount row worksheet. cells [(9 + totalCount), 1] = "Quality Control:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(9 + totalCount), 1]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAli Gn. xlHAlignLeft; worksheet. cells [(9 + totalCount), 2] = zhijiantb. text; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(9 + totalCount), 2]; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); worksheet. cells [(9 + totalCount), 3] = "warehouse:"; // Excel cell value range = (Microsoft. office. interop. exce L. range) worksheet. cells [(9 + totalCount), 3]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignCenter; worksheet. cells [(9 + totalCount), 4] = kufangtb. text; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(9 + totalCount), 4]; range. font. size = 10; range. horizontalAlignment = Microsoft. office. intero P. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); worksheet. cells [(9 + totalCount), 5] = "Purchase:"; // Excel cell value range = (Microsoft. office. interop. excel. range) worksheet. cells [(9 + totalCount), 5]; range. font. bold = true; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignCenter; worksheet. cells [(9 + totalCount), 6] = shengchantb. text; // Excel Unit Lattice value range = (Microsoft. office. interop. excel. range) worksheet. cells [(9 + totalCount), 6]; range. font. size = 10; range. horizontalAlignment = Microsoft. office. interop. excel. xlHAlign. xlHAlignLeft; range. entireColumn. autoFit (); range. borders [Microsoft. office. interop. excel. xlBordersIndex. xlInsideHorizontal]. weight = Microsoft. office. interop. excel. xlBorderWeight. xlThin; if (dt. columns. count> 1) {range. B Orders [Microsoft. office. interop. excel. xlBordersIndex. xlInsideVertical]. weight = Microsoft. office. interop. excel. xlBorderWeight. xlThin;} try {workbook. saved = true; workbook. saveCopyAs (System. web. httpRuntime. appDomainAppPath + "XMLFiles \ EduceWordFiles \" + ins_idtb.Text + ". xls ");} catch (Exception ex) {// lblMsg. text = "An error occurred while exporting the file. The file may be opened! \ N "+ ex. message; lblMsg. text = GetLocalResourceObject ("error "). toString () + "\ n" + ex. message;} workbooks. close (); if (xlApp! = Null) {xlApp. workbooks. close (); xlApp. quit (); int generation = System. GC. getGeneration (xlApp); System. runtime. interopServices. marshal. releaseComObject (xlApp); xlApp = null; System. GC. collect (generation);} GC. collect (); // forcibly destroy # region forcibly kills the recently opened Excel process System. diagnostics. process [] excelProc = System. diagnostics. process. getProcessesByName ("EXCEL"); System. dateTime startTime = new DateTime (); int m, killId = 0; for (m = 0; m <excelProc. length; m ++) {if (startTime <excelProc [m]. startTime) {startTime = excelProc [m]. startTime; killId = m ;}} if (excelProc [killId]. hasExited = false) {excelProc [killId]. kill () ;}# endregion // provides download // BIClass. businessLogic. util. responseFile (Page. request, Page. response, "ReportToExcel.xls" //, System. web. httpRuntime. appDomainAppPath + "XMLFiles \ EduceWordFiles \" + this. context. user. identity. name + ". xls ", 1024000); string fileName =" warehouse receiving ticket-"+ ins_idtb.Text; // string filePath = Server. mapPath (".. /XMLFiles/EduceWordFiles/"+ ins_idtb.Text + ". xls "); // path FileInfo fileInfo = new FileInfo (filePath); Response. clear (); Response. clearContent (); Response. clearHeaders (); Response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (fileName + ". xls ") +" "); Response. addHeader ("Content-Length", fileInfo. length. toString (); Response. addHeader ("Content-Transfer-Encoding", "binary"); Response. contentType = "application/octet-stream"; Response. charset = "UTF-8"; Response. contentEncoding = System. text. encoding. UTF8; Response. writeFile (fileInfo. fullName); Response. flush (); Response. end ();}