从Excel加载DataTable:无法将类型为“ System.DateTime”的对象转换为类型为“ System.String”的对象

时间:2018-07-16 13:18:29

标签: c# c#-4.0 c#-3.0

我正在尝试创建一个读取Excel文件的数据表,该文件具有DateTime列,其余所有其他列均为字符串。尝试将DateTime列转换为Datetime数据类型时,出现异常“无法将类型为'System.DateTime'的对象转换为类型为'System.String'的情况。”有人可以指导我如何解决这个问题。

describe('ClientDeactivateComponent', () => {
  let component: ClientDeactivateComponent;
  let fixture: ComponentFixture<ClientDeactivateComponent>;

  let spyDialogRef: any;

  const spyRouter = jasmine.createSpyObj('Router', ['navigate']);
  spyRouter.navigate.and.returnValue(Promise.resolve({}));

  const spyClientsService = jasmine.createSpyObj('ClientsService', ['deactivateClient']);
  spyClientsService.deactivateClient = () => of(true);

  spyDialogRef = jasmine.createSpy();
  spyDialogRef.componentInstance = {title: '', message: ''};
  spyDialogRef.afterClosed = () => of(true);

  const spyMatDialog = jasmine.createSpyObj('MatDialog', ['open']);
  spyMatDialog.open.and.returnValue(spyDialogRef);

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        ClientDeactivateComponent,
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      imports: [
        HttpClientModule,
        RouterTestingModule.withRoutes([{path: '**', component: ClientDeactivateComponent}])
      ],
      providers: [
        FormBuilder,
        ClientsService
      ]
    }).overrideComponent(ClientDeactivateComponent, {
        set: {
          providers: [
            {provide: Router, useValue: spyRouter},
            {provide: ClientsService, useValue: spyClientsService},
            {provide: MatDialog, useValue: spyMatDialog},
            {provide: Router, useValue: spyRouter},
            {provide: ActivatedRoute, useValue: {params: from([{id: 1}])}}
          ],
          template: '<div>Overridden template</div>'
        }
      }
    )
      .compileComponents();
  }));

  afterEach(() => {
    spyRouter.navigate.calls.reset();
    fixture.detectChanges();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(ClientDeactivateComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  describe('default', () => {
    it('should create', () => {
      expect(component).toBeTruthy();
    });

    it('should open a modal window when confirmDeactivation is called', () => {
      component.confirmDeactivation();
      expect(spyMatDialog.open).toHaveBeenCalled();
    });
  });
});

1 个答案:

答案 0 :(得分:0)

尝试加载到数据读取器中并遍历值。

        SqlDataReader dr = cmd.ExecuteReader();


        foreach (DataRow drow in dtSchema.Rows)
        {
                //manually add columns here from dr
        }

转到此处以获取详细信息: Populate data table from data reader