Excel.Worksheet
-
Thursday, April 12, 2012 8:05 AM
HI
I got the error like cannot implicitly convert type Excel.worksheet to (Microsoft.office.interop.excel.worksheet)An Explicit conversion exist (are you missing a cast)
this is my code
protected void Page_Load(object sender, EventArgs e)
{
String sFileImage = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["UploadPath"], Session["UserId"].ToString() + ".gif");
String sFilePath = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["UploadPath"], Session["UserId"].ToString() + ".xls");
if (File.Exists(sFilePath)) { File.Delete(sFilePath); }
Application objApp = new Application();
Microsoft.Office.Interop.Excel.Worksheet objSheet = new Microsoft.Office.Interop.Excel.Worksheet();
Microsoft.Office.Interop.Excel.Workbook objWorkBook = null;
//object missing = System.Reflection.Missing.Value;
try
{
objWorkBook = objApp.Workbooks.Add(Type.Missing);
objSheet = objWorkBook.ActiveSheet;
//Add picture to single sheet1
objSheet =objWorkBook.Sheets[1];
objSheet.Name = "Graph with Report";
////////////// Or multiple sheets
for (int iSheet = 0; iSheet < objWorkBook.Sheets.Count - 1; iSheet++)
{
objSheet = objWorkBook.Sheets[iSheet] as Worksheet;
///(objSheet as Microsoft.Office.Interop.Excel._Worksheet).Activate();
}
/////////////////
objSheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 700, 350);
objWorkBook.SaveAs(sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception)
{
//Error Alert
}
finally
{
objWorkBook = null;
objApp = null;
} Please help me out of this error.- Edited by sherisha Thursday, April 12, 2012 8:10 AM
- Moved by Barry WangMicrosoft Contingent Staff Friday, April 13, 2012 5:44 AM Excel Issue (From:.NET Framework Setup)
All Replies
-
Thursday, April 12, 2012 1:45 PM
Hi Sherisha,
your question is Regarding Asp.Net Forum and this forum Topic is .Net Framework Setup.
Post your question in below forum for proper answer.
Regards
-
Thursday, April 12, 2012 5:05 PM
-
Friday, April 13, 2012 6:06 AMModerator
Hi sherisha,
From which line, the error displays? I guess this line:
objSheet = objWorkBook.Sheets[iSheet] as Worksheet;Please correct me if I was wrong.
cannot implicitly convert type Excel.worksheet to (Microsoft.office.interop.excel.worksheet)An Explicit conversion exist (are you missing a cast)
According to this line, it seems that you have added an alias name for "Microsoft.Office.Interop.Excel" namespace.Is there a line existing like: Using Excel = Microsoft.Office.Interop.Excel; in the the "using" part for importing the namespaces?
If so, there are two ways to which deserve a try:
Way 1: objSheet = objWorkBook.Sheets[iSheet] as Microsoft.Office.Interop.Excel.Worksheet;
Way 2: For all the object from Excel object model, use the alias name to show as Microsoft.Office.Interop.Excel. For Example, instead of declaring:
Microsoft.Office.Interop.Excel.Worksheet objSheet = new Microsoft.Office.Interop.Excel.Worksheet(); Microsoft.Office.Interop.Excel.Workbook objWorkBook = null;
try:
Excel.Worksheet objSheet = new Excel.Worksheet(); Excel.Workbook objWorkBook = null;
Let us know if you need any help.
Thanks.
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Yoyo JiangMicrosoft Contingent Staff, Moderator Friday, April 20, 2012 4:14 AM

