Search code examples
c#excelremote-server

SpreadsheetDocument.Create() returns URI formats are not supported


I am creating a file (Excel) using C# code to a local server in my organisation.

I keep getting the error "URI formats are not supported" when I add "http" before the IP of the server. If I remove the "http" automatically "C://" is added which result in path not found error.

Can anybody suggest a way to properly format the URL?

  string uriPath = "http:\\1.1.1.1\\Test\\Reports-IdH-ls\\Reports\\tes.xlsx";

  ExportDSToExcel(DatasetItem, uriPath);

  ...
  private void ExportDSToExcel(DataSet ds, string destination)
  {
      using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
      {
          var workbookPart = workbook.AddWorkbookPart();
          workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
          workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

          uint sheetId = 1;

          foreach (DataTable table in ds.Tables)
          {
              // fill table ...
          }
      }
  }

Solution

  • Can you try the following?

    string uriPath = "\\\\1.1.1.1\\Test\\Reports-IdH-ls\\Reports\\tes.xlsx";
    

    In C#, if you want a backslash, you need to add another backslash to escape it to make it a literal backslash.