private void button2_Click(object sender, EventArgs e) { SFD.Filter = "Файлы Excel (*.xls; *.xlsx) | *.xls; *.xlsx"; if (SFD.ShowDialog() == DialogResult.OK) { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet = null; app.Visible = true; worksheet = workbook.ActiveSheet; worksheet.Name = "Выручка"; worksheet.Cells[1, 1] = "Выручка"; for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) { worksheet.Cells[2, i] = dataGridView1[i - 1, 0].Value; worksheet.Columns[i].ColumnWidth = 30; } for (int i = 1; i < dataGridView1.RowCount; i++) { for (int j = 0; j < dataGridView1.ColumnCount; j++) { worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value; } } workbook.SaveAs(SFD.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); app.Quit(); } }
private void button2_Click(object sender, EventArgs e) { //получение значения ИТОГО con = ClassSQL.GetConect(); string sc_selectID = "select sum(price) as 'Выручка за период' from ticket t join film_session s on s.session_id = t.session_id and t.payment = 1 and t.date_ticket <= dateadd(month," + znach + ", getdate()) and t.date_ticket >= dateadd(month, -" + znach + ", getdate());"; con.Open(); scom3 = new SqlCommand(sc_selectID, con); itogo = scom3.ExecuteScalar().ToString(); con.Close(); con.Dispose(); SFD.Filter = "Файлы Excel (*.xls; *.xlsx) | *.xls; *.xlsx"; if (SFD.ShowDialog() == DialogResult.OK) { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet = null; app.Visible = true; worksheet = workbook.ActiveSheet; worksheet.Name = "Выручка"; worksheet.Cells[1, 1] = "Фильм"; worksheet.Cells[1, 2] = "Количество проданных билетов"; worksheet.Cells[1, 3] = "Сумма(в руб.)"; worksheet.Cells[12, 1] = "Итого:"; worksheet.Cells[12, 3] = itogo; for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) { worksheet.Cells[2, i] = dataGridView1[i - 1, 0].Value; worksheet.Columns[i].ColumnWidth = 30; } for (int i = 1; i < dataGridView1.RowCount; i++) { for (int j = 0; j < dataGridView1.ColumnCount; j++) { worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value; } } workbook.SaveAs(SFD.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); app.Quit(); } }
private void exportToExcel_Click(DataGridView transcationTableDataGridView) { try { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet = null; app.Visible = true; worksheet = workbook.Sheets["Sheet1"]; worksheet = workbook.ActiveSheet; worksheet.Name = "Records"; try { /*for (int i = 0; i < transcationTableDataGridView.Columns.Count; i++) * { * worksheet.Cells[1, i + 1] = transcationTableDataGridView.Columns[i].HeaderText; * }*/ for (int i = 0; i < transcationTableDataGridView.Rows.Count; i++) { for (int j = 0; j < transcationTableDataGridView.Columns.Count; j++) { if (transcationTableDataGridView.Rows[i].Cells[j].Value != null && !transcationTableDataGridView.Rows[i].Cells[j].Value.ToString().Equals("")) { worksheet.Cells[i + 1, j + 1] = transcationTableDataGridView.Rows[i].Cells[j].Value.ToString(); /*Excel.Range ColorMeMine= worksheet.Cells[i + 1, j + 1]; * int ind = -1,ptr=0,color; * foreach (var index in transcationTableDataGridView.Rows[i].Cells[j].Value.ToString().findAll(Environment.NewLine)) * { * switch (ptr) { * case 0: color=System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black); * break; * case 1: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); * break; * case 2: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); * break; * case 3: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green); * break; * case 4: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue); * break; * case 5: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Cyan); * break; * case 6: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Magenta); * break; * case 7: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gold); * break; * case 8: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Navy); * break; * case 9: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Fuchsia); * break; * case 10: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Coral); * break; * case 11: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Indigo); * break; * case 12: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Lime); * break; * case 13: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Firebrick); * break; * case 14: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Tomato); * break; * default: * color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black); * break; * } * ColorMeMine.Characters[ind+1, index].Font.Color = color; * ind = index; * ptr = ptr + 1; * }*/ } else { worksheet.Cells[i + 1, j + 1] = ""; } } } //Getting the location and file name of the excel to save from user. SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; saveDialog.FilterIndex = 2; if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { workbook.SaveAs(saveDialog.FileName); MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } finally { app.Quit(); workbook = null; worksheet = null; } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }