Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what's my wrong everyone? i don't know why savefiledialog can't create file excel, i'm check see 1 file app created is shortcut temporary file in (C:\Users\admin\AppData\Roaming\Microsoft\Windows\Recent)

What I have tried:

private void btnExportExcel_Click(object sender, EventArgs e)  
     {  
           
         string filePath = GetFileNameToSave();  
  
         if (string.IsNullOrEmpty(filePath))  
         {  
               return;  
  
              
         }  
        pivotGrid.ExportToXlsx(filePath);  
         ActionNotifier.Information("Exported file  Excel.");  
  
         System.Diagnostics.Process.Start(filePath);  
     }  
  
     static string GetFileNameToSave()  
     {  
         string fNameDefault = String.Format("{0}.{1}.xlsx", "Export", DateTime.Now.ToString("yy.MM.dd_hh.mm"));  
         using (SaveFileDialog dlgSave = new SaveFileDialog { Title = "Export Excel", Filter = "Excel file |*.xlsx ", FileName = fNameDefault })  
         {  
               
             dlgSave.OverwritePrompt = true;  
             dlgSave.RestoreDirectory = true;  
             if (dlgSave.ShowDialog() != DialogResult.OK || !dlgSave.CheckPathExists)  
               
                 return null;  
                   
             dlgSave.AddExtension = true;  
  
               
               
             string fileName = dlgSave.FileName.Trim();  
              
             if (!fileName.EndsWith(".xlsx"))  
                 fileName += ".xlsx";  
               
             return fileName;  
         }  
     }  
Posted
Updated 21-Sep-17 17:26pm
Comments
Dave Kreskowiak 21-Sep-17 23:10pm    
What makes you think it doesn't work? What kind of app is this? ASP.NET or what?

Do you get a SaveFileDialog? Do you get the filename back from it as expected? Is a file being created at the path specified by fileName?

"It doesn't work" is not a proper problem description.
Đạt Lữ 21-Sep-17 23:22pm    
C# winform
description:
it's real no work, i didin't get file
Richard MacCutchan 22-Sep-17 3:26am    
Where is the code that saves the file to disk?

1 solution

OK, so put a breakpoint on the second line of the button Click handler and run the code.

When the breakpoint is hit, hover the mouse over filePath to inspect the content. You should get the fully qualified path to the file to write to. If not, you've got a problem in your GetFileNameToSave method. Put a breakpoint in there and run it again.

If you do have a filepath, the problem is going to be in your pivotGrid, whatever that is.
 
Share this answer
 
Comments
Đạt Lữ 21-Sep-17 23:50pm    
My problem just bug on server, mean(user just run app = shortcut with role) don't run directly app.
p/s: my app run on local normal dont have problem

Debug ok, path right not wrong.
Dave Kreskowiak 22-Sep-17 8:36am    
I have no idea what you're saying.

Server? This is a WinForms app. There is no server involved in this code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900