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.Now次のような形式にする必要がありますDateTime.Now.ToString("MMddyyyy");

于 2013-01-07T08:19:44.210 に答える