Поделиться через


Сохранение данных на сервере для подготовки к программному доступу

В этом примере показано, как сохранить книгу Excel на сервере, чтобы подготовить ее к программному доступу. Ниже описана последовательность действий.

  1. Create a workbook with named ranges.

  2. Save the workbook to a trusted SharePoint library location.

    Примечание.

    [!Примечание] It is assumed that you have already created a SharePoint document library and made it a trusted location. Дополнительные сведения см. в разделе Практическое руководство. Доверие к расположению.

  3. Programmatically specify values for the worksheet, named range, and cell value by using the Веб-службы Excel SetCellA1 method. The values are passed in as arguments—that is, args [1] and args [2]:

    status = xlServices.SetCellA1(sessionId, String.Empty, args[1], args[2]);
    
    status = xlServices.SetCellA1(sessionId, String.Empty, args(1), args(2))
    

Значения args [1] и args [2] можно указать с помощью веб-формы или из командной строки:

GetSnapshot.exe http://MyServer002/MyTrustedDocumentLibrary/TestMyParam.xlsx MyParam28 > MySnapshot.xlsx

В этом примере args [1]Это MyParam, args [2]28 , а GetSnapshot.exe — имя создаваемого приложения. To find a sample program, see How to: Get an Entire Workbook or a Snapshot.

To create a named range

  1. Запустите Excel.

  2. Rename Sheet1 to beMyParamSheet.

  3. In cell B2, type 20.

  4. In cell B3, type =2+B2.

  5. Make cell B3 bold.

  6. Make cell B2 into a named range:

    1. On the ribbon, click the Formulas tab, and then click cell B2 to select it.
    2. In the Defined Names group, click Define Name.
    3. In the New Name dialog box, in the Name text box, typeMyParam.
  7. Save the workbook to a location of your choice on the local drive. Name the workbook TestMyParam.xlsx.

To save to a SharePoint library

  1. В меню Файл выберите команду Сохранить & Отправить, а затем — Сохранить в SharePoint.
  2. In the Save to SharePoint dialog box, click Publish Options.
  3. In the Publish Options dialog box, on the Show tab, ensure that Entire Workbook is selected.
  4. Click Parameters.
  5. Click Add.
  6. In the Add Parameters list, you should see MyParam. Select the MyParam check box.
  7. Click OK. You should now see MyParam in the Parameters list.
  8. Click OK.
  9. In the Save to SharePoint dialog box, click Save As.
  10. In the Save As dialog box, clear the Open with Excel in the browser check box.
  11. In the File name box, type the path to the trusted SharePoint document library where you want to store this workbook. For example,http:// MyServer002/MyDocumentLibrary/TestParam.xlsx.
  12. Нажмите кнопку Сохранить.

To specify values programmatically

  1. Following is the signature for the SetCellA1 method in Веб-службы Excel:

      public void SetCellA1 (
    string sessionId,
    string sheetName,
    string rangeName,
    Object cellValue,
    Out Status[] status
    )
    
    Public Sub SetCellA1(ByVal sessionId As String,
                  ByVal sheetName As String,
                 ByVal rangeName As String,
                 ByVal cellValue As Object,
                 Out ByVal status() As Status)
    End Sub
    

    Set the values for the worksheet, named range, and cell value to the SetCellA1 method as follows:

    // Set a value into a cell.
    status = xlSrv.SetCellA1(sessionId, String.Empty, args[1], args[2]);
    
  2. In the preceding code:

  • args [1] is the name of the named range. In this example, it is MyParam.
  • args [2] is the value that you want to set in the cell. The cell where the value will be set is the named range in args [1] called MyParam.
  1. If you are using a command line, you can pass in the arguments as follows:

    GetSnapshot.exe http://MyServer002/MyTrustedDocumentLibrary/TestMyParam.xlsx MyParam 28 > MySnapshot.xlsx

  2. If you generate a snapshot of the workbook, you see the following:

  • Cell B2 (with the named range MyParam) now has a value that you fed through the program, which is 28.

  • Cell B3 has a new calculated value of 30.

  • Cell B3 does not show the original formula, which was "=2+B2".

  • Cell B3 retains its font format, which is bold.

Примечание.

Дополнительные сведения о моментальных снимках см. в разделе Практическое руководство. Получение всей книги или моментального снимка. For more information about the SetCellA1 method, see the Веб-службы Excel reference documentation. The namespace of the Web service is Microsoft.Office.Excel.Server.WebServices .

См. также

Задачи

Как сохранить на сервере данные из клиента Excel

Справочные материалы

Microsoft.Office.Excel.Server.WebServices

Понятия

Доступ к API SOAP

Loop-Back SOAP Calls and Direct Linking

Excel Services Alerts

Другие ресурсы

Пошаговое руководство. Разработка настраиваемого приложения с помощью веб-служб Excel