SharePoint - Read excel file from document library and display it has data grid

Asked By Siva S
02-Dec-10 02:25 AM
I need to read excel file from document library and display it as grid view for a application. I have used oledb connection string for this.. The path of the file is not recoginzed. Can anyone share your ideas

Regards
Siva
  Santosh Prajapati replied to Siva S
02-Dec-10 02:30 AM
Hi,
hear is c# code  it's office 2007(dll Version 11).

use this code and let me know.

 public DataTable ExcelToDataSet()
    {
    string Path = Server.MapPath(("~//UploadLead//") + FileUpload1.FileName);
    // Initialize the Excel Application class    
    Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
    // Create the workbook object by opening the excel file.     
    Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(Path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
    // Get the active worksheet using sheet name or active sheet
    Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;

    int index = 1;
    object rowIndex = 1;
    object colIndex1 = 1;
    object colIndex2 = 2;
    object row = 1;
    int z = 1;



    #region "DataTable"

    ArrayList arr = new ArrayList();
    arr.Add("Business_Name");
    arr.Add("Business_Owner");
    arr.Add("Business_Owner_First_Name");
    arr.Add("Business_Owner_Last_Name");
    arr.Add("Job_Title");
    arr.Add("Daytime_Phone");
    arr.Add("Fax_Number");
    arr.Add("Email");
    arr.Add("Email_Backup_1");
    arr.Add("Email_Backup_2");
    arr.Add("Business_Address");
    arr.Add("City");
    arr.Add("State_Territory");
    arr.Add("County");
    arr.Add("Zip");
    arr.Add("Website");
    arr.Add("NAICS");
    arr.Add("FSC");
    arr.Add("PSC");
    arr.Add("Year_Established");
    arr.Add("Purchase_Card");
    arr.Add("Veteran_Owned_Small_Business");
    arr.Add("Service-Disabled_Veteran_Owned_Small_Business");
    arr.Add("Registered_for_Government_Business");
    arr.Add("Registered_for_Non_Government_Business");
    arr.Add("Number_of_Employees");
    arr.Add("Number_of_Operating_Locations");
    arr.Add("Capabilities_Keywords");
    arr.Add("Capabilities_Narrative");




    DataTable dt = new DataTable();

    dt.Columns.Add("Business_Name");
    dt.Columns.Add("Business_Owner");
    dt.Columns.Add("Business_Owner_First_Name");
    dt.Columns.Add("Business_Owner_Last_Name");
    dt.Columns.Add("Job_Title");
    dt.Columns.Add("Daytime_Phone");
    dt.Columns.Add("Fax_Number");
    dt.Columns.Add("Email");
    dt.Columns.Add("Email_Backup_1");
    dt.Columns.Add("Email_Backup_2");
    dt.Columns.Add("Business_Address");
    dt.Columns.Add("City");
    dt.Columns.Add("State_Territory");
    dt.Columns.Add("County");
    dt.Columns.Add("Zip");
    dt.Columns.Add("Website");
    dt.Columns.Add("NAICS");
    dt.Columns.Add("FSC");
    dt.Columns.Add("PSC");
    dt.Columns.Add("Year_Established");
    dt.Columns.Add("Purchase_Card");
    dt.Columns.Add("Veteran_Owned_Small_Business");
    dt.Columns.Add("Service-Disabled_Veteran_Owned_Small_Business");
    dt.Columns.Add("Registered_for_Government_Business");
    dt.Columns.Add("Registered_for_Non_Government_Business");
    dt.Columns.Add("Number_of_Employees");
    dt.Columns.Add("Number_of_Operating_Locations");
    dt.Columns.Add("Capabilities_Keywords");
    dt.Columns.Add("Capabilities_Narrative");

    #endregion



    try
    {
      int usdRows = ((Microsoft.Office.Interop.Excel.Range)workSheet.UsedRange).Rows.Count;
      int usdcolumns = ((Microsoft.Office.Interop.Excel.Range)workSheet.UsedRange).Columns.Count;
      
      //while (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
      for (int ctr = 0; ctr < usdRows; ctr++)
      {

      int col = 1;

      z = z + 1;
      DataRow dr = dt.NewRow();
      for (int p = 0; p < usdcolumns; p++)
      {
        try
        {
        string s = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[row, col]).Value2.ToString();
        for (int i = 0; i < arr.Count; i++)
        {
          string a = arr[i].ToString();


          if (a == s)
          {
          try
          {

            string Name = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[z, col]).Value2.ToString();

            #region "dr"
            if (s == "Business_Name")
            {
            dr["Business_Name"] = Name.ToString();
            }
            else if (s == "Business_Owner")
            {
            dr["Business_Owner"] = Name.ToString();
            }
            else if (s == "Business_Owner_First_Name")
            {
            dr["Business_Owner_First_Name"] = Name.ToString();
            }
            else if (s == "Business_Owner_Last_Name")
            {
            dr["Business_Owner_Last_Name"] = Name.ToString();
            }
            else if (s == "Job_Title")
            {
            dr["Job_Title"] = Name.ToString();
            }
            else if (s == "Daytime_Phone")
            {
            dr["Daytime_Phone"] = Name.ToString();
            }

            else if (s == "Email")
            {
            dr["Email"] = Name.ToString();
            }
            else if (s == "Email_Backup_1")
            {
            dr["Email_Backup_1"] = Name.ToString();
            }
            else if (s == "Email_Backup_2")
            {
            dr["Email_Backup_2"] = Name.ToString();
            }
            else if (s == "Business_Address")
            {
            dr["Business_Address"] = Name.ToString();
            }
            else if (s == "City")
            {
            dr["City"] = Name.ToString();
            }
            else if (s == "State_Territory")
            {
            dr["State_Territory"] = Name.ToString();
            }
            else if (s == "County")
            {
            dr["County"] = Name.ToString();
            }
            else if (s == "Zip")
            {
            dr["Zip"] = Name.ToString();
            }
            else if (s == "Website")
            {
            dr["Website"] = Name.ToString();
            }
            else if (s == "NAICS")
            {
            dr["NAICS"] = Name.ToString();
            }

            else if (s == "FSC")
            {
            dr["FSC"] = Name.ToString();
            }
            else if (s == "PSC")
            {
            dr["PSC"] = Name.ToString();
            }
            else if (s == "Year_Established")
            {
            dr["Year_Established"] = Name.ToString();
            }
            else if (s == "Purchase_Card")
            {
            dr["Purchase_Card"] = Name.ToString();
            }
            else if (s == "Veteran_Owned_Small_Business")
            {
            dr["Veteran_Owned_Small_Business"] = Name.ToString();
            }


            else if (s == "Service-Disabled_Veteran_Owned_Small_Business")
            {
            dr["Service-Disabled_Veteran_Owned_Small_Business"] = Name.ToString();
            }
            else if (s == "Registered_for_Government_Business")
            {
            dr["Registered_for_Government_Business"] = Name.ToString();
            }

            else if (s == "Registered_for_Non_Government_Business")
            {
            dr["Registered_for_Non_Government_Business"] = Name.ToString();
            }
            else if (s == "Number_of_Employees")
            {
            dr["Number_of_Employees"] = Name.ToString();
            }
            else if (s == "Number_of_Operating_Locations")
            {
            dr["Number_of_Operating_Locations"] = Name.ToString();
            }
            else if (s == "Capabilities_Keywords")
            {
            dr["Capabilities_Keywords"] = Name.ToString();
            }
            else if (s == "Capabilities_Narrative")
            {
            dr["Capabilities_Narrative"] = Name.ToString();
            }


            #endregion
          }
          catch { }
          break;
           
          }
        }

        col = col + 1;
        }
        catch { }
      }





      index++;
      rowIndex = index;
      //row = index;
      dt.Rows.Add(dr);
      }

      foreach (DataRow dr in dt.Select())
      {
      if (dr["Business_Name"].ToString() == "" && dr["Daytime_Phone"].ToString() == "" && dr["Business_Owner"].ToString() == "" && dr["Business_Owner_First_Name"].ToString() == "" && dr["Business_Owner_Last_Name"].ToString() == "" && dr["Job_Title"].ToString() == "" && dr["Fax_Number"].ToString() == "" && dr["Email"].ToString() == "")
      {        
        dr.Delete();
        dt.AcceptChanges();
      }
      }

      app.Quit();
    }
    catch (Exception ex)
    {
      string s = ex.ToString();
      // Log the exception and quit...    
      app.Quit();
      //Console.WriteLine(ex.Message);  
    }

    
    return dt;
    }
  Siva S replied to Santosh Prajapati
