/// <summary> /// 从本地路径创建工作簿 /// </summary> /// <param name="path">本地路径</param> /// <param name="workbookStyle">工作簿类型</param> /// <returns></returns> public static IWorkbook CreateWorkbook(string path, WorkbookStyle workbookStyle) { using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { return(CreateWorkbook(stream, workbookStyle)); } }
static void Main() { string dataDir = Common.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string input = "sample1.xlsx"; string output = "ouput.xlsx"; WorkbookStyle style = new WorkbookStyle(); Common.StorageService.File.UploadFile(dataDir + input, input, storage: Common.STORAGE); Common.CellsService.WorksheetColumns.UpdateCellRangeStyle(input, "Sheet1", "A1:A10", style, Common.FOLDER, storage: Common.STORAGE); Common.StorageService.File.DownloadFile(input, dataDir + output, storage: Common.STORAGE); }
/// <summary> /// 从Stream创建工作簿,如果流为空,则新建工作簿 /// </summary> /// <param name="stream"></param> /// <param name="workbookStyle">指定创建工作簿的类型</param> /// <returns></returns> public static IWorkbook CreateWorkbook(Stream stream, WorkbookStyle workbookStyle) { //创建excel IWorkbook wb = workbookStyle == WorkbookStyle.HSSFWorkbook ? (IWorkbook)(stream == null ? new HSSFWorkbook() : new HSSFWorkbook(stream)) : (IWorkbook)(stream == null ? new XSSFWorkbook() : new XSSFWorkbook(stream)); try { wb.GetSheetAt(0); } catch (Exception) { wb.CreateSheet(); } return(wb); }
/// <summary> /// 创建新的工作簿 /// </summary> /// <param name="workbookStyle"></param> /// <returns></returns> public static IWorkbook CreateWorkbook(WorkbookStyle workbookStyle) { return(CreateWorkbook(stream: null, workbookStyle)); }