Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Excel file not created as expected

Hi,
Excel file is not created by these codes
                        string Path0="c:\\dp2\\Ex0.xlsx";
                        CreateSpreadsheetWorkbook3(Path0);

        public static void CreateSpreadsheetWorkbook3(string filepath)
        {
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                Create(filepath, SpreadsheetDocumentType.Workbook);

            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());

            DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
                AppendChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>(new DocumentFormat.OpenXml.Spreadsheet.Sheets());

            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.
                    GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "mySheet"
            };
            sheets.Append(sheet);

            workbookpart.Workbook.Save();

            spreadsheetDocument.Close();
        }

Open in new window

Why?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Telling us the error message would help us to explain why it doesn't work.

If there is no error message then you code is either working correctly or just not being run at all.
I can create the Excel file without any error using your existing code.

User generated image
hence, please make sure:
-the folder is exist
-you got enough permission to access/modify the folder
-you got enough permission to execute your code (standalone executable or asp.net page, etc)
Avatar of Peter Chan

ASKER

Sorry Ryan, file is created below
https://1drv.ms/x/s!Ai8CrEskdewXkyzD2kN7kufIeEvL

 but I get this error
User generated image
to open it.
As I said earlier - please tell us any error message you have.

What happens if you try 'yes' to the recover option?
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Andy,
There is no error got with the codes.

Ryan,
How to resolve the problematic message, when opening the file shown in ahove?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial