Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a requirement where I will have an Excel file in which there will be 'n' no. of sheets and each sheet will have data in 'n' no. of columns Now I need to read this excel file using Excel Services of Sharepoint. In the below code I'm hard coding Read from "A1:F10". I need to use used range of the excel sheet is it possible. There is no usedRange option in ExcelService like in Interop.

I just need to know how to customize to read many sheets and all the columns without specifying the Range.Please help.

C#
XLService.ExcelService objXL1 = new ExcelService();
                   ExcelService objXL = new ExcelService();
                   objXL.Url = "http://serverName:port/_vti_bin/excelservice.asmx";
                   objXL.PreAuthenticate = false;
                   objXL.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                   XLService.Status[] stat1;
                   XLService.Status[] stat2;
                   string sessionId = string.Empty;
                   StringBuilder sbResult = new StringBuilder();
                   sessionId = objXL.OpenWorkbook(strFileUrl, String.Empty, String.Empty, out stat1);

                   object[] sheetNames = objXL.GetSheetNames(sessionId, out stat2);
                   
                   foreach (object[] sheetName in sheetNames)
                   {
                       for (int iCount = 0; iCount < sheetName.Length; iCount++)
                       {
                           strSheetName = Convert.ToString(sheetName[iCount]);
                           
                           // retrieve the range as an array of arrays
                           object[] rangeResults = objXL.GetRangeA1(sessionId, strSheetName, "A1:F10", true, out stat1);
                           foreach (object[] rangeResult in rangeResults)
                           {
                               for (int iIndex = 0; iIndex < rangeResult.Length; iIndex++)
                               {
                                   sbResult.Append(Convert.ToString(rangeResult[iIndex]));
                               }
                           }

                           objXL.CloseWorkbook(sessionId);
                       }
                   }



Thanks & Regards,
Mathi
Posted
Updated 8-May-13 18:40pm
v2
Comments
Maciej Los 19-Apr-13 8:54am    
And the question is...
Maciej Los 19-Apr-13 11:26am    
Ok, try UsedRange instead get_range().
Mathi2code 22-Apr-13 5:06am    
We dont have UsedRange Properties in Excel Service I think...

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