C# Excel.Workbook.SaveAs(...) 同名文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18289194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel.Workbook.SaveAs(...) with a same name file
提问by Yassine
I'm working with xls file. How can I save it (if exist) with the same file name + "(copy 1)" like Windows desktop.
我正在处理 xls 文件。如何使用与 Windows 桌面相同的文件名 +“(副本 1)”保存它(如果存在)。
method saveCommande(...)
方法 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();
thanks
谢谢
采纳答案by Rohit
try using File object's Exists method:
尝试使用 File 对象的 Exists 方法:
if (!System.IO.File.Exists(@"C:\test2.xls"))
{
xlWorkBook.SaveAs(@"c:\test2.xls");
}
else
{
xlWorkBook.SaveAs(@"c:\test2(Copy).xls");
}
Or Alternatively you can overwrite your excel file by
或者,您可以通过以下方式覆盖您的 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);
Note:It will overwrite the existing excel (if exist) without asking the user.
注意:它会在不询问用户的情况下覆盖现有的 excel(如果存在)。
回答by Yassine
it works! thanks
有用!谢谢
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();
}
回答by Kiran
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();
}