下面小編就為大家帶來一篇C# WinForm建立Excel檔案的執行個體。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
執行個體如下:
private bool creatExcel(string xlsfile) { try { object missing = System.Reflection.Missing.Value; Excel.ApplicationClass myExcel = new Excel.ApplicationClass(); Excel._Workbook xBk; //工作薄 Excel._Worksheet xSt; //工作Sheet xBk = myExcel.Workbooks.Add(true); for (int i = 0; i < 1; i++) { xSt = (Excel._Worksheet)xBk.ActiveSheet; xSt.Name = "Sheet" + i; myExcel.Sheets.Add(missing, missing, 1, Excel.XlSheetType.xlWorksheet); } //myExcel.Visible = true; if (xlsfile.ToLower().EndsWith(".xlsx")) { xBk.SaveAs(xlsfile, 56, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing); } else { xBk.SaveAs(xlsfile, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing); } myExcel.Quit(); return true; } catch (Exception ex) { MessageBox.Show("Excel建立失敗!原因:" + ex.Message); return false; } }