Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can we write a particular row of the excel from c# , not to a cell the whole row. i have tried to write to a cell but to a row i failed to write

What I have tried:

C#
private void button2_Click(object sender, EventArgs e)
        {
            MyExcel.Application excelApp;
            MyExcel.Workbook excelWorkBook;
            MyExcel.Worksheet excelWorkSheet;
            MyExcel.Range range;

            string str;
            int rowCount = 0;
            int colCount = 0;

            excelApp = new MyExcel.Application();
            excelWorkBook = excelApp.Workbooks.Open("e:\\SampleFile.xls", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            excelWorkSheet = (MyExcel.Worksheet)excelWorkBook.Worksheets.get_Item(1);
 

            range = excelWorkSheet.UsedRange;

            for (rowCount = 1; rowCount <= range.Rows.Count; rowCount++)
            {
                for (colCount = 1; colCount <= range.Columns.Count; colCount++)
                {
                    str = (string)(range.Cells[rowCount, colCount] as MyExcel.Range).Value2;
                    MessageBox.Show(str);
                }
            }
            excelWorkSheet.Cells[rowCount, 1] = "new name";
            excelWorkBook.Save();
            excelWorkBook.Close();
            excelApp.Quit();

            releaseObject(excelWorkSheet);
            releaseObject(excelWorkBook);
            releaseObject(excelApp);
        }
Posted
Updated 9-Sep-16 22:20pm
v2
Comments
SwethaVijayan 10-Sep-16 4:21am    
the folowing answer solve my problem plese go throuhg that code if any one face the problem

Replace this:
C#
for (rowCount = 1; rowCount <= range.Rows.Count; rowCount++)
{
    for (colCount = 1; colCount <= range.Columns.Count; colCount++)
    {
        str = (string)(range.Cells[rowCount, colCount] as MyExcel.Range).Value2;
        MessageBox.Show(str);
    }
}

with:
C#
int r = 1; 
int rowCount = 10;
int c = 1; 
int colCount = 10;
//multiplication table
for (r = 1; r <= rowCount; r++)
{
    for (c = 1; c <= colCount; c++)
    {
        range.Cells[rowCount, colCount].Value = r*c;
        //MessageBox.Show(range.Cells[rowCount, colCount].Value);
    }
}
 
Share this answer
 
Comments
SwethaVijayan 10-Sep-16 4:22am    
ya it is corret in this way to insert a value in the cell
C#
private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
private static Microsoft.Office.Interop.Excel.Application oXL;
public static void ReadExistingExcel()
{
   string path = @"C:\Tool\Reports1.xls";
   oXL = new Microsoft.Office.Interop.Excel.Application();
   oXL.Visible = true;
   oXL.DisplayAlerts = false;
   mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
   //Get all the sheets in the workbook
  mWorkSheets = mWorkBook.Worksheets;
   //Get the allready exists sheet
   mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
   Microsoft.Office.Interop.Excel.Range range= mWSheet1.UsedRange;
   int colCount = range.Columns.Count;
   int rowCount= range.Rows.Count;
   for (int index = 1; index < 15; index++)
   {
      mWSheet1.Cells[rowCount + index, 1] = rowCount +index;
      mWSheet1.Cells[rowCount + index, 2] = "New Item"+index;
   }
   mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
   Missing.Value, Missing.Value, Missing.Value,    Missing.Value,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
   Missing.Value, Missing.Value, Missing.Value,
   Missing.Value, Missing.Value);
   mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
   mWSheet1 = null;
   mWorkBook = null;
   oXL.Quit();
   GC.WaitForPendingFinalizers();
   GC.Collect();
   GC.WaitForPendingFinalizers();
   GC.Collect();
} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900