Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9209493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:51:21+00:00 2026-06-18T00:51:21+00:00

I have a template excel file to generate excel files from it. My code

  • 0

I have a template excel file to generate excel files from it.

My code is as follows (This part is to create a new excel file from the template):

string currentFN = PropertyFinalResult[0].Fecha;
        string fixCurrentFN = currentFN.Replace('/', '_');

        string currentTime = DateTime.Now.ToLongTimeString();
        string fixCurrentTime = currentTime.Replace(':', '_');
        string addToFileName = fixCurrentTime.Replace(' ', '_');

        string newFN = fixCurrentFN + "-" + addToFileName;

        string SourceFile = Request.PhysicalApplicationPath + "Template\\ExcelTemplate.xlsx";
        string DestFile = Request.PhysicalApplicationPath + "Template\\" + newFN + ".xlsx";
        //To keep FileName for posterior deletion
        Session["sDestFile"] = DestFile;

        try
        {                
            File.Copy(SourceFile, DestFile);                
        }
        catch (Exception ex)
        {
            lblErrorSavingToDB.Text = "Error: " + ex.Message;
            lblErrorSavingToDB.Visible = true;
        }

after that I open the new excel file, insert the records in it and then, stream the file to the user by doing this:

//Streaming file to client                    
                string fileName = newFN + ".xlsx";
                Response.Redirect("../Template/" + fileName);

Now, my question is, whether the user save or not the file, when should I delete the generated file? I would prefer once the user closes the popup window regarding Open or Save the file. But how to know when the user closes that window?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:51:23+00:00Added an answer on June 18, 2026 at 12:51 am

    You can use TransmitFile and then close once the transmission is over. Example:

                    try
                    {
                        Response.ContentType = "application/octet-stream";
                        Response.AddHeader("content-disposition", "attachment;filename=\"" + Path.GetFileName(path.FullName) + "\"");
                        Response.AddHeader("content-length", path.Length.ToString());
                        Response.TransmitFile(path.FullName);
                        Response.Flush();
                    }
                    finally
                    {
                        File.Delete(Server.MapPath("~/"+tpacode+".zip"));
                    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using C# to generate new files from Excel's Template.xlsm workbook. Some sheets of
I need help in this matter: We have a template of Excel file in
I have a web application that generates excel files from a template. When the
I have the following code to open Excel template file and save it as
I have a desktop app in which I generate an excel file from a
Now, I'm trying to generate an excel file from my template. My case is
I have an excel file (that I use as a template) with two rows.
I have an Excel template file . Based on the Excel version, I would
I have an excel template that uses a macro to save the file, so
I have an Excel template that I use to create PDF specification sheets for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.