@@ -47,7 +47,7 @@ internal void SetData(QueryInfo queryinfo)
4747 ApplySettingsToGrid ( ) ;
4848 SetQueryIfChangesDesign ( ) ;
4949 txtPagingCookie . Text = queryinfo . Results . PagingCookie ;
50- mnuExcel . Enabled = queryinfo . Results ? . Entities ? . Count > 0 ;
50+ mnuExcel . Visible = queryinfo . Results ? . Entities ? . Count > 0 ;
5151
5252 mnuRetrieveTime . Text = queryinfo . Elapsed . ToSmartString ( ) ;
5353 mnuRetrieveTime . Visible = form . settings . Results . ShowRetrieveTime ;
@@ -134,11 +134,17 @@ internal void ApplySettingsToGrid()
134134 internal void SetQueryIfChangesDesign ( )
135135 {
136136 var changed = queryinfo ? . QuerySignature != form . dockControlBuilder ? . GetTreeChecksum ( null ) ;
137+ SetQueryChanged ( changed ) ;
138+ }
139+
140+ internal void SetQueryChanged ( bool changed )
141+ {
137142 Text = "Result View" + ( changed ? " *" : "" ) ;
138143 crmGridView1 . DefaultCellStyle . BackColor = changed ? System . Drawing . Color . LightGray : System . Drawing . Color . White ;
139144 crmGridView1 . DefaultCellStyle . ForeColor = changed ? System . Drawing . Color . Gray : System . Drawing . SystemColors . ControlText ;
140145 if ( changed )
141146 {
147+ mnuExcel . Visible = false ;
142148 mnuRetrieveTime . Visible = false ;
143149 mnuPage . Visible = false ;
144150 mnuPageMinus . Visible = false ;
@@ -262,17 +268,42 @@ private void OpenInExcel()
262268 var xlexcel = new Microsoft . Office . Interop . Excel . Application ( ) ;
263269 xlexcel . Visible = true ;
264270 var xlWorkBook = xlexcel . Workbooks . Add ( System . Reflection . Missing . Value ) ;
265- var xlWorkSheet = ( Microsoft . Office . Interop . Excel . Worksheet ) xlWorkBook . Worksheets . get_Item ( 1 ) ;
266- xlWorkSheet . Name = "FetchXML Builder" ;
267- Microsoft . Office . Interop . Excel . Range CR = ( Microsoft . Office . Interop . Excel . Range ) xlWorkSheet . Cells [ 1 , 1 ] ;
268- CR . Select ( ) ;
269- xlWorkSheet . PasteSpecial ( CR , Type . Missing , Type . Missing , Type . Missing , Type . Missing , Type . Missing , true ) ;
270- xlWorkSheet . Columns . AutoFit ( ) ;
271- var header = ( Microsoft . Office . Interop . Excel . Range ) xlWorkSheet . Rows [ 1 ] ;
271+
272+ // Create sheet for results
273+ var xlResultSheet = ( Microsoft . Office . Interop . Excel . Worksheet ) xlWorkBook . Worksheets . get_Item ( 1 ) ;
274+ xlResultSheet . Name = "FetchXML Builder - Result" ;
275+ // Paste all data
276+ var cellResultA1 = ( Microsoft . Office . Interop . Excel . Range ) xlResultSheet . Cells [ 1 , 1 ] ;
277+ cellResultA1 . Select ( ) ;
278+ xlResultSheet . PasteSpecial ( cellResultA1 , Type . Missing , Type . Missing , Type . Missing , Type . Missing , Type . Missing , true ) ;
279+ // Format width and headers
280+ var header = ( Microsoft . Office . Interop . Excel . Range ) xlResultSheet . Rows [ 1 ] ;
272281 header . Font . Bold = true ;
273282 header . Borders [ Microsoft . Office . Interop . Excel . XlBordersIndex . xlEdgeBottom ] . LineStyle = Microsoft . Office . Interop . Excel . XlLineStyle . xlContinuous ;
274283 header . Borders [ Microsoft . Office . Interop . Excel . XlBordersIndex . xlEdgeBottom ] . Weight = Microsoft . Office . Interop . Excel . XlBorderWeight . xlThick ;
275- xlWorkSheet . Range [ "A1" , "A1" ] . Select ( ) ;
284+ header . AutoFilter ( 1 , Type . Missing , Microsoft . Office . Interop . Excel . XlAutoFilterOperator . xlAnd , Type . Missing , true ) ;
285+ xlResultSheet . Activate ( ) ;
286+ xlResultSheet . Application . ActiveWindow . SplitRow = 1 ;
287+ xlResultSheet . Application . ActiveWindow . FreezePanes = true ;
288+ xlResultSheet . Columns . AutoFit ( ) ;
289+
290+ var xlSourceSheet = ( Microsoft . Office . Interop . Excel . Worksheet ) xlWorkBook . Sheets . Add ( After : xlWorkBook . Sheets [ xlWorkBook . Sheets . Count ] ) ;
291+ xlSourceSheet . Name = "FetchXML Builder - Source" ;
292+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 1 , 1 ] ) . Value = "Connection" ;
293+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 1 , 2 ] ) . Value = form . ConnectionDetail . ConnectionName ;
294+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 2 , 1 ] ) . Value = "URL" ;
295+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 2 , 2 ] ) . Value = form . ConnectionDetail . WebApplicationUrl ;
296+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 3 , 1 ] ) . Value = "Query" ;
297+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 3 , 2 ] ) . Value = queryinfo . Query is FetchExpression fetchexpr ? fetchexpr . Query : queryinfo . Query . ToString ( ) ;
298+ var sourceheader = ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Columns [ 1 ] ;
299+ sourceheader . Font . Bold = true ;
300+ sourceheader . Cells . VerticalAlignment = Microsoft . Office . Interop . Excel . XlVAlign . xlVAlignTop ;
301+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 1 , 1 ] ) . EntireColumn . AutoFit ( ) ;
302+ ( ( Microsoft . Office . Interop . Excel . Range ) xlSourceSheet . Cells [ 1 , 2 ] ) . EntireColumn . ColumnWidth = 150 ;
303+ xlSourceSheet . Rows . AutoFit ( ) ;
304+
305+ xlResultSheet . Activate ( ) ;
306+ xlResultSheet . Range [ "A1" , "A1" ] . Select ( ) ;
276307 }
277308 catch ( Exception ex )
278309 {
0 commit comments