0

I'm using 'ExcelDataReader' to read in an Excel file, I want to display the file on screen.

protected void Page_Load(object sender, EventArgs e)
{
    string filePath = @"C:\WORK\BoireannSVN\trunk\VS\CRCConnect\Spreadsheet\Spreadsheet.xlsx";

    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

    IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

    excelReader.IsFirstRowAsColumnNames = true;
    DataSet result = excelReader.AsDataSet();

    while (excelReader.Read())
    {
        Label x = new Label();
        x.Text = result.Tables[0].Rows[0].ItemArray[0].ToString();
        uploadExcel.Controls.Add(x); 

        excelReader.GetInt32(0);
    }

    excelReader.Close();

}

I've added in the

Label x = new Label(); 
x.Text = result.Tables[0].Rows[0].ItemArray[0].ToString();

Attempting to get the excel sheet to display on screen, although I'm having no luck with this. I think it is something like

table1.Columns.Add;
table1.Rows.Add;

but can't seem to get it going, any help much appreciated.

shA.t
  • 16,580
  • 5
  • 54
  • 111
John
  • 3,965
  • 21
  • 77
  • 163
  • Are you looking to display the entire document, or just a single cell? – TheGeekZn Jan 16 '13 at 10:12
  • @NewAmbition Well I was going to start with the entire document for learning purposes then narrow it down to certain cells...any advise? – John Jan 16 '13 at 10:21
  • well, for the entire document, you could probably look at this: http://www.codeproject.com/Articles/321304/Word-and-Excel-Documents-View-in-IFrame-using-Tree – TheGeekZn Jan 16 '13 at 10:25
  • @NewAmbition thank you for that had a quick look over it but i'm actually looking to display 2 or 3 cells is there a shorter way around this? somethig like table.columns.[0].display? Or even to get the value contained in the cell? – John Jan 16 '13 at 10:35
  • Sorry, thats paid for :/ Just disreguard this last rply if it isnt useful, http://support.microsoft.com/kb/306572, official microsoft documentation on loading spreadsheets. You might have to see from there how to get individual cells, or just do a more detailed search on google to see what youre after. This question, and many like it, have already been answered. – TheGeekZn Jan 16 '13 at 10:40
  • @NewAmbition again, thanks for the reply that support.microsoft link applies to using OleDb, i'm using 'Excel data reader' software, I was only curious as to how to display the cells on screen but for my project I actually only need the values inside a few cels, thats why i was trying x.Text = result.Tables[0].Rows[0].ItemArray[0].ToString(); but no joy. I was also trying DataTable table = new DataTable("Tables"); table.Columns.Add(); table.Rows.Add(); but nothing yet, any other help perhaps? – John Jan 16 '13 at 10:48
  • @NewAmbition Label id = new Label(); id.Text = result.Tables[0].Rows[0].ItemArray[0].ToString() returns that value of first cell(viewd from breakpoint) but how do i view that on screen? – John Jan 16 '13 at 11:02
  • it only allows me to select a colum or a row either id2.Text = result.Tables[0].Rows[1].ItemArray[0].ToString(); or Ref.Text = result.Tables[0].Columns[2].ToString(); how can i select a certain row in a certain column? – John Jan 16 '13 at 11:06
  • To show an Excel workbook that has one or more sheets I can suggest you to use a DropDown with sheet names and a DataGrid to show each sheet data by setting its data-source to `result.Tables[sheetname]` -HTH ;). – shA.t Aug 20 '18 at 07:10

1 Answers1

0

I was in a similar situation I did the following:

var rows = results.Tables[0].AsEnumerable();

Then you can iterate and select the value for columns:

foreach (var dataRow in rows)
            {

                Console.WriteLine(dataRow[0]);
                //or
                Console.WriteLine(dataRow["ColumnName"]);
            }
Amin
  • 73
  • 1
  • 7