Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Browse Button code as follows

C#
private void Browse_Click(object sender, EventArgs e)
{
   try
   {
      DialogResult MsgDr = MessageBox.Show("Excel file format should be in Student Name,Parent Name,Mobile No,Marks. \nAre you sure want to continue?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
      if (MsgDr == DialogResult.No)
         return;

      LblSheetName.Text = "";
      OpenFileDialog fldg = new OpenFileDialog();
      fldg.Title = "Select File";
      fldg.FileName = txtFileName.Text;
      fldg.Filter = "Excel Sheet(*.xls)|*.xls|Excel-2007 Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
      fldg.FilterIndex = 1;
      fldg.RestoreDirectory = true; //added
      if (fldg.ShowDialog() == DialogResult.OK)
      {
         txtFileName.Text = fldg.FileName;
         FileInfo file = new FileInfo(txtFileName.Text);
         if(IsFileLocked(file) == true) //added
         {
            MessageBox.Show("Selected file is opened already/not properly closed/using by some other person. Close it properly and try again", "File already opened", MessageBoxButtons.OK);
            txtFileName.Text = string.Empty;
            return;
         }     //added
         Import();
         System.Windows.Forms.Application.DoEvents();
      }
   }
   catch (Exception ex1)
   {
      progressBar1.Visible = false;
      MessageBox.Show("Error in open" + ex1.ToString(), "Error", MessageBoxButtons.OK);
      return;
   }
}

private void Import()
{
   string message = string.Empty;
   try
   {
      int i = 0;                

      progressBar1.Visible = true;
      progressBar1.Value = 700;
      String Pathname = txtFileName.Text;
      FileStream stream = File.Open(Pathname, FileMode.Open, FileAccess.Read);
      IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream)
      excelReader.IsFirstRowAsColumnNames = true;
      DataSet result = excelReader.AsDataSet();
      excelReader.Close();
      System.Data.DataTable table = result.Tables[0];
      for (int irow = 1; i < table.Rows.Count; irow++)
      {
         for(int icol = 3; icol < table.Columns.Count; icol++)
         {
            message += table.Rows[irow][icol].ToString();
         }
      }
      label2.Text = message.ToString();
      dataGridView1.DataSource = table;
      progressBar1.Visible = false;
   }
   catch (Exception ex)
   {
      MessageBox.Show(message.ToString());
      progressBar1.Visible = false;
      MessageBox.Show("Error in Import" + ex.ToString(), "Error", MessageBoxButtons.OK);
      return;
   }
}



Excel sheet as follows

REO   MC  CL PH

 50   60  70 80
 10   40  80 60



Run mode as follows

Exam Marks File textbox1 Browse(Button)

When i click the Browse (Button) Selected Excel File will be displayed in the Datagridview.

In DataGridView As follows;
REO   MC  CL   PH

 50    60  70  80
 10    40  80  60


What i am doing is i have one Label in that label i am displaying all the marks as follows(From the Excek File)
Marks (Label)    5060708010408060

When i click the Browse button marks are displaying in the Label2 as follows
Marks (Label)    5060708010408060

But i want the marks to be displayed along with the subject name as follows
Marks (Label)    REO50MC60CL70PH80REO10MC40CL80PH60


for that from my above code what is the mistake i made.

Regards,
Narasiman P.
Posted
Updated 3-Dec-13 22:31pm
v3

1 solution

Extract the headers separately and store it. You can alternatively print the headers and scores.
 
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