SIGN UP MEMBER LOGIN:    
ARTICLE

How to Edit / Write in Excel file Sheet (Second Sheet)

Posted by Lion Articles | Office Development March 24, 2011
Here you will see how to Edit / Write in Excel file Sheet (Second Sheet).
Reader Level:


Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook objworkbook;
            Microsoft.Office.Interop.Excel.Worksheet objworksheet;
            try
            {
 
                objExcel.DisplayAlerts = false;
                objExcel.Visible = false;
                Range objRange = null;
                int row = 1, col = 1;
                objworkbook = objExcel.Workbooks.Open(newFilePath, true, false,
                                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                      Type.Missing, Type.Missing, true, Type.Missing,
                                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing);               
                objworksheet = (Microsoft.Office.Interop.Excel.Worksheet)objworkbook.Sheets.get_Item("Sheet2");
                int j = col;
                for (int i = 0; i < Mastrerfieldname.Length; i++)
                {
                    objRange = (Range)objworksheet.Cells[row, j];
                    objRange.Value2 = Mastrerfieldname[i].ToString();
                    objRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
                    objRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkBlue);
                    j++;
                }
                row++;

                objworksheet.Name = "MasterDetails";
                object objOpt = Missing.Value;
                objworkbook.Save();
                objworkbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
                objExcel.Quit();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objExcel = null;
                objworkbook = null;
                objworksheet = null;
                ReleaseComObject(objExcel);
                ReleaseComObject(objworkbook);
                ReleaseComObject(objworksheet);
            }

 //Release COM objects from memory
public void ReleaseComObject(object reference)
{
try
{
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(reference) <= 0)
{
}
}
catch
{
}
}
}

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

Good Coding but if you explain something by theory it will better....

Posted by Manish Tewatia Mar 25, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor