From 4d13eba475543a0dd637cf3d511cad63122bca55 Mon Sep 17 00:00:00 2001 From: kougyokugentou <41278462+kougyokugentou@users.noreply.github.com> Date: Sun, 14 Feb 2021 21:11:34 -0800 Subject: [PATCH] Implement printing functionality by stealing excel. --- GreatHomeChildcare.csproj | 3 ++ frmReports.Designer.cs | 6 +++ frmReports.cs | 77 ++++++++++++++++++++++++++++++--------- frmReports.resx | 5 ++- 4 files changed, 72 insertions(+), 19 deletions(-) diff --git a/GreatHomeChildcare.csproj b/GreatHomeChildcare.csproj index 9f5e552..4b6ba1b 100644 --- a/GreatHomeChildcare.csproj +++ b/GreatHomeChildcare.csproj @@ -64,6 +64,9 @@ packages\Dapper.2.0.35\lib\net461\Dapper.dll + + True + packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.dll diff --git a/frmReports.Designer.cs b/frmReports.Designer.cs index 02074c3..84c0380 100644 --- a/frmReports.Designer.cs +++ b/frmReports.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/frmReports.cs b/frmReports.cs index 0e7679b..ec98d2e 100644 --- a/frmReports.cs +++ b/frmReports.cs @@ -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 - string filename = Environment.GetEnvironmentVariable("TEMP"); - filename += @"\GHCReport.csv"; + DialogResult dr = PrintDialog.ShowDialog(); - //Check to see if the file exists. If so, delete it. - if(File.Exists(filename)) + if (dr == DialogResult.OK) //print { - File.Delete(filename); + string filename = Environment.GetEnvironmentVariable("TEMP"); + filename += @"\GHCReport.csv"; + + //Check to see if the file exists. If so, delete it. + 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)) + { + 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); } - - //Write the CSV. - SaveCSV(filename); - - //Check again to see if writing the CSV was successful. - //If not, show a message. - 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. } } } diff --git a/frmReports.resx b/frmReports.resx index beafccf..7cd70ec 100644 --- a/frmReports.resx +++ b/frmReports.resx @@ -143,7 +143,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAAU - CQAAAk1TRnQBSQFMAgEBAgEAARgBAAEYAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo + CQAAAk1TRnQBSQFMAgEBAgEAASABAAEgAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA @@ -187,6 +187,9 @@ 151, 19 + + 351, 19 +