I want export datagrid to excel in wpf C# and code is error at GetClipboardContent()? -
i use bundle code work in winform wpf. code error @ getclipboardcontent()
; use:
using excel = microsoft.office.interop.excel; using system.io; public class mycommon { private static mainwindow mainwindow; public static void copyalltoclipboard() { mainwindow.grdkq_task.selectall(); dataobject dataobj = mainwindow.grdkq_task.getclipboardcontent(); if (dataobj != null) clipboard.setdataobject(dataobj); } public static void releaseobject(object obj) { //same @ other post } public static void export2excel() { copyalltoclipboard(); //same @ other post } }
error:
'datagrid' not contain definition 'getclipboardcontent' , no extension method 'getclipboardcontent' accepting first argument of type 'datagrid' found (are missing using directive or assembly reference?)
the function export datagrid excel got @ here, @ 2 second answer: how export datagridview data instantly excel on button click? how solve it?
i final found way solve problem above, replace clipboard datatable store datagrid view.
using excel = microsoft.office.interop.excel; private void btnex_click(object sender, routedeventargs e) { excel.application excel = null; excel.workbook wb = null; object missing = type.missing; excel.worksheet ws = null; excel.range rng = null; try { excel = new microsoft.office.interop.excel.application(); wb = excel.workbooks.add(); ws = (microsoft.office.interop.excel.worksheet)wb.activesheet; dataview view = (dataview)grdkq_task.itemssource; datatable dt = dataviewasdatatable(view); (int idx = 0; idx < dt.columns.count; idx++) { ws.range["a1"].offset[0, idx].value = dt.columns[idx].columnname; } (int idx = 0; idx < dt.rows.count; idx++) { ws.range["a2"].offset[idx].resize[1, dt.columns.count].value = dt.rows[idx].itemarray; } excel.visible = true; wb.activate(); } catch (comexception ex) { messagebox.show("error accessing excel: " + ex.tostring()); } catch (exception ex) { messagebox.show("error: " + ex.tostring()); } } public static datatable dataviewasdatatable(dataview dv) { datatable dt = dv.table.clone(); foreach (datarowview drv in dv) dt.importrow(drv.row); return dt; }
i hope someone.
Comments
Post a Comment