0

I have a desktop app in which I generate an excel file from a template, basically what I do is to make a copy of the template excel file to a new directory, open it, and add records to it.

this is a code snippet:

string ExcelFiles = txtFileOutput.Text; //Application.StartupPath + @"\ExcelFiles\"; 
        string DestFile = ExcelFiles + FinalListOfTrucks[0].Date.ToShortDateString().Replace('/', '_') + ".xlsx"; 
        if (File.Exists(DestFile))
        {
            try
            {
                File.Delete(DestFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error deleting file: " + ex.Message);
            }
        }

        try
        {
            File.Copy(Application.StartupPath + @"\ExcelTemplate.xlsx", DestFile);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error copying file: " + ex.Message);
        }

        string appPath = Path.GetDirectoryName(Application.ExecutablePath);            
        string strConnectionXls = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + DestFile + ";Extended Properties=Excel 12.0;";
        OleDbConnection cno = new OleDbConnection(strConnectionXls);
        OleDbCommand cmo = new OleDbCommand();
        cmo.Connection = cno;
        cmo.CommandType = CommandType.Text;

string QueryHeader = "INSERT INTO [RptTotal$] (NUMERO,FECHA,JORNADA,PLACA,HORA_DE_LLEGADA,HORA_FIN_CARGUE,HORA_FIN_DESCARGUE,VOLUMEN,DISTANCIA,TIPO_DE_VEHICULO,MATERIAL,MISSED_DATE)";

cmo.CommandText = QueryHeader + " VALUES ('" + No.ToString() + "','" + Fecha + "','" + Jornada + "','" + Placa + "','" + HoraDeLlegada + "','" + HoraFinCargue + "','" +
                                                        HoraFinDescargue + "','" + Volumen + "','" + Distancia + "','" + TipoDeVehiculo + "','" + Material + "','" + MissedDate + "')";
                cmo.ExecuteNonQuery();

Now, let's say that I can Add a folder to my webapp, and place the excel template there, how to start from there to achieve the same functionality in web :\ ?

1 Answer 1

1

Same thing you are doing on your desktop application, just need to clean up your code.

  1. Copy template, save templete same where in your server
  2. Need to remove all message boxes
  3. Use OleDbConnection to modify your excel file( would strongly recommed OpenXML or libraries build on top of openXML like Epplus)
  4. Save changes
  5. Stream the file to client
  6. Figure out how to clean streamed files
Sign up to request clarification or add additional context in comments.

2 Comments

But I want to keep the template file empty, that's why I first make a copy of it to another location, I guess I'll need to do the same thing here right? Something like upload the template file to the server, change it and then stream it to the client. Yeap, that's what you said in the first point :|
You will need to do the samething, but you need to figure out naming scheme(so that different requests will have different files) and how to delete files after you sent them to the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.