Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void saveToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Microsoft.Office.Interop.Excel.Application ExcelApp =
- new Microsoft.Office.Interop.Excel.Application();
- Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook;
- Microsoft.Office.Interop.Excel.Worksheet ExcelWorkSheet;
- //Книга.
- ExcelWorkBook = ExcelApp.Workbooks.Add(System.Reflection.Missing.Value);
- //Таблица.
- ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelWorkBook.Worksheets.get_Item(1);
- for (int i = 0; i < dgvCustomer.ColumnCount; i++)
- {
- ExcelApp.Cells[1, i + 1] = Convert.ToString(dgvCustomer.Columns[i].HeaderText);
- }
- for (int i = 0; i < dgvCustomer.Rows.Count; i++)
- {
- for (int j = 0; j < dgvCustomer.ColumnCount; j++)
- {
- if (i==8)
- {
- Image data = ConvertBinaryToImage((byte[])dgvCustomer.Rows[4].Cells[0].Value);
- Clipboard.SetImage(data);
- }
- ExcelApp.Cells[i + 2, j + 1] = Convert.ToString(dgvCustomer.Rows[i].Cells[j].Value);
- }
- }
- int iLastRow = ExcelWorkSheet.Cells[ExcelWorkSheet.Rows.Count, "A"].End[Excel.XlDirection.xlUp].Row;
- for (int i = iLastRow; i >= 0; i--)
- {
- //ExcelWorkSheet.Cells[7].Delete();
- ExcelWorkSheet.Cells[8].Delete();
- ExcelWorkSheet.Cells[9].Delete();
- }
- for (int c = 1; c < 8; c++)
- {
- (ExcelWorkSheet.Rows[1].Cells[c] as Microsoft.Office.Interop.Excel.Range).Interior.Color = Color.LightGray;
- ExcelWorkSheet.Cells[1, c].HorizontalAlignment = 3;
- }
- for (int k = 2; k <= dgvCustomer.RowCount + 1; k++)
- {
- ExcelWorkSheet.Cells[k, 6].NumberFormat = ("+###\" \"(00) 000\"-\"00\"-\"00");
- // экранирование для формата номера
- // +###" "(00) 000"-"00"-"00
- ExcelWorkSheet.Hyperlinks.Add(ExcelWorkSheet.Cells[k, 7], ExcelWorkSheet.Cells[k, 7], Type.Missing, "Гиперссылка на картинку");
- }
- //ExcelWorkSheet.Shapes.AddPicture("D:\\test.png", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0, 80, 70);
- //label4.Text = string.Format("Total rows: {0}", dgvCustomer.RowCount.ToString());
- ExcelWorkSheet.Columns.AutoFit();
- Microsoft.Office.Interop.Excel.Range tRange = ExcelWorkSheet.UsedRange;
- tRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
- tRange.Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
- for (int t = dgvCustomer.RowCount + 2; t <= dgvCustomer.RowCount + 2; t++)
- {
- ExcelWorkSheet.Cells[t, 7] = string.Format("Total rows: {0}", dgvCustomer.RowCount.ToString());
- ExcelWorkSheet.Cells[t, 7].Font.Bold = true;
- ExcelWorkSheet.Cells[t, 7].HorizontalAlignment = Excel.Constants.xlRight;
- //ExcelWorkSheet.Cells[t, 7].Style.Font.Size = 12;
- }
- SaveFileDialog sfd = new SaveFileDialog()
- {
- Filter = "MS Excel dosuments (*.xlsx)|*.xlsx",
- DefaultExt = "*.xlsx",
- Title = "Укажите директорию и имя файла для сохранения"
- };
- if (sfd.ShowDialog() == DialogResult.OK)
- {
- ExcelWorkBook.SaveAs(sfd.FileName);
- ExcelApp.DisplayAlerts = true;
- }
- ExcelWorkBook.Close();
- ExcelApp.Application.Quit();
- ExcelApp.Quit();
- ExcelApp = null;
- ExcelWorkBook = null;
- ExcelWorkSheet = null;
- GC.Collect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement