Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

covert value in row(from excel) to variable

$
0
0
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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>