none
Export DataGridview to Excel save working local but not save over network RRS feed

  • Question

  • Problem

    Export datagridview To Excel file on network folder not save but it save on local computer without problem .

    version of Excel 2010 .

    operating system is windows 7 .

    VisualStudio version 2015

    my code as below 

    my Function ExportExcel not save on network folder when click button Export why ???

    public static void ExportExcel(string fileName, DataGridView myDGV)
            {
                if (myDGV.Rows.Count > 0)
                {
                    string saveFileName = "";
                    SaveFileDialog saveDialog = new SaveFileDialog();
                    saveDialog.DefaultExt = "xlsx";
                    saveDialog.Filter = "Excel file|*.xlsx";
                    saveDialog.FileName = fileName;
                    saveDialog.ShowDialog();
                    saveFileName = saveDialog.FileName;
                    if (saveFileName.IndexOf(":") < 0) return; //cancel  
                    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                    if (xlApp == null)
                    {
                        MessageBox.Show("can not create Excel file,make sure you installed Excel in your computer");
                        return;
                    }
    
                    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];//get sheet1  
    
                    //Header 
                    for (int i = 0; i < myDGV.ColumnCount; i++)
                    {
                        worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
                    }
                    //Content  
                    for (int r = 0; r < myDGV.Rows.Count; r++)
                    {
                        for (int i = 0; i < myDGV.ColumnCount; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
                        }
                        Application.DoEvents();
                    }
                    worksheet.Columns.EntireColumn.AutoFit();
    
                    if (saveFileName != "")
                    {
                        try
                        {
                            workbook.Saved = true;
                            workbook.SaveCopyAs(saveFileName);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error, the file might be opened\n" + ex.Message);
                        }
                    }
                    xlApp.Quit();
                    GC.Collect();
                    MessageBox.Show(fileName + "save successful", "prompt", MessageBoxButtons.OK);
                }
                else
                {
                    MessageBox.Show("the datagridview is empty", "prompt", MessageBoxButtons.OK);
                }
            }
            private void BtnExport_Click(object sender, EventArgs e)
            {
                string str = "WrongData" + DateTime.Now.ToString("dd-MM-yyyy");
                ExportExcel(str, GridShowErrors);
            }
    Why Not Accept To save over network folder on another computer share but save locally on my computer

    • Moved by CoolDadTx Monday, August 6, 2018 2:32 PM Office related
    Friday, August 3, 2018 8:36 AM

All replies

  • A guess would be this is a Windows permission/rights issue on the other computer, it's not a code issue.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, August 3, 2018 8:53 AM
  • What error are you getting?

    Kind regards

    Friday, August 3, 2018 9:52 AM
  • im not get any error 

    only saved on local computer but not save on another computer

    meaning if I saved on local computer the file saved and message success display

    but if i save on network path i  not get message success and file not saved .

    why and how to solve .

    Friday, August 3, 2018 11:02 AM
  • If i get it right, you don't get any message saying it is saved, and you don't get any exception?

    Maybe it takes loong time, just leave it longer..

    Kind regards

    Friday, August 3, 2018 11:05 AM
  • if id debug when reach to this line

    if (saveFileName.IndexOf(":") < 0) return; 

    it go out from function 

    if local program resum after this line 

    if network debug reach to this line and escape remaining line in functions

    are this line related to local save or network save

    i doubt in this line .

    Friday, August 3, 2018 11:54 AM
  • if id debug when reach to this line

    if (saveFileName.IndexOf(":") < 0) return; 

    it go out from function 

    if local program resum after this line 

    if network debug reach to this line and escape remaining line in functions

    are this line related to local save or network save

    i doubt in this line .

    Hi,

    The String.IndexOf Method reports the zero-based index of the first occurrence of the specified string in this instance.

    It returns the zero-based index position of value if that string is found, or -1 if it is not. If value is Empty, the return value is 0.

    In your case, I think it returns -1 from your description, that means the path does not contain the character ":", maybe your network path is like "//server//path...", so the code is broken because of the keyword(return).

    Try to comment out this line to run your code.

    Regards,

    Stanly


    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.

    Monday, August 6, 2018 3:11 AM