Export selected range of cells to CSV format

I have
   private void toolStripButton1_Click(object sender, EventArgs e)
        { //Export  of cells to CSV format...........
  private void toolStripButton2_Click(object sender, EventArgs e)
        { //Export selected range of cells to CSV format.............
1.
why it does not work
Export selected range of cells to CSV

2.
how to make it change its name dynamically
gridGroupingControl??

in
csvConverter.GridToCSV(this.gridGroupingControl1.TableModel, saveFileDialog.FileName);


depending where the last mouse cursor was
focus - gridGroupingControl1 or gridGroupingControl2

export from
gridGroupingControl
last mouse clicked 1 or 2


please help


Attachment: export_csv_f1ed470.rar

9 Replies

MG Mohanraj Gunasekaran Syncfusion Team July 26, 2018 12:56 PM UTC

 
Thanks for using Syncfusion product.  
 
Query  
Solution  
why it does not work
Export selected range of cells to CSV
  
  
  
Suggestion1  
By default, GridGroupingControl does not have the support to export the selected cells(ListBoxSelectionMode) in csv format. But you can export the selected cells by using AllowSelection property. Please refer to the below code example and refer to the below modified sample,  
 
Code example   
this.gridGroupingControl1.TableOptions.ListBoxSelectionMode = SelectionMode.MultiExtended;  
this.gridGroupingControl2.TableOptions.ListBoxSelectionMode = SelectionMode.MultiExtended;  
this.gridGroupingControl1.TableOptions.AllowSelection = GridSelectionFlags.Any;  
this.gridGroupingControl2.TableOptions.AllowSelection = GridSelectionFlags.Any;  
 
Suggestion2  
In order to export the selected records in csv using ListBoxSelectionMode, you can use the GridGroupingExcelConverterControl to export the record and QueryExportRowRange event to handle the unselected records. Please refer to the below code example,  
 
Code example  
GridGroupingExcelConverterControl converter = new GridGroupingExcelConverterControl();  
converter.QueryExportRowRange += Converter_QueryExportRowRange;  
converter.ExportToExcel(this.grid, "Sample.xlsx", new ExcelExportingOptions());  
using (ExcelEngine engine = new ExcelEngine())  
{  
    IWorkbook book = engine.Excel.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);  
    book.SaveAs("Sample.csv");  
    book.Close();  
}  
private void Converter_QueryExportRowRange(object sender, Syncfusion.GroupingGridExcelConverter.QueryExportRowRangeEventArgs e)  
{  
    if (!e.Element.IsRecord() || !e.Element.GetRecord().IsSelected())  
    {  
        e.Cancel = true;  
    }  
}   
how to make it change its name dynamically
gridGroupingControl?? 

in
  
csvConverter.GridToCSV(this.gridGroupingControl1.TableModel, saveFileDialog.FileName);  
  

depending where the last mouse cursor was
focus - gridGroupingControl1 or gridGroupingControl2

export from
gridGroupingControl 
  
last mouse clicked 1 or 2  
 
In order to get the last focused GridGroupingControl, you can use the TableControl.Click event. Please refer to the below code example,  
 
Code example  
GridGroupingControl grid;  
foreach (Control control in this.Controls)  
{  
    var ggc = control as GridGroupingControl;  
    if (ggc != null)  
    {  
        ggc.TableControl.Click += TableControl_Click;  
    }   
}  
  
private void TableControl_Click(object sender, EventArgs e)  
{  
    GridTableControl tableControl = sender as GridTableControl;  
    if (tableControl != null)  
        grid = tableControl.GroupingControl;  
}  
  
  
 
Sample link: GridGroupingControl  
 
Regards,  
Mohanraj G  
 



GP Gregory Pe replied to Mohanraj Gunasekaran July 26, 2018 07:53 PM UTC

 
Thanks for using Syncfusion product.  
 
Query  
Solution  
why it does not work
Export selected range of cells to CSV
  
  
  
Suggestion1  
By default, GridGroupingControl does not have the support to export the selected cells(ListBoxSelectionMode) in csv format. But you can export the selected cells by using AllowSelection property. Please refer to the below code example and refer to the below modified sample,  
 
Code example   
this.gridGroupingControl1.TableOptions.ListBoxSelectionMode = SelectionMode.MultiExtended;  
this.gridGroupingControl2.TableOptions.ListBoxSelectionMode = SelectionMode.MultiExtended;  
this.gridGroupingControl1.TableOptions.AllowSelection = GridSelectionFlags.Any;  
this.gridGroupingControl2.TableOptions.AllowSelection = GridSelectionFlags.Any;  
 
Suggestion2  
In order to export the selected records in csv using ListBoxSelectionMode, you can use the GridGroupingExcelConverterControl to export the record and QueryExportRowRange event to handle the unselected records. Please refer to the below code example,  
 
Code example  
GridGroupingExcelConverterControl converter = new GridGroupingExcelConverterControl();  
converter.QueryExportRowRange += Converter_QueryExportRowRange;  
converter.ExportToExcel(this.grid, "Sample.xlsx", new ExcelExportingOptions());  
using (ExcelEngine engine = new ExcelEngine())  
{  
    IWorkbook book = engine.Excel.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);  
    book.SaveAs("Sample.csv");  
    book.Close();  
}  
private void Converter_QueryExportRowRange(object sender, Syncfusion.GroupingGridExcelConverter.QueryExportRowRangeEventArgs e)  
{  
    if (!e.Element.IsRecord() || !e.Element.GetRecord().IsSelected())  
    {  
        e.Cancel = true;  
    }  
}   
how to make it change its name dynamically
gridGroupingControl?? 

