hello sir i am trying to convert .sdf file into excel shett. i get following error Cannot implicitly convert type 'object' to 'Excel._Worksheet'. An explicit conversion exists (are you missing a cast?)
i try this code
filenamesdf = textBox2.Text.ToString();
SqlCeConnection con = new SqlCeConnection("Data Source=" + filenamesdf + "");
SqlCeDataAdapter da = new SqlCeDataAdapter("select * from purchaseOrder", con);
DataTable dt1 = new DataTable();
da.Fill(dt1);
// creating Excel Application
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
// creating new Excelsheet in workbook
Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
excel.Visible = false;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];(in this line i get error)
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dt1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dt1.Columns[i - 1].Caption;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dt1.Rows.Count - 1; i++)
{
for (int j = 0; j < dt1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dt1.Rows[i][j].ToString();
}
}
// save the application
workbook.SaveAs("d:\\output.xls", Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel.Quit();
MessageBox.Show("Excel file created , you can find the file d:\\output.xls");
please tell me how to solve this problem