Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to export data from database to excel and silently save that excel at a location in my system without the prompt that asks to open/save the file.

I have exported the data to excel but dont know how to silently save it.
Please help.

Thanks
Posted

You don't specify if this is a Windows Forms app or an ASP.NET app. If a Windows Forms app, you just need to supply a filepath to the Workbook.SaveAs method.

If ASP.NET, you don't have a choice. Webbrowsers cannot be told to just save a file anywhere the site wants. This is for rather obvious security reasons.
 
Share this answer
 
Hi Aksha,

Please try following
C#
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);
            xlWorkSheet.Cells[1, 1] = "Test result";
            string FilePath =  "D:/Projects/CSharp/ExcelResult.xls";  
            xlWorkBook.SaveAs(FilePath , 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 created , you can find the file " + FilePath );
 
Share this answer
 
its an asp.net app..Following code exports the data into excel..can anything be done in it to save the excel?


protected void LinkButton1_Click1(object sender, EventArgs e)
{
string filepath;
string filename, fileExcel;
Random nRandom = new Random(DateTime.Now.Millisecond);
//Create a random file name.
fileExcel = "t" + nRandom.Next().ToString() + ".xls";
filepath = Server.MapPath("");
filename = filepath + "\\" + fileExcel;

SqlCommand cmd = new SqlCommand("select rs.contactID,SurveyType,ModuleName,LogDate from tblReportLOg rs" +
" inner join tblContact c on c.contactID = rs.contactID"
+ " where LogDate >= '2011-01-20 00:00:00.000' and c.email like '%micro%' and rs.contactid not in (39287,39286,27546) order by LogDate ", conn);
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(cmd);
DataSet d = new DataSet();
mySqlDataAdapter.Fill(d, "dataset");
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();

dg.DataSource = d.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
//File.WriteAllText(sw.ToString());


}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900