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

View File

@@ -89,6 +89,10 @@
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <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>
<ItemGroup> <ItemGroup>
<Compile Include="Class1.cs" /> <Compile Include="Class1.cs" />
@@ -172,8 +176,11 @@
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Components\log4net.dll" />
<None Include="Components\Yaulw.dll" />
<Content Include="lazy.ico" /> <Content Include="lazy.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- 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. 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;
using System.Data.OleDb; using System.Data.OleDb;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Diagnostics;
using Yaulw.Win32;
namespace Advantage_Final namespace Advantage_Final
{ {
@@ -68,9 +70,9 @@ namespace Advantage_Final
try try
{ {
//excelEx.DisplayAlerts = false; excelEx.DisplayAlerts = false;
//workSheetEx.SaveAs(ExcelFilePath); workSheetEx.SaveAs(ExcelFilePath);
//excelEx.DisplayAlerts = true; excelEx.DisplayAlerts = true;
// Close the workbook *Auto-Save* // Close the workbook *Auto-Save*
workbook.Close(true, ExcelFilePath, false); workbook.Close(true, ExcelFilePath, false);
@@ -78,19 +80,14 @@ namespace Advantage_Final
if (excelEx.Workbooks.Count == 0) if (excelEx.Workbooks.Count == 0)
{ {
excelEx.Quit(); excelEx.Quit();
Marshal.FinalReleaseComObject(excelEx); Marshal.FinalReleaseComObject(excelEx);
} }
//Marshal.ReleaseComObject(workSheetEx);
//Marshal.ReleaseComObject(excelEx);
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n" throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message); + ex.Message);
} }
} }
@@ -123,12 +120,7 @@ namespace Advantage_Final
if (ExcelFilePath != null && ExcelFilePath != "") if (ExcelFilePath != null && ExcelFilePath != "")
{ {
try try
{ {
//workSheet.SaveAs(ExcelFilePath);
//excelApp.Workbooks.Close();
//excelApp.Quit();
//excelApp = null;
// Close the workbook *Auto-Save* // Close the workbook *Auto-Save*
workbook.Close(true, ExcelFilePath, false); workbook.Close(true, ExcelFilePath, false);
@@ -141,10 +133,9 @@ namespace Advantage_Final
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n" throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message); + ex.Message);
} }
} }
else // no filepath is given else // no filepath is given
{ {
@@ -157,6 +148,41 @@ namespace Advantage_Final
throw new Exception("ExportToExcel: \n" + ex.Message); 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) 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.