Click here to Skip to main content
15,896,315 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi ,
I have designed a bulk export in my project and it exports in Excel file when user exports the data. Actually i dont have Microsoft excel and I have designed the data in HTML table view and binded to .xls and exporter the file.. So now when the user tries to open the file... it will be opened in excel sheet with customized table view.....


Now my big problem is that now the same file will be modified by the user and the file will uploaded for Bulk Impporting data... So as it is not pure excel file I am unable to read the data from it.... please help me out... generally the below code I will use for reading excel data... please review code and help me out

C#
public void ReadExcelData(string FilePath)
  {
      try
      {
          if (!string.IsNullOrEmpty(FilePath))
          {
              string strExtension = Path.GetExtension(FilePath);
              string strFileName = string.Empty;
              string FileOrginalName = FilePath.Split('|')[0];
              string action = string.Empty;
              string connectionString = string.Empty;
              OleDbCommand ExcelCommand;
              OleDbDataAdapter ExcelAdapter;
              DataTable dtSuspendUsers = new DataTable();
              DataTable dtRawData = new DataTable();
              strFileName = Path.GetFileNameWithoutExtension(FilePath);
              string strSourcePath = Server.MapPath("../ShippingDataUploadFiles/" + strFileName);
              if (strExtension == ".xls" || strExtension == ".xlsx")
              {

                  lblErrorMessage.Text = "";
                  if (strExtension == ".xls")
                  {
                      connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strSourcePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                  }
                  else if (strExtension == ".xlsx")
                  {
                      connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strSourcePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; ;
                  }
                  OleDbConnection con = new OleDbConnection(connectionString);
                  OleDbCommand cmd = new OleDbCommand();
                  con.Open();
                  //Get the list of sheet available in excel sheet
                  DataTable dtExcelDetails = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                  //Get first sheet name
                  string getExcelSheetName = string.Empty;

                  //getExcelSheetName = foundRows["Table_Name"].ToString();

                  if (true)
                  {
                      //Select rows from second sheet in excel sheet and fill into dataset
                      ExcelCommand = new OleDbCommand(@"SELECT * FROM [" + "AddressBook$" + @"]", con);
                      ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
                      ExcelAdapter.Fill(dtRawData);
                      con.Close();

                      if (dtRawData.Rows.Count > 0)
                      {
                          ViewState["dtRawData"] = dtRawData;
                          // FormatShippingAddress(dtRawData);
                      }


                      if (dtRawData.Rows.Count > 0)
                      {
                          string[] strAddTemplateColumns = { "ShippingAddressID", "LineUp_Address_Id", "LineUp" };
                          for (int i = 0; i < strAddTemplateColumns.Count(); i++)
                          {
                              if (dtRawData.Columns.Contains(strAddTemplateColumns[i]))
                              {
                                  hdnTemplateType.Value = "Add/DeleteExportTemplate";
                              }

                          }
                          if (string.IsNullOrEmpty(hdnTemplateType.Value))
                          {
                              hdnTemplateType.Value = "DataFeedTemplate";
                          }
                      }


                      string[] FileDetails = new string[10];
                      FileDetails[0] = strFileName;
                      if (Session["UserName"] != null)
                      {
                          FileDetails[1] = Session["UserName"].ToString();
                      }
                      else
                      {
                          FileDetails[1] = "Admin";
                      }
                      FileDetails[2] = DateTime.Now.ToString();
                      FileDetails[3] = dtRawData.Rows.Count.ToString();
                      ShowUploadedDetails(FileDetails);
                  }
              }
              else
              {
                  lblErrorMessage.Text = "Please upload only .xls or .xlsx file format.";
              }
          }
      }
      catch (Exception ex)
      {

          throw;
      }
  }
Posted

1 solution

For bulk upload, I recommend you to use .CSV files. These files are easily can be read in c#.
Best of luck.
 
Share this answer
 
Comments
jing567 30-Jan-15 2:59am    
I canr recomment Client yar :)
Actually end user make some changes in excel and then try to import... so inorder to make easy i have to export the file in excel...
Hope u understand sanjay :)

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