private void copyGridtoClipboard() { Clipboard.Clear(); if (DataCreationGrid.GetClipboardContent() != null) { DataCreationGrid.SelectAll(); Clipboard.SetDataObject(DataCreationGrid.GetClipboardContent()); Clipboard.GetData(DataFormats.Text); IDataObject dt = Clipboard.GetDataObject(); if (dt.GetDataPresent(typeof(string))) { string tb = (string)(dt.GetData(typeof(string))); Encoding encoding = Encoding.GetEncoding(1251); byte[] dataStr = encoding.GetBytes(tb); Clipboard.SetDataObject(encoding.GetString(dataStr)); } } }
public void SaveDataToExcel(string fileName) { copyGridtoClipboard(); object misValue = System.Reflection.Missing.Value; Excel.Application xlexcel = new Excel.Application(); Excel.Workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet; xlexcel.Visible = false; xlexcel.DisplayAlerts = false; Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); // Delete blank column A and select cell A1 Excel.Range delRng = xlWorkSheet.get_Range("A:A").Cells; delRng.Delete(Type.Missing); xlWorkSheet.get_Range("A1").Select(); xlWorkBook.SaveAs(fileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlexcel.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlexcel); Clipboard.Clear(); DataCreationGrid.ClearSelection(); /* try { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Visible = true; Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(System.Reflection.Missing.Value); Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; int StartCol = 1; int StartRow = 1; //Write Headers for (int col = 0; col < DataCreationGrid.Columns.Count; col++) { Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[StartRow, StartCol + col]; myRange.Value2 = DataCreationGrid.Columns[col].HeaderText; } StartRow++; //Write datagridview content for (int row = 0; row < DataCreationGrid.Rows.Count; row++) { for (int col = 0; col < DataCreationGrid.Columns.Count; col++) { try { Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[StartRow + row, StartCol + col]; myRange.Value2 = DataCreationGrid[col, row].Value == null ? "" : DataCreationGrid[col, row].Value; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } */ }