public void exportToExcel(System.Windows.Controls.DataGrid grid, string fileName = "Excel") { try { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel Documents (*.xls)|*.xls"; sfd.FileName = fileName + ".xls"; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // Copy DataGridView results to clipboard copyAlltoClipboard(grid); object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.DisplayAlerts = false; // Without this you will get two confirm overwrite prompts Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Format column D as text before pasting results, this was required for my data Microsoft.Office.Interop.Excel.Range rng = xlWorkSheet.get_Range("D:D").Cells; rng.NumberFormat = "@"; // Paste clipboard results to worksheet range Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); // For some reason column A is always blank in the worksheet. ¯\_(ツ)_/¯ // Delete blank column A and select cell A1 Microsoft.Office.Interop.Excel.Range delRng = xlWorkSheet.get_Range("A:A").Cells; delRng.Delete(Type.Missing); xlWorkSheet.get_Range("A1").Select(); // Save the excel file under the captured location from the SaveFileDialog xlWorkBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlexcel.DisplayAlerts = true; xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlexcel); // Clear Clipboard and DataGridView selection System.Windows.Clipboard.Clear(); // roomsGrid.ClearSelection(); // Open the newly saved excel file if (File.Exists(sfd.FileName)) { System.Diagnostics.Process.Start(sfd.FileName); } } } catch (Exception e) { System.Windows.MessageBox.Show(e.ToString()); } }
public static void v_excel(SMRC.DGVt dgv1) { // if (dgv1.GetClipboardContent() == null || !Clipboard.ContainsText()) return; //if (dgv1.GetClipboardContent() == null ) return; if (dgv1.RowCount == 0) { return; } dgv1.SelectAll(); dgv1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText; Clipboard.SetDataObject(dgv1.GetClipboardContent()); Microsoft.Office.Interop.Excel.Application ExlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook WrkBk = ExlApp.Workbooks.Add(System.Reflection.Missing.Value); //WrkBk = null; //ExlApp.Quit(); ExlApp = null; GC.Collect(); return; Microsoft.Office.Interop.Excel.Worksheet WrkSht = (Microsoft.Office.Interop.Excel.Worksheet)WrkBk.ActiveSheet; WrkSht.Cells.NumberFormat = "@"; WrkSht.Cells.ColumnWidth = 20; WrkSht.Cells.WrapText = true; ExlApp.Visible = true; WrkSht.get_Range("A1", "A1").Select(); //WrkSht.PasteSpecial("Текст", false, false, null, null, null); // , null --2k3 if (Clipboard.ContainsText()) { WrkSht.PasteSpecial("Текст", false, false, null, null, null, null); } object m_objOpt = System.Reflection.Missing.Value; //if (dgv1.GetClipboardContent() == null) return; //dgv1.CurrentCell = dgv1.FirstDisplayedCell; //dgv1.SelectAll(); //dgv1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText; //Clipboard.SetDataObject(dgv1.GetClipboardContent()); //Microsoft.Office.Interop.Excel.Workbook WrkBk; Microsoft.Office.Interop.Excel.Worksheet WrkSht; //Microsoft.Office.Interop.Excel.Application ExlDb = new Microsoft.Office.Interop.Excel.Application(); //WrkBk = ExlDb.Workbooks.Add(m_objOpt); //WrkSht = (Microsoft.Office.Interop.Excel.Worksheet)WrkBk.ActiveSheet; //WrkSht.get_Range("A1", m_objOpt).Select(); // //((Microsoft.Office.Interop.Excel.Range)ExlDb.Selection).ColumnWidth = 20; //((Microsoft.Office.Interop.Excel.Range)ExlDb.Selection).WrapText = true; //Int32 i = 0; //for (Int32 iCol = 1; iCol <= dgv1.Columns.Count; iCol++) //{ // if (!dgv1.Columns[iCol - 1].Visible) // { // ((Microsoft.Office.Interop.Excel.Range)WrkSht.Columns[iCol + i, m_objOpt]).Delete(Microsoft.Office.Interop.Excel.XlDirection.xlToLeft); // i = i - 1; // } //} WrkSht.get_Range("A1", "A1").Select(); ExlApp.Visible = true; WrkSht = null; WrkBk = null; /*ExlApp.Quit();*/ ExlApp = null; GC.Collect(); }
public void TransferringDatagridviewtoExcel(DataGridView dataGridView, string FileName) { try { DialogResult dialogResult = MessageBox.Show("Kayıtları Excel'e Aktarılsın mı?", "Excel Aktarma", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dialogResult == DialogResult.Yes) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel Documents (*.xls)|*.xls"; sfd.FileName = FileName + ".xls"; if (sfd.ShowDialog() == DialogResult.OK) { copyAlltoClipboard(dataGridView); object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.DisplayAlerts = false; Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Microsoft.Office.Interop.Excel.Range rng = xlWorkSheet.get_Range("D:D").Cells; rng.NumberFormat = "@"; Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); Microsoft.Office.Interop.Excel.Range delRng = xlWorkSheet.get_Range("A:A").Cells; delRng.Delete(Type.Missing); xlWorkSheet.get_Range("A1").Select(); xlWorkBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlexcel.DisplayAlerts = true; xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlexcel); Clipboard.Clear(); dataGridView.ClearSelection(); if (File.Exists(sfd.FileName)) { System.Diagnostics.Process.Start(sfd.FileName); } } } } catch (Exception) { MessageBox.Show("Excel'e Aktarılma Sırasında Hata Oluştu."); } }
private void shrani_tabelo_bt_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel Documents (*.xls)|*.xls"; sfd.FileName = "tabela_ahp_metode.xls"; if (sfd.ShowDialog() == DialogResult.OK) { // Copy DataGridView results to clipboard copyAlltoClipboard(); object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.DisplayAlerts = false; // Without this you will get two confirm overwrite prompts Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Format column D as text before pasting results, this was required for my data Microsoft.Office.Interop.Excel.Range rng = xlWorkSheet.get_Range("D:D").Cells; rng.NumberFormat = "@"; // Paste clipboard results to worksheet range Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); Microsoft.Office.Interop.Excel.Range delRng = xlWorkSheet.get_Range("A:A").Cells; delRng.Delete(Type.Missing); xlWorkSheet.get_Range("A1").Select(); // Save the excel file under the captured location from the SaveFileDialog xlWorkBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlexcel.DisplayAlerts = true; xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlexcel); // Clear Clipboard and DataGridView selection Clipboard.Clear(); dataGridView1.ClearSelection(); // Open the newly saved excel file if (File.Exists(sfd.FileName)) { System.Diagnostics.Process.Start(sfd.FileName); } } }
private void btnPrint_Click(object sender, EventArgs e) { Process[] processesBefore = Process.GetProcessesByName("excel"); //unformat the grid because it causes big issues foreach (DataGridViewColumn col in dataGridView1.Columns) { col.DefaultCellStyle.WrapMode = DataGridViewTriState.False; } dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; // int customer_index = 0; customer_index = dataGridView1.Columns["Customer"].Index; for (int i = 0; i < dataGridView1.Rows.Count; i++) { string temp = dataGridView1.Rows[i].Cells[customer_index].Value.ToString(); temp = temp.Trim(); dataGridView1.Rows[i].Cells[customer_index].Value = temp; } string FileName = @"C:\temp\temp.xls"; // Copy DataGridView results to clipboard dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText; dataGridView1.SelectAll(); DataObject dataObj = dataGridView1.GetClipboardContent(); if (dataObj != null) { Clipboard.SetDataObject(dataObj); } object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.DisplayAlerts = false; // Without this you will get two confirm overwrite prompts Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Format column D as text before pasting results, this was required for my data Microsoft.Office.Interop.Excel.Range rng = xlWorkSheet.get_Range("D:D").Cells; rng.NumberFormat = "@"; // Get Excel processes after opening the file. Process[] processesAfter = Process.GetProcessesByName("excel"); // Paste clipboard results to worksheet range Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); // For some reason column A is always blank in the worksheet. ¯\_(ツ)_/¯ // Delete blank column A and select cell A1 //Microsoft.Office.Interop.Excel.Range delRng = xlWorkSheet.get_Range("A:A").Cells; //delRng.Delete(Type.Missing); xlWorkSheet.get_Range("A1").Select(); Microsoft.Office.Interop.Excel.Worksheet ws = xlexcel.ActiveWorkbook.Worksheets[1]; Microsoft.Office.Interop.Excel.Range range = ws.UsedRange; //ws.Columns.ClearFormats(); //ws.Rows.ClearFormats(); //range.EntireColumn.AutoFit(); //range.EntireRow.AutoFit(); xlWorkSheet.Range["A1:G1"].Interior.Color = System.Drawing.Color.LightSkyBlue; xlWorkSheet.Columns[2].ColumnWidth = 98.14; xlWorkSheet.Columns[2].WrapText = true; xlWorkSheet.Range["H1:H300"].NumberFormat = "£#,###,###.00"; ws.Columns.AutoFit(); ws.Rows.AutoFit(); range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; range.Borders.Color = ColorTranslator.ToOle(Color.Black); // Save the excel file under the captured location from the SaveFileDialog xlWorkBook.SaveAs(FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlexcel.DisplayAlerts = true; xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit(); //releaseObject(xlWorkSheet); //releaseObject(xlWorkBook); //releaseObject(xlexcel); // Clear Clipboard and DataGridView selection Clipboard.Clear(); dataGridView1.ClearSelection(); // Open the newly saved excel file if (File.Exists(FileName)) { System.Diagnostics.Process.Start(FileName); } Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks workbooks; Microsoft.Office.Interop.Excel.Workbook excelBook; //app = null; //app = new Excel.Application(); // create a new instance excelApp.DisplayAlerts = false; //turn off annoying alerts that make me want to cryyyy workbooks = excelApp.Workbooks; excelBook = workbooks.Add(FileName); Microsoft.Office.Interop.Excel.Sheets sheets = excelBook.Worksheets; Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)(sheets[1]); //Range.Rows.AutoFit(); //Range.Columns.AutoFit(); excelApp.Quit(); // Now find the process id that was created, and store it. int processID = 0; foreach (Process process in processesAfter) { if (!processesBefore.Select(p => p.Id).Contains(process.Id)) { processID = process.Id; // And now kill the process. if (processID != 0) { Process process2 = Process.GetProcessById(processID); process2.Kill(); } } } }
private void btnPrint_Click(object sender, EventArgs e) { string FileName = @"C:\temp\temp.xls"; // Copy DataGridView results to clipboard dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText; dataGridView1.SelectAll(); DataObject dataObj = dataGridView1.GetClipboardContent(); if (dataObj != null) { Clipboard.SetDataObject(dataObj); } object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.DisplayAlerts = false; // Without this you will get two confirm overwrite prompts Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Format column D as text before pasting results, this was required for my data //Microsoft.Office.Interop.Excel.Range rng = xlWorkSheet.get_Range("D:D").Cells; //rng.NumberFormat = "@"; // Paste clipboard results to worksheet range Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); // For some reason column A is always blank in the worksheet. ¯\_(ツ)_/¯ // Delete blank column A and select cell A1 //Microsoft.Office.Interop.Excel.Range delRng = xlWorkSheet.get_Range("A:A").Cells; //delRng.Delete(Type.Missing); xlWorkSheet.get_Range("A1").Select(); Microsoft.Office.Interop.Excel.Worksheet ws = xlexcel.ActiveWorkbook.Worksheets[1]; Microsoft.Office.Interop.Excel.Range range = ws.UsedRange; //ws.Columns.ClearFormats(); //ws.Rows.ClearFormats(); //range.EntireColumn.AutoFit(); //range.EntireRow.AutoFit(); xlWorkSheet.Range["A1:D1"].Interior.Color = System.Drawing.Color.LightSkyBlue; ws.Columns.AutoFit(); ws.Rows.AutoFit(); range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; range.Borders.Color = ColorTranslator.ToOle(Color.Black); // Save the excel file under the captured location from the SaveFileDialog xlWorkBook.SaveAs(FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlexcel.DisplayAlerts = true; xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit(); //releaseObject(xlWorkSheet); //releaseObject(xlWorkBook); //releaseObject(xlexcel); // Clear Clipboard and DataGridView selection Clipboard.Clear(); dataGridView1.ClearSelection(); // Open the newly saved excel file if (File.Exists(FileName)) { System.Diagnostics.Process.Start(FileName); } Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks workbooks; Microsoft.Office.Interop.Excel.Workbook excelBook; //app = null; //app = new Excel.Application(); // create a new instance excelApp.DisplayAlerts = false; //turn off annoying alerts that make me want to cryyyy uint processID = 0; workbooks = excelApp.Workbooks; excelBook = workbooks.Add(FileName); Microsoft.Office.Interop.Excel.Sheets sheets = excelBook.Worksheets; Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)(sheets[1]); //Range.Rows.AutoFit(); //Range.Columns.AutoFit(); }
private void GenerateExcel(bool isPDF, SaveFileDialog sfd) { CopyAlltoClipboard(); object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlexcel = new Microsoft.Office.Interop.Excel.Application(); xlexcel.DisplayAlerts = false; Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int colStart = 1; int addCol = 0; int logoCellLeft = 0; if (cboReports.SelectedValue.ToString() == "Absentees Report" || cboReports.SelectedValue.ToString() == "No Time Out Report") { addCol = 2; colStart += addCol; logoCellLeft = 100; } else if (cboReports.SelectedValue.ToString() == "Attendance Report") { logoCellLeft = 12; } Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[8, colStart]; xlWorkSheet.Shapes.AddPicture(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @LOGO_PATH, MsoTriState.msoFalse, MsoTriState.msoCTrue, logoCellLeft, 0, 90, 90); xlWorkSheet.get_Range("A1", "A1").Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; xlWorkSheet.get_Range("A1", "A1").Style.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignTop; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); CR.Rows.AutoFit(); CR.get_Range("A1").Columns.EntireColumn.AutoFit(); CR.get_Range("C1", "K1").Columns.EntireColumn.AutoFit(); CR.get_Range("B1").Columns.ColumnWidth = 35; if (cboReports.SelectedValue.ToString().Trim() == "Attendance Report") { CR.get_Range("D1", "K1").Columns.EntireColumn.AutoFit(); CR.get_Range("C1").Columns.ColumnWidth = 20; } CR.get_Range("B1").Cells.Style.WrapText = true; xlWorkSheet.Cells[3, 3 + addCol - 1] = "' " + cboReports.SelectedValue.ToString(); xlWorkSheet.Cells[3, 3 + addCol - 1].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; xlWorkSheet.Cells[4, 3 + addCol - 1] = "' " + dtFrom.SelectedDate.Value.ToString("dd MMMMM yyyy") + " to " + dtTo.SelectedDate.Value.ToString("dd MMMM yyyy"); xlWorkSheet.Cells[4, 3 + addCol - 1].Cells.Style.WrapText = false; xlWorkSheet.Cells[4, 3 + addCol - 1].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; xlWorkSheet.Cells[5, 3 + addCol - 1] = "' " + GetFilterUsed().Replace("_", ""); xlWorkSheet.Cells[5, 3 + addCol - 1].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; int ctr = 1; while (ctr <= dgResults.Columns.Count) { ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[8, ctr + addCol]).Interior.Color = ColorTranslator.ToOle(Color.AliceBlue); ctr++; } for (int ctrC = 1; ctrC <= dgResults.Columns.Count; ctrC++) { for (int ctrR = 8; ctrR <= dgResults.Items.Count + 8; ctrR++) { ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[ctrR, ctrC + addCol]).BorderAround(LineStyle.Thin, Microsoft.Office.Interop.Excel.XlBorderWeight.xlHairline, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.AliceBlue)); } } Microsoft.Office.Interop.Excel.Range rg = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, colStart]; rg.EntireColumn.NumberFormat = "yyyy/MM/dd"; if (cboReports.SelectedValue.ToString() == "Consolidated Report") { rg.EntireColumn.NumberFormat = "@"; } xlWorkBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlexcel.DisplayAlerts = true; xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit(); ReleaseObject(xlWorkSheet); ReleaseObject(xlWorkBook); ReleaseObject(xlexcel); dgResults.UnselectAllCells(); Clipboard.Clear(); if (File.Exists(sfd.FileName) && !isPDF) { CheckIfFileIsOpen(sfd); //System.Diagnostics.Process.Start(sfd.FileName); System.Diagnostics.Process.Start(Path.GetDirectoryName(sfd.FileName)); } }