private bool export2Template(string filePath, List <object> rowTags, DetailExportTemplateAction action, int startIndex) { CSharpExcel excel = new CSharpExcel(); try { excel.Open(filePath); if (!excel.ActivateSheet("Template")) { return(false); } //设置单元格格式,防止科学计数 excel.SetWriteDataFormat(3, 1, rowTags.Count + 3, 46); if (rowTags.Count > 0) { for (int i = 0; i < rowTags.Count; i++) { action(rowTags[i], excel, i + startIndex); //excel.WriteData(devInfo.strAssetCode, i + start, 0); } } if (false == excel.SaveAs(filePath, true)) { if (ExportErrorInfoEvent != null) { ExportErrorInfoEvent("文件保存失败, 请确认文件未被占用 !"); } return(false); } excel.Close(); return(true); } catch (Exception ex) { Log.Write.Error(ex.Message); } return(false); }
/// <summary> /// 导出数据到模板 /// </summary> /// <param name="excelTemplatePath">excel模板所在位置</param> /// <param name="rowTags">数据源</param> /// <param name="action">具体的数据赋值操作</param> /// <param name="startRowIndex">从第几行开始赋值</param> /// <returns></returns> public bool Export2ExcelTemplate(string excelTemplatePath, List <object> rowTags, DetailExportTemplateAction action, int startRowIndex = 2) { SaveFileDialog sfd = null; bool isSuccess = false; try { if (File.Exists(excelTemplatePath)) { sfd = new SaveFileDialog(); sfd.FileName = Path.GetFileNameWithoutExtension(excelTemplatePath); if (excelTemplatePath.Contains("xlsx")) { sfd.Filter = "Excle文件|*.xlsx"; } else { sfd.Filter = "Excle文件|*.xls"; } if (DialogResult.OK == sfd.ShowDialog()) { if (File.Exists(sfd.FileName)) { File.Delete(sfd.FileName); } File.Copy(excelTemplatePath, sfd.FileName); isSuccess = export2Template(sfd.FileName, rowTags, action, startRowIndex); } } else { if (ExportErrorInfoEvent != null) { ExportErrorInfoEvent("模板不存在!"); } } } catch (Exception err) { Log.Write.Error(err.Message); } finally { if (null != sfd) { sfd.Dispose(); } } return(isSuccess); }
/// <summary> /// 导出数据到模板(默认异步) /// 注意不要进行子线程调用 /// </summary> /// <param name="excelTemplatePath">excel模板所在位置</param> /// <param name="rowTags">数据源</param> /// <param name="action">具体的数据赋值操作</param> /// <param name="startRowIndex">从第几行开始赋值</param> /// <param name="control">waitForm所在的窗体位置, 可以为空</param> /// <param name="isInMainThread">是否在主线程中执行</param> /// <returns></returns> public static bool Export2ExcelTemplate(string excelTemplatePath, List <object> rowTags, DetailExportTemplateAction action, Control control = null, int startRowIndex = 2, bool isInMainThread = false) { SaveFileDialog sfd = null; bool isSuccess = false; try { if (File.Exists(excelTemplatePath)) { sfd = new SaveFileDialog(); sfd.FileName = Path.GetFileNameWithoutExtension(excelTemplatePath); if (excelTemplatePath.Contains("xlsx")) { sfd.Filter = "Excle文件|*.xlsx"; } else { sfd.Filter = "Excle文件|*.xls"; } if (DialogResult.OK == sfd.ShowDialog()) { if (File.Exists(sfd.FileName)) { File.Delete(sfd.FileName); } File.Copy(excelTemplatePath, sfd.FileName); AnimateWaitForm.AnimatingWait(() => { isSuccess = export2Template(sfd.FileName, rowTags, action, startRowIndex); }, control, isInMainThread, true); } } else { SimpleMessageBox.ShowMessageBox("模板不存在!"); } } catch (Exception err) { Log.Write.Error(err.Message); } finally { if (null != sfd) { sfd.Dispose(); } } return(isSuccess); }