Excel.Workbook.SaveAs(...)具有相同的名称文件

时间:2013-08-17 13:09:07

标签: c# excel com

我正在使用xls文件。如何保存(如果存在)与Windows桌面相同的文件名+“(副本1)”。

方法saveCommande(...)

        if(!Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "EE_Commande_Fournisseur"))
        {
            Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\EE_Commande_Fournisseur");
        }
        xlWorkBook.SaveAs("EE_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) + "\\EE_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();

感谢

3 个答案:

答案 0 :(得分:14)

尝试使用File对象的Exists方法:

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

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

  }  

或者您也可以通过

覆盖您的Excel文件
   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(如果存在),而不会询问用户。

答案 1 :(得分: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();
    }

答案 2 :(得分: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();


        }