0

我正在尝试保存excel文件。如果我将文件保存在同一位置,它工作正常,但如果我想保存不同的位置,它会抛出错误。

错误:

System.NotSupportedException was unhandled
  Message=The given path's format is not supported.
  Source=mscorlib
  StackTrace:
       at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
       at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
       at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
       at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, AccessControlActions control, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       at System.IO.File.Create(String path)
       at Report.Form1.ExportToExcelReport(DataTable Tbl, String ExcelFilePath) in C:\SMARTAG_PROJECT\SUREREACH\EXCEL\Report\Report\Form1.cs:line 142
       at Report.Form1.button2_Click(Object sender, EventArgs e) in C:\SMARTAG_PROJECT\SUREREACH\EXCEL\Report\Report\Form1.cs:line 113
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Report.Program.Main() in C:\SMARTAG_PROJECT\SUREREACH\EXCEL\Report\Report\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我的代码

FileInfo newFile = new FileInfo("C:\\Excel\\SampleStockTakeReport.xlsx");
ExcelPackage pck = new ExcelPackage(newFile);
ExcelWorksheet ws = pck.Workbook.Worksheets[1];
var path = "C:\\Excel\\Report\\SampleStockTakeExceptionReport" + DateTime.Now + ".xlsx";
ws.View.ShowGridLines = false;
ws.Cells["G13"].Value = "Rent";
ws.Cells["G14"].Value = "Level 1";
ws.Cells["G15"].Value = "Cell 1";
ws.Cells["V14"].Value = "Level 3";
ws.Cells["V15"].Value = "Cell 3";
ws.Cells["W12"].Value = "Bukit Raja";
ws.Cells["AJ13"].Value = "Row 1";
var tracksql = new TrackingSql();
var numberoftag = tracksql.getNumberofTag();
ws.Cells["S19"].Value = DateTime.Now;
ws.Cells["Y19"].Value = numberoftag.ToString();
var numberoftagscanned = 9;
var diff = numberoftag - numberoftagscanned;
ws.Cells["Q22"].Value = numberoftagscanned;
ws.Cells["X22"].Value = numberoftag;
ws.Cells["AD22"].Value = diff;
var thispath = "testing path for report";
Stream stream = File.Create(path); // throw error here
pck.SaveAs(stream);
System.Diagnostics.Process.Start(path);
MessageBox.Show("Report Generated at " + path + "");

如果有人可以就此提供建议/帮助,我们将不胜感激。

4

2 回答 2

2

此问题与 EPPlus 无关。你得到NotSupportedException

Stream stream = File.Create(path);

此处记录:File.Create方法(字符串)

所以这个“路径格式无效”。因为时间部分的冒号DateTime.Now

var path = "C:\\Excel\\Report\\SampleStockTakeExceptionReport" + DateTime.Now + ".xlsx";

所以这应该工作:

var dayPart = DateTime.Now.ToString("yyyy-MM-dd");
var path = "C:\\Excel\\Report\\SampleStockTakeExceptionReport" + dayPart + ".xlsx";

如果您需要时间部分,您可以创建一个自定义DateTimeFormatInfo

var timeFormat = (DateTimeFormatInfo)CultureInfo.CurrentCulture.DateTimeFormat.Clone();
timeFormat.TimeSeparator = "_";
var dayPart = DateTime.Now.ToString(timeFormat); // replaces the colons with _ implicitely
于 2013-01-07T08:25:11.433 回答
1

您需要格式化DateTime.NowDateTime.Now.ToString("MMddyyyy");

于 2013-01-07T08:19:44.210 回答