SADAM  HUSSAIN

SADAM HUSSAIN

  • NA
  • 65
  • 4.2k

How to export my Grid to Excel with Password proteced

Dec 2 2016 5:58 AM

I have a code but got error.plz help me

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int CCol = 1;
for (int j = 1; j <= dgcBankSetup.Columns.Count; j++)
{
xlWorkSheet.Cells[1, CCol] = dgcBankSetup.Columns[j].Header;
xlWorkSheet.Cells[1, CCol].ColumnWidth = dgcBankSetup.Columns[j].Width;
xlWorkSheet.Cells[1, CCol].Font.Bold = true;
CCol = CCol + 1;
}
foreach (DataRowView i in dgcBankSetup.ItemsSource)
{
DataRow DR = i.Row;
CCol = 1;
for (int j = 1; j <= dgcBankSetup.Columns.Count; j++)
{
xlWorkSheet.Cells[i, CCol].value = dgcBankSetup.rows
//gcBankSetup.SelectAllCells();
CCol = CCol + 1;
}
}
xlWorkSheet.Cells.Borders.LineStyle = 1;
xlWorkBook.SaveAs(SaveDialogBox.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
 

Answers (4)