前提・実現したいこと
VB.NETでEPPlusを使用してEXCELの内容を書き換えるプログラムを作成中なのですが、上書き処理に失敗してしまい途方に暮れています。
原因究明の為に、NUGETでEPPlusだけを追加した新規プロジェクトで小さいサンプルプログラムを動作させてみたのですが、やはり例外が発生し、メッセージには「Error overwriting file」や「Error saving file」と表示されます。
何かお心当たりのある方は、ご連絡いただけないでしょうか。
ちなみに「Console.WriteLine(sheet.Cells("A1").Value)」でEXCELの内容が読み込めているので、ファイルの設置場所に間違いはないです。
発生している問題・エラーメッセージ
System.InvalidOperationException はハンドルされませんでした。 HResult=-2146233079 Message=Error saving file [ファイルパス] Source=EPPlus StackTrace: 場所 OfficeOpenXml.ExcelPackage.Save() 場所 EPPlusSample.Form1.Button1_Click(Object sender, EventArgs e) 場所 C:\Users[ユーザ名]\Documents\visual studio 2015\Projects\EPPlusSample\EPPlusSample\Form1.vb:行 13 場所 System.Windows.Forms.Control.OnClick(EventArgs e) 場所 System.Windows.Forms.Button.OnClick(EventArgs e) 場所 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 場所 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 場所 System.Windows.Forms.Control.WndProc(Message& m) 場所 System.Windows.Forms.ButtonBase.WndProc(Message& m) 場所 System.Windows.Forms.Button.WndProc(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 場所 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 場所 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 場所 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 場所 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 場所 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 場所 EPPlusSample.My.MyApplication.Main(String[] Args) 場所 :行 81 場所 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 場所 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 場所 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 場所 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 場所 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 場所 System.Threading.ThreadHelper.ThreadStart() InnerException: HResult=-2146233088 Message=Error overwriting file [ファイルパス] Source=EPPlus StackTrace: 場所 OfficeOpenXml.ExcelPackage.Save() InnerException: HResult=-2147024864 Message=別のプロセスで使用されているため、プロセスはファイル [ファイルパス] にアクセスできません。 Source=mscorlib StackTrace: 場所 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 場所 System.IO.File.InternalDelete(String path, Boolean checkHost) 場所 System.IO.File.Delete(String path) 場所 OfficeOpenXml.ExcelPackage.Save() InnerException:
該当のソースコード
VB
1Imports OfficeOpenXml 2Imports System.IO 3 4Public Class Form1 5 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 6 Dim file = New FileInfo("locked.xlsx") 7 8 Using package = New ExcelPackage(file, "123456789") 9 Dim sheet = package.Workbook.Worksheets.First 10 Console.WriteLine(sheet.Cells("A1").Value) 11 12 sheet = package.Workbook.Worksheets.Add("sampleAdd") 13 Dim wRange = sheet.Cells("A2") 14 wRange.Style.Numberformat.Format = "yyyy/m/d hh:mm:ss" 15 wRange.Value = Date.Now 16 package.Save() 17 Console.WriteLine("Saved!") 18 End Using 19 End Sub 20End Class
補足情報(FW/ツールのバージョンなど)
Windows 10 Pro
VS2015
EPPlus 4.5.3.1
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2019/04/02 00:02 編集
2019/04/02 02:55
2019/04/02 04:55
2019/04/02 07:59
2019/04/16 09:39