private void Delete(int?sheetNumber = null, string sheetName = null) { CheckFiles(); PrepareArchive(false); // Get worksheet details Worksheet worksheet = new Worksheet(); worksheet.GetWorksheetProperties(this, sheetNumber, sheetName); // Delete the file if (!string.IsNullOrEmpty(worksheet.FileName)) { ZipArchiveEntry entry = this.Archive.GetEntry(worksheet.FileName); if (entry != null) { entry.Delete(); } if (this.DeleteWorksheets == null) { this.DeleteWorksheets = new List <int>(); } this.DeleteWorksheets.Add(worksheet.Index); } }
private Worksheet Read(int?sheetNumber = null, string sheetName = null, int existingHeadingRows = 0) { CheckFiles(); PrepareArchive(); Worksheet worksheet = new Worksheet(); worksheet.ExistingHeadingRows = existingHeadingRows; worksheet.GetWorksheetProperties(this, sheetNumber, sheetName); IEnumerable <Row> rows = null; List <string> headings = new List <string>(); using (Stream stream = this.Archive.GetEntry(worksheet.FileName).Open()) { XDocument document = XDocument.Load(stream); int skipRows = 0; Row possibleHeadingRow = new Row(document.Descendants().Where(d => d.Name.LocalName == "row").FirstOrDefault(), this.SharedStrings); if (worksheet.ExistingHeadingRows == 1 && possibleHeadingRow.RowNumber == 1) { foreach (Cell headerCell in possibleHeadingRow.Cells) { headings.Add(headerCell.Value.ToString()); } } rows = GetRows(document.Descendants().Where(d => d.Name.LocalName == "row").Skip(skipRows)); } worksheet.Headings = headings; worksheet.Rows = rows; return(worksheet); }
private void Write(Worksheet worksheet, int?sheetNumber = null, string sheetName = null, int existingHeadingRows = 0) { CheckFiles(); try { if (!this.UpdateExisting) { File.Copy(this.TemplateFile.FullName, this.ExcelFile.FullName); } } catch (Exception ex) { throw new Exception("Could not copy template to output file path", ex); } PrepareArchive(); // Open worksheet worksheet.GetWorksheetProperties(this, sheetNumber, sheetName); worksheet.ExistingHeadingRows = existingHeadingRows; if (this.Archive.Mode != ZipArchiveMode.Update) { throw new Exception("FastExcel is in ReadOnly mode so cannot perform a write"); } // Check if ExistingHeadingRows will be overridden by the dataset if (worksheet.ExistingHeadingRows != 0 && worksheet.Rows.Where(r => r.RowNumber <= worksheet.ExistingHeadingRows).Any()) { throw new Exception("Existing Heading Rows was specified but some or all will be overridden by data rows. Check DataSet.Row.RowNumber against ExistingHeadingRows"); } using (Stream stream = this.Archive.GetEntry(worksheet.FileName).Open()) { // Open worksheet and read the data at the top and bottom of the sheet StreamReader streamReader = new StreamReader(stream); worksheet.ReadHeadersAndFooters(streamReader, ref worksheet); //Set the stream to the start stream.Position = 0; // Open the stream so we can override all content of the sheet StreamWriter streamWriter = new StreamWriter(stream); // TODO instead of saving the headers then writing them back get position where the headers finish then write from there streamWriter.Write(worksheet.Headers); if (!worksheet.Template) { worksheet.Headers = null; } this.SharedStrings.ReadWriteMode = true; // Add Rows foreach (Row row in worksheet.Rows) { streamWriter.Write(row.ToXmlString(this.SharedStrings)); } this.SharedStrings.ReadWriteMode = false; //Add Footers streamWriter.Write(worksheet.Footers); if (!worksheet.Template) { worksheet.Footers = null; } streamWriter.Flush(); } }