none
How do I automatically save in a SaveFileDialog, without the need for the dialog to be displayed? RRS feed

  • Question

  •  private bool DataGridviewImportToExcel(DataGridView[] dgv, string fileName)
            {

                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel file|*.xls";
                saveDialog.FileName = fileName;
                saveDialog.ShowDialog();
                
                
                saveFileName = saveDialog.FileName;
                if (saveFileName.IndexOf(":") < 0)
                    return false;



                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("can not create Excel");
                    return false;
                }
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                
                for (int index = 0; index < dgv.Length; index++)
                {
                    string[] sheetsCalidad = new string[]{ "TempZonaRec", "TempCalentamiento","TempHomogenizacion","TiemDeshornamiento","TempAustenizacion","TempConformado","TempTemple","FlujoAguaTamb2", "FlujoAguaTamb3","TempAguaTamb1","TempAguaTamb2","VelTamb2","VelTamb3","VelMesEnfr2","TempEcuLadDer","TempSalMesEnfr2" };

                    for (int i = 0; i < dgv[index].ColumnCount; i++)
                    {
                        worksheet.Cells[1, i + 1] = dgv[index].Columns[i].HeaderText;
                        
                    }

                    for (int r = 0; r < dgv[index].Rows.Count; r++)
                    {
                        for (int i = 0; i < dgv[index].ColumnCount; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = dgv[index].Rows[r].Cells[i].Value.ToString();

                        }
                        System.Windows.Forms.Application.DoEvents();
                    }
                    worksheet.Columns.EntireColumn.AutoFit();
                    worksheet.Name = sheetsCalidad[index];
                    if (index < dgv.Length - 1)
                    {
                        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add();
                    }
                }
                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error,file maybe is opening!\n" + ex.Message);
                        return false;
                    }
                }
                xlApp.Quit();
                GC.Collect();
                MessageBox.Show("File: " + fileName + ".xls save Successfully", "tip ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
    Tuesday, February 4, 2020 2:32 PM

All replies

  • Perhaps you misunderstand what SaveFileDialog does. All it does is display a UI to the user so they can select the path/file to save to. It doesn't actually do any saving itself. If you don't want the user to select the path/file to save to then simply use whatever filepath you want.

    //Set the path to save to
    saveFileName = @"C:\temp\test.txt";
    
    //Uncomment this to let the user decide where to save
    //SaveFileDialog saveDialog = new SaveFileDialog();
    //            saveDialog.DefaultExt = "xls";
    //            saveDialog.Filter = "Excel file|*.xls";
    //            saveDialog.FileName = fileName;
    //            saveDialog.ShowDialog();
                         
    //           saveFileName = saveDialog.FileName;
    //           if (saveFileName.IndexOf(":") < 0)
    //                return false;
    


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, February 4, 2020 2:37 PM
    Moderator
  • Hi OscarMolleapaza
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 13, 2020 5:55 AM