C#不创buildExcel文件,不能写入不同的Excel表单

首先在我的代码中,打开一个excel表单并将其读入列表中。 其次我要closures这个excel表格并创build一个新的excel表格:

excel_init("C:\\Users\\oma\\Desktop\\excel2.xlsx"); // this path does NOT exist yet 

接下来去这个方法:

 static void excel_init(String path) { appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); if (System.IO.File.Exists(path)) { // then go and load this into excel newWorkbook_First = appExcel.Workbooks.Open(path, true, true); objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet; } else { try { appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); appExcel.Visible = true; newWorkbook_First = appExcel.Workbooks.Add(1); objsheet = (Microsoft.Office.Interop.Excel.Worksheet)newWorkbook_First.Sheets[1]; objsheet.Name = ("test"); var newSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet); newSheet3.Name = "test2"; var newSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet); newSheet2.Name = "test3"; } catch (Exception e) { Console.Write("Error"); } finally { } } } 

它不会在我的桌面上创buildexcel2.xlsx,为什么不呢?

接下来在我的主要中,我这样叫excel_setValue:

 excel_setValue("C", "hello", ""); 

这是excel_setValue函数:

 static void excel_setValue(string cellname, string value, string color) { objsheet.get_Range(cellname).set_Value(Type.Missing, value); if (kleur == "red") { objsheet.get_Range(cellname).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); } } 

我如何添加表单? 例如:excel_setValue(cellname,value,color,sheetname)

最后我做excel_close();

excel_close函数:

 static void excel_close() { if (appExcel != null) { try { newWorkbook_First.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel.ActiveWorkbook.ActiveSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel.ActiveWorkbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel); appExcel = null; objsheet = null; } catch (Exception ex) { appExcel = null; Console.WriteLine("Unable to release the Object " + ex.ToString()); } finally { GC.Collect(); } } } 

它closures并问我,如果我想保存/不保存或取消它。 它将保存@文件没有错误给出

我创build我的Excel这样对我工作得很好!

您需要在newWorkbook_First.close();之前的代码中执行下面的代码newWorkbook_First.close(); 部分:

  newWorkbook_First.SaveAs(totalPath); 

我希望这会帮助你解决问题..

 using Excel = Microsoft.Office.Interop.Excel; internal static bool ExportDGV(DataGridView DGV, List<string> selectedCustomerList, string path, string fileName, string exportName, int exportType, string mailSubject) { string totalPath; //Create an Excel application instance Excel.Application excelApp = new Excel.Application(); Excel.Workbook excelWorkBook = excelApp.Application.Workbooks.Add(); Excel._Worksheet worksheet = null; excelApp.Visible = false; worksheet = excelWorkBook.ActiveSheet; //set headers for (int i = 1; i < DataGridView.Columns.Count + 1; i++) { worksheet.Cells[1, i] = DGV.Columns[i - 1].HeaderText; } createList(worksheet, DGV); //Create excel with the choosen name Worksheet sheet1 = excelWorkBook.Worksheets[1]; worksheet.Name = fileName; totalPath = path + "/" + fileName + ".xlsx"; //if path exist add a number totalPath = directoryExist(totalPath, path, fileName); //Save exel and Quit exelApp excelWorkBook.SaveAs(totalPath); excelWorkBook.Close(); excelApp.Quit(); } 

您可以使用EpPlus图书馆为Excel。 这是非常容易使用和有据可查的。 你不需要使用COM类了