/// <summary> /// Export to a specified spreadsheet. /// </summary> /// <typeparam name="T">POCO with no internal List, Array or Class</typeparam> /// <param name="exportCollections">export collections</param> /// <param name="options">excel options.</param> /// <exception cref="ArgumentNullException"></exception> public void Export <T>(IList <T> exportCollections, ISpreadSheetOptions options = null) { if (exportCollections == null) { throw new ArgumentNullException(nameof(exportCollections)); } if (!exportCollections.Any()) { return; } UserCredential credential; using (var stream = new FileStream("C:/Workspace/credentials/credentials.json", FileMode.Open, FileAccess.Read)) { string credPath = "C:/Workspace/credentials/token.json"; credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; } var service = new SheetsService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); if (TypeUtil.IsPrimitive(typeof(T))) { PrimitiveTypeExport(service, exportCollections); } else { ClassTypeExport(service, exportCollections); } }
/// <summary> /// Export to a specified spreadsheet. /// </summary> /// <typeparam name="T">POCO with no internal List, Array or Class</typeparam> /// <param name="exportCollections">export collections</param> /// <param name="options">excel options.</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="IOException"></exception> public void Export <T>(IList <T> exportCollections, ISpreadSheetOptions options = null) { if (exportCollections == null) { throw new ArgumentNullException(nameof(exportCollections)); } if (!exportCollections.Any()) { return; } if (File.Exists(filePath)) { throw new IOException($"{filePath} already exists."); } if (options == null) { options = new ExcelSpreadSheetOptions(); } ExcelSpreadSheetOptions excelOpt = null; if (options != null) { if (!(options is ExcelSpreadSheetOptions)) { throw new ArgumentException($"The only Options that this class can receive are {nameof(ExcelSpreadSheetOptions)}."); } excelOpt = options as ExcelSpreadSheetOptions; } IWorkbook book; var extension = Path.GetExtension(filePath); if (extension == HSSF_EXTENSION) { book = new HSSFWorkbook(); } else if (extension == XSSF_EXTENSION) { book = new XSSFWorkbook(); } else { throw new ApplicationException($@"Invalid extension has been specified. Valid extensions are either '{HSSF_EXTENSION}' or '{XSSF_EXTENSION}'."); } if (TypeUtil.IsPrimitive(typeof(T))) { PrimitiveTypeExport(book, exportCollections, excelOpt); } else { ClassTypeExport(book, exportCollections, excelOpt); } using (var fs = new FileStream(filePath, FileMode.Create)) { book.Write(fs); } }