Click here to Skip to main content
15,904,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one excel sheet. in that first seven rows contains images. eighth row i have a data header . and nitnth row onwards i have data. how to read data in eighth row. in asp.net c#
Posted

You can provide range along with sheetname.

C#
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "A9:F25]";
da.SelectCommand = cmdExcel;
da.Fill(ds);
connExcel.Close();
 
Share this answer
 
It can be solved with "MicMicrosoft.Office.Interop.Excel".

1. Add the COM reference, "Microsoft Excel 14.0 Object Library", add "using Excel = MicMicrosoft.Office.Interop.Excel;"

2. Here is an example for reading data in the range "A8:B21" in "a.xls", and the sheet name is "Sheet1"

C#
var app = new Excel.Application();
Excel.Workbook workBook = app.Workbooks.Open(@"C:\Users\Administrator\Documents\a.xls");
Excel.Sheets sheets = workBook.Worksheets;
Excel.Worksheet datasheet = null;
foreach (Excel.Worksheet sheet in sheets)
{
    if (sheet.Name == "Sheet1")
    {
        datasheet = sheet;
        break;
    }
}

if (null == datasheet)
    return;

object[,] var = datasheet.get_Range("A9", "B21").get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);

for (int i = 1; i <= var.GetLength(0); ++i)
{
    for (int j = 1; j <= var.GetLength(1); ++j)
    {
        Console.Write((double)var[i, j] + " ");
    }
    Console.WriteLine();
}
workBook.Close();            
 
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