public void Excel_Context_Menu_Tests() { IExcelApp app = new ExcelApp(); app.CreateNewInstance(); IExcelWorkbook workbook = app.CreateAndActivateNewWorkbook(); IExcelSheet sheet = workbook.CreateSheet("Test"); MenuItem contextMenu = new MenuItem { ItemType = MenuItemType.ContextMenu }; contextMenu.Children.Add(new MenuItem { Caption = "First Command", CommandId = "FC01" }); contextMenu.Children.Add(new MenuItem { ItemType = MenuItemType.Separator }); contextMenu.Children.Add(new MenuItem { Caption = "Second Command", CommandId = "FC01" }); sheet.SetContextMenu(2, 2, contextMenu, "Application.Id"); }
public bool ApplyAnalysisAndSaveData(IEnumerable <int> data, ExcelContextForPhaseOne <IAnalysisPhaseOnePartTwo> excelContext) { data.ThrowIfNullOrEmpty(nameof(data)); excelContext.ThrowIfNull(nameof(excelContext)); using IExcelWorkbook workbook = ExcelHelper.GetOrCreateWorkbook(excelContext.OutputExcelFile); IExcelSheet sheet = workbook.GetOrCreateSheet(excelContext.SheetName); FillSheetHeader(sheet, excelContext.Args); IAnalysisPhaseOnePartTwo analysis = excelContext.CreatePartNAnalysis(); int rowCounter = 2; foreach (int item in data) { int currentRow = rowCounter++; sheet[ExcelColumnIndex.A, currentRow].SetValue(item); analysis.ApplyAnalysisToSingleLaunch(sheet, currentRow, item); } analysis.ApplyAnalysisToDataset(sheet); workbook.SaveToFile(excelContext.OutputExcelFile); excelContext.OutputExcelFile.Refresh(); return(analysis.CheckH0Hypothesis(sheet)); }
Transaction GetTransaction(int rowNumber) { IExcelSheet sheet = _app.GetActiveWorkbook().GetActiveSheet(); ExcelSheetData data = sheet.GetSheetData(1, rowNumber); if (data.Rows.Count < 2) { return(null); } ExcelRowData headerRowData = data.Rows[0]; ExcelRowData valueRowData = data.Rows[1]; string separator = ";"; string header = headerRowData.GetValuesInLine(separator); string values = valueRowData.GetValuesInLine(separator); TransactionParser parser = new TransactionParser(header, separator.FirstOrDefault()); Transaction transaction = parser.ParseFromLine(values); return(transaction); }
public void SetFilter(string header, string value) { ExcelRangeInfo excelRangeInfo = GetTransactionRange(); IExcelSheet sheet = _app.GetActiveWorkbook().GetActiveSheet(); sheet.SetFilter(excelRangeInfo, COLUMN_CATEGORY, value); }
private static void FillAdditionalDataColumn(IExcelSheet sheet, ParametersPack args, ref int currentColumnIndex) { var additionalDataColumnIndex = currentColumnIndex++.AsEnum <ExcelColumnIndex>(); sheet[additionalDataColumnIndex, 1].SetValue(ExcelStringsPhaseTwo.AdditionalDataColumnName); sheet[additionalDataColumnIndex, 2].SetValue(args.LaunchesNumber); sheet[additionalDataColumnIndex, 3].SetValue(args.Step); sheet[additionalDataColumnIndex, 4].SetValue(double.Parse(ExcelStringsPhaseTwo.ConfidenceFactorValue)); string significanceLevelFormula = string.Format( ExcelStringsPhaseTwo.SignificanceLevelFormula, sheet[additionalDataColumnIndex, 4].Address ); sheet[additionalDataColumnIndex, 5].SetFormula(significanceLevelFormula); // Provide help information to see which columns contain data. string operationsRange = $"{GetFirstDataRowIndex().ToString()}:" + $"{GetLastDataRowIndex(args).ToString()}"; sheet[additionalDataColumnIndex, 6].SetValue(operationsRange); string normalizedRange = $"{GetFirstNormalizedDataRowIndex(args).ToString()}:" + $"{GetLastNormalizedDataRowIndex(args).ToString()}"; sheet[additionalDataColumnIndex, 7].SetValue(normalizedRange); sheet.AutoSizeColumn(additionalDataColumnIndex); }
public GetTransactionsAction.Result Execute(Data data) { List <FileData> fileDatas = new List <FileData>(); string separator = data.Separator ?? ";"; foreach (string sheetName in data.SheetFileNames) { IExcelSheet sheet = data.Excel.GetSheet(sheetName); ExcelTransactionDataProvider excelDataProvider = new ExcelTransactionDataProvider(sheet, separator); FileData iFileData = excelDataProvider.GetTransactionData(); fileDatas.Add(iFileData); } GetTransactionsAction.Data getTransactionData = new GetTransactionsAction.Data() { FileDatas = fileDatas, Parsers = data.Parsers }; GetTransactionsAction transactionAction = new GetTransactionsAction(); GetTransactionsAction.Result result = transactionAction.Execute(getTransactionData); return(result); }
public void ApplyAnalysisToDataset(IExcelSheet sheet) { sheet[ExcelColumnIndex.J, 1].SetValue(ExcelStringsPhaseOnePartOne.NormalDistributionSolutionColumnName); sheet.GetOrCreateCenterizedCell(ExcelColumnIndex.K, 1); sheet.AddMergedRegion(ExcelColumnIndex.J, 1, ExcelColumnIndex.K, 1); sheet[ExcelColumnIndex.J, 2].SetValue(ExcelStringsPhaseOnePartOne.PreliminarySampleSize); sheet[ExcelColumnIndex.J, 3].SetValue(ExcelStringsPhaseOnePartOne.SampleMean); sheet[ExcelColumnIndex.J, 4].SetValue(ExcelStringsPhaseOnePartOne.SampleVariance); sheet[ExcelColumnIndex.J, 5].SetValue(ExcelStringsPhaseOnePartOne.SampleDeviation); sheet[ExcelColumnIndex.J, 6].SetValue(ExcelStringsPhaseOnePartOne.VariationCoefficient); sheet[ExcelColumnIndex.J, 7].SetValue(ExcelStringsPhaseOnePartOne.CalculatedSampleSize); string lastValueRowIndex = _args.LaunchesNumber.SkipHeader().ToString(); sheet[ExcelColumnIndex.K, 2].SetFormula("$F$6"); sheet[ExcelColumnIndex.K, 3].SetFormula(sheet.FormulaProvider.Average($"$A$2:$A${lastValueRowIndex}")); sheet[ExcelColumnIndex.K, 4].SetFormula(sheet.FormulaProvider.Var($"$A$2:$A${lastValueRowIndex}")); sheet[ExcelColumnIndex.K, 5].SetFormula(sheet.FormulaProvider.StdDev($"$A$2:$A${lastValueRowIndex}")); sheet[ExcelColumnIndex.K, 6].SetFormula(ManualFormulaProvider.VariationCoefficient("$K$3", "$K$5")); sheet[ExcelColumnIndex.K, 7].SetFormula(sheet.FormulaProvider.RoundUp("3.8416 * $K$6^2 / $F$9^2", "0")); sheet.AutoSizeColumn(ExcelColumnIndex.J); sheet.AutoSizeColumn(ExcelColumnIndex.K, useMergedCells: true); }
private void CreateIntervalData(IExcelSheet sheet, int histogramSegmentsNumber, string normalizedValueRange) { histogramSegmentsNumber.ThrowIfValueIsOutOfRange(nameof(histogramSegmentsNumber), 1, int.MaxValue); for (int i = 0; i <= histogramSegmentsNumber; ++i) { int currentRow = i.UseOneBasedIndexing().SkipHeader(); string currentRowStr = currentRow.ToString(); string previousRowStr = (currentRow - 1).ToString(); string pocketFormula = $"$J$10 + ($J$12 * {i.ToString()})"; sheet[ExcelColumnIndex.D, currentRow].SetFormula(pocketFormula); if (i == 0) { CreateFirstValueInInterval( sheet, currentRow, currentRowStr, normalizedValueRange ); } else if (i == histogramSegmentsNumber) { CreateLastValueInInterval( sheet, currentRow, previousRowStr, normalizedValueRange ); } else { CreateMediumValueInInterval( sheet, currentRow, currentRowStr, previousRowStr, normalizedValueRange ); } string relativeFrequencyFormula = $"$E{currentRowStr} / $J$6"; sheet[ExcelColumnIndex.F, currentRow].SetFormula(relativeFrequencyFormula); string chi2SingleFormula = ManualFormulaProvider.Chi2Single( $"$F{currentRowStr}", $"$G{currentRowStr}" ); sheet[ExcelColumnIndex.H, currentRow].SetFormula(chi2SingleFormula); } string lastRowIndexStr = histogramSegmentsNumber.UseOneBasedIndexing().SkipHeader().ToString(); int checkSumRowIndex = (histogramSegmentsNumber + 2).UseOneBasedIndexing().SkipHeader(); sheet[ExcelColumnIndex.D, checkSumRowIndex].SetValue(ExcelStringsPhaseOnePartTwo.Sum); sheet[ExcelColumnIndex.E, checkSumRowIndex].SetFormula( sheet.FormulaProvider.Sum($"$E2:$E${lastRowIndexStr}") ); sheet[ExcelColumnIndex.F, checkSumRowIndex].SetFormula( sheet.FormulaProvider.Sum($"$F2:$F${lastRowIndexStr}") ); sheet[ExcelColumnIndex.G, checkSumRowIndex].SetFormula( sheet.FormulaProvider.Sum($"$G2:$G${lastRowIndexStr}") ); sheet[ExcelColumnIndex.H, checkSumRowIndex].SetFormula( sheet.FormulaProvider.Sum($"$H2:$H${lastRowIndexStr}") ); }
/// <summary> /// 初始化一个<see cref="ExportBase"/>类型的实例 /// </summary> /// <param name="format">导出格式</param> protected ExportBase(ExportFormat format) { Sheet = new Table(); _format = format; _headStyle = CellStyle.Head(); _bodyStyle = CellStyle.Body(); _footStyle = CellStyle.Foot(); }
/// <summary> /// 使用指定的工作表和Excel形状初始化对象 /// </summary> /// <param name="Sheet">Excel形状所在的工作表</param> /// <param name="PackShape">被封装的Excel形状</param> public ExcelImageObj(IExcelSheet Sheet, Shape PackShape) : base(Sheet, PackShape) { if (!PackShape.IsImage()) { throw new ArgumentException("这个形状对象不是图片"); } }
public override IExcelObj <TChart> Copy(IExcelSheet Target) { var NewChart = Target.To <ExcelSheetMicrosoft>().PackSheet.Shapes.AddChart2(); PackChart.Copy(); NewChart.Chart.Paste(); NewChart.Chart.ChartType = PackChart.ChartType.To <Microsoft.Office.Interop.Excel.XlChartType>(); return((IExcelObj <TChart>)NewChart.ToChart(Target)); }
/// <summary> /// 使用指定的参数初始化对象 /// </summary> /// <param name="Sheet">Excel形状所在的工作表</param> /// <param name="PackShape">被封装的Excel形状</param> /// <param name="Content">被封装的Office图表</param> public ExcelObjChart(IExcelSheet Sheet, Shape PackShape, ChartType Content) : base(Sheet, PackShape) { if (!PackShape.IsChart()) { throw new ArgumentException("指定的形状不包含图表"); } this.Content = Content; }
public void RefreshPivotTable() { _app.Connect(); IExcelWorkbook workbook = _app.GetActiveWorkbook(); IExcelSheet sheet = workbook.GetActiveSheet(); sheet.RefreshPivotRable("Test"); }
public void ApplyAnalysisToDataset(IExcelSheet sheet, int currentColumnIndex) { FillStatisticalColumns(sheet, ref currentColumnIndex); // Remain blank column. ++currentColumnIndex; FillAnalysisColumns(sheet, ref currentColumnIndex); }
public void ApplyAnalysisToSingleLaunch(IExcelSheet sheet, int currentRow, int operationNumber) { string normalizedFormula = ManualFormulaProvider.Normalize( $"$A{currentRow.ToString()}", "$M$6", "$M$7" ); sheet[ExcelColumnIndex.B, currentRow].SetFormula(normalizedFormula); }
public override IExcelObj <ChartType> Copy(IExcelSheet Target) { PackShape.Copy(); var NewChart = Target.To <ExcelSheetMicrosoft>().PackSheet.Shapes.AddChart2(); NewChart.Chart.Paste(); NewChart.Chart.ChartType = PackShape.Chart.ChartType; return((IExcelObj <ChartType>)NewChart.ToChart(Sheet)); }
/// <summary> /// 设置表头样式 /// </summary> /// <param name="table">工作表</param> /// <param name="style">单元格样式</param> /// <returns></returns> public IExcel HeadStyle(IExcelSheet table, CellStyle style) { if (_headStyle == null) { _headStyle = CellStyleResolver.Resolve(_workbook, style); } Style(0, table.HeadRowCount - 1, 0, table.ColumnNumber - 1, _headStyle); return(this); }
/// <summary> /// 设置页脚样式 /// </summary> /// <param name="table">工作表</param> /// <param name="style">单元格样式</param> /// <returns></returns> public IExcel FootStyle(IExcelSheet table, CellStyle style) { if (_footStyle == null) { _footStyle = CellStyleResolver.Resolve(_workbook, style); } Style(table.HeadRowCount + table.BodyRowCount, table.Count - 1, 0, table.ColumnNumber - 1, _footStyle); return(this); }
public void Set(string key, IExcelSheet value) { if (_sheets.ContainsKey(key)) { _sheets[key] = value; } else { _sheets.Add(key, value); } }
private static void FillLaunchesHeader(IExcelSheet sheet, ParametersPack args, ref int currentColumnIndex) { for (int launchesNumber = args.StartValue; launchesNumber <= args.EndValue; launchesNumber += args.Step) { var launchesColumnIndex = currentColumnIndex++.AsEnum <ExcelColumnIndex>(); sheet[launchesColumnIndex, 1].SetValue(launchesNumber); sheet.AutoSizeColumn(launchesColumnIndex); } }
public override void Insert(int index, IExcelSheet item) { if (item is ExcelSheetNpoi) { var newSheet = (ExcelSheetNpoi)item.Copy(this); ExcelBookNpoi.SetSheetOrder(newSheet.Name, index); } else { throw new Exception($"{item}不是通过Npoi实现的工作表"); } }
private static int FillSheetHeader(IExcelSheet sheet, ParametersPack args) { // Columnm index starts with zero because we use doule-conversion trick // (int -> enum -> int). int currentColumnIndex = 0; FillLaunchesHeader(sheet, args, ref currentColumnIndex); FillAdditionalDataColumn(sheet, args, ref currentColumnIndex); FillBasicColumns(sheet, args, ref currentColumnIndex); return(currentColumnIndex); }
protected override Dynamic Process() { Dynamic data = new Dynamic() { ["Code"] = 0, ["Data"] = null }; try { ExcelPackage p = _provider.GetService(typeof(ExcelPackage)) as ExcelPackage; int tableIndex = 0; do { IExcelSheet sheet = p.AddSheet("Sheet" + (++tableIndex)); int rowIndex = 0; int rStart = 2, cStart = 1; while (ReadLine()) { for (int colIndex = 0; colIndex < FieldCount; colIndex++) { IExcelRange range = sheet.Range(rStart + rowIndex, cStart + colIndex); range.Value = GetFieldValue(colIndex); } rowIndex++; } rStart = 1; for (int colIndex = 0; colIndex < FieldCount; colIndex++) { IExcelRange range = sheet.Range(rStart, colIndex + cStart); range.Value = GetFieldName(colIndex); IExcelColumn column = sheet.Column(colIndex + cStart); column.SetFormat(GetFieldType(colIndex)); column.AutoFit(); } } while (ReadNextResult()); data["Data"] = p.GetBytes(); p.Dispose(); } catch (Exception) { data["Code"] = 1; } return(data); }
private static void FillBasicColumns(IExcelSheet sheet, ParametersPack args, ref int currentColumnIndex) { int rowIndex = 1; var sampleSizeColumnIndex = currentColumnIndex++.AsEnum <ExcelColumnIndex>(); sheet[sampleSizeColumnIndex, rowIndex].SetValue(ExcelStringsPhaseTwo.SampleSizeColumnName); var theoreticalMinColumnIndex = currentColumnIndex++.AsEnum <ExcelColumnIndex>(); sheet[theoreticalMinColumnIndex, rowIndex].SetValue(ExcelStringsPhaseTwo.TheoreticalMinColumnName); var theoreticalAverageColumnIndex = currentColumnIndex++.AsEnum <ExcelColumnIndex>(); sheet[theoreticalAverageColumnIndex, rowIndex].SetValue(ExcelStringsPhaseTwo.TheoreticalAverageColumnName); var theoreticalMaxColumnIndex = currentColumnIndex++.AsEnum <ExcelColumnIndex>(); sheet[theoreticalMaxColumnIndex, rowIndex].SetValue(ExcelStringsPhaseTwo.TheoreticalMaxColumnName); ++rowIndex; for (int launchesNumber = args.StartValue; launchesNumber <= args.ExtrapolationSegmentValue; launchesNumber += args.Step) { sheet[sampleSizeColumnIndex, rowIndex].SetValue(launchesNumber); string minFormula = ManualFormulaProvider.Min( sheet, sampleSizeColumnIndex, rowIndex, args.AlgorithmType ); sheet[theoreticalMinColumnIndex, rowIndex].SetFormula(minFormula); string averageFormula = ManualFormulaProvider.Average( sheet, sampleSizeColumnIndex, rowIndex, args.AlgorithmType ); sheet[theoreticalAverageColumnIndex, rowIndex].SetFormula(averageFormula); string maxFormula = ManualFormulaProvider.Max( sheet, sampleSizeColumnIndex, rowIndex, args.AlgorithmType ); sheet[theoreticalMaxColumnIndex, rowIndex].SetFormula(maxFormula); ++rowIndex; } sheet.AutoSizeColumn(sampleSizeColumnIndex); sheet.AutoSizeColumn(theoreticalMinColumnIndex); sheet.AutoSizeColumn(theoreticalAverageColumnIndex); sheet.AutoSizeColumn(theoreticalMaxColumnIndex); }
public ExcelTransactionHitInfo GetTransactionHitInfo() { IExcelSheet sheet = _app.GetActiveWorkbook().GetActiveSheet(); ExcelRangeInfo info = sheet.GetSelection(); int selectedRow = info.Start.Row; Transaction transaction = GetTransaction(selectedRow); return(new ExcelTransactionHitInfo { Transaction = transaction }); }
public void InitializeSurveySheet(string filename) { try { File.Delete(filename); ExcelHandler = ExcelHandlerFactory.Instance.Create(filename); CurrentSheet = ExcelHandler.CreateSheet("SampleTableSurvey"); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
/// <summary> /// 根据IExcelSheet构造一个用于对比的VSheet对象 /// </summary> /// <param name="sheet"></param> public VSheet(IExcelSheet sheet, int columnCount) { _name = sheet.name; _columnCount = columnCount; for (int i = 0; i < sheet.rowCount; i++) { VRow row = new VRow(this, i, sheet.GetRow(i)); _rows.Add(row); if (row.realRowIndex != -1) { _realRowMap.Add(row.realRowIndex, row); } } }
public void InsertPivotTableToSheet(int year, int month) { _app.Connect(); IExcelWorkbook workbook = _app.GetActiveWorkbook(); IExcelSheet sheet = workbook.GetActiveSheet(); ExcelSheetData sheetData = sheet.GetSheetData(); Transaction transaction = new Transaction(); int firstCol = Transaction.GetIndex(() => transaction.Owner); int lastCol = Transaction.GetIndex(() => transaction.Amount); int numberOfRows = sheetData.Rows.Count; ExcelRangeInfo dataRange = new ExcelRangeInfo(1, firstCol, numberOfRows, lastCol + 1); ExcelRangeInfo insertRange = new ExcelRangeInfo(numberOfRows + 2, firstCol); string[] rows = { transaction.GetName(() => transaction.TransactionType), transaction.GetName(() => transaction.Category), transaction.GetName(() => transaction.SubCategory) }; string[] columns = { transaction.GetName(() => transaction.Owner) }; string[] values = { transaction.GetName(() => transaction.Amount) }; PivotTableData data = new PivotTableData { Range = dataRange, InsertCell = insertRange, Columns = columns.ToList(), Rows = rows.ToList(), Values = values.ToList(), Name = "Test01", StyleName = "PivotStyleMedium2" }; sheet.InsertPivotTable(data); }
private static void FillAdditionalParametersColumns(IExcelSheet sheet, ParametersPack args) { // Descriptive cells. sheet[ExcelColumnIndex.I, 1].SetValue(ExcelStringsPhaseOnePartTwo.AdditionalParametersColumnName); sheet[ExcelColumnIndex.I, 2].SetValue(ExcelStringsPhaseOnePartTwo.InputDataSize); sheet[ExcelColumnIndex.I, 3].SetValue(ExcelStringsPhaseOnePartTwo.MinFunc); sheet[ExcelColumnIndex.I, 4].SetValue(ExcelStringsPhaseOnePartTwo.AverageFunc); sheet[ExcelColumnIndex.I, 5].SetValue(ExcelStringsPhaseOnePartTwo.MaxFunc); sheet[ExcelColumnIndex.I, 6].SetValue(ExcelStringsPhaseOnePartTwo.ExperimentsNumber); sheet[ExcelColumnIndex.I, 7].SetValue(ExcelStringsPhaseOnePartTwo.ConfidenceFactor); sheet[ExcelColumnIndex.I, 8].SetValue(ExcelStringsPhaseOnePartTwo.SignificanceLevel); sheet[ExcelColumnIndex.I, 9].SetValue(ExcelStringsPhaseOnePartTwo.Epsilon); sheet.AutoSizeColumn(ExcelColumnIndex.I); // Value cells. sheet[ExcelColumnIndex.J, 1].SetValue(ExcelStringsPhaseOnePartTwo.AdditionalParametersValuesColumnName); sheet[ExcelColumnIndex.J, 2].SetValue(args.StartValue); string minFormula = ManualFormulaProvider.Min( sheet, ExcelColumnIndex.J, 2, args.AlgorithmType ); sheet[ExcelColumnIndex.J, 3].SetFormula(minFormula); string averageFormula = ManualFormulaProvider.Average( sheet, ExcelColumnIndex.J, 2, args.AlgorithmType ); sheet[ExcelColumnIndex.J, 4].SetFormula(averageFormula); string maxFormula = ManualFormulaProvider.Max( sheet, ExcelColumnIndex.J, 2, args.AlgorithmType ); sheet[ExcelColumnIndex.J, 5].SetFormula(maxFormula); sheet[ExcelColumnIndex.J, 6].SetValue(args.LaunchesNumber); sheet[ExcelColumnIndex.J, 7].SetValue(double.Parse(ExcelStringsPhaseOnePartTwo.ConfidenceFactorValue)); string formulaJ8 = string.Format( ExcelStringsPhaseOnePartTwo.SignificanceLevelFormula, sheet[ExcelColumnIndex.J, 7].Address ); sheet[ExcelColumnIndex.J, 8].SetFormula(formulaJ8); sheet[ExcelColumnIndex.J, 9].SetValue(double.Parse(ExcelStringsPhaseOnePartTwo.EpsilonValue)); sheet.AutoSizeColumn(ExcelColumnIndex.J); }
int GetLastRowNumber() { IExcelSheet sheet = _app.GetActiveWorkbook().GetActiveSheet(); for (int r = ROW_FIRST_TRANSACTION;; r++) { var value = sheet.GetValue(r, COL_FIRST); if (value.IsNullObj()) { return(r - 1); } } return(-1); }
private static void CreateFirstValueInInterval(IExcelSheet sheet, int currentRow, string currentRowStr, string normalizedValueRange) { string empericalFrequencyFormula = sheet.FormulaProvider.CountIfS( normalizedValueRange, $"\"<\" & $D{currentRowStr}" ); sheet[ExcelColumnIndex.E, currentRow].SetFormula(empericalFrequencyFormula); string theoreticalFrequencyFormula = sheet.FormulaProvider.BetaDist( $"$D{currentRowStr}", "$M$11", "$M$12", cumulative: true ); sheet[ExcelColumnIndex.G, currentRow].SetFormula(theoreticalFrequencyFormula); }
private void SetHeaderList(IExcelSheet sheet) { sheet.SetCellValue(1, 1, @"Create Date"); sheet.SetCellValue(1, 2, @"File Name"); int col = 3; foreach (string s in columnList) { sheet.SetCellValue(1, col++, s); } }