2017-04-25 1 views
-2

Je veux créer un fichier Excel en utilisant EPPlus et l'enregistrer dans un dossier sur le serveur.Créer Excel en utilisant EPPlus et l'enregistrer sur le dossier du serveur

DataTable dt_final = get_gridview_format(); 
      for (int i = 0; i < gridreport.Rows.Count; i++) 
      { 
       string[] temp = new string[13]; 
       for (int j = 0; j < gridreport.Columns.Count; j++) 
       { 
        if (j != 12 && j != 13) 
        { 
         int index = j; 
         if (j > 13) index = j - 2; 

         if (j <= 11) 
         { 
          Label lbl = (Label)gridreport.Rows[i].Cells[j].FindControl("lbl" + j); 
          temp[index] = lbl.Text.Replace("->", "").Replace("<br/>", ""); 
         } 
         else 
         { 
          TextBox txt = (TextBox)gridreport.Rows[i].Cells[j].FindControl("txtfinal"); 
          temp[index] = txt.Text.Replace("->", "").Replace("<br/>", ""); 
         } 
        } 
       } 
       dt_final.Rows.Add(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], temp[9], temp[10], temp[11], temp[12]); 
      } 

      DataTable tbl = dt_final.Copy(); 

      DataView dv = tbl.DefaultView; 
      dv.Sort = "SrNo ASC"; 
      tbl = dv.ToTable(); 

      using (ExcelPackage pck = new ExcelPackage()) 
      { 
       //Create the worksheet 
       ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Updation"); 

       //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1 
       ws.Cells["A1"].LoadFromDataTable(tbl, true); 

       //Format the header for column 1-3 
       using (ExcelRange rng = ws.Cells["A1:K1"]) 
       { 
        rng.Style.Font.Bold = true; 
        rng.Style.Fill.PatternType = ExcelFillStyle.Solid;      //Set Pattern for the background to Solid 
        rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189)); //Set color to dark blue 
        rng.Style.Font.Color.SetColor(System.Drawing.Color.White); 
       } 

       //Example how to Format Column 1 as numeric 
       using (ExcelRange col = ws.Cells[2, 2, 2 + tbl.Rows.Count, 2]) 
       { 
        col.Style.Numberformat.Format = "#,##0.00"; 
        col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right; 
       } 
File.WriteAllText(Server.MapPath("files/12345"+txtfilename.Text+".xlsx"), pck.ToString()); // for save file on server but this is not working 


       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
       Response.AddHeader("content-disposition", "attachment; filename=Qa_Report.xlsx"); 
       Response.BinaryWrite(pck.GetAsByteArray()); 
      } 

ce code télécharger le fichier mais je veux l'enregistrer aussi sur le dossier du serveur. j'ai trouvé un code à partir d'un poste, mais cela ne fonctionne pas

+0

Avez-vous essayé d'écrire Server.MapPath ("~/files/12345" ...). Je fais référence à [this] (http://stackoverflow.com/questions/3077558/use-of-tilde-in-asp-net-path), où le tilde est remplacé du côté service. –

+0

utilisant déjà cette –

Répondre

0

Remplacer le File.WriteAllText() appel dans votre code avec:

File.WriteAllBytes(
    Server.MapPath("files/12345"+txtfilename.Text+".xlsx"), 
    pck.GetAsByteArray() 
); 
0

Vous pouvez utiliser ...

pck.SaveAs(New FileInfo(ServerFilePath)) 

mais vous ne pouvez pas utiliser cet objet plus. Vous devez donc exporter le fichier enregistré vers le client.