public void Print_Reestr(MySqlQueries mySqlQueries, string otdelenie, DateTimePicker dateTimePicker1, DateTimePicker dateTimePicker2, SaveFileDialog saveFileDialog, DialogResult dialog) { ExcelApplication ExcelApp = null; Workbooks workbooks = null; Workbook workbook = null; Worksheet worksheet = null; string fileName = null; saveFileDialog.DefaultExt = "Книга Excel|*.xlsx"; saveFileDialog.Filter = "Книга Excel|*.xlsx|Книга Excel 93-2003|*.xls|PDF|*.pdf"; saveFileDialog.Title = "Сохранить реестр эпикризов как"; saveFileDialog.FileName = "Реестр эпикризов отделения " + otdelenie + " с " + dateTimePicker1.Value.ToShortDateString() + " по " + dateTimePicker2.Value.ToShortDateString(); saveFileDialog.InitialDirectory = Application.StartupPath + "\\Реестры эпикризов\\"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { fileName = saveFileDialog.FileName; string id = Select_Text(MySqlQueries.Select_ID_Otdeleniya_ComboBox, null, otdelenie); string date1 = dateTimePicker1.Value.Year.ToString() + '-' + dateTimePicker1.Value.Month.ToString() + '-' + dateTimePicker1.Value.Day.ToString(); string date2 = dateTimePicker2.Value.Year.ToString() + '-' + dateTimePicker2.Value.Month.ToString() + '-' + dateTimePicker2.Value.Day.ToString(); DataTable data = Select_DataTable(MySqlQueries.Print_Reestr, id, date1, date2); try { ExcelApp = new ExcelApplication(); workbooks = ExcelApp.Workbooks; workbook = workbooks.Open(Application.StartupPath + "\\blanks\\reestr.xlsx"); worksheet = workbook.Worksheets.get_Item(1) as Worksheet; ExcelApp.Cells[1, 1] = "Реестр выписных эпикризов отделения " + '"' + otdelenie + '"'; ExcelApp.Cells[2, 1] = "c " + dateTimePicker1.Value.ToShortDateString() + " по " + dateTimePicker2.Value.ToShortDateString(); int ExCol = 1; int ExRow = 5; for (int i = 0; i < data.Rows.Count - 0; i++) { ExCol = 1; for (int j = 0; j < data.Columns.Count; j++) { ExcelApp.Cells[ExRow, ExCol] = data.Rows[i][j].ToString(); ExCol++; } ExRow++; } var cells = worksheet.get_Range("A4 ", "E" + (ExRow - 1).ToString()); cells.Borders[XlBordersIndex.xlInsideVertical].LineStyle = XlLineStyle.xlContinuous; cells.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = XlLineStyle.xlContinuous; cells.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous; cells.Borders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous; cells.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous; cells.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous; workbook.SaveAs(fileName); if (dialog == DialogResult.Yes) { ExcelApp.Quit(); } if (dialog == DialogResult.No) { workbook.PrintPreview(); ExcelApp.Visible = true; } if (dialog == DialogResult.Cancel) { workbook.PrintOut(); ExcelApp.Quit(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Marshal.ReleaseComObject(worksheet); Marshal.ReleaseComObject(workbook); Marshal.ReleaseComObject(workbooks); Marshal.ReleaseComObject(ExcelApp); } } }
public MySqlOperations(MySqlQueries sqlQueries) { this.MySqlQueries = sqlQueries; }