diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs index fb906e0..405623a 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs @@ -405,6 +405,7 @@ /// public void Save() { + checkForWorksheetsPresent(); if (_loadSource == XLLoadSource.New) throw new Exception("This is a new file, please use one of the SaveAs methods."); @@ -421,6 +422,7 @@ /// public void SaveAs(String file) { + checkForWorksheetsPresent(); PathHelper.CreateDirectory(Path.GetDirectoryName(file)); if (_loadSource == XLLoadSource.New) { @@ -462,11 +464,18 @@ throw new Exception(String.Format("Extension '{0}' is not supported. Supported extensions are '.xlsx' and '.xslm'.", extension)); } + private void checkForWorksheetsPresent() + { + if (Worksheets.Count() == 0) + throw new Exception("Workbooks need at least one worksheet."); + } + /// /// Saves the current workbook to a stream. /// public void SaveAs(Stream stream) { + checkForWorksheetsPresent(); if (_loadSource == XLLoadSource.New) { // dm 20130422, this method or better the method SpreadsheetDocument.Create which is called diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs index f99b0a2..abacd10 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs @@ -16,8 +16,6 @@ private static void Main(string[] args) { var wb = new XLWorkbook(); - var ws = wb.AddWorksheet("Sheet"); - wb.Protect(); wb.SaveAs(@"c:\temp\saved.xlsx"); Console.WriteLine("Done"); }