2013-08-17 25 views
7

Pracuję z plikiem xls. Jak mogę go zapisać (jeśli istnieje) o tej samej nazwie pliku + "(kopia 1)" jak na pulpicie systemu Windows.Excel.Workbook.SaveAs (...) z plikiem o tej samej nazwie

metoda saveCommande (...)

 if(!Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "EcoEtech_Commande_Fournisseur")) 
     { 
      Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\EcoEtech_Commande_Fournisseur"); 
     } 
     xlWorkBook.SaveAs("EcoEtech_Commande_Fournisseur\\" + path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, true, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
     path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\EcoEtech_Commande_Fournisseur\\" + path; 

     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 
     this.ReleaseComObject(xlApp,xlWorkBook); 
     //sauvergarde dans la base de données 
     _newAchatCommande.path = path; 
     this._fileName = path; 
     contexte.AddToAchatCommande(_newAchatCommande); 
     contexte.SaveChanges(); 

dzięki

Odpowiedz

11

Spróbuj użyć obiektu File Występuje metoda:

if (!System.IO.File.Exists(@"C:\test2.xls")) 
    { 
    xlWorkBook.SaveAs(@"c:\test2.xls"); 
    } 
    else 
    { 

    xlWorkBook.SaveAs(@"c:\test2(Copy).xls"); 

    } 

lub alternatywnie można nadpisać plik excel przez

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 

    excel.DisplayAlerts = false; 

    excelSheePrint.SaveAs(filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing); 

Uwaga: Nadpisze istniejący program Excel (jeśli istnieje) bez pytania użytkownika.

+0

Co jeśli chcę zapisać dane w tym samym pliku excel dla wielu iteracji (bez zastępowania istniejących danych)? – John

0

to działa! dzięki

private void SaveNewCommande(YetiBddEntities contexte, Excel.Application xlApp, Excel.Workbook xlWorkBook, string path, object misValue) 
    { 
     string tmpPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\EET_Commande_Fournisseur"; 
     if (!Directory.Exists(tmpPath)) 
     { 
      Directory.CreateDirectory(tmpPath); 
     } 
     foreach (string file in Directory.GetFiles(tmpPath)) 
     { 
      if ((path+".xls").Equals(file.Substring(tmpPath.Length+1))) 
       count++; 
     } 
     if (count>0) 
      xlWorkBook.SaveAs(string.Format("EET_Commande_Fournisseur\\{0}_({1}).xls", path, count), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
     else 
      xlWorkBook.SaveAs(string.Format("EET_Commande_Fournisseur\\{0}.xls", path) + path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 

     path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\EET_Commande_Fournisseur\\" + path+".xls"; 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 
     this.ReleaseComObject(xlApp,xlWorkBook); 
     //sauvergarde dans la base de données 
     _newAchatCommande.path = path; 
     this._fileName = path; 
     contexte.AddToAchatCommande(_newAchatCommande); 
     contexte.SaveChanges(); 
    } 
0
 DataTable dt= dtData; 

    using (XLWorkbook wb = new XLWorkbook()) 
    {  

       //Range(int firstCellRow,int firstCellColumn,int lastCellRow,int lastCellColumn) 
       int CCount = dt.Columns.Count; 

       int RCount = dt.Rows.Count; 

       var ws = wb.Worksheets.Add("Report"); //add worksheet to workbook 
       ws.Row(1).Cell(1).RichText.AddText(ReportName); 

       ws.Range(1, 1, 1, CCount).Merge(); 

       ws.Range(1, 1, 1, CCount).Style.Font.Bold = true;//Range(int firstCellRow,int firstCellColumn,int lastCellRow,int lastCellColumn) 
       ws.Range(1, 1, 1, CCount).Style.Font.FontSize = 16; 

       ws.Range(1, 1, 1, CCount).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; 

       ws.Range(1, 1, 1, CCount).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; 

       //ws.Range("A1:Z1").Style.Font.Bold = true; 

       ws.Row(2).Cell(1).InsertTable(dt); 

       ws.Range("A2:Z2").Style.Font.Bold = true; 

       ws.Range(2, 1, RCount + 2, CCount).Style.Border.OutsideBorder = XLBorderStyleValues.Medium; 

       wb.SaveAs(FolderPath + filename + ".xlsx"); 

       ws.Dispose(); 


     } 
Powiązane problemy