コード例 #1
0
        public async Task ExportWord_Test()
        {
            var exporter = new WordExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportWord_Test) + ".docx");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            //此处使用默认模板导出
            var result = await exporter.ExportListByTemplate(filePath, GenFu.GenFu.ListOf <ExportTestData>());

            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();
        }
コード例 #2
0
        public async Task ExportWordByTemplate_Test()
        {
            var tplPath  = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates", "tpl1.cshtml");
            var tpl      = File.ReadAllText(tplPath);
            var exporter = new WordExporter();
            var ex       = await Assert.ThrowsAnyAsync <ArgumentException>(async() => await exporter.ExportListByTemplate(null,
                                                                                                                          GenFu.GenFu.ListOf <ExportTestData>(), tpl));

            ex.Message.ShouldContain("文件名必须填写");

            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportWordByTemplate_Test) + ".docx");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            //此处使用默认模板导出
            var result = await exporter.ExportListByTemplate(filePath,
                                                             GenFu.GenFu.ListOf <ExportTestData>(), tpl);

            result.ShouldNotBeNull();
            File.Exists(filePath).ShouldBeTrue();
        }
コード例 #3
0
        public async Task ExportWordBytesByTemplate_Test()
        {
            var tplPath  = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates", "tpl1.cshtml");
            var tpl      = File.ReadAllText(tplPath);
            var exporter = new WordExporter();
            var ex       = await Assert.ThrowsAnyAsync <ArgumentException>(async() => await exporter.ExportListByTemplate(null,
                                                                                                                          GenFu.GenFu.ListOf <ExportTestData>(), tpl));

            ex.Message.ShouldContain(Resource.FileNameMustBeFilled);

            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportWordBytesByTemplate_Test) + ".docx");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            //此处使用默认模板导出
            var result = await exporter.ExportListBytesByTemplate(
                GenFu.GenFu.ListOf <ExportTestData>(), tpl);

            result.ShouldNotBeNull();
            result.Length.ShouldBeGreaterThan(0);

            using (var file = File.OpenWrite(filePath))
            {
                file.Write(result, 0, result.Length);
            }
            File.Exists(filePath).ShouldBeTrue();
        }