Implement printing functionality by stealing excel.
This commit is contained in:
parent
0be3d95598
commit
4d13eba475
@ -64,6 +64,9 @@
|
||||
<Reference Include="Dapper, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>packages\Dapper.2.0.35\lib\net461\Dapper.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
|
||||
<EmbedInteropTypes>True</EmbedInteropTypes>
|
||||
</Reference>
|
||||
<Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
|
||||
<HintPath>packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.dll</HintPath>
|
||||
</Reference>
|
||||
|
6
frmReports.Designer.cs
generated
6
frmReports.Designer.cs
generated
@ -50,6 +50,7 @@ namespace GreatHomeChildcare
|
||||
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
|
||||
this.btnPrint = new System.Windows.Forms.Button();
|
||||
this.saveFileDialogReport = new System.Windows.Forms.SaveFileDialog();
|
||||
this.PrintDialog = new System.Windows.Forms.PrintDialog();
|
||||
((System.ComponentModel.ISupportInitialize)(this.dgvReports)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
@ -217,6 +218,10 @@ namespace GreatHomeChildcare
|
||||
this.saveFileDialogReport.Filter = "CSV Files|*.csv";
|
||||
this.saveFileDialogReport.Title = "Save Report";
|
||||
//
|
||||
// PrintDialog
|
||||
//
|
||||
this.PrintDialog.UseEXDialog = true;
|
||||
//
|
||||
// frmReports
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
|
||||
@ -260,5 +265,6 @@ namespace GreatHomeChildcare
|
||||
private System.Windows.Forms.ImageList imageList1;
|
||||
private System.Windows.Forms.Button btnPrint;
|
||||
private System.Windows.Forms.SaveFileDialog saveFileDialogReport;
|
||||
private System.Windows.Forms.PrintDialog PrintDialog;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ using System.Data;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
@ -13,6 +14,7 @@ using GreatHomeChildcare.Models;
|
||||
|
||||
//REF:
|
||||
//https://stackoverflow.com/questions/9780800/what-event-is-raised-when-a-user-interacts-with-the-datetimepicker-control
|
||||
//https://stackoverflow.com/questions/2884356/how-do-i-auto-size-columns-through-the-excel-interop-objects
|
||||
|
||||
namespace GreatHomeChildcare
|
||||
{
|
||||
@ -280,27 +282,66 @@ namespace GreatHomeChildcare
|
||||
|
||||
//TODO: figure out a way to actually implement printing. It's one of the toolbox things.
|
||||
//1: Get temp environment variable
|
||||
DialogResult dr = PrintDialog.ShowDialog();
|
||||
|
||||
if (dr == DialogResult.OK) //print
|
||||
{
|
||||
string filename = Environment.GetEnvironmentVariable("TEMP");
|
||||
filename += @"\GHCReport.csv";
|
||||
|
||||
//Check to see if the file exists. If so, delete it.
|
||||
if(File.Exists(filename))
|
||||
try
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
MessageBox.Show("Please close Excel and try again.", "Great Home Childcare", MessageBoxButtons.OK, MessageBoxIcon.None);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//Write the CSV.
|
||||
SaveCSV(filename);
|
||||
|
||||
//Check again to see if writing the CSV was successful.
|
||||
//If not, show a message.
|
||||
if(!File.Exists(filename))
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
MessageBox.Show("Could not save temporary file to print the report.", "Great Home Childcare", MessageBoxButtons.OK, MessageBoxIcon.None);
|
||||
return;
|
||||
}
|
||||
|
||||
//Cheaply print via Excel.
|
||||
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
|
||||
excel.Visible = false;
|
||||
excel.DisplayAlerts = false;
|
||||
|
||||
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(filename);
|
||||
Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Sheets[1];
|
||||
|
||||
//ref: stack exchange
|
||||
worksheet.Columns.AutoFit();
|
||||
|
||||
//print to default printer.
|
||||
worksheet.PrintOutEx(Type.Missing, Type.Missing,
|
||||
Type.Missing, Type.Missing, Type.Missing,
|
||||
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
|
||||
|
||||
// Cleanup Excel
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
Marshal.FinalReleaseComObject(worksheet);
|
||||
|
||||
workbook.Close(false, Type.Missing, Type.Missing);
|
||||
Marshal.FinalReleaseComObject(workbook);
|
||||
|
||||
excel.Quit();
|
||||
Marshal.FinalReleaseComObject(excel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -143,7 +143,7 @@
|
||||
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
|
||||
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
|
||||
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAAU
|
||||
CQAAAk1TRnQBSQFMAgEBAgEAARgBAAEYAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
|
||||
CQAAAk1TRnQBSQFMAgEBAgEAASABAAEgAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
|
||||
AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA
|
||||
AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5
|
||||
AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA
|
||||
@ -187,6 +187,9 @@
|
||||
<metadata name="saveFileDialogReport.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>151, 19</value>
|
||||
</metadata>
|
||||
<metadata name="PrintDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>351, 19</value>
|
||||
</metadata>
|
||||
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
|
||||
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
|
Loading…
Reference in New Issue
Block a user