E
engahmedbarbary
Guest
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.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.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
Continue reading...
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.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.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
Continue reading...