0

I have this code which is to export data from datatable in excel, and what i want to do instead to write the location myself in the @"", i want to make the user to choose the location by himself, thanks.

DataTable dt = hpl.SearchUserGroup(Convert.ToInt32(txtSearch.Text));
Workbook book = new Workbook();                
Worksheet sheet = book.Worksheets[0];               
sheet.InsertDataTable(dt, true, 1, 1);
book.SaveToFile(@"", ExcelVersion.Version97to2003);  
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • This might be what you're looking for: https://stackoverflow.com/questions/11624298/how-to-use-openfiledialog-to-select-a-folder – Sasha Nov 27 '17 at 14:21
  • 2
    Asp.Net? WPF? Winforms? Console? – Crowcoder Nov 27 '17 at 14:21
  • @Jaxi I tried that, but i get this error "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process." i don't know why – Darko Arnaudov Nov 27 '17 at 14:24
  • @DarkoArnaudov Since a UI (often) requires a single main thread, your application must behave in a specific way ; see https://stackoverflow.com/questions/127188/could-you-explain-sta-and-mta – Timothy Groote Nov 27 '17 at 14:32

1 Answers1

0

You might need something like below. I didn't get a chance to test this in Visual Studio

string folderPath = string.Empty;  

DataTable dt = hpl.SearchUserGroup(Convert.ToInt32(txtSearch.Text));
Workbook book = new Workbook();                
Worksheet sheet = book.Worksheets[0];               
sheet.InsertDataTable(dt, true, 1, 1);

using(var folderLoc = new FolderBrowserDialog())
{
    DialogResult dlgResult = folderLoc.ShowDialog();

    if (dlgResult == DialogResult.OK )
    {
        folderPath = folderLoc.SelectedPath;        
    }
}
//create a temp file with .xls extension
folderPath += Path.GetTempFileName().Replace(".tmp", ".xls");

book.SaveToFile(folderPath, ExcelVersion.Version97to2003);  

Please let me know if you face any issues.

Shammas
  • 381
  • 1
  • 4
  • 15
  • what is fbd stands for? cause is showing me error on that word? – Darko Arnaudov Nov 27 '17 at 14:42
  • "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process." This error is showing me on this line DialogResult dlgResult = folderLoc.ShowDialog(); – Darko Arnaudov Nov 27 '17 at 14:46
  • Are you developing a desktop application or Console application?. – Shammas Nov 27 '17 at 14:47
  • Desktop Application – Darko Arnaudov Nov 27 '17 at 14:49
  • I presume you are developing windows application. Add the STA thread attribute before your main method. [STAThread] static void Main(string[] args) { } – Shammas Nov 27 '17 at 14:51
  • https://social.msdn.microsoft.com/Forums/windows/en-US/2411f889-8e30-4a6d-9e28-8a46e66c0fdb/current-thread-must-be-set-to-single-thread-apartment-sta-mode-before-ole?forum=clr – Shammas Nov 27 '17 at 14:52
  • nop..[STAThread] should be above your Main method in program.cs file – Shammas Nov 27 '17 at 15:02
  • but i dont have any main method, first is page load then are all events, im working on asp.net – Darko Arnaudov Nov 27 '17 at 15:13
  • Are you sure you are developing a windows application?. Can you find any program.cs file in your project directory?. – Shammas Nov 27 '17 at 15:14
  • Yes im sure, in asp.net – Darko Arnaudov Nov 27 '17 at 15:21
  • Sorry my friend.. you really need to understand the difference between Windows applications and web applications. Asp.net is used to develop web applications not windows/desktop applications – Shammas Nov 27 '17 at 15:27
  • What i need to do to solve this problem in web applications? – Darko Arnaudov Nov 27 '17 at 15:29