2013-08-17 291 views
7

我正在使用xls文件。如何使用相同的文件名+「(copy 1)」(如Windows桌面)將其保存(如果存在)。Excel.Workbook.SaveAs(...)具有相同的名稱文件

方法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(); 

感謝

回答

11

嘗試使用File對象的Exists方法:

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

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

    } 

或者你也可以通過

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); 
覆蓋你的Excel文件

注意:它會覆蓋現有的excel(如果存在)而不詢問用戶。

+0

如果我想將數據保存在同一個Excel文件迭代次數(不覆蓋現有數據)是什麼? – John

0

它的工作原理! 感謝

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(); 


     } 
相關問題