Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using this code to read an Excel sheet. I want to read only the first row in a single column table. My cell is "B1". How do i do that using this code?


C#
using Excel = Microsoft.Office.Interop.Excel;

string ExcelFile = Application.StartupPath + @"\SurveyLinks.xlsx";
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(ExcelFile);
Excel._Worksheet ExcelWorkSheet = ExcelWorkBook.Sheets[1];
Excel.Range ExcelRange = ExcelWorkSheet.
I dont know what to insert after "ExcelWorksheet." Got any ideas?
Posted
Updated 2-Jun-21 3:15am

 
Share this answer
 
Comments
phantomlakshan 27-May-12 23:54pm    
I'm using this code but how can i save the deleted row to a variable and save the document automaticall? My code is,

string ExcelFile = Application.StartupPath + @"\SurveyLinks.xlsx";
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(ExcelFile);
Excel._Worksheet ExcelWorkSheet = ExcelWorkBook.Sheets[1];
Excel.Range ExcelRange = (Excel.Range)ExcelWorkSheet.Application.Rows[1, Type.Missing];
ExcelRange.Select();
ExcelRange.Delete(Excel.XlDirection.xlUp);
ExcelWorkBook.Close(true);
ExcelApp.ActiveWorkbook.SaveAs(Application.StartupPath);
ExcelApp.Quit();
MessageBox.Show("Done!");
Hi, that seem almost the same question posted by you three day ago. And the solution for your earlier question can solve this question too. Have a check please: How to delete the first row of an excel file using Excel interop?[^]
 
Share this answer
 
Comments
phantomlakshan 28-May-12 0:13am    
I'm using this code but how can i save the deleted row to a variable and save the document automaticall? My code is, string ExcelFile = Application.StartupPath + @"\SurveyLinks.xlsx"; Excel.Application ExcelApp = new Excel.Application(); Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(ExcelFile); Excel._Worksheet ExcelWorkSheet = ExcelWorkBook.Sheets[1]; Excel.Range ExcelRange = (Excel.Range)ExcelWorkSheet.Application.Rows[1, Type.Missing]; ExcelRange.Select(); ExcelRange.Delete(Excel.XlDirection.xlUp); ExcelWorkBook.Close(true); ExcelApp.ActiveWorkbook.SaveAs(Application.StartupPath); ExcelApp.Quit(); MessageBox.Show("Done!");
range = excelSheet.UsedRange;

Excel.Range firstRow = range.Rows[1];


 foreach (Excel.Range cell in firstRow.Cells)
{
    Console.WriteLine(cell.Value);
}
 
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