2013-01-07 2 views
0

엑셀 파일을 저장하려고합니다. 같은 위치에 파일을 저장하면 잘 작동하지만 다른 위치를 저장하려고하면 오류가 발생합니다.EPPLUS - 다른 폴더에 저장할 수 없습니다.

오류 :

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 + ""); 

사람이에/도움 조언을 할 수 있다면 높이 평가.

답변

1

당신은 DateTime.Now.ToString("MMddyyyy");

2

이 문제는 EPPlus 관련이없는 것처럼 뭔가 DateTime.Now를 포맷해야합니다. File.Create Method (String)

그래서 "경로가 잘못된 형식으로되어 있습니다."당신은 NotSupportedException 여기에 설명되어있는

Stream stream = File.Create(path); 

얻을 때문에 DateTime.Now의 시간 부분에 콜론의 :

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

그래서이 작동합니다 :

var timeFormat = (DateTimeFormatInfo)CultureInfo.CurrentCulture.DateTimeFormat.Clone(); 
timeFormat.TimeSeparator = "_"; 
var dayPart = DateTime.Now.ToString(timeFormat); // replaces the colons with _ implicitely 
: 당신은 시간 부분이 필요하면

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

, 당신은 사용자 정의 DateTimeFormatInfo를 만들 수 있습니다

관련 문제