Excel Interop - prompt to save excel not appearing - c# .net -
i using excel interop export dataset excel file. below code exports , opens excel file directly. need prompt ask user save, open , cancel. please help..
public static void createworkbook(dataset ds, string path) { int rowindex = 0; int columnindex = 0; microsoft.office.interop.excel.application wapp = new microsoft.office.interop.excel.application(); microsoft.office.interop.excel.worksheet wsheet; microsoft.office.interop.excel.workbook wbook; wapp.visible = false; wbook = wapp.workbooks.add(true); wsheet = (worksheet)wbook.activesheet; try { (int = 0; < ds.tables[0].columns.count; i++) { wsheet.cells[1, + 1] = ds.tables[0].columns[i].columnname; } foreach (datarow row in ds.tables[0].rows) { rowindex++; columnindex = 0; foreach (datacolumn col in ds.tables[0].columns) { columnindex++; wsheet.cells[rowindex + 1, columnindex] = row[col.columnname]; } } } catch (exception ex) { string err = ex.message; } wapp.usercontrol = true; wapp.visible = true; }
kind of old-ish question @ point, if still looking help:
simply said, need savefiledialog or openfiledialog, i'd imagine can see each does.
it's simple enough make well:
savefiledialog savedialog = new savefiledialog();
see msdn article reference.
openfiledialogs work same way, keep in mind these dialogs don't on own. these dialogs define file names , locations when user hits ok (and stored properties).
there's 2 ways work these: using event fileok (raised when ok clicked) or using method showdialog(). it's lot easier work when use method.
as example, if opening file:
if savedialog.showdialog() == dialogresult.ok /// stuff, save excel sheet path in dialog. end if
note in itself, though: only sets file name typed out in dialog. not save anything. you'll still need call save/open methods excel interop methods.
keep in mind can preset things initial directory or file types above if statement using respective properties listed in article.
in case, see 'path' argument method. before method call (use openfiledialog, of course), add in above if/then block 'open' path, , pass in dialog.filename property path method.
putting together, opening:
openfiledialog opendialog = new openfiledialog(); if opendialog.showdialog() == dialogresult.ok createworkbook(ds:= dataset, opendialog.filename) end if
again, remember doesn't open it. somewhere in method, after define workbook, can open wbook.open(path).
and closing, i'd avoid giving user control or making app visible outside of debugging. that's asking issues left , right.
instead, insert in location in method:
savefiledialog savedialog = new savefiledialog(); if savedialog.showdialog() == dialogresult.ok wbook.close(true, savedialog.filename, ) else wbook.close(false, , ) end if wapp.quit()
once that, manually open excel editing if still necessary. depending on need, might need tinker around idea, think need.
edit: understanding code wbook.close, see workbook.close method in msdn: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.close.aspx
Comments
Post a Comment