2 Replies - 718 Views - Last Post: 15 January 2013 - 10:01 AM Rate Topic: -----

#1 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

covert value in row(from excel) to variable

Posted 15 January 2013 - 09:28 AM

So I have an excel doc with 3 columns (id, Name, Description) then...
    protected string Name;
    protected string Description;

    protected void Page_Load(object sender, EventArgs e)
    {
        ExcelFile ef = new ExcelFile();
        string fileName = @"C:\locationOfSpreadsheet.xlsx";
        ef.LoadXlsx(fileName, XlsxOptions.PreserveMakeCopy);

        StringBuilder sb = new StringBuilder();

        foreach (ExcelWorksheet sheet in ef.Worksheets)
        {
            sb.AppendLine();
            sb.AppendFormat("------{0}--------", sheet.Name);
            
            foreach (ExcelRow row in sheet.Rows)
            {
                sb.AppendLine();
                Description = row.Cells[0].Value;

                foreach (ExcelCell cell in row.AllocatedCells)
                {
                    if (cell.Value != null)
                    {
                        Label x = new Label();
                        x.Text = cell.Value.ToString();
                        uploadExcel.Controls.Add(x);                       

                        sb.AppendFormat("{0}({1})", cell.Value, cell.Value.GetType().Name);
                        sb.Append("\t");
                    }
                }
            }
        }      
        Console.WriteLine(sb.ToString());
    }


Inside the 2nd for loop I am using Description = row.Cells[0].Value; but its throwing an error about can't convert an object to a string, but I don't think i'm on the right path anyway so....
This reads in the excel doc fine as I can display it on screen but I want to transfer the data over to the variables declared at the top name and description, which are the second and third column in the doc.

Please help

Is This A Good Question/Topic? 0
  • +

Replies To: covert value in row(from excel) to variable

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: covert value in row(from excel) to variable

Posted 15 January 2013 - 09:51 AM

Have you tried Description = row.Cells[0].Value.ToString();?
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,576
  • Joined: 12-December 12

Re: covert value in row(from excel) to variable

Posted 15 January 2013 - 10:01 AM

You might have to use

Description = (row.Cells[0].Value).ToString();
// or
Description = (string) row.Cells[0].Value;


Description and Name are single strings so you will end up keeping just the last cell value. If you want to keep a number of values then you will either have to concatenate them to the string(s), perhaps separated by commas, or use an array.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1