Extend and Contribute to CRM Update Import Tool

Contents
[ ]

You can download the latest source code at:

Export Data from CRM to Excel

  SaveFileDialog fileDialog = new SaveFileDialog();

  fileDialog.DefaultExt = ".xlsx";

  fileDialog.FileName = "Aspose ImportTool Export.xlsx";

  if (fileDialog.ShowDialog().Value)

  {

      string FileName = fileDialog.FileName;

      Workbook workbook = new Workbook();

      workbook.Worksheets.Add("Aspose Export");

      Worksheet MyWorksheet = workbook.Worksheets["Aspose Export"];

      int i = 0;

      foreach (string attrib in SelectedAttributes)

          MyWorksheet.Cells[0, i++].Value = attrib;

      int Row = 1, cell = 0;

      foreach (Entity record in Result.Entities)

      {

          cell = 0;

          foreach (string attribute in SelectedAttributes)

          {

              if (record.Contains(attribute))

              {

                  MyWorksheet.Cells[Row, cell].Value = record[attribute].ToString();

              }

              cell++;

          }

          Row++;

      }

      workbook.Save(FileName, SaveFormat.Xlsx);

  }

Import Data from Excel to CRM

  if (ctrl.CrmConnectionMgr != null && ctrl.CrmConnectionMgr.CrmSvc != null && ctrl.CrmConnectionMgr.CrmSvc.IsReady)

  {

      CrmServiceClient svcClient = ctrl.CrmConnectionMgr.CrmSvc;

      if (svcClient.IsReady)

      {

          OpenFileDialog fileDialog = new OpenFileDialog();

          fileDialog.DefaultExt = ".xlsx";

          if (fileDialog.ShowDialog().Value)

          {

              string FileName = fileDialog.FileName;

              Workbook workbook = new Workbook(FileName);

              Worksheet MyWorksheet = workbook.Worksheets["Aspose Export"];

              DataTable dt = MyWorksheet.Cells.ExportDataTable(0, 0, MyWorksheet.Cells.Rows.Count, MyWorksheet.Cells.Columns.Count, true);

              foreach (DataRow dr in dt.Rows)

              {

                  Entity CrmRecord = new Entity(SelectedEntity);

                  foreach (DataColumn th in dt.Columns)

                  {

                      CrmRecord.Attributes.Add(th.ColumnName, dr[th.ColumnName].ToString());

                  }

                  CreateRequest req = new CreateRequest();

                  req.Target = CrmRecord;

                  CreateResponse res = (CreateResponse)svcClient.OrganizationServiceProxy.Execute(req);

              }

          }

      }

  }