简体   繁体   English

如何将Excel文件保存在特定的文件夹中

[英]How to Save Excel File in a specific Folder

I am exporting excel file from database using following method.But i have one problem when i am exporting excel file than it is automatically downloading to download folder and i don't want this to be happen,I want my excel file to be downloaded in my project folder 我正在使用以下方法从数据库中导出excel文件。但是我在导出excel文件时遇到了一个问题,即它会自动下载到下载文件夹,并且我不希望这种情况发生,我希望将excel文件下载到其中我的项目资料夹

var formsection = from fs in db.FormSections
                  join form in Form on fs.FormId equals form.FormId
                  select fs;
XLWorkbook wb = new XLWorkbook();              
string sheetName = "ARTICLE"; //Give name for export file.               

var Fs = wb.Worksheets.Add("FORMSECTION");
Fs.Cell(2, 1).InsertTable(formsection.ToList());// assign list here.
HttpContext.Response.Clear();
HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Response.AddHeader("content-disposition", String.Format(@"attachment;filename={0}.xlsx", sheetName.Replace(" ", "_")));
var filePath = Path.Combine(Server.MapPath("~/Content"));
using (MemoryStream memoryStream = new MemoryStream())
{                  
    wb.SaveAs(memoryStream);                     
    memoryStream.WriteTo(HttpContext.Response.OutputStream);
    memoryStream.Close();
}               
HttpContext.Response.End();

Here is an example that converts a datatable to a .csv file and save the file to the folder of your project for me its store the file in Domestic folder as the path given by me. 这是一个将数据表转换为.csv文件并将该文件保存到我的项目文件夹中的示例,该示例将文件存储在我的给定路径中的Domestic文件夹中。

private void test(DataTable dt1) {

    string csv = string.Empty;

    foreach (DataColumn column in dt1.Columns)
    {
        //Add the Header row for CSV file.
        csv += column.ColumnName + ',';
    }

    //Add new line.
    csv += "\r\n";

    foreach (DataRow row in dt1.Rows)
    {
        foreach (DataColumn column in dt1.Columns)
        {
            //Add the Data rows.
            csv += row[column.ColumnName].ToString().Replace(",", ";") +',';
        }

        //Add new line.
        csv += "\r\n";
    }

    string datetime = Convert.ToString(DateTime.Today.ToString("dd-MM-yyyy")).Trim();
    string filepath = "C:\\Users\\Prateek\\Desktop\\MMR New  27-07 -\\Domestic\\";
    string filename= @"BILLING_BOOK_NO" + "_4005" + "_"+datetime+".CSV";
    string combinepath = filepath + filename;          
   System.IO.File.WriteAllText(combinepath,csv);
}`

You need to write the excel to your server first. 您需要先将excel写入服务器。

wb.SaveAs(filePath);
//encrypt the file
Encrypt(filePath);
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        byte[] bytes = new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);
        memoryStream.Write(bytes, 0, (int)file.Length);
        memoryStream.WriteTo(HttpContext.Response.OutputStream);
    }
}

use code as below 使用如下代码

    public void ImportXLX()
        {

string filePath = string.Format("{0}/{1}", Server.MapPath("~/Content/UploadedFolder"), @"C:\Users\Vipin\Desktop\Sheets\MyXL6.xlsx");
                    if (System.IO.File.Exists(filePath))
                        System.IO.File.Delete(filePath);
                    Request.Files["xlsFile"].SaveAs(filePath);
                    Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM