I have a code that export a datagridview content to excel but it doesn't enable the user to name and locate the created excel file, i want to modify the code to enable the user to name and locate the created excel file using SaveFileDialog, here is the code
button2 in code is the export to excel button
private void button2_Click(object sender, EventArgs e)
{
Excel.
Application xlApp;
Excel.
Workbook xlWorkBook;
Excel.
Worksheet xlWorksheet;
object misValue = System.Reflection.Missing.Value;
xlApp =
new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorksheet = (Excel.
Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorksheet.Cells[i+1, j+1] = cell.Value;
}
}
xlWorkBook.SaveAs(
"Products.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(
true, misValue, misValue);
xlApp.Quit();
releaseobject(xlWorksheet);
releaseobject(xlWorkBook);
releaseobject(xlApp);
MessageBox.Show("Excel file is created successfully");
}
private void releaseobject(object obj)
{
try
{
System.Runtime.InteropServices.
Marshal.ReleaseComObject(obj);
obj =
null;
}
catch (Exception ex)
{
obj =
null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Ravishankar SinghPosted Jun 14, 2013, 2:18 PM
To achieve the functionality, you store the workbook in memorystream e.g.
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[0, 1] = new Cell(9999999);
workbook.Worksheets.Add(worksheet);
MemoryStream m = new MemoryStream();
workbook.SaveToStream(m);
and then you can write to the browser like below:
//Read the Excel file in a byte array. here pck is the Excelworkbook
Byte[] fileBytes = pck.GetAsByteArray();
//Clear the response
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//Add the header & other information
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
Response.AppendHeader("Pragma", "cache");
Response.AppendHeader("Expires", "60");
Response.AppendHeader("Content-Disposition", "attachment; " + "filename=\"ExcelReport.xlsx\"; " + "size=" + fileBytes.Length.ToString() + "; " + "creation-date=" + DateTime.Now.ToString("R") + "; " + "modification-date=" + DateTime.Now.ToString("R") + "; " + "read-date=" + DateTime.Now.ToString("R"));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Write it back to the client
Response.BinaryWrite(fileBytes);
Response.End();
Hope this will give you some fair idea.