02-Dec-10 04:10 AM
Server.mappath in sharepoint doesn't give the path of application folder. I tried the above too
Create New Account
help
Setup MS Office SharePoint 2007 Test / Dev Environment Authentication Problems Hello all, i like to setup a fast clonable has dsiplay some warning, regarding the installation of MSSQL on a Domain Controller. My single SharePoint admin account is named SPAdmin (domain admin, mssql: security, dbcreator), the local domain ist named EXE, onetnative.dll) 03 / 04 / 2010 13:53:04.95 OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Topology 0 Medium Diagnostics settings: 32768 03 / 04 / 2010 13:53:05.38 OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Topology 9e7d Medium Initializing the configuration database connection. 03 / 04 / 2010 13:53:11 35 OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Database 880i High System.Data.SqlClient.SqlException: Die von der Anmeldung angeforderte 'SharePoint_07_Config'-Datenbank ServerInfo serverInfo, Strin. . . 03 / 04 / 2010 13:53:11.35* OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Database 880i High . . .g newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) bei System.Data SqlClient.SqlConnectionFactory.CreateConn. . . 03 / 04 / 2010 13:53:11.35* OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Database 880i High . . .ection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) bei System
Sharepoint Services and Sharepoint Office SharePoint Okay can someone please give a simple list of the the differences the rhetoric that standard downloadable product and the non GNU like licensed product. I have decidd to deploy sharepoint for its searching capabilities however i need to know what the diffrences are in the diffrent line of sharepoint products. Finally, something else i cannot find in a FAQ: Can i apply my own code extension( C#, and AJAX ) functions to forms? SharePoint Discussions SharePoint (1) AJAX (1) Microsoft Office (1) InfoPath (1) Excel (1) Joe, There are basically three
Microsoft.Office.Interop.Excel Hi, I am using Microsoft.Office.Interop.Excel to export the System.data Datatable values to the MS Excel. using Excel = Microsoft.Office.Interop.Excel; Excel.Application App = null; Excel._Workbook WB = null; Excel._Worksheet WS = null; / / Start Excel and
Installation of Project Server and Sharepoint Server. Hi All, I would like to install the Project Server 2007 and the Sharepoint Server 2007. Would I be able to install them on a physical server which is Domain Controller. Awaiting your response at the earliest. Thanks and Regards Abishek.R.Srikaanth Deploy Office Project Server 2007 with Office SharePoint Server 2007 In this article: • Why deploy Microsoft Office Project Server 2007 with Microsoft Office SharePoint Server 2007 • Deployment scenarios This article describes how to install Microsoft Office Project Server 2007
the right permissions on the content type. Then, I created a column "external data" in Sharepoint that I linked to my content type. When I want to change the data in EventID Level Message Correlation 11 / 13 / 2011 00:08:30.31 w3wp.exe (0x1D9C) 0x1038 SharePoint Foundation Monitoring nasq Medium Entering monitored scope (Request (GET:http: / / tricoflex:80 / CRM / _layouts / FldNewEx Cust%2E%20Relation%20ID&DescriptionParam = &VldFormulaParam = &VldMessageParam = )) 11 / 13 / 2011 00:08:30.31 w3wp.exe (0x1D9C) 0x1038 SharePoint Foundation Logging Correlation Data xmnv Medium Name = Request (GET:http: / / tricoflex:80 / CRM / _layouts / FldNewEx e307-4c5d-b470-c4351e5dff53 11 / 13 / 2011 00:08:30.32 w3wp.exe (0x1D9C) 0x1038 SharePoint Foundation Logging Correlation Data xmnv Medium Site = / 9694064f-e307-4c5d-b470-c4351e5dff53 11 / 13 / 2011 00:08:30.35 w3wp.exe (0x1D9C) 0x1038 SharePoint Foundation Monitoring b4ly Medium Leaving Monitored Scope (Request (GET:http: / / tricoflex:80 / CRM / _layouts / FldNewEx e307-4c5d-b470-c4351e5dff53 11 / 13 / 2011 00:08:30.45 wsstracing.exe (0x06A0) 0x17A0 SharePoint Foundation Unified Logging Service b9wt High Log retention limit reached. Log file 'C: \ Program Files log' has been deleted. 11 / 13 / 2011 00:08:30.45 wsstracing.exe (0x06A0) 0x17A0 SharePoint Foundation Tracing Controller Service 8096 Information Usage log retention limit reached. Some old usage log files have been deleted. 11 / 13 / 2011 00:08:30.57 w3wp.exe (0x1D9C) 0x1038 SharePoint Foundation Monitoring nasq Medium Entering monitored scope (Request (GET:http: / / tricoflex:80 / SiteAssets / SitePages / Accueil