Checking in working Excel Closer

This commit is contained in:
2016-02-15 15:58:27 -05:00
parent 899c0351c2
commit 7dace13fc0
6 changed files with 56 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30501.0
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Advantage Final", "Advantage Final\Advantage Final.csproj", "{1F135F26-23F8-4EA6-B000-9D22FBE1B273}"
EndProject
@@ -13,11 +13,11 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1F135F26-23F8-4EA6-B000-9D22FBE1B273}.Debug|x86.ActiveCfg = Release|x86
{1F135F26-23F8-4EA6-B000-9D22FBE1B273}.Debug|x86.Build.0 = Release|x86
{1F135F26-23F8-4EA6-B000-9D22FBE1B273}.Debug|x86.ActiveCfg = Debug|x86
{1F135F26-23F8-4EA6-B000-9D22FBE1B273}.Debug|x86.Build.0 = Debug|x86
{1F135F26-23F8-4EA6-B000-9D22FBE1B273}.Release|x86.ActiveCfg = Release|x86
{1F135F26-23F8-4EA6-B000-9D22FBE1B273}.Release|x86.Build.0 = Release|x86
{3B803B90-204A-4AA1-B42B-EF8AE9DEF84A}.Debug|x86.ActiveCfg = Release
{3B803B90-204A-4AA1-B42B-EF8AE9DEF84A}.Debug|x86.ActiveCfg = Debug
{3B803B90-204A-4AA1-B42B-EF8AE9DEF84A}.Release|x86.ActiveCfg = Release
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution

View File

@@ -89,6 +89,10 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Yaulw, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Components\Yaulw.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
@@ -172,8 +176,11 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="Components\log4net.dll" />
<None Include="Components\Yaulw.dll" />
<Content Include="lazy.ico" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -5,6 +5,8 @@ using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Yaulw.Win32;
namespace Advantage_Final
{
@@ -68,9 +70,9 @@ namespace Advantage_Final
try
{
//excelEx.DisplayAlerts = false;
//workSheetEx.SaveAs(ExcelFilePath);
//excelEx.DisplayAlerts = true;
excelEx.DisplayAlerts = false;
workSheetEx.SaveAs(ExcelFilePath);
excelEx.DisplayAlerts = true;
// Close the workbook *Auto-Save*
workbook.Close(true, ExcelFilePath, false);
@@ -78,19 +80,14 @@ namespace Advantage_Final
if (excelEx.Workbooks.Count == 0)
{
excelEx.Quit();
Marshal.FinalReleaseComObject(excelEx);
}
//Marshal.ReleaseComObject(workSheetEx);
//Marshal.ReleaseComObject(excelEx);
Marshal.FinalReleaseComObject(excelEx);
}
}
catch (Exception ex)
{
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message);
}
}
}
@@ -123,12 +120,7 @@ namespace Advantage_Final
if (ExcelFilePath != null && ExcelFilePath != "")
{
try
{
//workSheet.SaveAs(ExcelFilePath);
//excelApp.Workbooks.Close();
//excelApp.Quit();
//excelApp = null;
{
// Close the workbook *Auto-Save*
workbook.Close(true, ExcelFilePath, false);
@@ -141,10 +133,9 @@ namespace Advantage_Final
}
catch (Exception ex)
{
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message);
}
}
}
else // no filepath is given
{
@@ -157,6 +148,41 @@ namespace Advantage_Final
throw new Exception("ExportToExcel: \n" + ex.Message);
}
// nicely added
ReallyCLOSEExcel();
}
public static bool ReallyCLOSEExcel()
{
try
{
var RunningExcelApplications = Yaulw.Win32.COM.GetRunningObjectsOfType<Microsoft.Office.Interop.Excel.Application>();
foreach (Microsoft.Office.Interop.Excel.Application app in RunningExcelApplications)
{
try
{
if (app.Workbooks.Count == 0)
{
int hWnd = app.Hwnd;
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up, should clean up excel process
// force close by closing process
Process p = Functions.GetProcessFromHandle((IntPtr) hWnd);
p.Kill();
}
}
catch (Exception e)
{
Marshal.FinalReleaseComObject(app); // force clean-up, should clean up excel process
string s = e.Message;
}
}
return true;
}
catch (Exception e) { string s = e.Message; }
return false;
}
public static void ExportToExcel2(this DataTable Tbl, string ExcelFilePath = null)

Binary file not shown.

Binary file not shown.

BIN
Advantage Final/Yaulw.dll Normal file

Binary file not shown.