C# 将文件导出到重写
我目前正在使用C# 将文件导出到重写,c#,excel,windowsformshost,C#,Excel,Windowsformshost,我目前正在使用Microsoft.Office.Core和将我的数据从windows窗体导出到excel 当我导出文件时,它总是被覆盖 有人知道解决这个问题的方法吗 xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue,
Microsoft.Office.Core使用Excel=Microsoft.Office.Interop.Excel进行编码>和
将我的数据从windows窗体导出到excel
当我导出文件时,它总是被覆盖
有人知道解决这个问题的方法吗
xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xls");
您可以首先检查所需的文件名是否已经存在,如果已经存在,请在其末尾附加日期时间。此外,路径前的“@”可以让您像在资源管理器中一样键入路径(不带双“\”):
我试过了,但不起作用。它不允许我指定要将文件保存到哪里。我取消了上次编辑并回滚到以前的版本。您的编辑更改了整个问题,因此使现有答案无效。请发一个新问题。
string MyExportFile = "csharp-Excel";
string FullPath = @"d:\";
if (File.Exists(Path.Combine(FullPath, MyExportFile)))
{
MyExportFile += DateTime.Now + ".xls";
FullPath += MyExportFile;
}
xlWorkBook.SaveAs(FullPath,
Excel.XlFileFormat.xlWorkbookNormal,
misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive,
misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();