如何将excel文件下载到asp.net中的特定文件夹

时间:2013-07-16 06:33:50

标签: asp.net excel path

我需要将excel下载到特定文件夹

示例:D:\ email

现在我可以在下载中下载excel文件....但我需要在D:\ email下载

这是我创建excel文件的代码:

             protected void UploadDataTableToExcel(DataTable dtRecords)
       {
        string XlsPath = Server.MapPath(@"~/Add_data/test.xls");
        string attachment = string.Empty;
        if (XlsPath.IndexOf("\\") != -1)
        {
            string[] strFileName = XlsPath.Split(new char[] { '\\' });
            attachment = "attachment; filename=" + strFileName[strFileName.Length - 1];
        }
        else
            attachment = "attachment; filename=" + XlsPath;
        try
        {
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            string tab = string.Empty;

            foreach (DataColumn datacol in dtRecords.Columns)
            {
                Response.Write(tab + datacol.ColumnName);
                tab = "\t";
            }
            Response.Write("\n");

            foreach (DataRow dr in dtRecords.Rows)
            {
                tab = "";
                for (int j = 0; j < dtRecords.Columns.Count; j++)
                {
                    Response.Write(tab + Convert.ToString(dr[j]));
                    tab = "\t";
                }

                Response.Write("\n");
            }
            Response.End();
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
        }
            }

1 个答案:

答案 0 :(得分:0)

您可以使用

在下载后手动移动文件
string sourceFile = @"C:\Users\test\Documents\Downloads\test.xls";
string destinationFile = @"D:\emails\test.xls";
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);