Save Excel ASP file c #

1

Good Night I would like you to help me. I'm doing an application and if I save the route correctly but when I want to run again, everything is deleted and created again. I would like it to be created in the same folder as well as copy and not be deleted.

string query = "SP_ReporteExcel";
using (SqlConnection con = new SqlConnection(ObtenerCadenaConexion()))
{
    using (SqlCommand cmd = new SqlCommand(query))
    {
        cmd.CommandTimeout = 90000000;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("opt", SqlDbType.Int).Value = 1;
        cmd.Parameters.Add("fechainicio", SqlDbType.DateTime).Value = txtFinicio.Text;
        cmd.Parameters.Add("fechafin", SqlDbType.DateTime).Value = txtFFin.Text;
        cmd.Parameters.Add("rubro", SqlDbType.VarChar, 250).Value = dprAgencia.SelectedItem.Value;
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;

            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);

                //Set Name of DataTables.

                ds.Tables[0].TableName = "Recibos-Energia Electrica";
                ds.Tables[1].TableName = "Recibos-Agua Potable";

                using (XLWorkbook wb = new XLWorkbook())
                {


                    foreach (DataTable dt in ds.Tables)
                    {

                        DateTime d3 = DateTime.Parse(txtFinicio.Text);
                        string Mes = d3.ToString("MMMM");

                        DateTime d4 = DateTime.Parse(txtFinicio.Text);
                        string Anio = d4.ToString("yyyy");

                        wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        wb.Style.Font.Bold = true;
                        wb.Style.Font.FontColor = XLColor.Black;
                        //wb.Style.Font.FontName = "calibri";
                        //wb.Style.Font.FontSize = 13;
                        wb.Style.Border.DiagonalBorderColor = XLColor.Red;
                        wb.Style.Border.RightBorderColor = XLColor.Red;
                        wb.Style.Border.LeftBorderColor = XLColor.Red;
                        wb.Style.Border.BottomBorderColor = XLColor.Red;

                        var ws = wb.Worksheets.Add(dt);

                        ws.Row(1).InsertRowsAbove(1);
                        ws.Cells("A2:H2").Style.Fill.BackgroundColor = XLColor.Black;
                        ws.Cells("A2:H2").Style.Font.FontColor = XLColor.Yellow;

                        ws.Cells("D1:H1").Style.Fill.BackgroundColor = XLColor.Black;
                        ws.Cells("D1:H1").Style.Font.FontColor = XLColor.Yellow;
                        string datetimeStringFileName = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                        ws.Range("D1:H1").Merge();

                        ws.Range("D1").Value = "Mes de " + Mes + " del " + Anio;
                        //ws.Cells("D1:H1").Value = "Mes";

                        //wb.Style.Fill.BackgroundColor = XLColor.Black;
                        //wb.Worksheets.Add(dt);

                    }

                    Response.Clear();
                    Response.Buffer = true;
                    Response.Charset = "";

                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    Response.AddHeader("content-disposition", "attachment;filename=Reporte_Mensualess.xlsx");

                    using (MemoryStream MyMemoryStream = new MemoryStream())
                    {

                        wb.SaveAs("C:\DatosSiga\Logistica\Reporte_Mensualess.xlsx");
                        //wb.SaveAs(MyMemoryStream);
                        MyMemoryStream.WriteTo(Response.OutputStream);
                        Response.Flush();
                        Response.End();
                        Response.Redirect("FrmLogeo.aspx");
                    }
                }
            }
        }
    }
}

Image Error:

    
asked by PieroDev 24.03.2017 в 01:51
source

1 answer

2

A quick solution is to save with a different name each time, I'll explain:

wb.SaveAs("C:\DatosSiga\Logistica\Reporte_Mensualess.xlsx");

Instead of the previous line you have several options:

A) Use a GUID

var aux = Guid.NewGuid()

B) Use the current date and time

 DateTime fechaActual = new DateTime.Now;
 CultureInfo ci = CultureInfo.InvariantCulture;

var aux = fechaActual .ToString("yyyyMMddhhmm", ci));// formato 201703231842

Use any of the above options and change your line to this one:

wb.SaveAs("C:\DatosSiga\Logistica\Reporte_Mensualess" + aux + ".xlsx");

This way you will be saving a different document each time.

    
answered by 24.03.2017 / 02:45
source