private string CreateEmptyFile(ExcelWorkbookType workbookType) { var emptyFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".xlsx"); ExcelUtility.CreateWorkbook(emptyFilePath, workbookType); return(emptyFilePath); }
private static bool ValidateExtension(string path, ExcelWorkbookType workbookType) { switch (workbookType) { case ExcelWorkbookType.XLS: return(Path.GetExtension(path) == ".xls"); case ExcelWorkbookType.XLSX: return(Path.GetExtension(path) == ".xlsx"); default: break; } return(false); }
private static IWorkbook CreateWorkbook(ExcelWorkbookType workbookType) { switch (workbookType) { case ExcelWorkbookType.XLS: return(new HSSFWorkbook() as IWorkbook); case ExcelWorkbookType.XLSX: return(new XSSFWorkbook() as IWorkbook); default: break; } throw new ArgumentException("The specified excel type is not supported instantiating."); }
public static void CreateWorkbook(string path, ExcelWorkbookType workbookType) { if (!ValidateExtension(path, workbookType)) { throw new ArgumentException("The specified Excel type and path extension do not match."); } var workbook = CreateWorkbook(workbookType); using (var fileStream = new FileStream(path, FileMode.Create)) { workbook.Write(fileStream); } }