protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { PropertyDescriptor property = context.DataContext.GetProperties()[ExcelCreate.GetExcelAppTag]; Excel::Application excelApp = property.GetValue(context.DataContext) as Excel::Application; try { Int32 sheetIndex = SheetIndex.Get(context); string oldSheetName = OldSheetName.Get(context); string sheetName = SheetName.Get(context); Excel::Sheets sheets = excelApp.ActiveWorkbook.Sheets; if (oldSheetName != "" && oldSheetName != null) { sheets.Item[oldSheetName].Name = sheetName; } else { sheets.Item[sheetIndex].Name = sheetName; } System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets); sheets = null; GC.Collect(); } catch (Exception e) { new CommonVariable().realaseProcessExit(excelApp); SharedObject.Instance.Output(SharedObject.enOutputType.Error, "EXCEL重命名工作表执行过程出错", e.Message); } m_Delegate = new runDelegate(Run); return(m_Delegate.BeginInvoke(callback, state)); }
protected override void Execute(CodeActivityContext context) { var excelpath = ExcelPath.Get(context); var pageindex = SheetIndex.Get(context); var txtpath = TxtPath.Get(context); var splitchar = SplitChar.Get(context); var addheaders = AddHeaders.GetType(); Workbook wb = new Workbook(); wb.LoadFromFile(@excelpath); Worksheet ws = wb.Worksheets[pageindex]; DataTable dt = ws.ExportDataTable(); //TXT VARSA İÇİNİ TEMİZLE if (File.Exists(txtpath)) { File.WriteAllText(txtpath, ""); } //BAŞLIK if (AddHeaders) { string[] columnNames = dt.Columns.Cast <DataColumn>().Select(x => x.ColumnName).ToArray(); File.AppendAllText(@txtpath, string.Join(splitchar, columnNames) + Environment.NewLine); } //SATIRLAR foreach (DataRow dtR in dt.Rows) { File.AppendAllText(@txtpath, string.Join(splitchar, dtR.ItemArray) + Environment.NewLine); } }
protected override void Execute(CodeActivityContext context) { using (var workbook = new XLWorkbook(WorkbookPath.Get(context))) { var workSheet = workbook.Worksheet(SheetIndex.Get(context)); if (workSheet == null) { throw new InvalidOperationException("Worksheet is not in a valid range"); } SheetName.Set(context, workSheet.Name); } }