Creating excel(XLSX) file from StreamWriter - Error: file format or file extension is not valid

Anuj Kumar Roy 1 Reputation point
2023-01-09T08:57:05.067+00:00

Creating excel file(XLSX) using StreamWriter class corrupts the file.

Error : "Excel can not open the file filename.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file"

Code screenshot is attached.

277389-image.png

Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-01-09T11:10:26.927+00:00

    Your issue most likely is not properly writing the header, consider using SpreadSheetLight library which is free.

    Here are a few examples.

    public class Operations  
    {  
    	/// <summary>  
    	/// Create a new Excel file, rename the default sheet from  
    	/// Sheet1 to the value in pSheetName  
    	/// </summary>  
    	/// <param name="fileName"></param>  
    	/// <param name="sheetName"></param>  
    	/// <returns></returns>  
    	public bool CreateNewFile(string fileName, string sheetName)  
    	{  
    		using SLDocument document = new();  
    		document.RenameWorksheet("Sheet1", sheetName);  
    		document.SaveAs(fileName);  
    		return true;  
    	}  
    	/// <summary>  
    	/// Create a new Excel file  
    	/// </summary>  
    	/// <param name="fileName"></param>  
    	/// <returns></returns>  
    	public bool CreateNewFile(string fileName)  
    	{  
    		using SLDocument document = new();  
    		document.SaveAs(fileName);  
    		return true;  
    	}  
      
    	/// <summary>  
    	/// Add a new sheet if it does not currently exists.  
    	/// </summary>  
    	/// <param name="fileName"></param>  
    	/// <param name="sheetName"></param>  
    	/// <returns></returns>  
    	public bool AddNewSheet(string fileName, string sheetName)  
    	{  
    		using SLDocument document = new(fileName);  
    		if (!(document.GetSheetNames(false)  
    				.Any((workSheetName) => string.Equals(workSheetName, sheetName, StringComparison.CurrentCultureIgnoreCase))))  
    		{  
    			document.AddWorksheet(sheetName);  
    			document.Save();  
    			return true;  
    		}  
    		else  
    		{  
    			return false;  
    		}  
    	}  
    }  
      
    

    Otherwise, try the following

    public void CreateNewFile(string pFileName, string pSheetName)  
    {  
      
    	try  
    	{  
    		using (var doc = SpreadsheetDocument.Create(pFileName, SpreadsheetDocumentType.Workbook))  
    		{  
    			var wbp = doc.AddWorkbookPart();  
    			wbp.Workbook = new Workbook();  
      
    			var wsp = wbp.AddNewPart<WorksheetPart>();  
    			wsp.Worksheet = new Worksheet(new SheetData());  
      
    			var sheets = wbp.Workbook.AppendChild(new Sheets());  
      
    			var sheet = new Sheet()  
    			{  
    				Id = wbp.GetIdOfPart(wsp),  
    				SheetId = 1,  
    				Name = pSheetName  
    			};  
      
    			sheets?.Append(sheet);  
      
    			wbp.Workbook.Save();  
    		}  
    	}  
    	catch (Exception ex)  
    	{  
    		// TODO  
    	}  
    }  
    
    0 comments No comments

  2. Castorix31 90,686 Reputation points
    2023-01-10T09:53:46.357+00:00

    A .xlsx file is a ZIP file (PKZip format )

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.