in
  
csvConverter.GridToCSV(this.gridGroupingControl1.TableModel, saveFileDialog.FileName);  
  

depending where the last mouse cursor was
focus - gridGroupingControl1 or gridGroupingControl2

export from
gridGroupingControl 
  
last mouse clicked 1 or 2  
 
In order to get the last focused GridGroupingControl, you can use the TableControl.Click event. Please refer to the below code example,  
 
Code example  
GridGroupingControl grid;  
foreach (Control control in this.Controls)  
{  
    var ggc = control as GridGroupingControl;  
    if (ggc != null)  
    {  
        ggc.TableControl.Click += TableControl_Click;  
    }   
}  
  
private void TableControl_Click(object sender, EventArgs e)  
{  
    GridTableControl tableControl = sender as GridTableControl;  
    if (tableControl != null)  
        grid = tableControl.GroupingControl;  
}  
  
  
 
Sample link: GridGroupingControl  
 
Regards,  
Mohanraj G  
 


Hi, Mohanraj

Thank you for your answer
 private void button1_Click(object sender, EventArgs e)
        {
            GridGroupingExcelConverterControl converter = new GridGroupingExcelConverterControl();
            converter.QueryExportRowRange += Converter_QueryExportRowRange;
            converter.ExportToExcel(this.grid, "Sample.xlsx", new ExcelExportingOptions());
            using (ExcelEngine engine = new ExcelEngine())
            {
                IWorkbook book = engine.Excel.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
                book.SaveAs("Sample.csv");
                book.Close();
            }
            if (MessageBox.Show("Do you wish to open the csv file now?", "Export to Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo.FileName = "Sample.csv";
                proc.Start();
            }
        }

....................
Sample.csv   -but the file structure is not csv

Regards,
Gregory



MG Mohanraj Gunasekaran Syncfusion Team July 27, 2018 11:52 AM UTC

 
Sorry for the inconvenience.   
   
To save.xlsx file as .csv format, you should use SaveAs method with filename and separator (“,”). Please refer to the below code example and the sample,   
 
Code example 
IWorkbook book = engine.Excel.Workbooks.Open("Sample.xlsx",ExcelOpenType.Automatic);   
book.SaveAs("Sample.csv",”,”);   
book.Close();   
 
 
  
Sample link: GridGroupingControl 
 
Please refer to the below UG link, 
 
Regards, 
Mohanraj G 



GP Gregory Pe replied to Mohanraj Gunasekaran July 27, 2018 01:48 PM UTC

 
Sorry for the inconvenience.   
   
To save.xlsx file as .csv format, you should use SaveAs method with filename and separator (“,”). Please refer to the below code example and the sample,   
 
Code example 
IWorkbook book = engine.Excel.Workbooks.Open("Sample.xlsx",ExcelOpenType.Automatic);   
book.SaveAs("Sample.csv",”,”);   
book.Close();   
 
 
  
Sample link: GridGroupingControl 
 
Please refer to the below UG link, 
 
Regards, 
Mohanraj G 


Hi Mohanraj
thank you .. it's great
... is it possible to not export columns if width = 0


Regards, 
Gregory



MG Mohanraj Gunasekaran Syncfusion Team July 30, 2018 07:29 AM UTC

Hi Gregory, 
   
Thanks for your update.   
    
To exclude the column while exporting when column width is 0, you could use ExcludeColumns property. Please refer to the below code example and the UG link:  
  
Code example  
GridGroupingExcelConverterControl converter = new GridGroupingExcelConverterControl(); 
ExcelExportingOptions options = new ExcelExportingOptions(); 
List<string> hideColumns = new List<string>(); 
foreach (var column in this.grid.TableDescriptor.Columns) 
{ 
    if (column.Width == 0) 
        hideColumns.Add(column.Name); 
} 
options.ExcludeColumns = hideColumns; 
converter.ExportToExcel(this.grid, "Sample.xlsx", options); 
  
  
  
Sample link: GridGroupingControl 
  
Regards, 
Mohanraj G 
 



GP Gregory Pe replied to Mohanraj Gunasekaran July 30, 2018 09:06 AM UTC

Hi Gregory, 
   
Thanks for your update.   
    
To exclude the column while exporting when column width is 0, you could use ExcludeColumns property. Please refer to the below code example and the UG link:  
  
Code example  
GridGroupingExcelConverterControl converter = new GridGroupingExcelConverterControl(); 
ExcelExportingOptions options = new ExcelExportingOptions(); 
List<string> hideColumns = new List<string>(); 
foreach (var column in this.grid.TableDescriptor.Columns) 
{ 
    if (column.Width == 0) 
        hideColumns.Add(column.Name); 
} 
options.ExcludeColumns = hideColumns; 
converter.ExportToExcel(this.grid, "Sample.xlsx", options); 
  
  
  
Sample link: GridGroupingControl 
  
Regards, 
Mohanraj G 
 


Hi Mohanraj
thank you .. it's great
how to export to excel .. is it possible to not export columns if width = 0
private void button2_Click(object sender, EventArgs e)
        {//export to excel......


Regards, 
Gregory

Attachment: export_csv1230351560_135db1c9.rar


MG Mohanraj Gunasekaran Syncfusion Team August 1, 2018 12:03 PM UTC

Hi Gregory, 
 
Thanks for your update.  
 
As per our previous update, you could avoid the hided columns while exporting to excel by using ExcludeColumns property. Please refer to the below code example and the modified sample, 
 
Code example  
GridGroupingExcelConverterControl converter = newGridGroupingExcelConverterControl();  
ExcelExportingOptions options = new ExcelExportingOptions();  
List<string> hideColumns = new List<string>();  
foreach (var column in this.grid.TableDescriptor.Columns)  
 
    if (column.Width == 0)  
        hideColumns.Add(column.Name);  
 
options.ExcludeColumns = hideColumns;  
converter.ExportToExcel(this.grid, "Sample.xlsx", options);  
 
 
Screenshot 
 
 
Modified Sample link: GridGroupingControl 
 
Please let us know if we misunderstood your requirement. 

Regards, 
Mohanraj G 
 



GP Gregory Pe replied to Mohanraj Gunasekaran August 1, 2018 03:07 PM UTC

Hi Gregory, 
 
Thanks for your update.  
 
As per our previous update, you could avoid the hided columns while exporting to excel by using ExcludeColumns property. Please refer to the below code example and the modified sample, 
 
Code example  
GridGroupingExcelConverterControl converter = newGridGroupingExcelConverterControl();  
ExcelExportingOptions options = new ExcelExportingOptions();  
List<string> hideColumns = new List<string>();  
foreach (var column in this.grid.TableDescriptor.Columns)  
 
    if (column.Width == 0)  
        hideColumns.Add(column.Name);  
 
options.ExcludeColumns = hideColumns;  
converter.ExportToExcel(this.grid, "Sample.xlsx", options);  
 
 
Screenshot 
 
 
Modified Sample link: GridGroupingControl 
 
Please let us know if we misunderstood your requirement. 

Regards, 
Mohanraj G 
 


Hi Mohanraj
is great
thank you


Regards, 
Gregory



MG Mohanraj Gunasekaran Syncfusion Team August 2, 2018 05:25 AM UTC

Hi Gregory, 
 
Thanks for your update. 
 
We are glad to know that your reported problem has resolved. 
 
Please let us know if you have any concerns. 
 
Regards, 
Mohanraj G 


Loader.
Up arrow icon