Currently I m getting data from table and converting it to a excel workbook , then save it in the server space and then using Response class push the file to the client machine. the code works fine. But i have an idea of not storing the file in physical storage as it increases server storage and directly pushing it to the client machine. Kindly advice if it is possible in any way. Thanks in advance.
protected void GenerateExcelFile()
{
string data = null;
int i = 0;
int j = 0;
Application xlApp;
Workbook xlWorkBook;
Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
DataSet ds = new DataSet();
ds = GetTableData();// Returns dataset
for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
{
xlWorkSheet.Cells[1, k + 1] = ds.Tables[0].Columns[k].ColumnName;
}
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}
xlWorkBook.SaveAs("c:\\csharp.net-informations.xls", XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
string FileName = "Report.xls";
string filepath = "c:\\csharp.net-informations.xls";
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
FileInfo file = new FileInfo(filepath);
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.TransmitFile(file.FullName);
response.End();