Last active
December 4, 2025 09:16
-
-
Save aspose-cells-gists/59a1901d62ea9ceb08456a818431a898 to your computer and use it in GitHub Desktop.
Aspose.Cells for .NET Part 2
This gist exceeds the recommended number of files (~10).
To access all files, please clone this gist.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This Gist contains code example snippets for Aspose.Cells for .NET Part 2. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| // Get the first Worksheet Cells. | |
| Cells cells = workbook.Worksheets[0].Cells; | |
| // Fill some sample data into the cells. | |
| for (int i = 0; i < 3; i++) | |
| { | |
| for (int j = 0; j < 4; j++) | |
| { | |
| cells[i, j].PutValue(i.ToString() + "," + j.ToString()); | |
| } | |
| } | |
| // Create a range (A1:D3). | |
| Range range = cells.CreateRange("A1", "D3"); | |
| // Create a style object. | |
| Style style; | |
| style = workbook.CreateStyle(); | |
| // Specify the font attribute. | |
| style.Font.Name = "Calibri"; | |
| // Specify the shading color. | |
| style.ForegroundColor = Color.Yellow; | |
| style.Pattern = BackgroundType.Solid; | |
| // Specify the border attributes. | |
| style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.TopBorder].Color = Color.Blue; | |
| style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.BottomBorder].Color = Color.Blue; | |
| style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.LeftBorder].Color = Color.Blue; | |
| style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.RightBorder].Color = Color.Blue; | |
| // Create the styleflag object. | |
| StyleFlag flag1 = new StyleFlag(); | |
| // Implement font attribute | |
| flag1.FontName = true; | |
| // Implement the shading / fill color. | |
| flag1.CellShading = true; | |
| // Implment border attributes. | |
| flag1.Borders = true; | |
| // Set the Range style. | |
| range.ApplyStyle(style, flag1); | |
| workbook.Save("CopyRangeData_out1.xlsx"); | |
| // Create a second range (A10:D12). | |
| Range range2 = cells.CreateRange("A10", "D12"); | |
| // Copy the range data only. | |
| range2.CopyData(range); | |
| // Save the excel file. | |
| workbook.Save("CopyRangeData_out2.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| // Get the first Worksheet Cells. | |
| Cells cells = workbook.Worksheets[0].Cells; | |
| // Fill some sample data into the cells. | |
| for (int i = 0; i < 3; i++) | |
| { | |
| for (int j = 0; j < 4; j++) | |
| { | |
| cells[i, j].PutValue(i.ToString() + "," + j.ToString()); | |
| } | |
| } | |
| // Create a range (A1:D3). | |
| Range range = cells.CreateRange("A1", "D3"); | |
| // Create a style object. | |
| Style style; | |
| style = workbook.CreateStyle(); | |
| // Specify the font attribute. | |
| style.Font.Name = "Calibri"; | |
| // Specify the shading color. | |
| style.ForegroundColor = Color.Yellow; | |
| style.Pattern = BackgroundType.Solid; | |
| // Specify the border attributes. | |
| style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.TopBorder].Color = Color.Blue; | |
| style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.BottomBorder].Color = Color.Blue; | |
| style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.LeftBorder].Color = Color.Blue; | |
| style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.RightBorder].Color = Color.Blue; | |
| // Create the styleflag object. | |
| StyleFlag flag1 = new StyleFlag(); | |
| // Implement font attribute | |
| flag1.FontName = true; | |
| // Implement the shading / fill color. | |
| flag1.CellShading = true; | |
| // Implment border attributes. | |
| flag1.Borders = true; | |
| // Set the Range style. | |
| range.ApplyStyle(style, flag1); | |
| workbook.Save("CopyRange_out1.xlsx"); | |
| // Create a second range (A10:D12). | |
| Range range2 = cells.CreateRange("A10", "D12"); | |
| // Copy the range data with formatting. | |
| range2.Copy(range); | |
| // Save the excel file. | |
| workbook.Save("CopyRange_out2.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create workbook object | |
| Workbook workbook = new Workbook(); | |
| // Source worksheet | |
| Worksheet srcSheet = workbook.Worksheets[0]; | |
| // Write informative message in cell A1 of destination worksheet | |
| srcSheet.Cells["A1"].PutValue("change the row height of source range"); | |
| // Set the row height of the 4th and 6th rows. This row height will be copied to destination range | |
| srcSheet.Cells.SetRowHeight(3, 50); | |
| srcSheet.Cells.SetRowHeight(5, 80); | |
| // Create source range to be copied | |
| Range srcRange = srcSheet.Cells.CreateRange("A1:D10"); | |
| // Add destination worksheet | |
| Worksheet dstSheet = workbook.Worksheets.Add("Destination Sheet"); | |
| // Create destination range in destination worksheet | |
| Range dstRange = dstSheet.Cells.CreateRange("A1:D10"); | |
| workbook.Save("CopyRangeRowHeights_pre.xlsx", SaveFormat.Xlsx); | |
| // PasteOptions, we want to copy row heights of source range to destination range | |
| PasteOptions opts = new PasteOptions(); | |
| opts.PasteType = PasteType.RowHeights; | |
| // Copy source range to destination range with paste options | |
| dstRange.Copy(srcRange, opts); | |
| // Write informative message in cell D4 and D6 of destination worksheet | |
| dstSheet.Cells["D4"].PutValue("Row heights of source range copied to destination range"); | |
| dstSheet.Cells["D6"].PutValue("Row heights of source range copied to destination range"); | |
| // Save the workbook in xlsx format | |
| workbook.Save("CopyRangeRowHeights_out.xlsx", SaveFormat.Xlsx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| // Get the first Worksheet Cells. | |
| Cells cells = workbook.Worksheets[0].Cells; | |
| // Fill some sample data into the cells. | |
| for (int i = 0; i < 3; i++) | |
| { | |
| for (int j = 0; j < 4; j++) | |
| { | |
| cells[i, j].PutValue(i.ToString() + "," + j.ToString()); | |
| } | |
| } | |
| // Create a range (A1:D3). | |
| Range range = cells.CreateRange("A1", "D3"); | |
| // Create a style object. | |
| Style style; | |
| style = workbook.CreateStyle(); | |
| // Specify the font attribute. | |
| style.Font.Name = "Calibri"; | |
| // Specify the shading color. | |
| style.ForegroundColor = Color.Yellow; | |
| style.Pattern = BackgroundType.Solid; | |
| // Specify the border attributes. | |
| style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.TopBorder].Color = Color.Blue; | |
| style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.BottomBorder].Color = Color.Blue; | |
| style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.LeftBorder].Color = Color.Blue; | |
| style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; | |
| style.Borders[BorderType.RightBorder].Color = Color.Blue; | |
| // Create the styleflag object. | |
| StyleFlag flag1 = new StyleFlag(); | |
| // Implement font attribute | |
| flag1.FontName = true; | |
| // Implement the shading / fill color. | |
| flag1.CellShading = true; | |
| // Implment border attributes. | |
| flag1.Borders = true; | |
| // Set the Range style. | |
| range.ApplyStyle(style, flag1); | |
| workbook.Save("CopyRangeStyle_out1.xlsx"); | |
| // Create a second range (A10:D12). | |
| Range range2 = cells.CreateRange("A10", "D12"); | |
| // Copy the range style only. | |
| range2.CopyStyle(range); | |
| // Save the excel file. | |
| workbook.Save("CopyRangeStyle_out2.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create the workbook | |
| Workbook workbook = new Workbook("combo.xlsx"); | |
| // Access the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Add a stock vloume(VHLC) | |
| int pieIdx = worksheet.Charts.Add(ChartType.StockVolumeHighLowClose, 15, 0, 34, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "Combo Chart"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:E12", true); | |
| // Set category data | |
| chart.NSeries.CategoryData = "A2:A12"; | |
| // Set the Series[1] Series[2] and Series[3] to different Marker Style | |
| for (int j = 0; j < chart.NSeries.Count; j++) | |
| { | |
| switch (j) | |
| { | |
| case 1: | |
| chart.NSeries[j].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[j].Marker.MarkerSize = 15; | |
| chart.NSeries[j].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[j].Marker.Area.ForegroundColor = Color.Pink; | |
| chart.NSeries[j].Border.IsVisible = false; | |
| break; | |
| case 2: | |
| chart.NSeries[j].Marker.MarkerStyle = ChartMarkerType.Dash; | |
| chart.NSeries[j].Marker.MarkerSize = 15; | |
| chart.NSeries[j].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[j].Marker.Area.ForegroundColor = Color.Orange; | |
| chart.NSeries[j].Border.IsVisible = false; | |
| break; | |
| case 3: | |
| chart.NSeries[j].Marker.MarkerStyle = ChartMarkerType.Square; | |
| chart.NSeries[j].Marker.MarkerSize = 15; | |
| chart.NSeries[j].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[j].Marker.Area.ForegroundColor = Color.LightBlue; | |
| chart.NSeries[j].Border.IsVisible = false; | |
| break; | |
| } | |
| } | |
| // Set the chart type for Series[0] | |
| chart.NSeries[0].Type = ChartType.Line; | |
| // Set style for the border of first series | |
| chart.NSeries[0].Border.Style = LineType.Solid; | |
| // Set Color for the first series | |
| chart.NSeries[0].Border.Color = Color.DarkBlue; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.Formatting = FormattingType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Create a custom language class for chart component,here take Turkey for example, | |
| //which will translate the chart element to specific language | |
| public class TurkeyChartGlobalizationSettings : ChartGlobalizationSettings | |
| { | |
| public override string GetChartTitleName() | |
| { | |
| return "Grafik Başlığı";//Chart Title | |
| } | |
| public override string GetLegendIncreaseName() | |
| { | |
| return "Artış";//Increase | |
| } | |
| public override string GetLegendDecreaseName() | |
| { | |
| return "Düşüş";//Decrease; | |
| } | |
| public override string GetLegendTotalName() | |
| { | |
| return "Toplam";//Total | |
| } | |
| public override string GetAxisTitleName() | |
| { | |
| return "Eksen Başlığı";//Axis Title | |
| } | |
| } | |
| //Create an instance of existing Workbook | |
| Workbook workbook = new Workbook(filePath + "waterfall.xlsx"); | |
| //Set custom chartGlobalizationSettings, here is TurkeyChartGlobalizationSettings | |
| workbook.Settings.GlobalizationSettings.ChartSettings = new TurkeyChartGlobalizationSettings(); | |
| //Get the worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| ChartCollection chartCollection = worksheet.Charts; | |
| //Load the chart from source worksheet | |
| Chart chart = chartCollection[0]; | |
| //Chart Calculate | |
| chart.Calculate(); | |
| //Get the chart title | |
| Title title = chart.Title; | |
| //Output the name of the Chart title | |
| Console.WriteLine("\nWorkbook chart title: " + title.Text); | |
| string[] legendEntriesLabels = chart.Legend.GetLegendLabels(); | |
| //Output the name of the Legend | |
| for (int i = 0; i < legendEntriesLabels.Length; i++) | |
| { | |
| Console.WriteLine("\nWorkbook chart legend: " + legendEntriesLabels[i]); | |
| } | |
| //Output the name of the Axis title | |
| Title categoryAxisTitle = chart.CategoryAxis.Title; | |
| Console.WriteLine("\nWorkbook category axis tile: " + categoryAxisTitle.Text); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Russian Globalization | |
| class RussianGlobalization : GlobalizationSettings | |
| { | |
| public override string GetErrorValueString(string err) | |
| { | |
| switch (err.ToUpper()) | |
| { | |
| case "#NAME?": | |
| return "#RussianName-我技攸?"; | |
| } | |
| return "RussianError-抉扮我忌抗忘"; | |
| } | |
| public override string GetBooleanValueString(bool bv) | |
| { | |
| return bv ? "RussianTrue-扭把忘志忱忘" : "RussianFalse-抖抉忪扶抑抄"; | |
| } | |
| } | |
| //Load the source workbook | |
| Workbook wb = new Workbook("sample_file.xlsx"); | |
| //Set GlobalizationSettings in Russian Language | |
| wb.Settings.GlobalizationSettings = new RussianGlobalization(); | |
| //Calculate the formula | |
| wb.CalculateFormula(); | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Save the workbook in pdf format | |
| wb.Save("outputRussianGlobalization.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Implement GlobalizationSettings class | |
| class CustomFormulaGlobalizationSettings : GlobalizationSettings | |
| { | |
| public override string GetLocalFunctionName(string standardName) | |
| { | |
| //Change the SUM function name as per your needs. | |
| if (standardName == "SUM") | |
| { | |
| return "UserFormulaLocal_SUM"; | |
| } | |
| //Change the AVERAGE function name as per your needs. | |
| if (standardName == "AVERAGE") | |
| { | |
| return "UserFormulaLocal_AVERAGE"; | |
| } | |
| return ""; | |
| }//GetLocalFunctionName | |
| } | |
| //Create workbook | |
| Workbook wb = new Workbook(); | |
| //Assign GlobalizationSettings implementation class | |
| wb.Settings.GlobalizationSettings = new CustomFormulaGlobalizationSettings(); | |
| //Access first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Access some cell | |
| Cell cell = ws.Cells["C4"]; | |
| //Assign SUM formula and print its FormulaLocal | |
| cell.Formula = "SUM(A1:A2)"; | |
| Console.WriteLine("Formula Local: " + cell.FormulaLocal); | |
| //Assign AVERAGE formula and print its FormulaLocal | |
| cell.Formula = "=AVERAGE(B1:B2, B5)"; | |
| Console.WriteLine("Formula Local: " + cell.FormulaLocal); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private class CustomPivotTableGlobalizationSettings : PivotGlobalizationSettings | |
| { | |
| //Gets the name of "Total" label in the PivotTable. | |
| //You need to override this method when the PivotTable contains two or more PivotFields in the data area. | |
| public override string GetTextOfTotal() | |
| { | |
| return "AsposeGetPivotTotalName"; | |
| } | |
| //Gets the name of "Grand Total" label in the PivotTable. | |
| public override string GetTextOfGrandTotal() | |
| { | |
| return "AsposeGetPivotGrandTotalName"; | |
| } | |
| //Gets the name of "(Multiple Items)" label in the PivotTable. | |
| public override string GetTextOfMultipleItems() | |
| { | |
| return "AsposeGetMultipleItemsName"; | |
| } | |
| //Gets the name of "(All)" label in the PivotTable. | |
| public override string GetTextOfAll() | |
| { | |
| return "AsposeGetAllName"; | |
| } | |
| //Gets the name of "Column Labels" label in the PivotTable. | |
| public override string GetTextOfColumnLabels() | |
| { | |
| return "AsposeGetColumnLabelsOfPivotTable"; | |
| } | |
| //Gets the name of "Row Labels" label in the PivotTable. | |
| public override string GetTextOfRowLabels() | |
| { | |
| return "AsposeGetRowLabelsNameOfPivotTable"; | |
| } | |
| //Gets the name of "(blank)" label in the PivotTable. | |
| public override string GetTextOfEmptyData() | |
| { | |
| return "(blank)AsposeGetEmptyDataName"; | |
| } | |
| //Gets the name of PivotFieldSubtotalType type in the PivotTable. | |
| public override string GetTextOfSubTotal(PivotFieldSubtotalType subTotalType) | |
| { | |
| switch (subTotalType) | |
| { | |
| case PivotFieldSubtotalType.Sum: | |
| return "AsposeSum"; | |
| case PivotFieldSubtotalType.Count: | |
| return "AsposeCount"; | |
| case PivotFieldSubtotalType.Average: | |
| return "AsposeAverage"; | |
| case PivotFieldSubtotalType.Max: | |
| return "AsposeMax"; | |
| case PivotFieldSubtotalType.Min: | |
| return "AsposeMin"; | |
| case PivotFieldSubtotalType.Product: | |
| return "AsposeProduct"; | |
| case PivotFieldSubtotalType.CountNums: | |
| return "AsposeCount"; | |
| case PivotFieldSubtotalType.Stdev: | |
| return "AsposeStdDev"; | |
| case PivotFieldSubtotalType.Stdevp: | |
| return "AsposeStdDevp"; | |
| case PivotFieldSubtotalType.Var: | |
| return "AsposeVar"; | |
| case PivotFieldSubtotalType.Varp: | |
| return "AsposeVarp"; | |
| } | |
| return "AsposeSubTotalName"; | |
| } | |
| } | |
| //Load your excel file | |
| Workbook wb = new Workbook("samplePivotTableGlobalizationSettings.xlsx"); | |
| //Setting Custom Pivot Table Globalization Settings | |
| wb.Settings.GlobalizationSettings.PivotSettings = new CustomPivotTableGlobalizationSettings(); | |
| //Hide first worksheet that contains the data of the pivot table | |
| wb.Worksheets[0].IsVisible = false; | |
| //Access second worksheet | |
| Worksheet ws = wb.Worksheets[1]; | |
| //Access the pivot table, refresh and calculate its data | |
| PivotTable pt = ws.PivotTables[0]; | |
| pt.RefreshData(); | |
| pt.CalculateData(); | |
| //Pdf save options - save entire worksheet on a single pdf page | |
| PdfSaveOptions options = new PdfSaveOptions(); | |
| options.OnePagePerSheet = true; | |
| //Save the output pdf | |
| wb.Save("outputPivotTableGlobalizationSettings.pdf", options); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Load an existing workbook that already contains a chart. | |
| var wb = new Workbook("ExistingWorkbook.xlsx"); | |
| Worksheet ws = wb.Worksheets[0]; | |
| // Assume the first chart is the target. | |
| Chart existingChart = ws.Charts[0]; | |
| // Change every series to Area100PercentStacked. | |
| // (If the original chart is of a different base type, you may also need to | |
| // set the chart's PlotArea type.) | |
| foreach (Series series in existingChart.NSeries) | |
| { | |
| series.Type = ChartType.Area100PercentStacked; | |
| } | |
| // Update title and axis formatting as required. | |
| existingChart.Title.Text = "Updated 100% Stacked Area Chart"; | |
| existingChart.ValueAxis.TickLabels.NumberFormat = "0%"; | |
| // Save the updated workbook. | |
| wb.Save("UpdatedWorkbook.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new Workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill worksheet with sample data. | |
| // ----------------------------------------------------------------- | |
| // A B C D | |
| // 1 Month ProductA ProductB ProductC | |
| // 2 Jan 120 80 50 | |
| // 3 Feb 150 90 70 | |
| // 4 Mar 180 110 90 | |
| // 5 Apr 200 130 110 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "ProductA"; | |
| sheet.Cells["C1"].Value = "ProductB"; | |
| sheet.Cells["D1"].Value = "ProductC"; | |
| string[] months = { "Jan", "Feb", "Mar", "Apr" }; | |
| double[] productA = { 120, 150, 180, 200 }; | |
| double[] productB = { 80, 90, 110, 130 }; | |
| double[] productC = { 50, 70, 90, 110 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = months[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = productA[i]; // Column B | |
| sheet.Cells[i + 1, 2].Value = productB[i]; // Column C | |
| sheet.Cells[i + 1, 3].Value = productC[i]; // Column D | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add an Area100PercentStacked chart. | |
| // ----------------------------------------------------------------- | |
| // Chart position: rows 6?25, columns 0?10 (adjust as needed) | |
| int chartIdx = sheet.Charts.Add(ChartType.Area100PercentStacked, 6, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Quarterly Sales ¨C 100% Stacked Area"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for each product. | |
| // ----------------------------------------------------------------- | |
| // Series 0 ¨C ProductA | |
| int seriesA = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesA].Name = "ProductA"; | |
| // Series 1 ¨C ProductB | |
| int seriesB = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesB].Name = "ProductB"; | |
| // Series 2 ¨C ProductC | |
| int seriesC = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[seriesC].Name = "ProductC"; | |
| // ----------------------------------------------------------------- | |
| // 5. Set category (X?axis) data ¨C the months. | |
| // ----------------------------------------------------------------- | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 6. Optional customisation. | |
| // ----------------------------------------------------------------- | |
| // Show legend at the bottom. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Axis titles. | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Share of Total Sales (%)"; | |
| // Set a light background colour for the plot area. | |
| chart.PlotArea.Area.BackgroundColor = Color.FromArgb(240, 240, 250); | |
| chart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Area100PercentStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook. | |
| Workbook workbook = new Workbook(); | |
| // 2. Access the first worksheet. | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // 3. Populate the worksheet with sample data. | |
| // Row 0: header | |
| // Rows 1?5: data series | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| string[] months = { "Jan", "Feb", "Mar", "Apr", "May" }; | |
| double[] productA = { 120, 150, 170, 130, 190 }; | |
| double[] productB = { 80, 110, 130, 95, 140 }; | |
| double[] productC = { 60, 90, 100, 85, 120 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = months[i]; // Column A ¨C Month | |
| sheet.Cells[i + 1, 1].Value = productA[i]; // Column B ¨C Product A | |
| sheet.Cells[i + 1, 2].Value = productB[i]; // Column C ¨C Product B | |
| sheet.Cells[i + 1, 3].Value = productC[i]; // Column D ¨C Product C | |
| } | |
| // 4. Add a new Area3D chart to the worksheet. | |
| int chartIndex = sheet.Charts.Add(ChartType.Area3D, 7, 0, 25, 7); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // 5. Set the data source for the chart. | |
| // The first row (A1:D1) contains series names. | |
| // The first column (A2:A6) contains categories (months). | |
| // Data range: B2:D6 (values for each product). | |
| chart.NSeries.Add("=Sheet1!$B$2:$D$6", true); | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$6"; | |
| // 6. Customize appearance. | |
| chart.Title.Text = "Monthly Sales ¨C 3D Area Chart"; | |
| chart.Title.Font.Size = 12; | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // Axis titles (optional) | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Units Sold"; | |
| // Apply a predefined chart style. | |
| chart.Style = 13; // Equivalent to built?in style #13. | |
| // 7. Save the workbook. | |
| workbook.Save("Area3DChartDemo.xlsx"); | |
| Console.WriteLine("Workbook with Area3D chart created successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].PutValue("Quarter"); | |
| sheet.Cells["B1"].PutValue("Product A"); | |
| sheet.Cells["C1"].PutValue("Product B"); | |
| sheet.Cells["D1"].PutValue("Product C"); | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] prodA = { 12000, 15000, 17000, 19000 }; | |
| double[] prodB = { 8000, 11000, 13000, 16000 }; | |
| double[] prodC = { 6000, 9000, 11500, 14000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| int row = i + 1; // Excel rows are 0?based in Aspose.Cells | |
| sheet.Cells[row, 0].PutValue(quarters[i]); // Column A | |
| sheet.Cells[row, 1].PutValue(prodA[i]); // Column B | |
| sheet.Cells[row, 2].PutValue(prodB[i]); // Column C | |
| sheet.Cells[row, 3].PutValue(prodC[i]); // Column D | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a 3?D Area chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.Area3D, 6, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C Area3D Chart"; | |
| // ------------------------------------------------------------ | |
| // 4. Define series for each product. | |
| // ------------------------------------------------------------ | |
| // Series 1 ¨C Product A | |
| int seriesA = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesA].Name = "Product A"; | |
| // Series 2 ¨C Product B | |
| int seriesB = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesB].Name = "Product B"; | |
| // Series 3 ¨C Product C | |
| int seriesC = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[seriesC].Name = "Product C"; | |
| // Category (X?axis) data ¨C Quarter labels | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize chart appearance (optional). | |
| // ------------------------------------------------------------ | |
| // Set a light gray background for the plot area. | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.Solid; | |
| chart.PlotArea.Area.ForegroundColor = Color.LightGray; | |
| // Enable a legend and place it at the bottom. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "Area3DChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create workbook and worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Revenue"; | |
| // 2. Fill worksheet with sample data. | |
| // A B C D | |
| // 1 Region Jan Feb Mar | |
| // 2 North 3000 3500 4000 | |
| // 3 South 2500 2800 3100 | |
| // 4 East 2000 2300 2600 | |
| // 5 West 1500 1700 1900 | |
| sheet.Cells["A1"].PutValue("Region"); | |
| sheet.Cells["B1"].PutValue("Jan"); | |
| sheet.Cells["C1"].PutValue("Feb"); | |
| sheet.Cells["D1"].PutValue("Mar"); | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] rev = { | |
| {3000, 3500, 4000}, | |
| {2500, 2800, 3100}, | |
| {2000, 2300, 2600}, | |
| {1500, 1700, 1900} | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(regions[i]); | |
| for (int j = 0; j < 3; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].PutValue(rev[i, j]); | |
| } | |
| } | |
| // 3. Add the 3?D stacked area chart. | |
| int idx = sheet.Charts.Add(ChartType.Area3DStacked, 6, 0, 24, 9); | |
| Chart chart = sheet.Charts[idx]; | |
| chart.Title.Text = "Monthly Revenue by Region (3D Stacked Area)"; | |
| // Link data. | |
| chart.SetChartDataRange("A1:D5", true); | |
| chart.NSeries.CategoryData = "B1:D1"; | |
| // 4. Apply custom colors and transparency to each series. | |
| Color[] seriesColors = { Color.DarkBlue, Color.SeaGreen, Color.OrangeRed, Color.SlateGray }; | |
| double[] transparencies = { 0.0, 0.15, 0.25, 0.35 }; // 0 = opaque, 1 = fully transparent | |
| for (int s = 0; s < chart.NSeries.Count; s++) | |
| { | |
| chart.NSeries[s].Name = regions[s]; | |
| chart.NSeries[s].Type = ChartType.Area3DStacked; // Explicitly set (optional) | |
| // Set fill color. | |
| chart.NSeries[s].Area.ForegroundColor = seriesColors[s]; | |
| chart.NSeries[s].Area.Transparency = transparencies[s]; | |
| chart.NSeries[s].Area.Formatting = FormattingType.Custom; | |
| // Optional: hide border for a cleaner look. | |
| chart.NSeries[s].Border.IsVisible = false; | |
| } | |
| // 5. Add axis titles for better readability. | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Revenue (USD)"; | |
| // 6. Save workbook. | |
| string outFile = "Area3DStacked_Chart_Customized.xlsx"; | |
| workbook.Save(outFile); | |
| Console.WriteLine($"Customized workbook saved to {outFile}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Initialise a new workbook and get the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D E | |
| // 1 Quarter Q1 2024 Q2 2024 Q3 2024 Q4 2024 | |
| // 2 Product A 1200 1500 1800 2100 | |
| // 3 Product B 800 1100 1300 1600 | |
| // 4 Product C 600 900 1100 1400 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Quarter"); | |
| sheet.Cells["B1"].PutValue("Q1 2024"); | |
| sheet.Cells["C1"].PutValue("Q2 2024"); | |
| sheet.Cells["D1"].PutValue("Q3 2024"); | |
| sheet.Cells["E1"].PutValue("Q4 2024"); | |
| sheet.Cells["A2"].PutValue("Product A"); | |
| sheet.Cells["A3"].PutValue("Product B"); | |
| sheet.Cells["A4"].PutValue("Product C"); | |
| double[,] values = { | |
| {1200, 1500, 1800, 2100}, | |
| { 800, 1100, 1300, 1600}, | |
| { 600, 900, 1100, 1400} | |
| }; | |
| for (int row = 0; row < 3; row++) | |
| { | |
| for (int col = 0; col < 4; col++) | |
| { | |
| sheet.Cells[row + 1, col + 1].PutValue(values[row, col]); | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add an Area3DStacked chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: type, upper-left row, upper-left column, lower-right row, lower-right column | |
| int chartIndex = sheet.Charts.Add(ChartType.Area3DStacked, 7, 0, 25, 9); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C 3D Stacked Area"; | |
| // ------------------------------------------------------------ | |
| // 4. Define the chart data range. | |
| // ------------------------------------------------------------ | |
| // Category (X) axis ¨C quarters (B1:E1) | |
| // Series ¨C each product (B2:E4) | |
| chart.SetChartDataRange("A1:E4", true); | |
| chart.NSeries.CategoryData = "A2:A4"; | |
| // ------------------------------------------------------------ | |
| // 5. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "Area3DStacked_Chart_Basic.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1 Create a new workbook and obtain the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2 Fill the worksheet with sample data (Month vs. Sales). | |
| // ------------------------------------------------------------ | |
| // A B C | |
| // 1 Month ProductA ProductB | |
| // 2 Jan 120 80 | |
| // 3 Feb 150 95 | |
| // 4 Mar 180 110 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "ProductA"; | |
| sheet.Cells["C1"].Value = "ProductB"; | |
| string[] months = { "Jan", "Feb", "Mar" }; | |
| double[] prodA = { 120, 150, 180 }; | |
| double[] prodB = { 80, 95, 110 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = months[i]; | |
| sheet.Cells[i + 1, 1].Value = prodA[i]; | |
| sheet.Cells[i + 1, 2].Value = prodB[i]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 3 Add an Area chart (base type) to the worksheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: ChartType, upper left row, upper left column, | |
| // lower right row, lower right column | |
| int chartIndex = sheet.Charts.Add(ChartType.Area, 5, 0, 20, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Monthly Sales ¨C Area Chart"; | |
| // ------------------------------------------------------------ | |
| // 4 Add two series ¨C one for each product. | |
| // ------------------------------------------------------------ | |
| // Series 0 ¨C ProductA | |
| int seriesA = chart.NSeries.Add("=Sheet1!$B$2:$B$4", true); | |
| chart.NSeries[seriesA].Name = "ProductA"; | |
| // Series 1 ¨C ProductB | |
| int seriesB = chart.NSeries.Add("=Sheet1!$C$2:$C$4", true); | |
| chart.NSeries[seriesB].Name = "ProductB"; | |
| // ------------------------------------------------------------ | |
| // 5 (Optional) Customize chart appearance. | |
| // ------------------------------------------------------------ | |
| // Set the category (X) axis titles. | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| // Set the primary value (Y) axis title. | |
| chart.ValueAxis.Title.Text = "Units Sold"; | |
| // Use a soft gradient for the plot area background. | |
| chart.PlotArea.Area.ForegroundColor = Color.FromArgb(255, 240, 255, 240); | |
| chart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| // ------------------------------------------------------------ | |
| // 6 Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "AreaChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample quarterly sales data. | |
| // ----------------------------------------------------------------- | |
| // A B C D | |
| // 1 Quarter ProductA ProductB ProductC | |
| // 2 Q1 12000 15000 10000 | |
| // 3 Q2 18000 13000 12000 | |
| // 4 Q3 16000 17000 14000 | |
| // 5 Q4 20000 19000 18000 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "ProductA"; | |
| sheet.Cells["C1"].Value = "ProductB"; | |
| sheet.Cells["D1"].Value = "ProductC"; | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| int[] prodA = { 12000, 18000, 16000, 20000 }; | |
| int[] prodB = { 15000, 13000, 17000, 19000 }; | |
| int[] prodC = { 10000, 12000, 14000, 18000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = quarters[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = prodA[i]; // Column B | |
| sheet.Cells[i + 1, 2].Value = prodB[i]; // Column C | |
| sheet.Cells[i + 1, 3].Value = prodC[i]; // Column D | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add an Area Stacked chart (base type) to the worksheet. | |
| // ----------------------------------------------------------------- | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column | |
| int chartIndex = sheet.Charts.Add(ChartType.AreaStacked, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C Area Stacked Chart"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for each product. | |
| // ----------------------------------------------------------------- | |
| // Series order follows the columns B, C, D. | |
| chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); // ProductA | |
| chart.NSeries[0].Name = "Product A"; | |
| chart.NSeries.Add("=Sheet1!$C$2:$C$5", true); // ProductB | |
| chart.NSeries[1].Name = "Product B"; | |
| chart.NSeries.Add("=Sheet1!$D$2:$D$5", true); // ProductC | |
| chart.NSeries[2].Name = "Product C"; | |
| // ----------------------------------------------------------------- | |
| // 5. Configure axes titles and formatting (optional but recommended). | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| // Apply a light gray background to the plot area. | |
| chart.PlotArea.Area.ForegroundColor = Color.FromArgb(240, 240, 240); | |
| chart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook to disk. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "AreaStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to \"{outputPath}\""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Load workbook that already contains a Bar100PercentStacked chart | |
| var workbook = new Workbook("Bar100PercentStackedChart_Output.xlsx"); | |
| var sheet = workbook.Worksheets[0]; | |
| // Assume the chart we added earlier is the first chart in the sheet | |
| Chart chart = sheet.Charts[0]; | |
| // -------------------- Change series colors -------------------- | |
| Color[] palette = { Color.DarkCyan, Color.IndianRed, Color.Goldenrod }; | |
| for (int i = 0; i < chart.NSeries.Count && i < palette.Length; i++) | |
| { | |
| chart.NSeries[i].Area.ForegroundColor = palette[i]; | |
| } | |
| // -------------------- Add a legend (if not present) ---------- | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // -------------------- Update the chart title ----------------- | |
| chart.Title.Text = "Updated Quarterly Sales (% of Region Total)"; | |
| // -------------------- Save the modified workbook --------------- | |
| workbook.Save("Bar100PercentStackedChart_Modified.xlsx"); | |
| Console.WriteLine("Modified workbook saved as 'Bar100PercentStackedChart_Modified.xlsx'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| // ------------------------------------------------------------ | |
| // 2. obtain the first worksheet | |
| // ------------------------------------------------------------ | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 3. Populate worksheet with sample data | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Region Q1 Sales Q2 Sales Q3 Sales | |
| // 2 North 1200 1500 1300 | |
| // 3 South 900 1100 1000 | |
| // 4 East 700 800 750 | |
| // 5 West 1100 1250 1150 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "Q1 Sales"; | |
| sheet.Cells["C1"].Value = "Q2 Sales"; | |
| sheet.Cells["D1"].Value = "Q3 Sales"; | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] sales = { | |
| { 1200, 1500, 1300 }, | |
| { 900, 1100, 1000 }, | |
| { 700, 800, 750 }, | |
| { 1100, 1250, 1150 } | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = regions[i]; | |
| sheet.Cells[i + 1, 1].Value = sales[i, 0]; | |
| sheet.Cells[i + 1, 2].Value = sales[i, 1]; | |
| sheet.Cells[i + 1, 3].Value = sales[i, 2]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 4. Add a Bar100PercentStacked chart (starts at row 7, column 0) | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.Bar100PercentStacked, 7, 0, 25, 12); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales Distribution (100% Stacked Bar)"; | |
| // ------------------------------------------------------------ | |
| // 5. Add series ¨C each quarter becomes a separate series | |
| // ------------------------------------------------------------ | |
| // Series 0 ¨C Q1 | |
| int series0 = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[series0].Name = "Q1 Sales"; | |
| // Series 1 ¨C Q2 | |
| int series1 = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[series1].Name = "Q2 Sales"; | |
| // Series 2 ¨C Q3 | |
| int series2 = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[series2].Name = "Q3 Sales"; | |
| // ------------------------------------------------------------ | |
| // 6. Set category (X?axis) data ¨C region names | |
| // ------------------------------------------------------------ | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 7. Enable data labels to show percentages | |
| // ------------------------------------------------------------ | |
| foreach (Series ser in chart.NSeries) | |
| { | |
| ser.DataLabels.ShowValue = false; // hide raw value | |
| ser.DataLabels.ShowPercentage = true; // show % of total | |
| ser.DataLabels.Position = LabelPositionType.InsideEnd; | |
| ser.DataLabels.Font.Color = Color.White; | |
| ser.DataLabels.Font.Size = 10; | |
| } | |
| // ------------------------------------------------------------ | |
| // 8. Customize axes (optional) | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Percentage of Total Sales"; | |
| // Ensure the value axis shows 0?100% | |
| chart.ValueAxis.MinValue = 0; | |
| chart.ValueAxis.MaxValue = 100; | |
| chart.ValueAxis.TickLabels.NumberFormat = "0%"; | |
| // ------------------------------------------------------------ | |
| // 9. Save the workbook | |
| // ------------------------------------------------------------ | |
| string outputPath = filePath + "Bar100PercentStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialise a new workbook and obtain its first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "QuarterlySales"; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Category Product A Product B Product C | |
| // 2 Q1 120 80 100 | |
| // 3 Q2 150 70 130 | |
| // 4 Q3 170 90 110 | |
| // 5 Q4 200 110 150 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Product A"); | |
| sheet.Cells["C1"].PutValue("Product B"); | |
| sheet.Cells["D1"].PutValue("Product C"); | |
| string[] categories = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[,] sales = { | |
| { 120, 80, 100 }, | |
| { 150, 70, 130 }, | |
| { 170, 90, 110 }, | |
| { 200, 110, 150 } | |
| }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| // Category column (A) | |
| sheet.Cells[i + 1, 0].PutValue(categories[i]); | |
| // Product data columns (B?D) | |
| for (int j = 0; j < 3; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].PutValue(sales[i, j]); | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Bar3D100PercentStacked chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.Bar3D100PercentStacked, 7, 0, 24, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title. | |
| chart.Title.Text = "Quarterly Sales ¨C 100?% Stacked 3?D Bar Chart"; | |
| // Define the data range for the series. | |
| // The first argument is the range containing the series values. | |
| // Setting the second argument to true indicates that the series | |
| // names are taken from the first row of the range. | |
| chart.NSeries.Add("=QuarterlySales!$B$2:$D$5", true); | |
| // Set category (X?axis) labels. | |
| chart.NSeries.CategoryData = "=QuarterlySales!$A$2:$A$5"; | |
| // Optional: customize axes titles. | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Percentage of Total Sales"; | |
| // Optional: display data labels as percentages. | |
| foreach (var series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.ShowPercentage = true; | |
| series.DataLabels.ShowCategoryName = false; | |
| series.DataLabels.ShowSeriesName = false; | |
| } | |
| // ------------------------------------------------------------ | |
| // 4. Save the workbook to an XLSX file. | |
| // ------------------------------------------------------------ | |
| string outputPath = "Bar3D100PercentStacked_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] prodA = { 15000, 18000, 21000, 24000 }; | |
| double[] prodB = { 12000, 14000, 16000, 19000 }; | |
| double[] prodC = { 9000, 11000, 13000, 15000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = quarters[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = prodA[i]; // Column B | |
| sheet.Cells[i + 1, 2].Value = prodB[i]; // Column C | |
| sheet.Cells[i + 1, 3].Value = prodC[i]; // Column D | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Bar3DClustered chart. | |
| // ----------------------------------------------------------------- | |
| // Parameters: (type, upper-left row, upper-left column, lower-right row, lower-right column) | |
| int chartIndex = sheet.Charts.Add(ChartType.Bar3DClustered, 6, 0, 22, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales (3?D Bar)"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define series for each product. | |
| // ----------------------------------------------------------------- | |
| // Series for Product A | |
| int seriesA = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesA].Name = "Product A"; | |
| // Series for Product B | |
| int seriesB = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesB].Name = "Product B"; | |
| // Series for Product C | |
| int seriesC = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[seriesC].Name = "Product C"; | |
| // Set category (X?axis) data ¨C quarters | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 5. Customize axis titles and appearance (optional). | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| chart.PlotArea.Area.Formatting = FormattingType.None; // Transparent plot area | |
| // Example: change the bar gap width (percentage of the bar width) | |
| chart.GapWidth = 20; // Smaller gap makes bars appear thicker | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Bar3DClustered_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data for a stacked bar chart. | |
| // A B C D | |
| // 1 Category Q1 Q2 Q3 | |
| // 2 North 120 150 130 | |
| // 3 South 100 110 115 | |
| // 4 East 90 80 95 | |
| // 5 West 70 85 75 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| string[] categories = { "North", "South", "East", "West" }; | |
| double[,] values = { | |
| {120, 150, 130}, | |
| {100, 110, 115}, | |
| { 90, 80, 95}, | |
| { 70, 85, 75} | |
| }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; // Column A ¨C Category name | |
| sheet.Cells[i + 1, 1].Value = values[i, 0]; // Column B ¨C Q1 | |
| sheet.Cells[i + 1, 2].Value = values[i, 1]; // Column C ¨C Q2 | |
| sheet.Cells[i + 1, 3].Value = values[i, 2]; // Column D ¨C Q3 | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Bar3DStacked chart to the worksheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.Bar3DStacked, 7, 0, 25, 12); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales (3?D Stacked Bar)"; | |
| // ------------------------------------------------------------ | |
| // 4. Define the data range that the chart will use. | |
| // ------------------------------------------------------------ | |
| // Include headers so that the chart can retrieve series names. | |
| chart.SetChartDataRange("A1:D5", true); | |
| // Category (X?axis) data ¨C the list of regions. | |
| chart.NSeries.CategoryData = "A2:A5"; | |
| // Add series for each quarter. | |
| chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); // Q1 | |
| chart.NSeries[0].Name = "Q1"; | |
| chart.NSeries.Add("=Sheet1!$C$2:$C$5", true); // Q2 | |
| chart.NSeries[1].Name = "Q2"; | |
| chart.NSeries.Add("=Sheet1!$D$2:$D$5", true); // Q3 | |
| chart.NSeries[2].Name = "Q3"; | |
| // ------------------------------------------------------------ | |
| // 5. Optional: customize the appearance of each series. | |
| // ------------------------------------------------------------ | |
| chart.NSeries[0].Border.Color = Color.DarkBlue; | |
| chart.NSeries[1].Border.Color = Color.DarkGreen; | |
| chart.NSeries[2].Border.Color = Color.DarkRed; | |
| // Example: set a light gray background for the plot area. | |
| chart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| chart.PlotArea.Area.ForegroundColor = Color.LightGray; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook with the chart. | |
| // ------------------------------------------------------------ | |
| workbook.Save("Bar3DStackedChart_Output.xlsx"); | |
| Console.WriteLine("Bar3DStacked chart created successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize workbook and worksheet. | |
| // ----------------------------------------------------------------- | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // ----------------------------------------------------------------- | |
| // 2. Insert data representing market share per region. | |
| // ----------------------------------------------------------------- | |
| // A B C D | |
| // 1 Region North South East | |
| // 2 2020 30 45 25 | |
| // 3 2021 35 40 25 | |
| // 4 2022 40 35 25 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "North"; | |
| sheet.Cells["C1"].Value = "South"; | |
| sheet.Cells["D1"].Value = "East"; | |
| string[] years = { "2020", "2021", "2022" }; | |
| double[,] share = { | |
| { 30, 45, 25 }, | |
| { 35, 40, 25 }, | |
| { 40, 35, 25 } | |
| }; | |
| for (int i = 0; i < years.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = years[i]; | |
| for (int j = 0; j < 3; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].Value = share[i, j]; | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Stacked Bar chart. | |
| // ----------------------------------------------------------------- | |
| int chartIdx = sheet.Charts.Add(ChartType.BarStacked, 6, 0, 22, 10); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Annual Market Share ¨C Stacked Bar"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for each region. | |
| // ----------------------------------------------------------------- | |
| // Series order matters for stacked charts ¨C first series appears at bottom. | |
| int index = chart.NSeries.Add("=Sheet1!$B$2:$B$4", true); | |
| chart.NSeries[index].Name = "North"; | |
| index = chart.NSeries.Add("=Sheet1!$C$2:$C$4", true); | |
| chart.NSeries[index].Name = "South"; | |
| index = chart.NSeries.Add("=Sheet1!$D$2:$D$4", true); | |
| chart.NSeries[index].Name = "East"; | |
| // ----------------------------------------------------------------- | |
| // 5. Customize colors for each series. | |
| // ----------------------------------------------------------------- | |
| Color[] seriesColors = { Color.CornflowerBlue, Color.IndianRed, Color.DarkSeaGreen }; | |
| for (int i = 0; i < chart.NSeries.Count; i++) | |
| { | |
| chart.NSeries[i].Area.ForegroundColor = seriesColors[i]; | |
| chart.NSeries[i].Border.IsVisible = false; | |
| } | |
| // Show legend at the bottom. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Enable data labels inside the bars. | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.Position = LabelPositionType.Center; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string workbookPath = "BarChart_Stacked_Output.xlsx"; | |
| workbook.Save(workbookPath); | |
| Console.WriteLine($"Workbook saved to {workbookPath}"); | |
| // ----------------------------------------------------------------- | |
| // 7. Export the chart as a PNG image (optional). | |
| // ----------------------------------------------------------------- | |
| // The chart can be exported directly without opening Excel. | |
| chart.ToImage(workbookPath.Replace(".xlsx", "_Chart.png"), ImageType.Png); | |
| Console.WriteLine("Chart exported as PNG image."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // ----------------------------------------------------------------- | |
| // A B C D | |
| // 1 Quarter Q1 Q2 Q3 | |
| // 2 Product A 120 150 180 | |
| // 3 Product B 100 130 160 | |
| // 4 Product C 90 110 140 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| string[] products = { "Product A", "Product B", "Product C" }; | |
| double[,] sales = { | |
| { 120, 150, 180 }, | |
| { 100, 130, 160 }, | |
| { 90, 110, 140 } | |
| }; | |
| for (int i = 0; i < products.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = products[i]; | |
| for (int j = 0; j < 3; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].Value = sales[i, j]; | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Bar chart (horizontal clustered) to the worksheet. | |
| // ----------------------------------------------------------------- | |
| // Parameters: chart type, upper-left row, upper-left column, | |
| // lower-right row, lower-right column | |
| int chartIndex = sheet.Charts.Add(ChartType.Bar, 6, 0, 22, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C Bar Chart"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for each quarter. | |
| // ----------------------------------------------------------------- | |
| // Series 1 ¨C Q1 | |
| int series1 = chart.NSeries.Add("=Sheet1!$B$2:$B$4", true); | |
| chart.NSeries[series1].Name = "Q1"; | |
| // Series 2 ¨C Q2 | |
| int series2 = chart.NSeries.Add("=Sheet1!$C$2:$C$4", true); | |
| chart.NSeries[series2].Name = "Q2"; | |
| // Series 3 ¨C Q3 | |
| int series3 = chart.NSeries.Add("=Sheet1!$D$2:$D$4", true); | |
| chart.NSeries[series3].Name = "Q3"; | |
| // ----------------------------------------------------------------- | |
| // 5. Optional customizations. | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Products"; | |
| chart.ValueAxis.Title.Text = "Sales (Units)"; | |
| // Show data labels on each bar for better readability. | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.Position = LabelPositionType.InsideEnd; | |
| } | |
| // Set chart style | |
| chart.Style = 6; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outPath = "BarChart_Simple_Output.xlsx"; | |
| workbook.Save(outPath); | |
| Console.WriteLine($"Workbook saved to {outPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Load an existing workbook that already contains a Bar Stacked chart. | |
| var workbook = new Workbook("BarStackedChart_Output.xlsx"); | |
| var sheet = workbook.Worksheets[0]; | |
| // Assume the first chart on the sheet is the Bar Stacked chart we want to edit. | |
| Chart chart = sheet.Charts[0]; | |
| // Change the chart title. | |
| chart.Title.Text = "Updated Quarterly Sales (Stacked)"; | |
| // Modify the fill color of each series. | |
| // Series order is the same as when they were originally added. | |
| chart.NSeries[0].Area.ForegroundColor = Color.LightCoral; // Product A | |
| chart.NSeries[1].Area.ForegroundColor = Color.MediumSeaGreen; // Product B | |
| chart.NSeries[2].Area.ForegroundColor = Color.CornflowerBlue; // Product C | |
| // Add a new series ¨C Product D. | |
| // First, add data for Product D to the worksheet. | |
| double[] prodD = { 40, 60, 80, 100 }; | |
| for (int i = 0; i < prodD.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 4].Value = prodD[i]; // Column E (index 4) | |
| } | |
| sheet.Cells[0, 4].Value = "Product D"; // Header | |
| // Add the new series to the chart. | |
| int seriesD = chart.NSeries.Add("=Sheet1!$E$2:$E$5", true); | |
| chart.NSeries[seriesD].Name = "Product D"; | |
| chart.NSeries[seriesD].Area.ForegroundColor = Color.Goldenrod; | |
| // Save the updated workbook. | |
| workbook.Save("BarStackedChart_Updated.xlsx"); | |
| Console.WriteLine("Chart updated and saved as BarStackedChart_Updated.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // The data represents quarterly sales of three product lines. | |
| // ----------------------------------------------------------------- | |
| // A B C D | |
| // 1 Quarter Product A Product B Product C | |
| // 2 Q1 120 80 50 | |
| // 3 Q2 150 90 70 | |
| // 4 Q3 180 110 90 | |
| // 5 Q4 200 130 110 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] prodA = { 120, 150, 180, 200 }; | |
| double[] prodB = { 80, 90, 110, 130 }; | |
| double[] prodC = { 50, 70, 90, 110 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = quarters[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = prodA[i]; // Column B | |
| sheet.Cells[i + 1, 2].Value = prodB[i]; // Column C | |
| sheet.Cells[i + 1, 3].Value = prodC[i]; // Column D | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Bar Stacked chart (horizontal stacked bars). | |
| // The chart will be placed starting at row 7, column 0 | |
| // and will span 20 rows and 10 columns. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.BarStacked, 7, 0, 27, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales (Stacked Bar)"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for each product. The first series (Product A) | |
| // will be the base, the second (Product B) and third (Product C) | |
| // will be stacked on top. | |
| // ----------------------------------------------------------------- | |
| // Series for Product A | |
| int seriesA = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[seriesA].Name = "Product A"; | |
| // Series for Product B | |
| int seriesB = chart.NSeries.Add("=Sheet1!$C$2:$C$5", true); | |
| chart.NSeries[seriesB].Name = "Product B"; | |
| // Series for Product C | |
| int seriesC = chart.NSeries.Add("=Sheet1!$D$2:$D$5", true); | |
| chart.NSeries[seriesC].Name = "Product C"; | |
| // ----------------------------------------------------------------- | |
| // 5. Set category (X?axis) data ¨C the quarters. | |
| // ----------------------------------------------------------------- | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 6. Optional: customize the look of the chart. | |
| // ----------------------------------------------------------------- | |
| chart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| chart.PlotArea.Area.ForegroundColor = Color.WhiteSmoke; | |
| // Set axis titles | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (Units)"; | |
| // Change the bar gap width (makes bars thicker) | |
| chart.GapWidth = 75; // Percent (default is 150) | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "BarStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| ws.Name = "AdvancedData"; | |
| // ------- Sample data (same layout as previous example) ------- | |
| ws.Cells["A1"].Value = "Category"; | |
| ws.Cells["B1"].Value = "GroupA"; | |
| ws.Cells["C1"].Value = "GroupB"; | |
| string[] cats = { "Sample1", "Sample2", "Sample3", "Sample4", "Sample5" }; | |
| double[,] data = { | |
| { 5, 7 }, | |
| { 9, 11 }, | |
| { 4, 6 }, | |
| { 15, 14 }, | |
| { 8, 12 } | |
| }; | |
| for (int i = 0; i < cats.Length; i++) | |
| { | |
| ws.Cells[i + 1, 0].Value = cats[i]; | |
| ws.Cells[i + 1, 1].Value = data[i, 0]; | |
| ws.Cells[i + 1, 2].Value = data[i, 1]; | |
| } | |
| // ----------------- Create BoxWhisker chart ----------------- | |
| int idx = ws.Charts.Add(ChartType.BoxWhisker, 7, 0, 25, 12); | |
| Chart chart = ws.Charts[idx]; | |
| chart.Title.Text = "Advanced BoxWhisker Styling"; | |
| chart.NSeries.Add("=AdvancedData!$B$2:$C$6", true); | |
| chart.NSeries.CategoryData = "=AdvancedData!$A$2:$A$6"; | |
| // ----- Styling each series individually ----- | |
| // Series 0 (GroupA) ¨C light blue fill, dashed outline | |
| chart.NSeries[0].Border.IsVisible = true; | |
| chart.NSeries[0].Border.Color = Color.DarkBlue; | |
| chart.NSeries[0].Border.Style = LineType.DashDot; | |
| chart.NSeries[0].Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[0].Area.ForegroundColor = Color.LightBlue; | |
| // Series 1 (GroupB) ¨C light green fill, solid outline | |
| chart.NSeries[1].Border.IsVisible = true; | |
| chart.NSeries[1].Border.Color = Color.DarkGreen; | |
| chart.NSeries[1].Border.Style = LineType.Solid; | |
| chart.NSeries[1].Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[1].Area.ForegroundColor = Color.LightGreen; | |
| // ----- Show outliers with a distinct marker ----- | |
| // Enable outlier display | |
| chart.NSeries[0].LayoutProperties.ShowOutlierPoints = true; | |
| // Customize outlier marker (red circle) | |
| chart.NSeries[0].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[0].Marker.MarkerSize = 12; | |
| chart.NSeries[0].Marker.Area.ForegroundColor = Color.Red; | |
| // Apply axis titles | |
| chart.CategoryAxis.Title.Text = "Sample Groups"; | |
| chart.ValueAxis.Title.Text = "Measurements"; | |
| // Save the workbook | |
| string file = filePath + "AdvancedBoxWhiskerChart.xlsx"; | |
| wb.Save(file); | |
| Console.WriteLine($"Advanced chart saved to {file}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Statistics"; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample data. | |
| // Each column represents a data series (e.g., Test1, Test2). | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Category Test1 Test2 Test3 | |
| // 2 Set1 12 14 13 | |
| // 3 Set2 15 17 16 | |
| // 4 Set3 11 13 12 | |
| // 5 Set4 20 22 21 | |
| // 6 Set5 18 19 20 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Test1"; | |
| sheet.Cells["C1"].Value = "Test2"; | |
| sheet.Cells["D1"].Value = "Test3"; | |
| string[] categories = { "Set1", "Set2", "Set3", "Set4", "Set5" }; | |
| double[,] values = { | |
| { 12, 14, 13 }, | |
| { 15, 17, 16 }, | |
| { 11, 13, 12 }, | |
| { 20, 22, 21 }, | |
| { 18, 19, 20 } | |
| }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| // Category name | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| // Data series values | |
| for (int j = 0; j < 3; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].Value = values[i, j]; | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a BoxWhisker chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper-left row, upper-left column, | |
| // lower-right row, lower-right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.BoxWhisker, 7, 0, 25, 10); | |
| Chart boxChart = sheet.Charts[chartIndex]; | |
| boxChart.Title.Text = "Statistical Distribution (BoxWhisker)"; | |
| // Attach the data range. The first row contains series names, | |
| // the first column contains category names. | |
| boxChart.NSeries.Add("=Statistics!$B$2:$D$6", true); | |
| boxChart.NSeries.CategoryData = "=Statistics!$A$2:$A$6"; | |
| // ------------------------------------------------------------ | |
| // 4. Optional customizations. | |
| // ------------------------------------------------------------ | |
| boxChart.PlotArea.Area.ForegroundColor = Color.LightBlue; | |
| boxChart.ValueAxis.Title.Text = "Value"; | |
| boxChart.CategoryAxis.Title.Text = "Data Set"; | |
| // Enable data labels inside the bars. | |
| foreach (Series series in boxChart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.Position = LabelPositionType.InsideEnd; | |
| } | |
| // ------------------------------------------------------------ | |
| // 5. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "BoxWhiskerChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // Column A ¨C Category (string) | |
| // Column B ¨C X value (numeric) | |
| // Column C ¨C Y value (numeric) | |
| // Column D ¨C Bubble size (numeric) | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].PutValue("Region"); | |
| sheet.Cells["B1"].PutValue("Revenue (M)"); | |
| sheet.Cells["C1"].PutValue("Profit (M)"); | |
| sheet.Cells["D1"].PutValue("Market Share (%)"); | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[] revenue = { 45.2, 38.5, 52.7, 40.1 }; | |
| double[] profit = { 12.4, 9.8, 15.0, 10.5 }; | |
| double[] share = { 25, 20, 30, 25 }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| int row = i + 2; // Data starts at row 2 | |
| sheet.Cells[row, 0].PutValue(regions[i]); // A column | |
| sheet.Cells[row, 1].PutValue(revenue[i]); // B column | |
| sheet.Cells[row, 2].PutValue(profit[i]); // C column | |
| sheet.Cells[row, 3].PutValue(share[i]); // D column | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Bubble3D chart to the worksheet. | |
| // Parameters: (type, upperRow, leftColumn, lowerRow, rightColumn) | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.Bubble3D, 7, 0, 25, 10); | |
| Chart bubbleChart = sheet.Charts[chartIndex]; | |
| // ----------------------------------------------------------------- | |
| // 4. Set chart title and axis titles. | |
| // ----------------------------------------------------------------- | |
| bubbleChart.Title.Text = "Regional Sales Performance (Bubble3D)"; | |
| bubbleChart.Title.Font.IsBold = true; | |
| bubbleChart.Title.Font.Size = 14; | |
| // X?Axis (Revenue) | |
| bubbleChart.CategoryAxis.Title.Text = "Revenue (Million USD)"; | |
| bubbleChart.CategoryAxis.Title.Font.IsBold = true; | |
| // Y?Axis (Profit) | |
| bubbleChart.ValueAxis.Title.Text = "Profit (Million USD)"; | |
| bubbleChart.ValueAxis.Title.Font.IsBold = true; | |
| // Z?Axis (Bubble Size) ¨C displayed as ¡°Size¡± in the Legend. | |
| bubbleChart.SecondValueAxis.Title.Text = "Market Share (%)"; | |
| bubbleChart.SecondValueAxis.Title.Font.IsBold = true; | |
| // ----------------------------------------------------------------- | |
| // 5. Bind series data. | |
| // - Category data comes from column A (Region names). | |
| // - X?values from column B, Y?values from column C, Size from column D. | |
| // ----------------------------------------------------------------- | |
| // Add a series; the first argument is the range for the series name, | |
| // the second argument indicates if the first row contains titles. | |
| int seriesIndex = bubbleChart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| Series series = bubbleChart.NSeries[seriesIndex]; | |
| series.Name = "Revenue vs Profit"; | |
| bubbleChart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; // Regions as categories | |
| series.Values = "=SalesData!$C$2:$C$5"; // Y?values (Profit) | |
| series.BubbleSizes = "=SalesData!$D$2:$D$5"; // Bubble size (Market Share) | |
| // ----------------------------------------------------------------- | |
| // 6. Optional: customize appearance (colors, markers). | |
| // ----------------------------------------------------------------- | |
| // Set a semi?transparent fill for bubbles. | |
| series.Marker.MarkerStyle = ChartMarkerType.Circle; | |
| series.Marker.Area.ForegroundColor = Color.FromArgb(128, Color.CornflowerBlue); | |
| series.Border.IsVisible = true; | |
| series.Border.Color = Color.DarkBlue; | |
| series.Border.Weight = WeightType.WideLine; | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook to an Excel file. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Bubble3DChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialize workbook and worksheet. | |
| // ------------------------------------------------------------ | |
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Insert data for two product groups. | |
| // ------------------------------------------------------------ | |
| // A B C D E F | |
| // 1 Category Sales1 Profit1 Share1 Sales2 Profit2 Share2 | |
| // 2 Jan 80 20 0.15 65 30 0.10 | |
| // 3 Feb 95 35 0.25 70 40 0.20 | |
| // 4 Mar 110 50 0.30 85 55 0.35 | |
| // ------------------------------------------------------------ | |
| ws.Cells["A1"].PutValue("Month"); | |
| ws.Cells["B1"].PutValue("Sales Group 1"); | |
| ws.Cells["C1"].PutValue("Profit Group 1"); | |
| ws.Cells["D1"].PutValue("Share Group 1"); | |
| ws.Cells["E1"].PutValue("Sales Group 2"); | |
| ws.Cells["F1"].PutValue("Profit Group 2"); | |
| ws.Cells["G1"].PutValue("Share Group 2"); | |
| string[] months = { "Jan", "Feb", "Mar" }; | |
| double[] sales1 = { 80, 95, 110 }; | |
| double[] profit1 = { 20, 35, 50 }; | |
| double[] share1 = { 0.15, 0.25, 0.30 }; | |
| double[] sales2 = { 65, 70, 85 }; | |
| double[] profit2 = { 30, 40, 55 }; | |
| double[] share2 = { 0.10, 0.20, 0.35 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| int row = i + 1; | |
| ws.Cells[row, 0].PutValue(months[i]); // A column | |
| ws.Cells[row, 1].PutValue(sales1[i]); // B column | |
| ws.Cells[row, 2].PutValue(profit1[i]); // C column | |
| ws.Cells[row, 3].PutValue(share1[i]); // D column | |
| ws.Cells[row, 4].PutValue(sales2[i]); // E column | |
| ws.Cells[row, 5].PutValue(profit2[i]); // F column | |
| ws.Cells[row, 6].PutValue(share2[i]); // G column | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Bubble chart (combined for two groups). | |
| // ------------------------------------------------------------ | |
| int chartIdx = ws.Charts.Add(ChartType.Bubble, 6, 0, 26, 15); | |
| Chart bubbleChart = ws.Charts[chartIdx]; | |
| bubbleChart.Title.Text = "Quarterly Sales vs Profit (Bubble = Market Share)"; | |
| bubbleChart.ShowLegend = true; | |
| // ------------------------------------------------------------ | |
| // 4. First series ¨C Group 1 | |
| // ------------------------------------------------------------ | |
| int series1 = bubbleChart.NSeries.Add("=Sheet1!$B$2:$B$4", true); | |
| bubbleChart.NSeries[series1].Values = "=Sheet1!$C$2:$C$4"; | |
| bubbleChart.NSeries[series1].BubbleSizes = "=Sheet1!$D$2:$D$4"; | |
| bubbleChart.NSeries[series1].Name = "Group 1"; | |
| // Apply custom formatting to series 1 bubbles | |
| bubbleChart.NSeries[series1].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| bubbleChart.NSeries[series1].Marker.MarkerSize = 12; // Size of marker (not bubble size) | |
| bubbleChart.NSeries[series1].Marker.Area.Formatting = FormattingType.Custom; | |
| bubbleChart.NSeries[series1].Marker.Area.ForegroundColor = Color.CornflowerBlue; | |
| bubbleChart.NSeries[series1].Marker.Border.IsVisible = true; | |
| bubbleChart.NSeries[series1].Marker.Border.Color = Color.DarkBlue; | |
| bubbleChart.NSeries[series1].Marker.Border.Weight = WeightType.WideLine; | |
| // ------------------------------------------------------------ | |
| // 5. Second series ¨C Group 2 (placed on secondary axis) | |
| // ------------------------------------------------------------ | |
| int series2 = bubbleChart.NSeries.Add("=Sheet1!$E$2:$E$4", true); | |
| bubbleChart.NSeries[series2].Values = "=Sheet1!$F$2:$F$4"; | |
| bubbleChart.NSeries[series2].BubbleSizes = "=Sheet1!$G$2:$G$4"; | |
| bubbleChart.NSeries[series2].Name = "Group 2"; | |
| bubbleChart.NSeries[series2].PlotOnSecondAxis = true; // Use secondary axis | |
| // Custom style for series 2 bubbles | |
| bubbleChart.NSeries[series2].Marker.MarkerStyle = ChartMarkerType.Square; | |
| bubbleChart.NSeries[series2].Marker.MarkerSize = 12; | |
| bubbleChart.NSeries[series2].Marker.Area.Formatting = FormattingType.Custom; | |
| bubbleChart.NSeries[series2].Marker.Area.ForegroundColor = Color.IndianRed; | |
| bubbleChart.NSeries[series2].Marker.Border.IsVisible = true; | |
| bubbleChart.NSeries[series2].Marker.Border.Color = Color.Maroon; | |
| bubbleChart.NSeries[series2].Marker.Border.Weight = WeightType.WideLine; | |
| // ------------------------------------------------------------ | |
| // 6. Axis titles and formatting (optional) | |
| // ------------------------------------------------------------ | |
| bubbleChart.CategoryAxis.Title.Text = "Sales"; | |
| bubbleChart.ValueAxis.Title.Text = "Profit"; | |
| bubbleChart.ValueAxis.MinValue = 0; | |
| bubbleChart.ValueAxis.MaxValue = 120; | |
| // Secondary axis ¨C make its title distinguishable | |
| bubbleChart.SecondValueAxis.Title.Text = "Profit (Group 2)"; | |
| bubbleChart.SecondValueAxis.Title.Font.Color = Color.IndianRed; | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook to XLSX and as PNG image (optional export) | |
| // ------------------------------------------------------------ | |
| wb.Save("BubbleChart_Customized.xlsx"); | |
| // Export the chart area as an image (requires Aspose.Cells rendering license) | |
| bubbleChart.ToImage("BubbleChart_Customized.png"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data for the bubble chart. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Product Sales Profit MarketShare | |
| // 2 Product A 120 30 0.20 | |
| // 3 Product B 150 45 0.35 | |
| // 4 Product C 180 55 0.50 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Product"); | |
| sheet.Cells["B1"].PutValue("Sales"); | |
| sheet.Cells["C1"].PutValue("Profit"); | |
| sheet.Cells["D1"].PutValue("MarketShare"); | |
| string[] products = { "Product A", "Product B", "Product C" }; | |
| double[] sales = { 120, 150, 180 }; | |
| double[] profit = { 30, 45, 55 }; | |
| double[] marketShare = { 0.20, 0.35, 0.50 }; | |
| for (int i = 0; i < products.Length; i++) | |
| { | |
| int row = i + 1; // Excel rows are 0?based in the API | |
| sheet.Cells[row, 0].PutValue(products[i]); // Column A | |
| sheet.Cells[row, 1].PutValue(sales[i]); // Column B | |
| sheet.Cells[row, 2].PutValue(profit[i]); // Column C | |
| sheet.Cells[row, 3].PutValue(marketShare[i]); // Column D | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Bubble chart to the worksheet. | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.Bubble, 5, 0, 25, 15); | |
| Chart bubbleChart = sheet.Charts[chartIndex]; | |
| bubbleChart.Title.Text = "Product Sales vs Profit (Bubble Size = Market Share)"; | |
| bubbleChart.ShowLegend = true; | |
| // ------------------------------------------------------------ | |
| // 4. Define the data series for the Bubble chart. | |
| // ------------------------------------------------------------ | |
| // Series 0 ¨C X values (Sales) ¨C Column B | |
| int seriesIdx = bubbleChart.NSeries.Add("=Sheet1!$B$2:$B$4", true); | |
| // Set Y values (Profit) ¨C Column C | |
| bubbleChart.NSeries[seriesIdx].Values = "=Sheet1!$C$2:$C$4"; | |
| // Set Bubble Sizes ¨C Column D (must be a positive number) | |
| bubbleChart.NSeries[seriesIdx].BubbleSizes = "=Sheet1!$D$2:$D$4"; | |
| bubbleChart.NSeries[seriesIdx].Name = "Products"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize axes (optional). | |
| // ------------------------------------------------------------ | |
| bubbleChart.CategoryAxis.Title.Text = "Sales"; | |
| bubbleChart.ValueAxis.Title.Text = "Profit"; | |
| bubbleChart.ValueAxis.MinValue = 0; | |
| bubbleChart.ValueAxis.MaxValue = 100; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook to an XLSX file. | |
| // ------------------------------------------------------------ | |
| workbook.Save("BubbleChart_Basic.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialize a new Workbook. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "QuarterlySales"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Quarter ProductA ProductB ProductC | |
| // 2 Q1 12000 15000 8000 | |
| // 3 Q2 18000 13000 9000 | |
| // 4 Q3 16000 17000 11000 | |
| // 5 Q4 20000 19000 14000 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Quarter"); | |
| sheet.Cells["B1"].PutValue("ProductA"); | |
| sheet.Cells["C1"].PutValue("ProductB"); | |
| sheet.Cells["D1"].PutValue("ProductC"); | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[,] sales = { | |
| { 12000, 15000, 8000 }, | |
| { 18000, 13000, 9000 }, | |
| { 16000, 17000, 11000 }, | |
| { 20000, 19000, 14000 } | |
| }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(quarters[i]); // Column A ¨C Quarter | |
| sheet.Cells[i + 1, 1].PutValue(sales[i, 0]); // Column B ¨C ProductA | |
| sheet.Cells[i + 1, 2].PutValue(sales[i, 1]); // Column C ¨C ProductB | |
| sheet.Cells[i + 1, 3].PutValue(sales[i, 2]); // Column D ¨C ProductC | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Column100PercentStacked chart. | |
| // ------------------------------------------------------------ | |
| // Chart position: rows 7?25, columns B?H (zero?based indices) | |
| int chartIndex = sheet.Charts.Add(ChartType.Column100PercentStacked, 6, 1, 25, 7); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales ¨C 100% Stacked Column"; | |
| // Define the data range for the series (B2:D5) | |
| // Category (X?axis) data will be taken automatically from A2:A5. | |
| chart.NSeries.Add("=QuarterlySales!$B$2:$D$5", true); | |
| chart.NSeries.CategoryData = "=QuarterlySales!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 4. Customize chart appearance. | |
| // ------------------------------------------------------------ | |
| // Show data labels as percentages | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowCategoryName = false; | |
| series.DataLabels.ShowSeriesName = false; | |
| series.DataLabels.ShowValue = false; | |
| series.DataLabels.ShowPercentage = true; | |
| } | |
| // Position the legend at the bottom | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set the primary value axis title | |
| chart.ValueAxis.Title.Text = "Percentage of Total Sales"; | |
| chart.PlotEmptyCellsType = PlotEmptyCellsType.Zero; | |
| chart.GapWidth = 50; // 0?100, default is 150 | |
| // ------------------------------------------------------------ | |
| // 5. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "Column100PercentStackedChart.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and get the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate worksheet with sample data. | |
| // The layout: | |
| // A B C D | |
| // 1 Quarter Product A Product B Product C | |
| // 2 Q1 120 80 50 | |
| // 3 Q2 150 95 70 | |
| // 4 Q3 180 110 90 | |
| // 5 Q4 200 130 110 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] prodA = { 120, 150, 180, 200 }; | |
| double[] prodB = { 80, 95, 110, 130 }; | |
| double[] prodC = { 50, 70, 90, 110 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| int row = i + 1; // Excel rows are 0?based in Aspose.Cells | |
| sheet.Cells[row, 0].Value = quarters[i]; | |
| sheet.Cells[row, 1].Value = prodA[i]; | |
| sheet.Cells[row, 2].Value = prodB[i]; | |
| sheet.Cells[row, 3].Value = prodC[i]; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Column3D100PercentStacked chart. | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.Column3D100PercentStacked, 7, 0, 25, 8); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales ¨C 100% Stacked 3D Column"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for each product. | |
| // The NSeries.Add method receives the data range and a flag | |
| // indicating whether the series name should be taken from the | |
| // first cell in the range (true = use first cell as name). | |
| // ----------------------------------------------------------------- | |
| // Series for Product A | |
| int index = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[index].Name = "Product A"; | |
| // Series for Product B | |
| index = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[index].Name = "Product B"; | |
| // Series for Product C | |
| index = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[index].Name = "Product C"; | |
| // Set category (X?axis) labels ¨C quarters | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 5. Optional: Customize axes and data labels. | |
| // ----------------------------------------------------------------- | |
| // Category (X) axis title | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| // Value (Y) axis title ¨C because the chart is 100% stacked, | |
| // the axis shows percentages. | |
| chart.ValueAxis.Title.Text = "Percentage of Total Sales"; | |
| // Show data labels as percentages | |
| foreach (var series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowPercentage = true; | |
| series.DataLabels.Position = LabelPositionType.Center; | |
| } | |
| // Legend placement | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Column3D100PercentStacked_Chart.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Chart created successfully and saved to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] productA = { 12000, 15000, 17000, 14000 }; | |
| double[] productB = { 10000, 13000, 16000, 11000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| int row = i + 1; // Excel rows are 0?based in API | |
| sheet.Cells[row, 0].Value = quarters[i]; | |
| sheet.Cells[row, 1].Value = productA[i]; | |
| sheet.Cells[row, 2].Value = productB[i]; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Column3D chart (base type) to the worksheet. | |
| // ----------------------------------------------------------------- | |
| // Parameters: ChartType, first row, first column, last row, last column | |
| int chartIndex = sheet.Charts.Add(ChartType.Column3D, 6, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales ¨C 3D Column Chart"; | |
| // ----------------------------------------------------------------- | |
| // 4. Create series for each product. | |
| // ----------------------------------------------------------------- | |
| // Series for Product A (primary axis) | |
| int seriesAIndex = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesAIndex].Name = "Product A"; | |
| // Series for Product B (primary axis) | |
| int seriesBIndex = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesBIndex].Name = "Product B"; | |
| // Assign category (X?axis) data ¨C quarters | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 5. Customize axes titles and appearance. | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Revenue (USD)"; | |
| // Optional: Apply a built?in chart style (e.g., style index 7) | |
| chart.Style = 7; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook in XLSX format. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Column3DChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); | |
| // ----------------------------------------------------------------- | |
| // 7. Export the chart as a PNG image (useful for reports/web). | |
| // ----------------------------------------------------------------- | |
| var imageOptions = new ImageOrPrintOptions | |
| { | |
| ImageType = ImageType.Png, | |
| HorizontalResolution = 96, | |
| VerticalResolution = 96 | |
| }; | |
| // Render the chart and write to file | |
| string imgPath = "Column3DChart.png"; | |
| chart.ToImage(imgPath, imageOptions); | |
| Console.WriteLine($"Chart image saved to {imgPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].PutValue("Quarter"); | |
| sheet.Cells["B1"].PutValue("Product A"); | |
| sheet.Cells["C1"].PutValue("Product B"); | |
| sheet.Cells["D1"].PutValue("Product C"); | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] prodA = { 12000, 15000, 13000, 17000 }; | |
| double[] prodB = { 10000, 14000, 11500, 16000 }; | |
| double[] prodC = { 9000, 12000, 11000, 15000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| int row = i + 1; // Excel rows are 0?based in the API | |
| sheet.Cells[row, 0].PutValue(quarters[i]); // Column A ¨C Quarter | |
| sheet.Cells[row, 1].PutValue(prodA[i]); // Column B ¨C Product A | |
| sheet.Cells[row, 2].PutValue(prodB[i]); // Column C ¨C Product B | |
| sheet.Cells[row, 3].PutValue(prodC[i]); // Column D ¨C Product C | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Column3DClustered chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: (type, upper-left row, upper-left column, lower-right row, lower-right column) | |
| int chartIndex = sheet.Charts.Add(ChartType.Column3DClustered, 6, 0, 26, 8); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales ¨C 3D Column Chart"; | |
| // ------------------------------------------------------------ | |
| // 4. Add series for each product. | |
| // ------------------------------------------------------------ | |
| // Series 0 ¨C Product A | |
| int series0 = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[series0].Name = "Product A"; | |
| // Series 1 ¨C Product B | |
| int series1 = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[series1].Name = "Product B"; | |
| // Series 2 ¨C Product C | |
| int series2 = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[series2].Name = "Product C"; | |
| // Category data (quarters) | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize chart appearance (optional). | |
| // ------------------------------------------------------------ | |
| // Set legend position | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| // Apply distinct colors to each series | |
| chart.NSeries[series0].Area.ForegroundColor = Color.FromArgb(0, 112, 192); // Blue | |
| chart.NSeries[series1].Area.ForegroundColor = Color.FromArgb(0, 176, 80); // Green | |
| chart.NSeries[series2].Area.ForegroundColor = Color.FromArgb(255, 192, 0); // Orange | |
| // Add a slight gap between columns for better readability | |
| chart.GapDepth = 1; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "Column3DClusteredChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and get the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] productA = { 12000, 15000, 17000, 13000 }; | |
| double[] productB = { 8000, 9000, 11000, 9500 }; | |
| double[] productC = { 6000, 7000, 8000, 7500 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| int row = i + 1; // Excel rows are 0?based in Aspose.Cells | |
| sheet.Cells[row, 0].Value = quarters[i]; | |
| sheet.Cells[row, 1].Value = productA[i]; | |
| sheet.Cells[row, 2].Value = productB[i]; | |
| sheet.Cells[row, 3].Value = productC[i]; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Column3DStacked chart. | |
| // ----------------------------------------------------------------- | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.Column3DStacked, 6, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales (3D Stacked)"; | |
| chart.Title.Font.Color = System.Drawing.Color.DarkBlue; | |
| chart.Title.Font.Size = 14; | |
| // ----------------------------------------------------------------- | |
| // 4. Add data series for each product. | |
| // ----------------------------------------------------------------- | |
| // Series 1 ¨C Product A | |
| chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[0].Name = "Product A"; | |
| // Series 2 ¨C Product B | |
| chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[1].Name = "Product B"; | |
| // Series 3 ¨C Product C | |
| chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[2].Name = "Product C"; | |
| // Set the category (X?axis) labels ¨C quarters | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 5. Customize axes and legend. | |
| // ----------------------------------------------------------------- | |
| // Category axis (X?axis) title | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.CategoryAxis.Title.Font.Size = 12; | |
| // Value axis (Y?axis) title | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| chart.ValueAxis.Title.Font.Size = 12; | |
| // Show legend at the bottom | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Optional: apply a preset style for better visual appeal | |
| chart.Style = 2; // Predefined chart style index | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Column3DStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Load the workbook that already contains a chart. | |
| var workbook = new Workbook("ColumnChart_Output.xlsx"); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // Assume the first chart is the one we want to modify. | |
| Chart chart = sheet.Charts[0]; | |
| // Change the chart type to Stacked Column. | |
| chart.Type = ChartType.ColumnStacked; | |
| // Add a new series for "Product C". | |
| // First, add data for Product C if it does not exist. | |
| double[] productC = { 900, 1300, 1500 }; | |
| for (int i = 0; i < productC.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 3].Value = productC[i]; // Column D (index 3) | |
| if (i == 0) sheet.Cells[0, 3].Value = "ProductC"; // Header | |
| } | |
| // Add the series referencing the new data. | |
| int newSeriesIdx = chart.NSeries.Add("=Sheet1!$D$2:$D$4", true); | |
| chart.NSeries[newSeriesIdx].Name = "Product C"; | |
| chart.NSeries[newSeriesIdx].Type = ChartType.ColumnStacked; | |
| // Optionally, adjust the legend position. | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // Save the updated workbook. | |
| workbook.Save("ColumnChart_Updated.xlsx"); | |
| Console.WriteLine("Chart updated and saved as ColumnChart_Updated.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample data. | |
| // ------------------------------------------------------------------ | |
| // A B C | |
| // 1 Month ProductA ProductB | |
| // 2 Jan 1200 800 | |
| // 3 Feb 1500 1100 | |
| // 4 Mar 1700 1300 | |
| // ------------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "ProductA"; | |
| sheet.Cells["C1"].Value = "ProductB"; | |
| string[] months = { "Jan", "Feb", "Mar" }; | |
| double[] productA = { 1200, 1500, 1700 }; | |
| double[] productB = { 800, 1100, 1300 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = months[i]; | |
| sheet.Cells[i + 1, 1].Value = productA[i]; | |
| sheet.Cells[i + 1, 2].Value = productB[i]; | |
| } | |
| // ------------------------------------------------------------------ | |
| // 3. Add a Column chart to the worksheet. | |
| // ------------------------------------------------------------------ | |
| // Parameters: type, upper-left row, upper-left column, lower-right row, lower-right column | |
| int chartIndex = sheet.Charts.Add(ChartType.Column, 5, 0, 20, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C Column Chart"; | |
| // ------------------------------------------------------------------ | |
| // 4. Add the first series (ProductA) ¨C primary axis. | |
| // ------------------------------------------------------------------ | |
| int seriesAIndex = chart.NSeries.Add("=SalesData!$B$2:$B$4", true); | |
| chart.NSeries[seriesAIndex].Name = "Product A"; | |
| chart.NSeries[seriesAIndex].Type = ChartType.Column; // explicit, though default for column chart | |
| // ------------------------------------------------------------------ | |
| // 5. Add the second series (ProductB) ¨C primary axis. | |
| // ------------------------------------------------------------------ | |
| int seriesBIndex = chart.NSeries.Add("=SalesData!$C$2:$C$4", true); | |
| chart.NSeries[seriesBIndex].Name = "Product B"; | |
| chart.NSeries[seriesBIndex].Type = ChartType.Column; | |
| // ------------------------------------------------------------------ | |
| // 6. Set the category (X) axis data. | |
| // ------------------------------------------------------------------ | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$4"; | |
| // ------------------------------------------------------------------ | |
| // 7. Customize axes titles (optional). | |
| // ------------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Sales (Units)"; | |
| // ------------------------------------------------------------------ | |
| // 8. Apply a built?in chart style for a nicer look. | |
| // ------------------------------------------------------------------ | |
| chart.Style = 11; // style index varies from 0?48 | |
| // ------------------------------------------------------------------ | |
| // 9. Save the workbook. | |
| // ------------------------------------------------------------------ | |
| string outputPath = "ColumnChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Column chart created successfully: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Insert sample data. | |
| // The layout follows the typical pattern required for a stacked chart: | |
| // | A | B | C | D | | |
| // | Month | Q1 | Q2 | Q3 | | |
| // | Jan | 120 | 80 | 50 | | |
| // | Feb | 150 | 95 | 65 | | |
| // | Mar | 180 | 110 | 85 | | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Month"); | |
| sheet.Cells["B1"].PutValue("Q1"); | |
| sheet.Cells["C1"].PutValue("Q2"); | |
| sheet.Cells["D1"].PutValue("Q3"); | |
| string[] months = { "Jan", "Feb", "Mar" }; | |
| double[] q1 = { 120, 150, 180 }; | |
| double[] q2 = { 80, 95, 110 }; | |
| double[] q3 = { 50, 65, 85 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(months[i]); // Column A ¨C Month | |
| sheet.Cells[i + 1, 1].PutValue(q1[i]); // Column B ¨C Q1 | |
| sheet.Cells[i + 1, 2].PutValue(q2[i]); // Column C ¨C Q2 | |
| sheet.Cells[i + 1, 3].PutValue(q3[i]); // Column D ¨C Q3 | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Column Stacked chart. | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.ColumnStacked, 6, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales (Stacked)"; | |
| // ------------------------------------------------------------ | |
| // 4. Define the series for each quarter. | |
| // The NSeries.Add method takes a data range and a boolean indicating | |
| // whether the series should use the first column as category (true). | |
| // ------------------------------------------------------------ | |
| // Q1 series | |
| int q1SeriesIdx = chart.NSeries.Add("=SalesData!$B$2:$B$4", true); | |
| chart.NSeries[q1SeriesIdx].Name = "Q1"; | |
| chart.NSeries[q1SeriesIdx].Area.ForegroundColor = Color.CornflowerBlue; | |
| // Q2 series | |
| int q2SeriesIdx = chart.NSeries.Add("=SalesData!$C$2:$C$4", true); | |
| chart.NSeries[q2SeriesIdx].Name = "Q2"; | |
| chart.NSeries[q2SeriesIdx].Area.ForegroundColor = Color.SeaGreen; | |
| // Q3 series | |
| int q3SeriesIdx = chart.NSeries.Add("=SalesData!$D$2:$D$4", true); | |
| chart.NSeries[q3SeriesIdx].Name = "Q3"; | |
| chart.NSeries[q3SeriesIdx].Area.ForegroundColor = Color.IndianRed; | |
| // ------------------------------------------------------------ | |
| // 5. Optional customizations. | |
| // ------------------------------------------------------------ | |
| // Set the category (X) axis title. | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| // Set the primary value (Y) axis title. | |
| chart.ValueAxis.Title.Text = "Sales (Units)"; | |
| // Position the legend at the top?right corner. | |
| chart.Legend.Position = LegendPositionType.Top; | |
| // Show data labels (the actual values) on each stacked segment. | |
| chart.NSeries[q1SeriesIdx].DataLabels.ShowValue = true; | |
| chart.NSeries[q2SeriesIdx].DataLabels.ShowValue = true; | |
| chart.NSeries[q3SeriesIdx].DataLabels.ShowValue = true; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "ColumnStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // The layout is: | |
| // A B C D | |
| // 1 Region ProductA ProductB ProductC | |
| // 2 North 30 20 50 | |
| // 3 South 40 30 30 | |
| // 4 East 20 40 40 | |
| // 5 West 10 50 40 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].PutValue("Region"); | |
| sheet.Cells["B1"].PutValue("ProductA"); | |
| sheet.Cells["C1"].PutValue("ProductB"); | |
| sheet.Cells["D1"].PutValue("ProductC"); | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] values = { | |
| { 30, 20, 50 }, | |
| { 40, 30, 30 }, | |
| { 20, 40, 40 }, | |
| { 10, 50, 40 } | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(regions[i]); // Column A ¨C Region | |
| sheet.Cells[i + 1, 1].PutValue(values[i, 0]); // Column B ¨C ProductA | |
| sheet.Cells[i + 1, 2].PutValue(values[i, 1]); // Column C ¨C ProductB | |
| sheet.Cells[i + 1, 3].PutValue(values[i, 2]); // Column D ¨C ProductC | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Cone100PercentStacked chart. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.Cone100PercentStacked, 7, 0, 25, 12); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title. | |
| chart.Title.Text = "Regional Sales Distribution (100?% Stacked Cone)"; | |
| // Define the data range for the chart. | |
| // Series data: B2:D5 (product values) | |
| // Category data: A2:A5 (region names) | |
| chart.NSeries.Add("=SalesData!$B$2:$D$5", true); | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // Optional: rename series to improve legend readability. | |
| chart.NSeries[0].Name = "Product A"; | |
| chart.NSeries[1].Name = "Product B"; | |
| chart.NSeries[2].Name = "Product C"; | |
| // ----------------------------------------------------------------- | |
| // 4. Customize axes titles (optional but recommended for clarity). | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Percentage of Total Sales"; | |
| // Because the chart is 100?% stacked, the primary value axis will | |
| // automatically be formatted as a percentage. | |
| chart.ValueAxis.TickLabels.NumberFormat = "0%"; | |
| // ----------------------------------------------------------------- | |
| // 5. Configure the legend (place it at the bottom of the chart). | |
| // ----------------------------------------------------------------- | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Cone100PercentStackedChart.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Quarter Sales | |
| // 2 Q1 15000 | |
| // 3 Q2 21000 | |
| // 4 Q3 18000 | |
| // 5 Q4 24000 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Quarter"); | |
| sheet.Cells["B1"].PutValue("Sales"); | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] sales = { 15000, 21000, 18000, 24000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(quarters[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(sales[i]); // Column B | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Cone chart (2?D) to the worksheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column | |
| int chartIdx = sheet.Charts.Add(ChartType.Cone, 7, 0, 23, 10); | |
| Chart coneChart = sheet.Charts[chartIdx]; | |
| coneChart.Title.Text = "Quarterly Sales (Cone Chart)"; | |
| // ------------------------------------------------------------ | |
| // 4. Add the data series. | |
| // ------------------------------------------------------------ | |
| // First argument: data range, second argument: is vertical (true for column series) | |
| int seriesIdx = coneChart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| coneChart.NSeries[seriesIdx].Name = "Sales"; | |
| // Optional: set category (X?axis) data | |
| coneChart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize axes and appearance (optional but recommended) | |
| // ------------------------------------------------------------ | |
| coneChart.CategoryAxis.Title.Text = "Quarter"; | |
| coneChart.ValueAxis.Title.Text = "Sales (USD)"; | |
| coneChart.ValueAxis.TickLabels.NumberFormat = "$#,##0"; | |
| // Set the fill color of the cone for better visual appeal | |
| coneChart.NSeries[seriesIdx].Area.Formatting = FormattingType.Custom; | |
| coneChart.NSeries[seriesIdx].Area.ForegroundColor = Color.CornflowerBlue; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outPath = "ConeChart_Basic.xlsx"; | |
| workbook.Save(outPath); | |
| Console.WriteLine($"Workbook saved to {outPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------------ | |
| // 2. Populate the worksheet with sample data. | |
| // The data represents quarterly sales for three product lines. | |
| // ------------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[,] sales = { | |
| { 15000, 12000, 8000 }, | |
| { 18000, 14000, 9500 }, | |
| { 21000, 16000, 11000 }, | |
| { 24000, 19000, 13000 } | |
| }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| // Column A ¨C Quarter name | |
| sheet.Cells[i + 1, 0].Value = quarters[i]; | |
| // Columns B?D ¨C Sales figures | |
| for (int j = 0; j < 3; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].Value = sales[i, j]; | |
| } | |
| } | |
| // ------------------------------------------------------------------ | |
| // 3. Add a Cone Stacked chart. | |
| // The chart will be placed starting at row 7, column 0 (G1) and | |
| // will occupy a rectangle that spans 15 rows and 10 columns. | |
| // ------------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.ConeStacked, 6, 0, 21, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set a descriptive title | |
| chart.Title.Text = "Quarterly Sales ¨C Cone Stacked Chart"; | |
| // ------------------------------------------------------------------ | |
| // 4. Add series for each product. | |
| // The first parameter is the data range for the series values. | |
| // The second parameter (true) indicates that the series name | |
| // should be taken from the first row of the range. | |
| // ------------------------------------------------------------------ | |
| // Product A (range B2:B5) | |
| int seriesA = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesA].Name = "Product A"; | |
| // Product B (range C2:C5) | |
| int seriesB = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesB].Name = "Product B"; | |
| // Product C (range D2:D5) | |
| int seriesC = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[seriesC].Name = "Product C"; | |
| // Assign category (X?axis) data ¨C the quarters. | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------------ | |
| // 5. Optional customizations | |
| // ------------------------------------------------------------------ | |
| // Show legend at the right side of the chart | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| // Set a background color for the plot area (light gray) | |
| chart.PlotArea.Area.ForegroundColor = Color.FromArgb(240, 240, 240); | |
| // ------------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------------ | |
| string outputPath = "ConeStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook successfully saved to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Load the workbook created in the previous example. | |
| var workbook = new Workbook("ConicalBar100PercentStackedChart.xlsx"); | |
| var sheet = workbook.Worksheets[0]; | |
| // Assume the chart we added is the first chart in the collection. | |
| Chart chart = sheet.Charts[0]; | |
| // 1. Change the chart title. | |
| chart.Title.Text = "Updated Quarterly Sales (Stacked 100%)"; | |
| // 2. Modify the series colors for better contrast. | |
| // Q1 ¨C DarkBlue, Q2 ¨C DarkGreen, Q3 ¨C DarkRed | |
| chart.NSeries[0].Area.ForegroundColor = Color.DarkBlue; | |
| chart.NSeries[1].Area.ForegroundColor = Color.DarkGreen; | |
| chart.NSeries[2].Area.ForegroundColor = Color.DarkRed; | |
| // 3. Move the legend to the right side of the chart. | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // 4. Add a data label to display the percentage on each segment. | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.NumberFormat = "0%"; | |
| series.DataLabels.Position = LabelPositionType.Center; | |
| } | |
| // Save the modified workbook. | |
| string updatedPath = "ConicalBar100PercentStackedChart_Updated.xlsx"; | |
| workbook.Save(updatedPath); | |
| Console.WriteLine($"Updated workbook saved to {updatedPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate the worksheet with sample data. | |
| // The data layout is: | |
| // | A | B | C | D | | |
| // |-------|-------|-------|-------| | |
| // |Month | Q1 | Q2 | Q3 | | |
| // |Jan | 30 | 50 | 20 | | |
| // |Feb | 40 | 35 | 25 | | |
| // |Mar | 45 | 55 | 30 | | |
| // ------------------------------------------------------------ | |
| string[] months = { "Jan", "Feb", "Mar" }; | |
| double[,] values = { | |
| { 30, 50, 20 }, | |
| { 40, 35, 25 }, | |
| { 45, 55, 30 } | |
| }; | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = months[i]; | |
| sheet.Cells[i + 1, 1].PutValue(values[i, 0]); | |
| sheet.Cells[i + 1, 2].PutValue(values[i, 1]); | |
| sheet.Cells[i + 1, 3].PutValue(values[i, 2]); | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a ConicalBar 100% Stacked chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.ConicalBar100PercentStacked, 5, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales Distribution (100% Stacked)"; | |
| // ------------------------------------------------------------ | |
| // 4. Add series for each quarter. | |
| // ------------------------------------------------------------ | |
| // Series are added using an Excel?style formula. | |
| // "=Sheet1!$B$2:$B$4" refers to Q1 values for all months, etc. | |
| chart.NSeries.Add("=SalesData!$B$2:$B$4", true); | |
| chart.NSeries.Add("=SalesData!$C$2:$C$4", true); | |
| chart.NSeries.Add("=SalesData!$D$2:$D$4", true); | |
| // Assign series names from the header row. | |
| chart.NSeries[0].Name = "Q1"; | |
| chart.NSeries[1].Name = "Q2"; | |
| chart.NSeries[2].Name = "Q3"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize axes and appearance. | |
| // ------------------------------------------------------------ | |
| // Category (X) axis ¨C months. | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| // Value (Y) axis ¨C percentage (0?100%). The chart automatically | |
| // normalizes the data because it is a 100% stacked chart. | |
| chart.ValueAxis.Title.Text = "Percentage"; | |
| chart.ValueAxis.TickLabels.NumberFormat = "0%"; | |
| // Optional: Apply a built?in style for a modern look. | |
| chart.Style = 13; // Predefined style index. | |
| // Optional: Remove the legend if it clutters the view. | |
| // chart.ShowLegend = false; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "ConicalBar100PercentStackedChart.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 2. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 3. Populate the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] productA = { 15000, 21000, 18000, 24000 }; | |
| double[] productB = { 12000, 19000, 16000, 22000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = quarters[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = productA[i]; // Column B | |
| sheet.Cells[i + 1, 2].Value = productB[i]; // Column C | |
| } | |
| // ------------------------------------------------------------ | |
| // 4. Add a ConicalBar chart (base type) to the worksheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, first row, first column, last row, last column | |
| int chartIndex = sheet.Charts.Add(ChartType.ConicalBar, 6, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C ConicalBar Chart"; | |
| // ------------------------------------------------------------ | |
| // 5. Add the first series (Product A) ¨C uses the default (primary) axis. | |
| // ------------------------------------------------------------ | |
| int seriesAIndex = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesAIndex].Name = "Product A"; | |
| // ------------------------------------------------------------ | |
| // 6. Add the second series (Product B) ¨C also on the primary axis. | |
| // ------------------------------------------------------------ | |
| int seriesBIndex = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesBIndex].Name = "Product B"; | |
| // ------------------------------------------------------------ | |
| // 7. Set the category (X?axis) data ¨C the quarters. | |
| // ------------------------------------------------------------ | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 8. Optional: customize axes titles and appearance. | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| chart.ValueAxis.TickLabels.NumberFormat = "\"$\"#,##0"; | |
| // Change series colors for better visual distinction. | |
| chart.NSeries[seriesAIndex].Area.ForegroundColor = Color.CornflowerBlue; | |
| chart.NSeries[seriesBIndex].Area.ForegroundColor = Color.OrangeRed; | |
| // ------------------------------------------------------------ | |
| // 9. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "ConicalBarChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Load the workbook created in Example?1. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook("ConicalBarStacked_Chart.xlsx"); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Retrieve the first chart (our Conical Bar Stacked chart). | |
| // ------------------------------------------------------------ | |
| Chart chart = sheet.Charts[0]; | |
| // ------------------------------------------------------------ | |
| // 3. Change the color of each series to custom shades. | |
| // ------------------------------------------------------------ | |
| chart.NSeries[0].Area.ForegroundColor = Color.FromArgb(92, 184, 92); // Q1 ¨C green | |
| chart.NSeries[1].Area.ForegroundColor = Color.FromArgb(240, 173, 78); // Q2 ¨C orange | |
| chart.NSeries[2].Area.ForegroundColor = Color.FromArgb(91, 192, 222); // Q3 ¨C blue | |
| // ------------------------------------------------------------ | |
| // 4. Add data labels to show exact values on each segment. | |
| // ------------------------------------------------------------ | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.Position = LabelPositionType.InsideEnd; | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.Font.Size = 9; | |
| } | |
| // ------------------------------------------------------------ | |
| // 5. Save the updated workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save("ConicalBarStacked_Chart_Updated.xlsx"); | |
| // ------------------------------------------------------------ | |
| // 6. Export the workbook to a PDF. | |
| // ------------------------------------------------------------ | |
| workbook.Save("ConicalBarStacked_Chart.pdf", SaveFormat.Pdf); | |
| Console.WriteLine("Chart updated and exported to PDF successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Region Q1 Q2 Q3 | |
| // 2 North 120 150 130 | |
| // 3 South 100 110 115 | |
| // 4 East 140 135 150 | |
| // 5 West 130 125 140 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Region"); | |
| sheet.Cells["B1"].PutValue("Q1"); | |
| sheet.Cells["C1"].PutValue("Q2"); | |
| sheet.Cells["D1"].PutValue("Q3"); | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] values = { | |
| { 120, 150, 130 }, | |
| { 100, 110, 115 }, | |
| { 140, 135, 150 }, | |
| { 130, 125, 140 } | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(regions[i]); // Region name | |
| sheet.Cells[i + 1, 1].PutValue(values[i, 0]); // Q1 | |
| sheet.Cells[i + 1, 2].PutValue(values[i, 1]); // Q2 | |
| sheet.Cells[i + 1, 3].PutValue(values[i, 2]); // Q3 | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Conical Bar Stacked chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: (ChartType, upper-left row, upper-left column, | |
| // lower-right row, lower-right column) | |
| int chartIndex = sheet.Charts.Add(ChartType.ConicalBarStacked, 6, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales by Region"; | |
| // ------------------------------------------------------------ | |
| // 4. Add series for each quarter. | |
| // ------------------------------------------------------------ | |
| // Series order: Q1, Q2, Q3 (stacked on top of each other) | |
| int index = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[index].Name = "Q1"; | |
| index = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[index].Name = "Q2"; | |
| index = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[index].Name = "Q3"; | |
| // ------------------------------------------------------------ | |
| // 5. Configure axes (optional but improves readability). | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Revenue (k)"; | |
| chart.ValueAxis.TickLabelPosition = TickLabelPositionType.Low; | |
| chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.Low; | |
| // ------------------------------------------------------------ | |
| // 6. Apply a predefined style for better appearance. | |
| // ------------------------------------------------------------ | |
| chart.Style = 9; // Choose any built?in style you like. | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook to XLSX and PNG formats. | |
| // ------------------------------------------------------------ | |
| workbook.Save("ConicalBarStacked_Chart.xlsx"); | |
| workbook.Save("ConicalBarStacked_Chart.png", SaveFormat.Png); | |
| Console.WriteLine("Workbook and chart created successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 02. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 03. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C | |
| // 1 Quarter Q1 Sales Q2 Sales | |
| // 2 Q1 15000 18000 | |
| // 3 Q2 21000 24000 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "2024 Q1"; | |
| sheet.Cells["C1"].Value = "2024 Q2"; | |
| string[] quarters = { "Q1", "Q2" }; | |
| double[] salesQ1 = { 15000, 21000 }; | |
| double[] salesQ2 = { 18000, 24000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = quarters[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = salesQ1[i]; // Column B | |
| sheet.Cells[i + 1, 2].Value = salesQ2[i]; // Column C | |
| } | |
| // ------------------------------------------------------------ | |
| // 04. Add a ConicalColumn3D chart. | |
| // Parameters: chart type, upper left row, upper left column, | |
| // lower right row, lower right column. | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add( | |
| ChartType.ConicalColumn3D, // Desired 3?D conical column chart | |
| 5, // Upper left row | |
| 0, // Upper left column | |
| 25, // Lower right row | |
| 10 // Lower right column | |
| ); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales Comparison (2024)"; | |
| // ------------------------------------------------------------ | |
| // 05. Add the first series (2024 Q1) ¨C remains the default column type. | |
| // ------------------------------------------------------------ | |
| int series1Idx = chart.NSeries.Add("=SalesData!$B$2:$B$3", true); | |
| chart.NSeries[series1Idx].Name = "2024 Q1"; | |
| // ------------------------------------------------------------ | |
| // 06. Add the second series (2024 Q2) ¨C also a column series. | |
| // ------------------------------------------------------------ | |
| int series2Idx = chart.NSeries.Add("=SalesData!$C$2:$C$3", true); | |
| chart.NSeries[series2Idx].Name = "2024 Q2"; | |
| // ------------------------------------------------------------ | |
| // 07. Optional customizations | |
| // ------------------------------------------------------------ | |
| // Set category axis (X?axis) title. | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| // Set primary value axis (Y?axis) title. | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| // Apply different fill colors to each series. | |
| chart.NSeries[series1Idx].Area.ForegroundColor = Color.CornflowerBlue; | |
| chart.NSeries[series2Idx].Area.ForegroundColor = Color.IndianRed; | |
| // Turn off the chart legend if not needed. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // ------------------------------------------------------------ | |
| // 08. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "ConicalColumn3D_Chart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Workbook wb = new Workbook(); | |
| Worksheet ws = wb.Worksheets[0]; | |
| Cells c = ws.Cells; | |
| // ----- Populate data ----- | |
| c["A1"].PutValue("Quarter"); | |
| c["B1"].PutValue("Revenue"); | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] revenue = { 12000, 15000, 9000, 18000 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| c[i + 1, 0].PutValue(quarters[i]); // Column A | |
| c[i + 1, 1].PutValue(revenue[i]); // Column B | |
| } | |
| // ----- Add Pie3D chart ----- | |
| int idx = ws.Charts.Add(ChartType.Pie3D, 6, 0, 22, 9); | |
| Chart chart = ws.Charts[idx]; | |
| chart.Title.Text = "Quarterly Revenue (3?D)"; | |
| chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ----- Customize slices ----- | |
| int[] explodePercentages = { 10, 0, 15, 0 }; // Percent distance for each slice | |
| Color[] sliceColors = { Color.CornflowerBlue, Color.Orange, Color.LightGreen, Color.Goldenrod }; | |
| for (int i = 0; i < chart.NSeries[0].Points.Count; i++) | |
| { | |
| // Set explosion distance (in percentage of the radius) | |
| chart.NSeries[0].Points[i].Explosion = explodePercentages[i]; | |
| // Apply custom fill color | |
| chart.NSeries[0].Points[i].Area.ForegroundColor = sliceColors[i]; | |
| // Show percentage label for each slice | |
| chart.NSeries[0].Points[i].DataLabels.ShowPercentage = true; | |
| chart.NSeries[0].Points[i].DataLabels.ShowCategoryName = true; | |
| } | |
| // ----- Save result ----- | |
| string file = "CustomizedPie3DChart.xlsx"; | |
| wb.Save(file); | |
| Console.WriteLine($"Customized chart saved to {file}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ---------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ---------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ---------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // ---------------------------------------------------------------- | |
| // A B C D | |
| // 1 Quarter ProductA ProductB ProductC | |
| // 2 Q1 120 80 100 | |
| // 3 Q2 150 70 130 | |
| // 4 Q3 170 90 110 | |
| // 5 Q4 200 110 150 | |
| // ---------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Quarter"; | |
| sheet.Cells["B1"].Value = "ProductA"; | |
| sheet.Cells["C1"].Value = "ProductB"; | |
| sheet.Cells["D1"].Value = "ProductC"; | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] productA = { 120, 150, 170, 200 }; | |
| double[] productB = { 80, 70, 90, 110 }; | |
| double[] productC = { 100, 130, 110, 150 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = quarters[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = productA[i]; // Column B | |
| sheet.Cells[i + 1, 2].Value = productB[i]; // Column C | |
| sheet.Cells[i + 1, 3].Value = productC[i]; // Column D | |
| } | |
| // ---------------------------------------------------------------- | |
| // 3. Add a Cylinder100PercentStacked chart. | |
| // ---------------------------------------------------------------- | |
| // The chart will be placed from row 7, column 0 to row 25, column 10. | |
| int chartIndex = sheet.Charts.Add(ChartType.Cylinder100PercentStacked, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set a meaningful title. | |
| chart.Title.Text = "Quarterly Sales ¨C 100% Stacked Cylinder Chart"; | |
| // ---------------------------------------------------------------- | |
| // 4. Define the series for each product. | |
| // ---------------------------------------------------------------- | |
| // Series 1 ¨C ProductA | |
| int seriesA = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[seriesA].Name = "ProductA"; | |
| // Series 2 ¨C ProductB | |
| int seriesB = chart.NSeries.Add("=Sheet1!$C$2:$C$5", true); | |
| chart.NSeries[seriesB].Name = "ProductB"; | |
| // Series 3 ¨C ProductC | |
| int seriesC = chart.NSeries.Add("=Sheet1!$D$2:$D$5", true); | |
| chart.NSeries[seriesC].Name = "ProductC"; | |
| // Set the category (X?axis) data ¨C the quarters. | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ---------------------------------------------------------------- | |
| // 5. Customize chart appearance (optional but recommended). | |
| // ---------------------------------------------------------------- | |
| // Legend at the bottom. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Axes titles. | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (Percent)"; | |
| // Optional: change the color of each series for better visual distinction. | |
| chart.NSeries[seriesA].Area.ForegroundColor = Color.SteelBlue; | |
| chart.NSeries[seriesB].Area.ForegroundColor = Color.Orange; | |
| chart.NSeries[seriesC].Area.ForegroundColor = Color.MediumSeaGreen; | |
| // ---------------------------------------------------------------- | |
| // 6. Save the workbook to disk. | |
| // ---------------------------------------------------------------- | |
| string outputPath = "Cylinder100PercentStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Cylinder100PercentStacked chart created successfully: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Value"; | |
| // Sample data | |
| string[] categories = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] values = { 15000, 23000, 18000, 26000 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = values[i]; // Column B | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Cylinder chart (3?D) to the worksheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column | |
| int chartIndex = sheet.Charts.Add(ChartType.Cylinder, 5, 0, 20, 10); | |
| Chart cylinderChart = sheet.Charts[chartIndex]; | |
| // Chart title | |
| cylinderChart.Title.Text = "Quarterly Sales ¨C Cylinder Chart"; | |
| // ------------------------------------------------------------ | |
| // 4. Create the data series. | |
| // ------------------------------------------------------------ | |
| // Add the series and bind it to the range B2:B5 (values) | |
| int seriesIndex = cylinderChart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| cylinderChart.NSeries[seriesIndex].Name = "Sales"; | |
| cylinderChart.NSeries[seriesIndex].Type = ChartType.Cylinder; // Explicitly set type | |
| // Category (X?axis) labels ¨C A2:A5 | |
| cylinderChart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize axes titles (optional but recommended). | |
| // ------------------------------------------------------------ | |
| cylinderChart.CategoryAxis.Title.Text = "Quarter"; | |
| cylinderChart.ValueAxis.Title.Text = "Revenue (USD)"; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "CylinderChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Cylinder chart saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and get the first worksheet. | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Quarter Product A Product B Product C | |
| // 2 Q1 120 80 45 | |
| // 3 Q2 150 95 60 | |
| // 4 Q3 130 110 70 | |
| // 5 Q4 170 130 85 | |
| // ------------------------------------------------------------ | |
| string[] headers = { "Quarter", "Product A", "Product B", "Product C" }; | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[,] values = { | |
| { 120, 80, 45 }, | |
| { 150, 95, 60 }, | |
| { 130, 110, 70 }, | |
| { 170, 130, 85 } | |
| }; | |
| // Write headers | |
| for (int c = 0; c < headers.Length; c++) | |
| cells[0, c].PutValue(headers[c]); | |
| // Write data rows | |
| for (int r = 0; r < quarters.Length; r++) | |
| { | |
| cells[r + 1, 0].PutValue(quarters[r]); // Category (Quarter) | |
| for (int c = 0; c < values.GetLength(1); c++) | |
| { | |
| cells[r + 1, c + 1].PutValue(values[r, c]); | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a CylinderStacked chart (3?D stacked cylinder columns) | |
| // ------------------------------------------------------------ | |
| // Chart will be placed from row 7, column 0 to row 25, column 9. | |
| int chartIdx = sheet.Charts.Add(ChartType.CylinderStacked, 7, 0, 25, 9); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| // ------------------------------------------------------------ | |
| // 4. Set data range and category axis | |
| // ------------------------------------------------------------ | |
| // Data range includes headers and all data cells. | |
| chart.SetChartDataRange("A1:D5", true); | |
| // Category (X?axis) data ¨C quarters (A2:A5) | |
| chart.NSeries.CategoryData = "A2:A5"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize chart appearance | |
| // ------------------------------------------------------------ | |
| chart.Title.Text = "Quarterly Sales ¨C CylinderStacked Chart"; | |
| chart.Title.IsVisible = true; | |
| // Show legend at the bottom | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales Amount"; | |
| // Optional: Apply a built?in style for a clean look | |
| chart.Style = 8; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook | |
| // ------------------------------------------------------------ | |
| string outPath = "CylinderStackedChart_Output.xlsx"; | |
| workbook.Save(outPath); | |
| Console.WriteLine($"Workbook saved to {outPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| cells["A1"].PutValue("Quarter"); | |
| cells["B1"].PutValue("Product A"); | |
| cells["C1"].PutValue("Product B"); | |
| cells["D1"].PutValue("Product C"); | |
| // Data rows | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[,] sales = { | |
| { 15000, 12000, 8000 }, | |
| { 18000, 14000, 9000 }, | |
| { 16000, 13000, 9500 }, | |
| { 20000, 15000, 11000 } | |
| }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| cells[i + 1, 0].PutValue(quarters[i]); // Column A ¨C Quarter | |
| cells[i + 1, 1].PutValue(sales[i, 0]); // Column B ¨C Product A | |
| cells[i + 1, 2].PutValue(sales[i, 1]); // Column C ¨C Product B | |
| cells[i + 1, 3].PutValue(sales[i, 2]); // Column D ¨C Product C | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a CylindricalBar100PercentStacked chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column (in pixel units) | |
| int chartIndex = sheet.Charts.Add(ChartType.CylindricalBar100PercentStacked, 6, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales ¨C 100?% Stacked Cylindrical Bar"; | |
| // ------------------------------------------------------------ | |
| // 4. Add series ¨C each product line becomes a series. | |
| // ------------------------------------------------------------ | |
| // Series for Product A | |
| int seriesA = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[seriesA].Name = "Product A"; | |
| // Series for Product B | |
| int seriesB = chart.NSeries.Add("=Sheet1!$C$2:$C$5", true); | |
| chart.NSeries[seriesB].Name = "Product B"; | |
| // Series for Product C | |
| int seriesC = chart.NSeries.Add("=Sheet1!$D$2:$D$5", true); | |
| chart.NSeries[seriesC].Name = "Product C"; | |
| // Category (X) axis uses the Quarter column | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 5. Optional formatting ¨C 3?D rotation and series colors. | |
| // ------------------------------------------------------------ | |
| // Rotate the chart to give a better 3?D perspective | |
| chart.RotationAngle = 30; | |
| // Assign custom colors (optional) | |
| chart.NSeries[seriesA].Area.ForegroundColor = System.Drawing.Color.FromArgb(91, 155, 213); // Blue | |
| chart.NSeries[seriesB].Area.ForegroundColor = System.Drawing.Color.FromArgb(237, 125, 49); // Orange | |
| chart.NSeries[seriesC].Area.ForegroundColor = System.Drawing.Color.FromArgb(165, 165, 165); // Gray | |
| // Axis titles (helpful for end users) | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Sales (100?% Stacked)"; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "CylindricalBar100PercentStacked_Chart.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to \"{outputPath}\""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Region Q1 Q2 Q3 | |
| // 2 North 12000 15000 13000 | |
| // 3 South 10000 11500 10800 | |
| // 4 East 9500 12300 11000 | |
| // 5 West 10500 14000 12500 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] sales = { | |
| {12000, 15000, 13000}, | |
| {10000, 11500, 10800}, | |
| { 9500, 12300, 11000}, | |
| {10500, 14000, 12500} | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = regions[i]; // Column A | |
| sheet.Cells[i + 1, 1].PutValue(sales[i, 0]); // Column B | |
| sheet.Cells[i + 1, 2].PutValue(sales[i, 1]); // Column C | |
| sheet.Cells[i + 1, 3].PutValue(sales[i, 2]); // Column D | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Cylindrical Bar chart. | |
| // ------------------------------------------------------------ | |
| // The chart is placed between rows 7?25 and columns 0?8. | |
| int chartIndex = sheet.Charts.Add(ChartType.CylindricalBar, 7, 0, 25, 8); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set a meaningful title. | |
| chart.Title.Text = "Quarterly Sales by Region (Cylindrical Bar)"; | |
| // ------------------------------------------------------------ | |
| // 4. Add series for each quarter. | |
| // ------------------------------------------------------------ | |
| // Series 1 ¨C Q1 | |
| int q1Series = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[q1Series].Name = "Q1"; | |
| // Series 2 ¨C Q2 | |
| int q2Series = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[q2Series].Name = "Q2"; | |
| // Series 3 ¨C Q3 | |
| int q3Series = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[q3Series].Name = "Q3"; | |
| // Use the region names as category (X?axis) data. | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize chart appearance (optional but recommended). | |
| // ------------------------------------------------------------ | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| // Legend placement | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| chart.ShowLegend = true; | |
| // Apply distinct colors to each series. | |
| chart.NSeries[q1Series].Area.ForegroundColor = Color.CornflowerBlue; | |
| chart.NSeries[q2Series].Area.ForegroundColor = Color.MediumSeaGreen; | |
| chart.NSeries[q3Series].Area.ForegroundColor = Color.Salmon; | |
| // Add a 3?D rotation for better visual effect. | |
| chart.RotationAngle = 30; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "CylindricalBarChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Instantiate a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample hierarchical sales data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Region ProductA ProductB ProductC | |
| // 2 North 120 80 50 | |
| // 3 South 90 70 40 | |
| // 4 East 100 60 30 | |
| // 5 West 80 90 20 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "ProductA"; | |
| sheet.Cells["C1"].Value = "ProductB"; | |
| sheet.Cells["D1"].Value = "ProductC"; | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[] productA = { 120, 90, 100, 80 }; | |
| double[] productB = { 80, 70, 60, 90 }; | |
| double[] productC = { 50, 40, 30, 20 }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = regions[i]; // Column A ¨C Region | |
| sheet.Cells[i + 1, 1].Value = productA[i]; // Column B ¨C ProductA | |
| sheet.Cells[i + 1, 2].Value = productB[i]; // Column C ¨C ProductB | |
| sheet.Cells[i + 1, 3].Value = productC[i]; // Column D ¨C ProductC | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Cylindrical Bar Stacked chart (base type) to the sheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.CylindricalBarStacked, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C Stacked Cylindrical Bar"; | |
| // ------------------------------------------------------------ | |
| // 4. Define series for each product. | |
| // ------------------------------------------------------------ | |
| // Series 0 ¨C ProductA | |
| int series0 = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[series0].Name = "ProductA"; | |
| // Series 1 ¨C ProductB | |
| int series1 = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[series1].Name = "ProductB"; | |
| // Series 2 ¨C ProductC | |
| int series2 = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[series2].Name = "ProductC"; | |
| // ------------------------------------------------------------ | |
| // 5. Set the category (X) axis data ¨C the region names. | |
| // ------------------------------------------------------------ | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 6. Optional: Customize the look of the chart. | |
| // ------------------------------------------------------------ | |
| // 6.1 Chart border | |
| chart.ChartArea.Border.IsVisible = true; | |
| chart.ChartArea.Border.Color = System.Drawing.Color.DarkSlateGray; | |
| chart.ChartArea.Border.Weight = WeightType.WideLine; | |
| // 6.2 Plot area ¨C make background transparent | |
| chart.PlotArea.Area.Formatting = FormattingType.None; | |
| // 6.3 Legend position | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // 6.4 Axes titles | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Sales (Units)"; | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "CylindricalBarStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ---------------------------------------------------------------- | |
| // 1. Initialize a new Workbook and obtain the first worksheet. | |
| // ---------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ---------------------------------------------------------------- | |
| // 2. Fill sample data (Months vs. Quarterly Sales). | |
| // ---------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].PutValue("Month"); | |
| sheet.Cells["B1"].PutValue("Q1"); | |
| sheet.Cells["C1"].PutValue("Q2"); | |
| sheet.Cells["D1"].PutValue("Q3"); | |
| sheet.Cells["E1"].PutValue("Q4"); | |
| // Data rows | |
| string[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun" }; | |
| double[,] sales = { | |
| { 15000, 18000, 21000, 24000 }, | |
| { 16000, 19000, 22000, 25000 }, | |
| { 17000, 20000, 23000, 26000 }, | |
| { 18000, 21000, 24000, 27000 }, | |
| { 19000, 22000, 25000, 28000 }, | |
| { 20000, 23000, 26000, 29000 } | |
| }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(months[i]); // Column A ¨C Month | |
| for (int q = 0; q < 4; q++) | |
| { | |
| sheet.Cells[i + 1, q + 1].PutValue(sales[i, q]); // Columns B?E ¨C Quarterly values | |
| } | |
| } | |
| // ---------------------------------------------------------------- | |
| // 3. Add a CylindricalColumn3D chart object. | |
| // Parameters: Chart type, upper?left row, column, lower?right row, column. | |
| // ---------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.CylindricalColumn3D, 8, 0, 28, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // ---------------------------------------------------------------- | |
| // 4. Define the data source for the chart. | |
| // The first series (Q1) will be the primary series; remaining quarters | |
| // are added automatically when the data range includes them. | |
| // ---------------------------------------------------------------- | |
| chart.SetChartDataRange("A1:E7", true); | |
| chart.NSeries.CategoryData = "Sheet1!$A$2:$A$7"; // Months as categories | |
| // Rename each series for clarity. | |
| chart.NSeries[0].Name = "Q1"; | |
| chart.NSeries[1].Name = "Q2"; | |
| chart.NSeries[2].Name = "Q3"; | |
| chart.NSeries[3].Name = "Q4"; | |
| // ---------------------------------------------------------------- | |
| // 5. Customize chart appearance. | |
| // ---------------------------------------------------------------- | |
| chart.Title.Text = "Quarterly Sales ¨C CylindricalColumn3D"; | |
| chart.Title.Font.IsBold = true; | |
| chart.Title.Font.Size = 14; | |
| // 3?D rotation (optional ¨C gives a better visual depth) | |
| chart.RotationAngle = 30; | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Sales (USD)"; | |
| // Apply a built-in style | |
| chart.Style = 8; | |
| // Show data labels for each column | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.Font.Size = 9; | |
| series.DataLabels.Font.Color = Color.White; | |
| } | |
| // ---------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ---------------------------------------------------------------- | |
| string outputPath = "CylindricalColumn3DChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Initialize workbook and sheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Write sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Category Q1 Q2 Q3 | |
| // 2 Marketing 5000 7000 6000 | |
| // 3 R&D 8000 9000 8500 | |
| // 4 Operations 6500 6200 7100 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| string[] categories = { "Marketing", "R&D", "Operations" }; | |
| double[,] values = { | |
| { 5000, 7000, 6000 }, | |
| { 8000, 9000, 8500 }, | |
| { 6500, 6200, 7100 } | |
| }; | |
| for (int r = 0; r < categories.Length; r++) | |
| { | |
| sheet.Cells[r + 1, 0].Value = categories[r]; | |
| for (int c = 0; c < 3; c++) | |
| { | |
| sheet.Cells[r + 1, c + 1].Value = values[r, c]; | |
| } | |
| } | |
| // 3. Add a multi?series Doughnut chart. | |
| int chartIdx = sheet.Charts.Add(ChartType.Doughnut, 6, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Quarterly Expenses by Department"; | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$4"; | |
| // 4. Add three series ¨C one for each quarter. | |
| // Series 1 (Q1) | |
| int s1 = chart.NSeries.Add("=Sheet1!$B$2:$B$4", true); | |
| chart.NSeries[s1].Name = "Q1"; | |
| chart.NSeries[s1].DoughnutHoleSize = 60; | |
| chart.NSeries[s1].DataLabels.ShowPercentage = true; | |
| chart.NSeries[s1].Border.Color = Color.DarkBlue; | |
| chart.NSeries[s1].Area.ForegroundColor = Color.LightBlue; | |
| // Series 2 (Q2) | |
| int s2 = chart.NSeries.Add("=Sheet1!$C$2:$C$4", true); | |
| chart.NSeries[s2].Name = "Q2"; | |
| chart.NSeries[s2].DoughnutHoleSize = 50; | |
| chart.NSeries[s2].DataLabels.ShowPercentage = true; | |
| chart.NSeries[s2].Border.Color = Color.DarkGreen; | |
| chart.NSeries[s2].Area.ForegroundColor = Color.LightGreen; | |
| // Series 3 (Q3) | |
| int s3 = chart.NSeries.Add("=Sheet1!$D$2:$D$4", true); | |
| chart.NSeries[s3].Name = "Q3"; | |
| chart.NSeries[s3].DoughnutHoleSize = 40; | |
| chart.NSeries[s3].DataLabels.ShowPercentage = true; | |
| chart.NSeries[s3].Border.Color = Color.DarkRed; | |
| chart.NSeries[s3].Area.ForegroundColor = Color.LightCoral; | |
| // 5. Adjust chart appearance. | |
| chart.Legend.Position = LegendPositionType.Right; | |
| chart.PlotArea.Area.Formatting = FormattingType.None; // Transparent plot area. | |
| // 6. Save the workbook. | |
| string output = "DoughnutChart_MultiSeries.xlsx"; | |
| workbook.Save(output); | |
| Console.WriteLine($"Workbook with multi?series Doughnut chart saved to {output}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and obtain the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Category Sales | |
| // 2 Electronics 35000 | |
| // 3 Clothing 21000 | |
| // 4 Groceries 15000 | |
| // 5 Furniture 12000 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Sales"; | |
| string[] categories = { "Electronics", "Clothing", "Groceries", "Furniture" }; | |
| double[] sales = { 35000, 21000, 15000, 12000 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| sheet.Cells[i + 1, 1].Value = sales[i]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Doughnut chart object. | |
| // ------------------------------------------------------------ | |
| // Parameters: (ChartType, upper left row, upper left column, lower right row, lower right column) | |
| int chartIndex = sheet.Charts.Add(ChartType.Doughnut, 5, 0, 20, 8); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Sales Distribution by Category"; | |
| // ------------------------------------------------------------ | |
| // 4. Add the data series ¨C the range includes only the numeric values. | |
| // ------------------------------------------------------------ | |
| // The first argument is the data range for the series. | |
| // The second argument (true) indicates that the series will have a name. | |
| int seriesIndex = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[seriesIndex].Name = "Sales"; | |
| // ------------------------------------------------------------ | |
| // 5. Bind category (X) labels. | |
| // ------------------------------------------------------------ | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 6. Optional customizations. | |
| // ------------------------------------------------------------ | |
| // Show data labels (percentage) inside the doughnut. | |
| chart.NSeries[seriesIndex].DataLabels.Position = LabelPositionType.InsideBase; | |
| chart.NSeries[seriesIndex].DataLabels.ShowPercentage = true; | |
| // Set the doughnut hole size (percentage of the chart area). | |
| chart.NSeries[seriesIndex].DoughnutHoleSize = 50; // 0 ¨C 100 | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "DoughnutChart_Simple.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // A B | |
| // 1 Category Amount | |
| // 2 Food 4500 | |
| // 3 Clothing 2600 | |
| // 4 Electronics 3400 | |
| // 5 Travel 1500 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Amount"); | |
| string[] categories = { "Food", "Clothing", "Electronics", "Travel" }; | |
| double[] amounts = { 4500, 2600, 3400, 1500 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(categories[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(amounts[i]); // Column B | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a DoughnutExploded chart to the worksheet. | |
| // The chart will be placed at rows 7?25 and columns 0?9. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.DoughnutExploded, 6, 0, 24, 9); | |
| Chart doughnutChart = sheet.Charts[chartIndex]; | |
| // Set a meaningful title. | |
| doughnutChart.Title.Text = "Quarterly Sales Distribution"; | |
| // ----------------------------------------------------------------- | |
| // 4. Create a series that points to the data range. | |
| // The series uses column B (Amount) as values and column A (Category) as categories. | |
| // ----------------------------------------------------------------- | |
| int seriesIndex = doughnutChart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| doughnutChart.NSeries[seriesIndex].Name = "Sales Amount"; | |
| // Assign category (X) data. | |
| doughnutChart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // Optional: Set individual slice "explode" distances. | |
| // The Explode factor is a percentage (0?100) of the default slice radius. | |
| // Here we emphasize the "Electronics" slice. | |
| for (int i = 0; i < doughnutChart.NSeries[seriesIndex].Points.Count; i++) | |
| { | |
| // Default explode distance is 0 (no separation). | |
| doughnutChart.NSeries[seriesIndex].Points[i].Explosion = 0; | |
| } | |
| // Explode the third slice (index 2) ¨C "Electronics". | |
| doughnutChart.NSeries[seriesIndex].Points[2].Explosion = 20; // 20?% offset | |
| // ----------------------------------------------------------------- | |
| // 5. Apply some visual customizations (optional). | |
| // ----------------------------------------------------------------- | |
| // Set the chart background to transparent. | |
| doughnutChart.PlotArea.Area.Formatting = FormattingType.None; | |
| // Show data labels with percentages. | |
| doughnutChart.NSeries[seriesIndex].DataLabels.ShowPercentage = true; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "DoughnutExplodedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Initialize workbook and worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // 2. Fill data: Category and Value. | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Value"; | |
| string[] categories = { "North", "South", "East", "West" }; | |
| double[] values = { 30, 20, 35, 15 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| sheet.Cells[i + 1, 1].Value = values[i]; | |
| } | |
| // 3. Add an exploded Pie chart. | |
| int chartIdx = sheet.Charts.Add(ChartType.PieExploded, 7, 0, 26, 12); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Regional Sales Distribution"; | |
| // 4. Bind series data. | |
| chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // 5. Customize each slice. | |
| // a) Explosion (distance from center) | |
| // b) Fill color | |
| // c) Data label format | |
| int[] explodePercentages = { 10, 0, 15, 0 }; // Percent distance for each slice | |
| Color[] sliceColors = { Color.CornflowerBlue, Color.Orange, Color.LightGreen, Color.Goldenrod }; | |
| for (int i = 0; i < chart.NSeries[0].Points.Count; i++) | |
| { | |
| // Set explosion distance (in percentage of the radius) | |
| chart.NSeries[0].Points[i].Explosion = explodePercentages[i]; | |
| // Apply custom fill color | |
| chart.NSeries[0].Points[i].Area.ForegroundColor = sliceColors[i]; | |
| // Show percentage label for each slice | |
| chart.NSeries[0].Points[i].DataLabels.ShowPercentage = true; | |
| chart.NSeries[0].Points[i].DataLabels.ShowCategoryName = true; | |
| } | |
| // 6. Adjust legend position (optional). | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // 7. Save the workbook. | |
| workbook.Save("ExplodedPieChart_Output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| sheet.Name = "FunnelData"; | |
| // ------------------------------------------------------------------ | |
| // 2. Populate worksheet with sample funnel data. | |
| // A column ¨C Stage description | |
| // B column ¨C Number of items at each stage | |
| // ------------------------------------------------------------------ | |
| // A B | |
| // 1 Stage Count | |
| // 2 Lead 5000 | |
| // 3 Qualified 3200 | |
| // 4 Proposal 2100 | |
| // 5 Negotiation1500 | |
| // 6 Closed 950 | |
| // ------------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Stage"); | |
| sheet.Cells["B1"].PutValue("Count"); | |
| string[] stages = { "Lead", "Qualified", "Proposal", "Negotiation", "Closed" }; | |
| int[] counts = { 5000, 3200, 2100, 1500, 950 }; | |
| for (int i = 0; i < stages.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(stages[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(counts[i]); // Column B | |
| } | |
| // ------------------------------------------------------------------ | |
| // 3. Add a Funnel chart object. | |
| // ------------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.Funnel, 7, 0, 25, 12); | |
| Chart funnelChart = sheet.Charts[chartIndex]; | |
| funnelChart.Title.Text = "Sales Funnel"; | |
| // ------------------------------------------------------------------ | |
| // 4. Define the data range for the chart. | |
| // Series data ¨C B2:B6 (counts) | |
| // Category data ¨C A2:A6 (stages) | |
| // ------------------------------------------------------------------ | |
| funnelChart.NSeries.Add("=FunnelData!$B$2:$B$6", true); | |
| funnelChart.NSeries.CategoryData = "=FunnelData!$A$2:$A$6"; | |
| // ------------------------------------------------------------------ | |
| // 5. Save the workbook. | |
| // ------------------------------------------------------------------ | |
| string outputPath = "FunnelChart_Basic.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------------ | |
| // 1. Prepare workbook and data. | |
| // ------------------------------------------------------------------ | |
| var wb = new Workbook(); | |
| Worksheet ws = wb.Worksheets[0]; | |
| ws.Name = "Pipeline"; | |
| ws.Cells["A1"].PutValue("Stage"); | |
| ws.Cells["B1"].PutValue("Leads"); | |
| string[] stageNames = { "Awareness", "Interest", "Consideration", "Intent", "Purchase" }; | |
| int[] leadCounts = { 8000, 5600, 3800, 2100, 950 }; | |
| for (int i = 0; i < stageNames.Length; i++) | |
| { | |
| ws.Cells[i + 1, 0].PutValue(stageNames[i]); | |
| ws.Cells[i + 1, 1].PutValue(leadCounts[i]); | |
| } | |
| // ------------------------------------------------------------------ | |
| // 2. Insert a Funnel chart. | |
| // ------------------------------------------------------------------ | |
| int idx = ws.Charts.Add(ChartType.Funnel, 8, 0, 28, 13); | |
| Chart funnel = ws.Charts[idx]; | |
| funnel.Title.Text = "Marketing Funnel"; | |
| // Bind series and categories. | |
| funnel.NSeries.Add("=Pipeline!$B$2:$B$6", true); | |
| funnel.NSeries.CategoryData = "=Pipeline!$A$2:$A$6"; | |
| // ------------------------------------------------------------------ | |
| // 3. Apply custom colors to each funnel slice. | |
| // ------------------------------------------------------------------ | |
| Color[] sliceColors = { | |
| Color.FromArgb(91, 155, 213), // Awareness | |
| Color.FromArgb(237, 125, 49), // Interest | |
| Color.FromArgb(165, 165, 165), // Consideration | |
| Color.FromArgb(255, 192, 0), // Intent | |
| Color.FromArgb(112, 173, 71) // Purchase | |
| }; | |
| for (int i = 0; i < funnel.NSeries[0].Points.Count; i++) | |
| { | |
| funnel.NSeries[0].Points[i].Area.ForegroundColor = sliceColors[i]; | |
| funnel.NSeries[0].Points[i].Area.Formatting = FormattingType.Custom; | |
| } | |
| // ------------------------------------------------------------------ | |
| // 4. Show data labels (value + percentage). | |
| // ------------------------------------------------------------------ | |
| funnel.NSeries[0].DataLabels.ShowValue = true; | |
| funnel.NSeries[0].DataLabels.ShowPercentage = true; | |
| funnel.NSeries[0].DataLabels.Position = LabelPositionType.Center; | |
| // ------------------------------------------------------------------ | |
| // 5. Adjust legend ¨C place it at the bottom and use a horizontal layout. | |
| // ------------------------------------------------------------------ | |
| funnel.ShowLegend = true; | |
| funnel.Legend.Position = LegendPositionType.Bottom; | |
| // ------------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------------ | |
| string outFile = "FunnelChart_Customized.xlsx"; | |
| wb.Save(outFile); | |
| Console.WriteLine($"Customized funnel chart saved to {outFile}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and obtain the first worksheet. | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // 2. Populate the worksheet with sample numeric data (e.g., test scores). | |
| // We'll generate 100 random scores between 0 and 100. | |
| Random rnd = new Random(); | |
| sheet.Cells["A1"].PutValue("Score"); // Header | |
| for (int i = 0; i < 100; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(rnd.NextDouble() * 100); | |
| } | |
| // 3. Add a Histogram chart. | |
| // The chart is positioned from row 5, column 2 to row 25, column 12. | |
| int chartIdx = sheet.Charts.Add(ChartType.Histogram, 5, 1, 25, 11); | |
| Chart histogram = sheet.Charts[chartIdx]; | |
| histogram.Title.Text = "Score Distribution"; | |
| // 4. Define the data range for the histogram. | |
| // The NSeries.Add method expects a range string and a flag indicating that the data are y-values. | |
| // Histogram charts ignore the X?axis; they automatically compute bins from the y?values. | |
| histogram.NSeries.Add("=Sheet1!$A$2:$A$101", true); | |
| histogram.NSeries[0].Name = "Scores"; | |
| // 5. Fine?tune the appearance (optional). | |
| histogram.PlotArea.Area.ForegroundColor = System.Drawing.Color.White; | |
| histogram.Legend.Position = LegendPositionType.Bottom; | |
| // 5. Save the workbook. | |
| string outPath = "HistogramChart_Output.xlsx"; | |
| workbook.Save(outPath); | |
| Console.WriteLine($"Histogram chart created successfully: {outPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate worksheet with sample data. | |
| // Row 0 ¨C Headers, Row 1..N ¨C Data. | |
| // ----------------------------------------------------------------- | |
| // A B C D | |
| // 1 Region Q1 Q2 Q3 | |
| // 2 North 30 45 25 | |
| // 3 South 20 35 45 | |
| // 4 East 40 30 30 | |
| // 5 West 10 20 70 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] values = { | |
| { 30, 45, 25 }, | |
| { 20, 35, 45 }, | |
| { 40, 30, 30 }, | |
| { 10, 20, 70 } | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = regions[i]; | |
| for (int j = 0; j < 3; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].Value = values[i, j]; | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Line100PercentStacked chart. | |
| // ----------------------------------------------------------------- | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| // ----------------------------------------------------------------- | |
| int chartIdx = sheet.Charts.Add(ChartType.Line100PercentStacked, 6, 0, 22, 10); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Quarterly Sales Distribution (100?% Stacked Line)"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define series ¨C each quarter will be a separate series. | |
| // ----------------------------------------------------------------- | |
| // Series 1 ¨C Q1 | |
| int s1 = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[s1].Name = "Q1"; | |
| // Series 2 ¨C Q2 | |
| int s2 = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[s2].Name = "Q2"; | |
| // Series 3 ¨C Q3 | |
| int s3 = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[s3].Name = "Q3"; | |
| // Category (X) axis ¨C Region names | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 5. Optional formatting | |
| // ----------------------------------------------------------------- | |
| chart.Legend.Position = LegendPositionType.Right; | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Percentage of Total Sales"; | |
| // Show markers so each data point is visible. | |
| foreach (Series ns in chart.NSeries) | |
| { | |
| ns.Marker.MarkerSize = 8; | |
| ns.Marker.MarkerStyle = ChartMarkerType.Circle; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 6. Save workbook. | |
| // ----------------------------------------------------------------- | |
| string outFile = "Line100PercentStackedChart.xlsx"; | |
| workbook.Save(outFile); | |
| Console.WriteLine($"Chart created successfully ¡ú {outFile}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and get the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // A B C D | |
| // Region Q1 Q2 Q3 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] sales = { | |
| { 120, 150, 180 }, | |
| { 90, 110, 140 }, | |
| { 130, 160, 200 }, | |
| { 100, 130, 170 } | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| // Column A ¨C region names | |
| sheet.Cells[i + 1, 0].Value = regions[i]; | |
| // Columns B?D ¨C quarterly sales | |
| for (int q = 0; q < 3; q++) | |
| { | |
| sheet.Cells[i + 1, q + 1].Value = sales[i, q]; | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Line100PercentStackedWithDataMarkers chart. | |
| // The chart is placed between rows 7?20 and columns A?F. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.Line100PercentStackedWithDataMarkers, 6, 0, 20, 5); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C 100% Stacked Line with Markers"; | |
| // ----------------------------------------------------------------- | |
| // 4. Create a series for each quarter. | |
| // Use the same data range format as Excel formulas. | |
| // ----------------------------------------------------------------- | |
| // Q1 series (primary axis) | |
| int q1Series = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[q1Series].Name = "Q1"; | |
| // Q2 series (primary axis) | |
| int q2Series = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[q2Series].Name = "Q2"; | |
| // Q3 series (primary axis) | |
| int q3Series = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[q3Series].Name = "Q3"; | |
| // ----------------------------------------------------------------- | |
| // 5. Enable data markers for each series and customize their look. | |
| // ----------------------------------------------------------------- | |
| foreach (Series series in chart.NSeries) | |
| { | |
| // Set a consistent marker size | |
| series.Marker.MarkerSize = 10; | |
| // Choose a distinct marker shape per series | |
| // (you can also set colors per series if desired) | |
| if (series.Name == "Q1") | |
| { | |
| series.Marker.MarkerStyle = ChartMarkerType.Circle; | |
| series.Marker.Area.ForegroundColor = Color.Crimson; | |
| } | |
| else if (series.Name == "Q2") | |
| { | |
| series.Marker.MarkerStyle = ChartMarkerType.Square; | |
| series.Marker.Area.ForegroundColor = Color.DarkOrange; | |
| } | |
| else // Q3 | |
| { | |
| series.Marker.MarkerStyle = ChartMarkerType.Diamond; | |
| series.Marker.Area.ForegroundColor = Color.SeaGreen; | |
| } | |
| // Remove marker border for a cleaner look | |
| series.Marker.Border.IsVisible = false; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 6. Optional axis customizations. | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Sales (% of total)"; | |
| chart.ValueAxis.TickLabels.NumberFormat = "0%"; // Show percentages | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Line100PercentStackedWithDataMarkers.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // A B C D | |
| // 1 Quarter 2022 2023 2024 | |
| // 2 Q1 15000 17000 19000 | |
| // 3 Q2 18000 20000 22000 | |
| // 4 Q3 21000 23000 25000 | |
| // 5 Q4 24000 26000 28000 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].PutValue("Quarter"); | |
| sheet.Cells["B1"].PutValue("2022"); | |
| sheet.Cells["C1"].PutValue("2023"); | |
| sheet.Cells["D1"].PutValue("2024"); | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[,] sales = { | |
| {15000, 17000, 19000}, | |
| {18000, 20000, 22000}, | |
| {21000, 23000, 25000}, | |
| {24000, 26000, 28000} | |
| }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(quarters[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(sales[i, 0]); // Column B | |
| sheet.Cells[i + 1, 2].PutValue(sales[i, 1]); // Column C | |
| sheet.Cells[i + 1, 3].PutValue(sales[i, 2]); // Column D | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Line3D chart. | |
| // Parameters: (ChartType, firstRow, firstColumn, totalRows, totalColumns) | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.Line3D, 7, 0, 27, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Annual Sales Trend (3?D Line)"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for each year. | |
| // The NSeries.Add method takes a formula string that references the data. | |
| // ----------------------------------------------------------------- | |
| // Series for 2022 | |
| int s2022 = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[s2022].Name = "2022"; | |
| // Series for 2023 | |
| int s2023 = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[s2023].Name = "2023"; | |
| // Series for 2024 | |
| int s2024 = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[s2024].Name = "2024"; | |
| // ----------------------------------------------------------------- | |
| // 5. Set category (X?axis) data ¨C quarters. | |
| // ----------------------------------------------------------------- | |
| chart.NSeries.CategoryData = "SalesData!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 6. Optional styling ¨C change line colors and set a smooth look. | |
| // ----------------------------------------------------------------- | |
| chart.NSeries[0].Area.ForegroundColor = Color.FromArgb(0, 112, 192); // Blue | |
| chart.NSeries[1].Area.ForegroundColor = Color.FromArgb(0, 176, 80); // Green | |
| chart.NSeries[2].Area.ForegroundColor = Color.FromArgb(192, 80, 77); // Red | |
| // Enable markers for better readability | |
| foreach (var series in chart.NSeries) | |
| { | |
| series.Marker.MarkerSize = 8; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook to XLSX format. | |
| // ----------------------------------------------------------------- | |
| string outPath = "Line3DChart_Basic.xlsx"; | |
| workbook.Save(outPath); | |
| Console.WriteLine($"Workbook saved to {outPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // The layout mirrors a typical stacked line scenario: | |
| // | Month | Product A | Product B | Product C | | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| string[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun" }; | |
| double[] prodA = { 30, 45, 50, 65, 70, 80 }; | |
| double[] prodB = { 20, 25, 35, 40, 45, 55 }; | |
| double[] prodC = { 15, 20, 25, 30, 35, 40 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| int row = i + 2; // Data starts from row 2 | |
| sheet.Cells[row, 0].Value = months[i]; | |
| sheet.Cells[row, 1].Value = prodA[i]; | |
| sheet.Cells[row, 2].Value = prodB[i]; | |
| sheet.Cells[row, 3].Value = prodC[i]; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a LineStacked chart. | |
| // Parameters: (chart type, upper-left row, upper-left column, | |
| // lower-right row, lower-right column) | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.LineStacked, 8, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Product Sales (Stacked Line)"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define series for each product. | |
| // The NSeries.Add method takes a range string and a boolean | |
| // indicating whether the series is a column (true = vertical). | |
| // ----------------------------------------------------------------- | |
| // Series for Product A | |
| int seriesA = chart.NSeries.Add("=SalesData!$B$2:$B$7", true); | |
| chart.NSeries[seriesA].Name = "Product A"; | |
| // Series for Product B | |
| int seriesB = chart.NSeries.Add("=SalesData!$C$2:$C$7", true); | |
| chart.NSeries[seriesB].Name = "Product B"; | |
| // Series for Product C | |
| int seriesC = chart.NSeries.Add("=SalesData!$D$2:$D$7", true); | |
| chart.NSeries[seriesC].Name = "Product C"; | |
| // ----------------------------------------------------------------- | |
| // 5. Set the category (X) axis data ¨C the months. | |
| // ----------------------------------------------------------------- | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$7"; | |
| // ----------------------------------------------------------------- | |
| // 6. Optional: customize visual appearance. | |
| // ----------------------------------------------------------------- | |
| // Chart background | |
| chart.PlotArea.Area.ForegroundColor = Color.White; | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Units Sold"; | |
| // Legend placement | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Data labels ¨C show the cumulative values on each point | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| series.DataLabels.Position = LabelPositionType.Center; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "LineStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to \"{outputPath}\""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 2. Create a new workbook and get the first worksheet | |
| // ------------------------------------------------------------ | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 3. Populate the worksheet with sample data | |
| // ------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "Product A"; | |
| sheet.Cells["C1"].Value = "Product B"; | |
| sheet.Cells["D1"].Value = "Product C"; | |
| // Sample values | |
| string[] months = { "Jan", "Feb", "Mar", "Apr", "May" }; | |
| double[] prodA = { 120, 135, 150, 160, 170 }; | |
| double[] prodB = { 80, 90, 100, 110, 115 }; | |
| double[] prodC = { 50, 60, 65, 70, 75 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| // Column A ¨C month label | |
| sheet.Cells[i + 1, 0].Value = months[i]; | |
| // Columns B?D ¨C product values | |
| sheet.Cells[i + 1, 1].Value = prodA[i]; | |
| sheet.Cells[i + 1, 2].Value = prodB[i]; | |
| sheet.Cells[i + 1, 3].Value = prodC[i]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 4. Add a LineStackedWithDataMarkers chart | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, lower?right row, lower?right column | |
| int chartIndex = sheet.Charts.Add(ChartType.LineStackedWithDataMarkers, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Chart title | |
| chart.Title.Text = "Cumulative Sales (Stacked Line with Markers)"; | |
| chart.Title.Font.Size = 14; | |
| chart.Title.Font.IsBold = true; | |
| // ------------------------------------------------------------ | |
| // 5. Add series ¨C one series per product column | |
| // ------------------------------------------------------------ | |
| // Series 1 ¨C Product A | |
| int seriesA = chart.NSeries.Add("=Sheet1!$B$2:$B$6", true); | |
| chart.NSeries[seriesA].Name = "Product A"; | |
| // Use a distinct marker style for each series | |
| chart.NSeries[seriesA].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[seriesA].Marker.MarkerSize = 9; | |
| chart.NSeries[seriesA].Marker.Area.ForegroundColor = Color.CornflowerBlue; | |
| // Series 2 ¨C Product B | |
| int seriesB = chart.NSeries.Add("=Sheet1!$C$2:$C$6", true); | |
| chart.NSeries[seriesB].Name = "Product B"; | |
| chart.NSeries[seriesB].Marker.MarkerStyle = ChartMarkerType.Square; | |
| chart.NSeries[seriesB].Marker.MarkerSize = 9; | |
| chart.NSeries[seriesB].Marker.Area.ForegroundColor = Color.LightGreen; | |
| // Series 3 ¨C Product C | |
| int seriesC = chart.NSeries.Add("=Sheet1!$D$2:$D$6", true); | |
| chart.NSeries[seriesC].Name = "Product C"; | |
| chart.NSeries[seriesC].Marker.MarkerStyle = ChartMarkerType.Diamond; | |
| chart.NSeries[seriesC].Marker.MarkerSize = 9; | |
| chart.NSeries[seriesC].Marker.Area.ForegroundColor = Color.OrangeRed; | |
| // ------------------------------------------------------------ | |
| // 6. Configure category axis (X?axis) and value axis (Y?axis) | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Units Sold"; | |
| // Optional: format the Y?axis to show integer values only | |
| chart.ValueAxis.TickLabels.NumberFormat = "0"; | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook | |
| // ------------------------------------------------------------ | |
| string outputPath = "LineStackedWithDataMarkers_Example.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to \"{outputPath}\""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------- | |
| // 1. Initialize workbook and worksheet. | |
| // ------------------------------------------------------------- | |
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| // ------------------------------------------------------------- | |
| // 2. Insert sample data (Quarterly profit). | |
| // ------------------------------------------------------------- | |
| // A B | |
| // 1 Quarter Profit | |
| // 2 Q1 45 | |
| // 3 Q2 60 | |
| // 4 Q3 55 | |
| // 5 Q4 70 | |
| // ------------------------------------------------------------- | |
| ws.Cells["A1"].Value = "Quarter"; | |
| ws.Cells["B1"].Value = "Profit"; | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] profit = { 45, 60, 55, 70 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| ws.Cells[i + 1, 0].Value = quarters[i]; | |
| ws.Cells[i + 1, 1].Value = profit[i]; | |
| } | |
| // ------------------------------------------------------------- | |
| // 3. Add the chart (Line with markers) and set title. | |
| // ------------------------------------------------------------- | |
| int idx = ws.Charts.Add(ChartType.LineWithDataMarkers, 7, 0, 22, 10); | |
| Chart chart = ws.Charts[idx]; | |
| chart.Title.Text = "Quarterly Profit (Custom Markers)"; | |
| // ------------------------------------------------------------- | |
| // 4. Create a series for profit values. | |
| // ------------------------------------------------------------- | |
| int sIdx = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[sIdx].Name = "Profit"; | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ------------------------------------------------------------- | |
| // 5. Customize the markers. | |
| // ------------------------------------------------------------- | |
| // Access the marker object. | |
| var marker = chart.NSeries[sIdx].Marker; | |
| // Change shape to Diamond. | |
| marker.MarkerStyle = ChartMarkerType.Diamond; | |
| // Increase size (default is 8 points). | |
| marker.MarkerSize = 12; | |
| // Set fill color to LightGreen. | |
| marker.Area.Formatting = FormattingType.Custom; | |
| marker.Area.ForegroundColor = Color.LightGreen; | |
| // Set border (outline) to DarkGreen, solid line, width 1. | |
| marker.Border.IsVisible = true; | |
| marker.Border.Color = Color.DarkGreen; | |
| marker.Border.Weight = WeightType.WideLine; | |
| marker.Border.Style = LineType.Solid; | |
| // ------------------------------------------------------------- | |
| // 6. Optional: Fine?tune axis titles. | |
| // ------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Quarter"; | |
| chart.ValueAxis.Title.Text = "Profit (in $k)"; | |
| // ------------------------------------------------------------- | |
| // 7. Save the result. | |
| // ------------------------------------------------------------- | |
| string outFile = "LineWithDataMarkers_Customized.xlsx"; | |
| wb.Save(outFile); | |
| Console.WriteLine($"Customized chart saved to {outFile}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // ----------------------------------------------------------------- | |
| // A B | |
| // 1 Month Sales | |
| // 2 Jan 150 | |
| // 3 Feb 200 | |
| // 4 Mar 180 | |
| // 5 Apr 220 | |
| // 6 May 190 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Month"; | |
| sheet.Cells["B1"].Value = "Sales"; | |
| string[] months = { "Jan", "Feb", "Mar", "Apr", "May" }; | |
| double[] sales = { 150, 200, 180, 220, 190 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = months[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = sales[i]; // Column B | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a LineWithDataMarkers chart. | |
| // ----------------------------------------------------------------- | |
| // Parameters: ChartType, upper-left row, upper-left column, lower-right row, lower-right column | |
| int chartIndex = sheet.Charts.Add(ChartType.LineWithDataMarkers, 7, 0, 22, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Monthly Sales (Line with Data Markers)"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define the series ¨C the sales values. | |
| // ----------------------------------------------------------------- | |
| // The first argument is the data range; the second argument indicates that the series has a name. | |
| int seriesIndex = chart.NSeries.Add("=Sheet1!$B$2:$B$6", true); | |
| chart.NSeries[seriesIndex].Name = "Sales"; | |
| // Set the category (X?axis) data ¨C months. | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$6"; | |
| // ----------------------------------------------------------------- | |
| // 5. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "LineWithDataMarkers_Basic.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Initialize workbook and worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Add sample data for two metrics: Revenue (primary) and | |
| // Profit Margin (secondary). | |
| // ------------------------------------------------------------ | |
| // A B C | |
| // 1 Month Revenue Profit% | |
| // 2 Jan 3000 12 | |
| // 3 Feb 3500 15 | |
| // 4 Mar 4000 13 | |
| // 5 Apr 3800 14 | |
| // ------------------------------------------------------------ | |
| string[] months = { "Jan", "Feb", "Mar", "Apr" }; | |
| double[] revenue = { 3000, 3500, 4000, 3800 }; | |
| double[] profitPct = { 12, 15, 13, 14 }; | |
| sheet.Cells["A1"].PutValue("Month"); | |
| sheet.Cells["B1"].PutValue("Revenue"); | |
| sheet.Cells["C1"].PutValue("Profit%"); | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(months[i]); // Month | |
| sheet.Cells[i + 1, 1].PutValue(revenue[i]); // Revenue | |
| sheet.Cells[i + 1, 2].PutValue(profitPct[i]); // Profit% | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Insert a Line chart. | |
| // ------------------------------------------------------------ | |
| int chartIdx = sheet.Charts.Add(ChartType.Line, 7, 0, 27, 12); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Revenue vs. Profit Margin"; | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 4. Add Revenue series (primary axis). | |
| // ------------------------------------------------------------ | |
| int revSeriesIdx = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[revSeriesIdx].Name = "Revenue"; | |
| chart.NSeries[revSeriesIdx].Type = ChartType.Line; // Explicit, though default | |
| // ------------------------------------------------------------ | |
| // 5. Add Profit% series (secondary axis) and set marker style. | |
| // ------------------------------------------------------------ | |
| int profitSeriesIdx = chart.NSeries.Add("=Sheet1!$C$2:$C$5", true); | |
| chart.NSeries[profitSeriesIdx].Name = "Profit%"; | |
| chart.NSeries[profitSeriesIdx].Type = ChartType.Line; | |
| chart.NSeries[profitSeriesIdx].PlotOnSecondAxis = true; // Use secondary Y?axis | |
| // Optional: customize marker for profit series | |
| chart.NSeries[profitSeriesIdx].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[profitSeriesIdx].Marker.MarkerSize = 10; | |
| chart.NSeries[profitSeriesIdx].Marker.Area.ForegroundColor = Color.Orange; | |
| // ------------------------------------------------------------ | |
| // 6. Axis titles. | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Revenue (USD)"; | |
| chart.SecondValueAxis.Title.Text = "Profit %"; | |
| // ------------------------------------------------------------ | |
| // 7. Save workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save("MultiSeriesLineChart.xlsx"); | |
| Console.WriteLine("Workbook saved as MultiSeriesLineChart.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and get the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Defects"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data (Category | Defect Count) | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Defect Count | |
| // 2 A 120 | |
| // 3 B 90 | |
| // 4 C 45 | |
| // 5 D 30 | |
| // 6 E 15 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Defect"; | |
| sheet.Cells["B1"].Value = "Count"; | |
| string[] categories = { "A", "B", "C", "D", "E" }; | |
| double[] counts = { 120, 90, 45, 30, 15 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| sheet.Cells[i + 1, 1].Value = counts[i]; | |
| } | |
| // 3. Calculate cumulative percentages (required for Pareto line) | |
| double total = 0; | |
| foreach (var c in counts) total += c; | |
| double cumulative = 0; | |
| for (int i = 0; i < counts.Length; i++) | |
| { | |
| cumulative += counts[i]; | |
| // Store cumulative percentage in column C (as a decimal, e.g., 0.75) | |
| sheet.Cells[i + 1, 2].Value = cumulative / total; | |
| } | |
| sheet.Cells["C1"].Value = "Cumulative %"; | |
| // 4. Add a Column chart (base type) | |
| int chartIndex = sheet.Charts.Add(ChartType.Column, 7, 0, 25, 15); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "ParetoLine Chart ¨C Defect Distribution"; | |
| // 5. Add the primary series (Column) ¨C Defect Count | |
| int columnSeriesIdx = chart.NSeries.Add("=Defects!$B$2:$B$6", true); | |
| chart.NSeries[columnSeriesIdx].Name = "Count"; | |
| chart.NSeries[columnSeriesIdx].Type = ChartType.Column; // Explicitly set Column | |
| // 6. Add the secondary series (Line) ¨C Cumulative Percentage | |
| int lineSeriesIdx = chart.NSeries.Add("=Defects!$C$2:$C$6", true); | |
| chart.NSeries[lineSeriesIdx].Name = "Cumulative %"; | |
| chart.NSeries[lineSeriesIdx].Type = ChartType.Line; // Set to Line chart | |
| chart.NSeries[lineSeriesIdx].PlotOnSecondAxis = true; // Use secondary axis | |
| // 7. Configure the secondary axis to display percentage format | |
| chart.SecondValueAxis.TickLabels.NumberFormat = "0%"; | |
| chart.SecondValueAxis.Title.Text = "Cumulative Percentage"; | |
| // 8. Optional: Show data labels for better readability | |
| chart.NSeries[columnSeriesIdx].DataLabels.ShowValue = true; | |
| chart.NSeries[lineSeriesIdx].DataLabels.ShowValue = true; | |
| chart.NSeries[lineSeriesIdx].DataLabels.NumberFormat = "0%"; | |
| // 9. Set the primary axis title | |
| chart.CategoryAxis.Title.Text = "Defect Type"; | |
| chart.ValueAxis.Title.Text = "Count"; | |
| // 10. Save the workbook in XLSX format (can also be saved as PDF, PNG, etc.) | |
| string outputPath = "ParetoLineChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"ParetoLine chart created successfully. File saved at: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Category Value | |
| // 2 Apples 50 | |
| // 3 Oranges 30 | |
| // 4 Bananas 20 | |
| // ------------------------------------------------------------ | |
| cells["A1"].PutValue("Category"); | |
| cells["B1"].PutValue("Value"); | |
| string[] categories = { "Apples", "Oranges", "Bananas" }; | |
| double[] values = { 50, 30, 20 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| // Row index is i+1 because the header occupies row 0. | |
| cells[i + 1, 0].PutValue(categories[i]); // Column A | |
| cells[i + 1, 1].PutValue(values[i]); // Column B | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Pie3D chart to the worksheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.Pie3D, 5, 0, 20, 7); | |
| Chart pie3d = sheet.Charts[chartIndex]; | |
| // Set chart title. | |
| pie3d.Title.Text = "Fruit Distribution (3?D)"; | |
| // Define the data range for the series. | |
| // Series values: B2:B4, Category names: A2:A4. | |
| pie3d.NSeries.Add("=Sheet1!$B$2:$B$4", true); | |
| pie3d.NSeries.CategoryData = "=Sheet1!$A$2:$A$4"; | |
| // Optional: explode the first slice for emphasis. | |
| pie3d.NSeries[0].Explosion = 15; | |
| // ------------------------------------------------------------ | |
| // 4. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "Pie3DChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and get the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate the worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Category Value | |
| // 2 Cars 45 | |
| // 3 Bikes 30 | |
| // 4 Trucks 15 | |
| // 5 Buses 10 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Value"; | |
| string[] categories = { "Cars", "Bikes", "Trucks", "Buses" }; | |
| double[] values = { 45, 30, 15, 10 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| sheet.Cells[i + 1, 1].Value = values[i]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Pie3D chart and explode the whole series. | |
| // ------------------------------------------------------------ | |
| // The chart will be placed at rows 7?25 and columns 0?7. | |
| int chartIndex = sheet.Charts.Add(ChartType.Pie3D, 7, 0, 25, 7); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Vehicle Distribution (Exploded)"; | |
| // Add a series that references the values range. | |
| int seriesIndex = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[seriesIndex].Name = "Vehicles"; | |
| // Optional: set the explode distance | |
| chart.NSeries[seriesIndex].Explosion = 15; | |
| // ------------------------------------------------------------ | |
| // 4. Save the workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save("Pie3DExploded_Basic.xlsx"); | |
| Console.WriteLine("Workbook 'Pie3DExploded_Basic.xlsx' created successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create workbook and get worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // 2. Insert data ¨C this time we will explode only the ¡°Bikes¡± slice. | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Value"; | |
| string[] categories = { "Cars", "Bikes", "Trucks", "Buses" }; | |
| double[] values = { 45, 30, 15, 10 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| sheet.Cells[i + 1, 1].Value = values[i]; | |
| } | |
| // 3. Add a Pie3D chart. | |
| int chartIdx = sheet.Charts.Add(ChartType.Pie3D, 7, 0, 25, 7); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Vehicle Distribution ¨C Highlighted Slice"; | |
| // 4. Create a series bound to the values. | |
| int seriesIdx = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[seriesIdx].Name = "Vehicles"; | |
| // 5. Explode only the second slice (index 1 ¨C ¡°Bikes¡±). | |
| // Aspose.Cells allows us to specify exploded points via the ExplodePoints property. | |
| // The array contains zero?based indices of slices to explode. | |
| chart.NSeries[seriesIdx].Explosion = 20; // make the exploded slice stand out | |
| // 6. Apply custom colors to each slice for better visual distinction. | |
| Color[] sliceColors = { Color.DarkBlue, Color.Orange, Color.ForestGreen, Color.SlateGray }; | |
| for (int i = 0; i < sliceColors.Length; i++) | |
| { | |
| // Each point (slice) can be accessed via the Points collection. | |
| var point = chart.NSeries[seriesIdx].Points[i]; | |
| point.Area.ForegroundColor = sliceColors[i]; | |
| } | |
| // 7. Optional ¨C Turn on data labels to show values. | |
| chart.NSeries[seriesIdx].DataLabels.ShowCategoryName = true; | |
| chart.NSeries[seriesIdx].DataLabels.ShowValue = true; | |
| chart.NSeries[seriesIdx].DataLabels.Position = LabelPositionType.Center; | |
| // 8. Save the workbook as XLSX. | |
| workbook.Save("Pie3DExploded_Custom.xlsx"); | |
| // 9. Additionally, export the chart as a high?resolution PNG image. | |
| // First, obtain the chart¡¯s image as a stream. | |
| var pngOptions = new ImageOrPrintOptions | |
| { | |
| ImageType = ImageType.Png, | |
| HorizontalResolution = 300, | |
| VerticalResolution = 300 | |
| }; | |
| chart.ToImage("Pie3DExploded_Custom.png", pngOptions); | |
| Console.WriteLine("Files 'Pie3DExploded_Custom.xlsx' and 'Pie3DExploded_Custom.png' created."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Path where the output workbook will be saved | |
| string outputPath = "PieBarChart_Output.xlsx"; | |
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| cells["A1"].Value = "Product"; | |
| cells["B1"].Value = "Units Sold"; | |
| cells["C1"].Value = "Revenue ($)"; | |
| // Sample data | |
| string[] products = { "A", "B", "C", "D", "E" }; | |
| double[] units = { 1200, 850, 430, 300, 150 }; | |
| double[] revenue = { 54000, 38250, 21500, 15000, 7500 }; | |
| for (int i = 0; i < products.Length; i++) | |
| { | |
| cells[i + 1, 0].Value = products[i]; // Column A | |
| cells[i + 1, 1].Value = units[i]; // Column B | |
| cells[i + 1, 2].Value = revenue[i]; // Column C | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a chart object (base type = Pie). The chart will be | |
| // positioned starting at row 7, column 0 and will occupy | |
| // rows 7?27, columns 0?10. | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.PieBar, 7, 0, 27, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Product Sales: Units (Bar) & Revenue (Pie)"; | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$6"; | |
| // ------------------------------------------------------------ | |
| // 4. Add the Pie series ¨C this will display the Revenue share. | |
| // ------------------------------------------------------------ | |
| int pieSeriesIdx = chart.NSeries.Add("=Sheet1!$C$2:$C$6", true); | |
| chart.NSeries[pieSeriesIdx].Name = "Revenue Share"; | |
| chart.NSeries[pieSeriesIdx].Type = ChartType.Pie; // Explicitly set Pie type | |
| // ------------------------------------------------------------ | |
| // 5. Add the Bar series ¨C this will display the absolute Units. | |
| // The bar series is placed on the secondary axis so both | |
| // series are visible without overlapping each other. | |
| // ------------------------------------------------------------ | |
| int barSeriesIdx = chart.NSeries.Add("=Sheet1!$B$2:$B$6", true); | |
| chart.NSeries[barSeriesIdx].Name = "Units Sold"; | |
| chart.NSeries[barSeriesIdx].Type = ChartType.Bar; // Bar chart type | |
| chart.NSeries[barSeriesIdx].PlotOnSecondAxis = true; // Use secondary axis | |
| // ------------------------------------------------------------ | |
| // 6. Customize axes (optional but improves readability) | |
| // ------------------------------------------------------------ | |
| // Primary axis (for the Pie) ¨C hide the value axis as it's not needed. | |
| chart.ValueAxis.IsVisible = false; | |
| // Secondary axis (for the Bar) ¨C give it a title. | |
| chart.SecondValueAxis.Title.Text = "Units Sold"; | |
| chart.SecondValueAxis.Title.Font.Size = 10; | |
| chart.SecondValueAxis.Title.Font.IsBold = true; | |
| // Category axis (shared) ¨C show product names. | |
| chart.CategoryAxis.Title.Text = "Product"; | |
| chart.CategoryAxis.Title.Font.Size = 10; | |
| chart.CategoryAxis.Title.Font.IsBold = true; | |
| // ------------------------------------------------------------ | |
| // 7. Apply simple formatting to make the chart look polished. | |
| // ------------------------------------------------------------ | |
| // Add a legend and position it at the right?bottom corner. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // Set the pie chart to display percentages. | |
| chart.NSeries[pieSeriesIdx].DataLabels.ShowPercentage = true; | |
| chart.NSeries[pieSeriesIdx].DataLabels.Position = LabelPositionType.Center; | |
| // ------------------------------------------------------------ | |
| // 8. Save the workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to \"{outputPath}\""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialise a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Insert sample data that will be visualised in the chart. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].Value = "Product"; | |
| sheet.Cells["B1"].Value = "Revenue"; | |
| // Data rows | |
| string[] products = { "Laptop", "Tablet", "Smartphone", "Desktop", "Accessory" }; | |
| double[] revenue = { 150000, 85000, 230000, 120000, 40000 }; | |
| for (int i = 0; i < products.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = products[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = revenue[i]; // Column B | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a PieExploded chart (the first slice will be exploded). | |
| // ------------------------------------------------------------ | |
| // Parameters: (type, upper-left row, upper-left column, lower-right row, lower-right column) | |
| int chartIndex = sheet.Charts.Add(ChartType.PieExploded, 7, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set a descriptive title. | |
| chart.Title.Text = "Revenue Distribution by Product"; | |
| // Define the data range for the series. | |
| // "=SalesData!$B$2:$B$6" ¨C values | |
| // "=SalesData!$A$2:$A$6" ¨C categories (product names) | |
| chart.NSeries.Add("=SalesData!$B$2:$B$6", true); | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$6"; | |
| chart.NSeries[0].Name = "Revenue"; | |
| // ------------------------------------------------------------ | |
| // 4. Optional customisations. | |
| // ------------------------------------------------------------ | |
| // Explode distance ¨C moves the first slice away from the centre. | |
| // The value is a percentage of the pie radius (default = 0%). | |
| chart.NSeries[0].Explosion = 20; // 20% distance | |
| // Show the legend at the bottom. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set the first slice fill colour to make it stand out. | |
| chart.NSeries[0].Points[0].Area.ForegroundColor = Color.OrangeRed; | |
| // Enable data labels to display percentages. | |
| chart.NSeries[0].DataLabels.ShowPercentage = true; | |
| chart.NSeries[0].DataLabels.ShowValue = true; | |
| // ------------------------------------------------------------ | |
| // 5. Save the workbook to an XLSX file. | |
| // ------------------------------------------------------------ | |
| string outputPath = "PieExplodedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and obtain the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Data"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Category Value | |
| // 2 A 40 | |
| // 3 B 30 | |
| // 4 C 15 | |
| // 5 D 10 | |
| // 6 Others 5 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Value"); | |
| string[] categories = { "A", "B", "C", "D", "Others" }; | |
| double[] values = { 40, 30, 15, 10, 5 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(categories[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(values[i]); // Column B | |
| } | |
| // 3. Add a chart object ¨C start with a normal Pie chart (base type). | |
| int chartIdx = sheet.Charts.Add(ChartType.PiePie, 7, 0, 27, 20); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Sales Distribution ¨C Pie of Pie"; | |
| // 4. Set the series data range (values) and indicate that categories are present. | |
| chart.NSeries.Add("=Data!$B$2:$B$6", true); | |
| chart.NSeries[0].Name = "Sales"; | |
| // Optional: explode the secondary pie for better visual separation. | |
| chart.NSeries[0].Explosion = 15; | |
| // 5. Save the workbook. | |
| workbook.Save("PieOfPieChart_Output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // The data represents product categories and their market share. | |
| // ----------------------------------------------------------------- | |
| // A B C | |
| // 1 Category Q1 Q2 | |
| // 2 Hardware 30 20 | |
| // 3 Software 40 35 | |
| // 4 Services 20 25 | |
| // 5 Support 10 20 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| string[] categories = { "Hardware", "Software", "Services", "Support" }; | |
| double[] q1 = { 30, 40, 20, 10 }; | |
| double[] q2 = { 20, 35, 25, 20 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| sheet.Cells[i + 1, 1].Value = q1[i]; | |
| sheet.Cells[i + 1, 2].Value = q2[i]; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Pyramid100PercentStacked chart. | |
| // The chart will be placed from row 7, column 0 to row 25, column 10. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.Pyramid100PercentStacked, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set a meaningful title. | |
| chart.Title.Text = "Quarterly Market Share ¨C 100% Stacked Pyramid"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add series for Q1 and Q2. | |
| // The first series (Q1) uses the default pyramid style; | |
| // the second series (Q2) is also a pyramid segment. | |
| // ----------------------------------------------------------------- | |
| // Series for Q1 | |
| int q1SeriesIndex = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[q1SeriesIndex].Name = "Q1"; | |
| // Series for Q2 | |
| int q2SeriesIndex = chart.NSeries.Add("=Sheet1!$C$2:$C$5", true); | |
| chart.NSeries[q2SeriesIndex].Name = "Q2"; | |
| // ----------------------------------------------------------------- | |
| // 5. Assign category (X?axis) data. | |
| // ----------------------------------------------------------------- | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ----------------------------------------------------------------- | |
| // 6. Optional customizations. | |
| // ----------------------------------------------------------------- | |
| // Set the background of the plot area to a light color. | |
| chart.PlotArea.Area.ForegroundColor = Color.FromArgb(240, 240, 255); | |
| // Show data labels inside each pyramid segment. | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.Position = LabelPositionType.Center; | |
| series.DataLabels.ShowPercentage = true; | |
| } | |
| // Enable the legend and position it at the bottom. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Pyramid100PercentStacked_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Initialize workbook and worksheet. | |
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| ws.Name = "Revenue"; | |
| // 2. Insert data (Region vs. Product A/B/C). | |
| ws.Cells["A1"].PutValue("Region"); | |
| ws.Cells["B1"].PutValue("Product A"); | |
| ws.Cells["C1"].PutValue("Product B"); | |
| ws.Cells["D1"].PutValue("Product C"); | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] revenue = { | |
| { 250, 180, 70 }, | |
| { 200, 150, 50 }, | |
| { 300, 210, 90 }, | |
| { 180, 130, 40 } | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| ws.Cells[i + 1, 0].PutValue(regions[i]); | |
| for (int j = 0; j < 3; j++) | |
| ws.Cells[i + 1, j + 1].PutValue(revenue[i, j]); | |
| } | |
| // 3. Add the PyramidBar100PercentStacked chart. | |
| int idx = ws.Charts.Add(ChartType.PyramidBar100PercentStacked, 6, 0, 24, 12); | |
| Chart chart = ws.Charts[idx]; | |
| chart.Title.Text = "Product Revenue Share ¨C 100% Pyramid"; | |
| // 4. Add three series (Product A, B, C). | |
| int index = chart.NSeries.Add("=Revenue!$B$2:$B$5", true); | |
| chart.NSeries[index].Name = "Product A"; | |
| index = chart.NSeries.Add("=Revenue!$C$2:$C$5", true); | |
| chart.NSeries[index].Name = "Product B"; | |
| index = chart.NSeries.Add("=Revenue!$D$2:$D$5", true); | |
| chart.NSeries[index].Name = "Product C"; | |
| // 5. Bind region names as category labels. | |
| chart.NSeries.CategoryData = "=Revenue!$A$2:$A$5"; | |
| // 6. Enable data labels (show values inside each segment). | |
| foreach (Series ser in chart.NSeries) | |
| { | |
| ser.DataLabels.ShowValue = true; | |
| ser.DataLabels.Position = LabelPositionType.Center; // Centered inside segment | |
| } | |
| // 7. Customize series colors. | |
| chart.NSeries[0].Area.ForegroundColor = Color.FromArgb(0, 112, 192); // Blue | |
| chart.NSeries[1].Area.ForegroundColor = Color.FromArgb(255, 192, 0); // Orange | |
| chart.NSeries[2].Area.ForegroundColor = Color.FromArgb(112, 173, 71); // Green | |
| // 8. Format axes. | |
| chart.CategoryAxis.TickLabels.RotationAngle = 30; | |
| // Value axis ¨C display percentages. | |
| chart.ValueAxis.TickLabels.NumberFormat = "0%"; | |
| // 9. Position the legend at the bottom. | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // 10. Save the workbook. | |
| string outPath = "PyramidBar100PercentStacked_Custom.xlsx"; | |
| wb.Save(outPath); | |
| Console.WriteLine($"Customized workbook saved to '{outPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 2. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 3. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C D | |
| // 1 Region Q1 Q2 Q3 | |
| // 2 North 120 150 130 | |
| // 3 South 100 140 110 | |
| // 4 East 130 120 140 | |
| // 5 West 90 130 120 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Region"); | |
| sheet.Cells["B1"].PutValue("Q1"); | |
| sheet.Cells["C1"].PutValue("Q2"); | |
| sheet.Cells["D1"].PutValue("Q3"); | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] sales = { | |
| {120, 150, 130}, | |
| {100, 140, 110}, | |
| {130, 120, 140}, | |
| { 90, 130, 120} | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(regions[i]); // Region name | |
| for (int j = 0; j < 3; j++) // Q1?Q3 values | |
| { | |
| sheet.Cells[i + 1, j + 1].PutValue(sales[i, j]); | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 4. Add a PyramidBar100PercentStacked chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: type, upper?left row, upper?left column, lower?right row, lower?right column | |
| int chartIndex = sheet.Charts.Add(ChartType.PyramidBar100PercentStacked, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C 100% Stacked Pyramid"; | |
| // ------------------------------------------------------------ | |
| // 5. Add series for each quarter. | |
| // ------------------------------------------------------------ | |
| // Series 0 ¨C Q1 | |
| int seriesQ1 = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesQ1].Name = "Q1"; | |
| // Series 1 ¨C Q2 | |
| int seriesQ2 = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesQ2].Name = "Q2"; | |
| // Series 2 ¨C Q3 | |
| int seriesQ3 = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[seriesQ3].Name = "Q3"; | |
| // ------------------------------------------------------------ | |
| // 6. Set category (X?axis) labels ¨C use the Region column. | |
| // ------------------------------------------------------------ | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outFile = "PyramidBar100PercentStacked.xlsx"; | |
| workbook.Save(outFile); | |
| Console.WriteLine($"Workbook '{outFile}' created successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Add sample data that will be visualized by the chart. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| sheet.Cells["E1"].Value = "Q4"; | |
| // Data rows | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] quarterlySales = { | |
| {1200, 1500, 1300, 1700}, | |
| {1000, 1100, 1150, 1400}, | |
| { 900, 950, 980, 1100}, | |
| {1300, 1600, 1500, 1800} | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| // Region name | |
| sheet.Cells[i + 1, 0].Value = regions[i]; | |
| // Quarterly values | |
| for (int q = 0; q < 4; q++) | |
| { | |
| sheet.Cells[i + 1, q + 1].Value = quarterlySales[i, q]; | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Insert a Pyramid Bar chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper-left row, upper-left column, | |
| // lower-right row, lower-right column (in cell units) | |
| int chartIndex = sheet.Charts.Add(ChartType.PyramidBar, 7, 0, 25, 10); | |
| Chart pyramidChart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| pyramidChart.Title.Text = "Quarterly Sales by Region (Pyramid Bar)"; | |
| // Show legend at the bottom | |
| pyramidChart.ShowLegend = true; | |
| pyramidChart.Legend.Position = LegendPositionType.Bottom; | |
| // ------------------------------------------------------------ | |
| // 4. Define the data series for the chart. | |
| // ------------------------------------------------------------ | |
| // Add a series for each quarter (Q1?Q4). The NSeries collection | |
| // automatically creates a series for each column of data. | |
| pyramidChart.NSeries.Add("=SalesData!$B$2:$E$5", true); | |
| pyramidChart.NSeries.CategoryData = "=SalesData!$A$2:$A$5"; | |
| // Optional: customize each series (color, marker, etc.) | |
| Color[] seriesColors = { Color.CornflowerBlue, Color.MediumSeaGreen, | |
| Color.Salmon, Color.Goldenrod }; | |
| for (int i = 0; i < pyramidChart.NSeries.Count; i++) | |
| { | |
| pyramidChart.NSeries[i].Name = $"Q{i + 1}"; | |
| pyramidChart.NSeries[i].Border.Color = seriesColors[i]; | |
| pyramidChart.NSeries[i].Border.Weight = WeightType.HairLine; | |
| } | |
| // ------------------------------------------------------------ | |
| // 5. Customize axes (titles, number format). | |
| // ------------------------------------------------------------ | |
| pyramidChart.CategoryAxis.Title.Text = "Region"; | |
| pyramidChart.ValueAxis.Title.Text = "Sales (USD)"; | |
| pyramidChart.ValueAxis.TickLabels.NumberFormat = "$#,##0"; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "PyramidBarChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook with Pyramid Bar chart saved to: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample hierarchical data. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].Value = "Region"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| sheet.Cells["E1"].Value = "Q4"; | |
| // Data rows | |
| string[] regions = { "North", "South", "East", "West" }; | |
| double[,] quarterly = { | |
| { 120, 150, 130, 170 }, // North | |
| { 80, 110, 95, 130 }, // South | |
| { 100, 130, 115, 150 }, // East | |
| { 90, 120, 105, 140 } // West | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = regions[i]; | |
| for (int j = 0; j < 4; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].Value = quarterly[i, j]; | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Pyramid Bar Stacked chart (base type: PyramidBarStacked). | |
| // ----------------------------------------------------------------- | |
| // Chart position: rows 7?25, columns B?I (indexes are zero?based) | |
| int chartIndex = sheet.Charts.Add(ChartType.PyramidBarStacked, 6, 1, 25, 8); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C Pyramid Bar Stacked"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define series ¨C each quarter will be a separate stacked segment. | |
| // ----------------------------------------------------------------- | |
| // Series 1 ¨C Q1 | |
| int seriesIdx = chart.NSeries.Add("=SalesData!$B$2:$B$5", true); | |
| chart.NSeries[seriesIdx].Name = "Q1"; | |
| // Series 2 ¨C Q2 | |
| seriesIdx = chart.NSeries.Add("=SalesData!$C$2:$C$5", true); | |
| chart.NSeries[seriesIdx].Name = "Q2"; | |
| // Series 3 ¨C Q3 | |
| seriesIdx = chart.NSeries.Add("=SalesData!$D$2:$D$5", true); | |
| chart.NSeries[seriesIdx].Name = "Q3"; | |
| // Series 4 ¨C Q4 | |
| seriesIdx = chart.NSeries.Add("=SalesData!$E$2:$E$5", true); | |
| chart.NSeries[seriesIdx].Name = "Q4"; | |
| // ----------------------------------------------------------------- | |
| // 5. Optional customisation ¨C colors, axis titles, and legend. | |
| // ----------------------------------------------------------------- | |
| // Set distinct colors for each quarter | |
| Color[] palette = { Color.CornflowerBlue, Color.SeaGreen, Color.Orange, Color.DarkMagenta }; | |
| for (int i = 0; i < chart.NSeries.Count; i++) | |
| { | |
| chart.NSeries[i].Area.ForegroundColor = palette[i]; | |
| } | |
| // Axis titles | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Sales (Units)"; | |
| // Legend position | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "PyramidBarStackedChart.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample hierarchical data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Category Amount | |
| // 2 Lead 5000 | |
| // 3 Prospect 3500 | |
| // 4 Qualified 2000 | |
| // 5 Proposal 1200 | |
| // 6 Closed 800 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Amount"); | |
| string[] categories = { "Lead", "Prospect", "Qualified", "Proposal", "Closed" }; | |
| double[] amounts = { 5000, 3500, 2000, 1200, 800 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(categories[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(amounts[i]); // Column B | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Pyramid chart (base type) to the worksheet. | |
| // ------------------------------------------------------------ | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIdx = sheet.Charts.Add(ChartType.Pyramid, 7, 0, 25, 10); | |
| Chart pyramidChart = sheet.Charts[chartIdx]; | |
| // Set a meaningful title. | |
| pyramidChart.Title.Text = "Sales Funnel ¨C Pyramid Chart"; | |
| // ------------------------------------------------------------ | |
| // 4. Define the series for the chart. | |
| // ------------------------------------------------------------ | |
| // The first (and only) series uses the amount column. | |
| // Category (X) data comes from column A. | |
| pyramidChart.NSeries.Add("=SalesData!$B$2:$B$6", true); | |
| pyramidChart.NSeries[0].Name = "Amount"; | |
| // Bind the category (X?axis) data. | |
| pyramidChart.NSeries.CategoryData = "=SalesData!$A$2:$A$6"; | |
| // Optional: format the data labels to show values. | |
| pyramidChart.NSeries[0].DataLabels.ShowValue = true; | |
| // ------------------------------------------------------------ | |
| // 5. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "PyramidChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Pyramid chart created successfully ¡ú {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| // ------------------------------------------------------------ | |
| // 2. Write sample data that will be displayed in the chart. | |
| // ------------------------------------------------------------ | |
| // Header row | |
| cells["A1"].PutValue("Product"); | |
| cells["B1"].PutValue("Q1 Sales"); | |
| cells["C1"].PutValue("Q2 Sales"); | |
| cells["D1"].PutValue("Q3 Sales"); | |
| cells["E1"].PutValue("Q4 Sales"); | |
| // Data rows | |
| string[] products = { "Laptop", "Tablet", "Smartphone", "Desktop", "Monitor" }; | |
| double[,] sales = { | |
| { 150, 180, 210, 190 }, | |
| { 120, 140, 160, 150 }, | |
| { 200, 230, 250, 240 }, | |
| { 100, 110, 130, 120 }, | |
| { 80, 95, 110, 105 } | |
| }; | |
| for (int i = 0; i < products.Length; i++) | |
| { | |
| cells[i + 1, 0].PutValue(products[i]); // Column A ¨C product name | |
| for (int j = 0; j < 4; j++) | |
| { | |
| cells[i + 1, j + 1].PutValue(sales[i, j]); // Columns B?E ¨C quarterly sales | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Insert a PyramidColumn3D chart. | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.PyramidColumn3D, 7, 0, 27, 10); | |
| Chart pyramidChart = sheet.Charts[chartIndex]; | |
| pyramidChart.Title.Text = "Quarterly Sales ¨C Pyramid 3D"; | |
| // ------------------------------------------------------------ | |
| // 4. Define the chart data range. | |
| // - Series data (values) ¨C columns B?E (rows 2?6) | |
| // - Category data (X?Axis) ¨C column A (rows 2?6) | |
| // ------------------------------------------------------------ | |
| pyramidChart.NSeries.Add("=Sheet1!$B$2:$E$6", true); | |
| pyramidChart.NSeries.CategoryData = "=Sheet1!$A$2:$A$6"; | |
| // ------------------------------------------------------------ | |
| // 5. Save the workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save("PyramidColumn3D_Basic.xlsx"); | |
| Console.WriteLine("Workbook saved as PyramidColumn3D_Basic.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create workbook and fill data (same as basic example). | |
| // ------------------------------------------------------------ | |
| Workbook wb = new Workbook(); | |
| Worksheet ws = wb.Worksheets[0]; | |
| Cells c = ws.Cells; | |
| c["A1"].PutValue("Region"); | |
| c["B1"].PutValue("2019"); | |
| c["C1"].PutValue("2020"); | |
| c["D1"].PutValue("2021"); | |
| string[] regions = { "North", "South", "East", "West", "Central" }; | |
| double[,] revenue = { | |
| { 85, 95, 105 }, | |
| { 70, 80, 90 }, | |
| { 60, 75, 85 }, | |
| { 55, 65, 78 }, | |
| { 50, 60, 70 } | |
| }; | |
| for (int i = 0; i < regions.Length; i++) | |
| { | |
| c[i + 1, 0].PutValue(regions[i]); | |
| for (int j = 0; j < 3; j++) | |
| { | |
| c[i + 1, j + 1].PutValue(revenue[i, j]); | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 2. Add a PyramidColumn3D chart. | |
| // ------------------------------------------------------------ | |
| int idx = ws.Charts.Add(ChartType.PyramidColumn3D, 6, 0, 26, 12); | |
| Chart chart = ws.Charts[idx]; | |
| chart.Title.Text = "Annual Revenue ¨C 3D Pyramid"; | |
| // ------------------------------------------------------------ | |
| // 3. Define data series and categories. | |
| // ------------------------------------------------------------ | |
| chart.NSeries.Add("=Sheet1!$B$2:$D$6", true); | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$6"; | |
| // ------------------------------------------------------------ | |
| // 4. Customize each series (three years) with distinct colors. | |
| // ------------------------------------------------------------ | |
| Color[] palette = { Color.CornflowerBlue, Color.MediumSeaGreen, Color.IndianRed }; | |
| for (int s = 0; s < chart.NSeries.Count; s++) | |
| { | |
| chart.NSeries[s].Area.ForegroundColor = palette[s]; | |
| chart.NSeries[s].Border.IsVisible = false; | |
| chart.NSeries[s].DataLabels.ShowValue = true; | |
| chart.NSeries[s].DataLabels.Position = LabelPositionType.OutsideEnd; | |
| } | |
| // ------------------------------------------------------------ | |
| // 5. Legend ¨C place it at the bottom and use a transparent background. | |
| // ------------------------------------------------------------ | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| chart.Legend.Area.Formatting = FormattingType.None; | |
| // ------------------------------------------------------------ | |
| // 6. Axis titles and formatting. | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Region"; | |
| chart.ValueAxis.Title.Text = "Revenue (Million $)"; | |
| chart.ValueAxis.TickLabels.NumberFormat = "\"$\"0.##"; | |
| // Optional ¨C set primary axis line color. | |
| chart.ValueAxis.AxisLine.Color = Color.DarkGray; | |
| // ------------------------------------------------------------ | |
| // 7. Save the customized workbook. | |
| // ------------------------------------------------------------ | |
| wb.Save("PyramidColumn3D_Customized.xlsx"); | |
| Console.WriteLine("Workbook saved as PyramidColumn3D_Customized.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialise a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample hierarchical data. | |
| // ----------------------------------------------------------------- | |
| // Data layout: | |
| // A B C D | |
| // 1 Category Q1 Q2 Q3 | |
| // 2 Food 30 20 25 | |
| // 3 Clothing 15 10 12 | |
| // 4 Electronics5 8 7 | |
| // ----------------------------------------------------------------- | |
| string[] categories = { "Food", "Clothing", "Electronics" }; | |
| double[] q1 = { 30, 15, 5 }; | |
| double[] q2 = { 20, 10, 8 }; | |
| double[] q3 = { 25, 12, 7 }; | |
| // Header row | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Q1"); | |
| sheet.Cells["C1"].PutValue("Q2"); | |
| sheet.Cells["D1"].PutValue("Q3"); | |
| // Fill data rows | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(categories[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(q1[i]); // Column B | |
| sheet.Cells[i + 1, 2].PutValue(q2[i]); // Column C | |
| sheet.Cells[i + 1, 3].PutValue(q3[i]); // Column D | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Pyramid Stacked chart. | |
| // ----------------------------------------------------------------- | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column. | |
| int chartIndex = sheet.Charts.Add(ChartType.PyramidStacked, 6, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Sales ¨C Pyramid Stacked"; | |
| // ----------------------------------------------------------------- | |
| // 4. Set the data source for the chart. | |
| // ----------------------------------------------------------------- | |
| // Category (X?axis) data ¨C column A, rows 2?4. | |
| chart.NSeries.CategoryData = "=SalesData!$A$2:$A$4"; | |
| // Series data ¨C each quarter becomes a separate series. | |
| chart.NSeries.Add("=SalesData!$B$2:$B$4", true); // Q1 | |
| chart.NSeries[0].Name = "Q1"; | |
| chart.NSeries.Add("=SalesData!$C$2:$C$4", true); // Q2 | |
| chart.NSeries[1].Name = "Q2"; | |
| chart.NSeries.Add("=SalesData!$D$2:$D$4", true); // Q3 | |
| chart.NSeries[2].Name = "Q3"; | |
| // ----------------------------------------------------------------- | |
| // 5. Optional ¨C customize axes titles and formatting. | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Product Category"; | |
| chart.ValueAxis.Title.Text = "Sales (Units)"; | |
| // Show data labels for better readability | |
| foreach (Series series in chart.NSeries) | |
| { | |
| series.DataLabels.ShowValue = true; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "PyramidStackedChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook. | |
| var workbook = new Workbook(); | |
| // 2. Get the first worksheet. | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 3. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C | |
| // 1 Metric Car A Car B | |
| // 2 Speed 80 70 | |
| // 3 Reliability 90 85 | |
| // 4 Comfort 75 80 | |
| // 5 Safety 95 90 | |
| // 6 Efficiency 85 80 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Metric"; | |
| sheet.Cells["B1"].Value = "Car A"; | |
| sheet.Cells["C1"].Value = "Car B"; | |
| string[] metrics = { "Speed", "Reliability", "Comfort", "Safety", "Efficiency" }; | |
| double[] carA = { 80, 90, 75, 95, 85 }; | |
| double[] carB = { 70, 85, 80, 90, 80 }; | |
| for (int i = 0; i < metrics.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = metrics[i]; | |
| sheet.Cells[i + 1, 1].Value = carA[i]; | |
| sheet.Cells[i + 1, 2].Value = carB[i]; | |
| } | |
| // 4. Add a RadarFilled chart. | |
| int chartIndex = sheet.Charts.Add(ChartType.RadarFilled, 8, 0, 28, 15); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Car Performance Comparison"; | |
| // 5. Add series for Car A and Car B. | |
| // The first parameter defines the Y?values range. | |
| // Set the second parameter to true so that the X?axis (category) data is taken from column A. | |
| int seriesA = chart.NSeries.Add("=Sheet1!$B$2:$B$6", true); | |
| chart.NSeries[seriesA].Name = "Car A"; | |
| int seriesB = chart.NSeries.Add("=Sheet1!$C$2:$C$6", true); | |
| chart.NSeries[seriesB].Name = "Car B"; | |
| // 6. Save the workbook. | |
| workbook.Save("RadarFilledChart_Basic.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Initialise workbook and worksheet. | |
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| // 2. Insert data (same layout as the basic example). | |
| ws.Cells["A1"].Value = "Metric"; | |
| ws.Cells["B1"].Value = "Product X"; | |
| ws.Cells["C1"].Value = "Product Y"; | |
| string[] categories = { "Quality", "Durability", "Design", "Price", "Innovation" }; | |
| double[] productX = { 85, 78, 92, 70, 88 }; | |
| double[] productY = { 80, 82, 85, 75, 90 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| ws.Cells[i + 1, 0].Value = categories[i]; | |
| ws.Cells[i + 1, 1].Value = productX[i]; | |
| ws.Cells[i + 1, 2].Value = productY[i]; | |
| } | |
| // 3. Add a RadarFilled chart. | |
| int idx = ws.Charts.Add(ChartType.RadarFilled, 10, 0, 30, 15); | |
| Chart radar = ws.Charts[idx]; | |
| radar.Title.Text = "Product Comparison"; | |
| // 4. Create series. | |
| int sX = radar.NSeries.Add("=Sheet1!$B$2:$B$6", true); | |
| radar.NSeries[sX].Name = "Product X"; | |
| int sY = radar.NSeries.Add("=Sheet1!$C$2:$C$6", true); | |
| radar.NSeries[sY].Name = "Product Y"; | |
| // 5. Custom fill colors for each series. | |
| radar.NSeries[sX].Area.ForegroundColor = Color.FromArgb(102, 153, 255); // light blue | |
| radar.NSeries[sY].Area.ForegroundColor = Color.FromArgb(255, 153, 102); // light orange | |
| // 6. Add markers to make data points more visible. | |
| radar.NSeries[sX].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| radar.NSeries[sX].Marker.MarkerSize = 12; | |
| radar.NSeries[sY].Marker.MarkerStyle = ChartMarkerType.Square; | |
| radar.NSeries[sY].Marker.MarkerSize = 12; | |
| // 7. Axis titles (optional for radar charts ¨C they appear as a legend in the plot area). | |
| radar.CategoryAxis.Title.Text = "Metrics"; | |
| radar.ValueAxis.Title.Text = "Score"; | |
| // 8. Adjust the chart style ¨C hide the border for a cleaner look. | |
| radar.PlotArea.Border.IsVisible = false; | |
| // 9. Save the result. | |
| wb.Save("RadarFilledChart_Customized.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Performance"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // ----------------------------------------------------------------- | |
| // A B C D E | |
| // 1 Metric Q1 Q2 Q3 Q4 | |
| // 2 Sales 130 150 170 190 | |
| // 3 Profit 40 55 65 80 | |
| // 4 Growth 10 20 30 40 | |
| // ----------------------------------------------------------------- | |
| sheet.Cells["A1"].Value = "Metric"; | |
| sheet.Cells["B1"].Value = "Q1"; | |
| sheet.Cells["C1"].Value = "Q2"; | |
| sheet.Cells["D1"].Value = "Q3"; | |
| sheet.Cells["E1"].Value = "Q4"; | |
| string[] metrics = { "Sales", "Profit", "Growth" }; | |
| double[,] values = { | |
| {130, 150, 170, 190}, | |
| { 40, 55, 65, 80}, | |
| { 10, 20, 30, 40} | |
| }; | |
| for (int i = 0; i < metrics.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = metrics[i]; | |
| for (int j = 0; j < 4; j++) | |
| { | |
| sheet.Cells[i + 1, j + 1].Value = values[i, j]; | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a RadarWithDataMarkers chart. | |
| // ----------------------------------------------------------------- | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column | |
| // ----------------------------------------------------------------- | |
| int chartIdx = sheet.Charts.Add(ChartType.RadarWithDataMarkers, 6, 0, 26, 10); | |
| Chart radarChart = sheet.Charts[chartIdx]; | |
| radarChart.Title.Text = "Quarterly Performance Radar"; | |
| // Assign the category (X) data ¨C the quarter labels. | |
| radarChart.NSeries.CategoryData = "Performance!$B$1:$E$1"; | |
| // ----------------------------------------------------------------- | |
| // 4. Add a series for each metric. | |
| // ----------------------------------------------------------------- | |
| // The NSeries collection expects a formula string that points to | |
| // the range of Y?values. The second argument indicates that the | |
| // series should be plotted on the primary axis (true). | |
| // ----------------------------------------------------------------- | |
| for (int i = 0; i < metrics.Length; i++) | |
| { | |
| // Example formula: =Performance!$B$2:$E$2 (row i+2) | |
| string seriesFormula = $"=Performance!$B${i + 2}:$E${i + 2}"; | |
| int seriesIdx = radarChart.NSeries.Add(seriesFormula, true); | |
| radarChart.NSeries[seriesIdx].Name = metrics[i]; // Series name from column A | |
| // Customize data markers for better readability. | |
| radarChart.NSeries[seriesIdx].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| radarChart.NSeries[seriesIdx].Marker.MarkerSize = 10; | |
| radarChart.NSeries[seriesIdx].Marker.Area.ForegroundColor = GetSeriesColor(i); | |
| radarChart.NSeries[seriesIdx].Marker.Border.IsVisible = true; | |
| radarChart.NSeries[seriesIdx].Marker.Border.Color = Color.Black; | |
| } | |
| // ----------------------------------------------------------------- | |
| // 5. Fine?tune axes and legend (optional). | |
| // ----------------------------------------------------------------- | |
| radarChart.Legend.Position = LegendPositionType.Right; | |
| radarChart.ValueAxis.MinorUnit = 10; | |
| radarChart.ValueAxis.MaxValue = 200; | |
| radarChart.ValueAxis.MajorUnit = 50; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook to an XLSX file. | |
| // ----------------------------------------------------------------- | |
| string outPath = "RadarWithDataMarkers_Example.xlsx"; | |
| workbook.Save(outPath); | |
| Console.WriteLine($"Workbook saved to {outPath}"); | |
| /// <summary> | |
| /// Returns a distinct color for each series based on its index. | |
| /// </summary> | |
| private static Color GetSeriesColor(int index) | |
| { | |
| Color[] palette = { | |
| Color.FromArgb(0x4F, 0x81, 0xBD), // Blue | |
| Color.FromArgb(0xC0, 0x50, 0x4D), // Red | |
| Color.FromArgb(0x9B, 0xBA, 0x59) // Green | |
| }; | |
| return palette[index % palette.Length]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Data"; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample X?Y data. | |
| // Column A ¨C X values, Column B ¨C Y values. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].Value = "X Value"; | |
| sheet.Cells["B1"].Value = "Y Value"; | |
| // Sample data points | |
| double[] xValues = { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
| double[] yValues = { 2.5, 3.6, 1.8, 4.2, 3.9, 5.1, 4.8, 6.0 }; | |
| for (int i = 0; i < xValues.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = xValues[i]; // Column A (0?based) | |
| sheet.Cells[i + 1, 1].Value = yValues[i]; // Column B (1?based) | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Scatter chart to the worksheet. | |
| // ----------------------------------------------------------------- | |
| // Parameters: Chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column | |
| int chartIndex = sheet.Charts.Add(ChartType.Scatter, 10, 0, 30, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Sample Scatter Chart"; | |
| chart.ShowLegend = true; | |
| // ----------------------------------------------------------------- | |
| // 4. Define the data range for the series. | |
| // The first (and only) series uses column A for X values and | |
| // column B for Y values. | |
| // ----------------------------------------------------------------- | |
| // Add a series ¨C the first argument is the Y?range, the second | |
| // boolean indicates that the series should be plotted on the primary axis. | |
| int seriesIdx = chart.NSeries.Add("=Data!$B$2:$B$9", true); | |
| chart.NSeries[seriesIdx].XValues = "=Data!$A$2:$A$9"; | |
| chart.NSeries[seriesIdx].Name = "Data Points"; | |
| // ----------------------------------------------------------------- | |
| // 5. Customize the series appearance. | |
| // ----------------------------------------------------------------- | |
| // Use circular markers, size 10, with a solid red fill. | |
| chart.NSeries[seriesIdx].Type = ChartType.Scatter; // Explicit type | |
| chart.NSeries[seriesIdx].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[seriesIdx].Marker.MarkerSize = 10; | |
| chart.NSeries[seriesIdx].Marker.Area.ForegroundColor = Color.Red; | |
| chart.NSeries[seriesIdx].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[seriesIdx].Border.IsVisible = false; // No line around markers | |
| // ----------------------------------------------------------------- | |
| // 6. Configure axes titles and scaling (optional but typical). | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "X Axis"; | |
| chart.ValueAxis.Title.Text = "Y Axis"; | |
| // Example: set X axis minimum/maximum to improve readability | |
| chart.CategoryAxis.MinValue = 0; | |
| chart.CategoryAxis.MaxValue = 9; | |
| chart.ValueAxis.MinValue = 0; | |
| chart.ValueAxis.MaxValue = 7; | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook to an Excel file. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "ScatterChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Scatter chart created successfully: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----- Create workbook and add data (same as previous example) ----- | |
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| ws.Name = "Data"; | |
| ws.Cells["A1"].PutValue("X"); | |
| ws.Cells["B1"].PutValue("Y"); | |
| double[] x = { 1, 2, 3, 4, 5 }; | |
| double[] y = { 2, 4, 1, 5, 3 }; | |
| for (int i = 0; i < x.Length; i++) | |
| { | |
| ws.Cells[i + 1, 0].PutValue(x[i]); | |
| ws.Cells[i + 1, 1].PutValue(y[i]); | |
| } | |
| // ----- Add the ScatterConnectedByCurvesWithDataMarker chart ----- | |
| int chartIdx = ws.Charts.Add(ChartType.ScatterConnectedByCurvesWithDataMarker, 7, 0, 27, 10); | |
| Chart chart = ws.Charts[chartIdx]; | |
| chart.Title.Text = "Customized Scatter Chart"; | |
| // ----- Add series ----- | |
| int seriesIdx = chart.NSeries.Add("=Data!$A$2:$A$6", true); | |
| chart.NSeries[seriesIdx].Values = "=Data!$B$2:$B$6"; | |
| chart.NSeries[seriesIdx].Name = "Demo Series"; | |
| // ----- Customize the data marker for the series ----- | |
| // Access the marker object | |
| var marker = chart.NSeries[seriesIdx].Marker; | |
| // Shape: Diamond | |
| marker.MarkerStyle = ChartMarkerType.Diamond; | |
| // Size: 12 points | |
| marker.MarkerSize = 12; | |
| // Fill color: LightSeaGreen | |
| marker.Area.ForegroundColor = Color.LightSeaGreen; | |
| marker.Area.Formatting = FormattingType.Custom; // Use custom fill | |
| // Border: Solid, 1.5 pt, DarkSlateGray | |
| marker.Border.IsVisible = true; | |
| marker.Border.Color = Color.DarkSlateGray; | |
| marker.Border.Weight = WeightType.HairLine; | |
| marker.Border.Style = LineType.Solid; | |
| // (Optional) Add a second series to show a different marker style | |
| int seriesIdx2 = chart.NSeries.Add("=Data!$A$2:$A$6", true); | |
| chart.NSeries[seriesIdx2].Values = "=Data!$B$2:$B$6"; | |
| chart.NSeries[seriesIdx2].Name = "Secondary Series"; | |
| // Make the second series a simple line to highlight the first series markers | |
| chart.NSeries[seriesIdx2].Type = ChartType.Line; | |
| chart.NSeries[seriesIdx2].Border.Color = Color.Gray; | |
| // ----- Save results ----- | |
| string outPath = "ScatterChart_CustomMarkers.xlsx"; | |
| wb.Save(outPath); | |
| Console.WriteLine($"Customized chart saved to {outPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Data"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample X?Y data. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].PutValue("X Value"); | |
| sheet.Cells["B1"].PutValue("Y Value"); | |
| // Sample data points | |
| double[] xValues = { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
| double[] yValues = { 3, 5, 2, 8, 7, 6, 9, 4 }; | |
| for (int i = 0; i < xValues.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(xValues[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(yValues[i]); // Column B | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a ScatterConnectedByCurvesWithDataMarker chart. | |
| // ----------------------------------------------------------------- | |
| // Parameters: chart type, upper?left row, column, lower?right row, column | |
| int chartIndex = sheet.Charts.Add( | |
| ChartType.ScatterConnectedByCurvesWithDataMarker, // Desired chart type | |
| 10, 0, 30, 10); // Position on the sheet | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Scatter Connected By Curves With Data Markers"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define the data series. | |
| // ----------------------------------------------------------------- | |
| // Add a series that uses columns A (X) and B (Y) as the source. | |
| int seriesIndex = chart.NSeries.Add("=Data!$A$2:$A$9", true); | |
| chart.NSeries[seriesIndex].Values = "=Data!$B$2:$B$9"; | |
| chart.NSeries[seriesIndex].Name = "Sample Series"; | |
| // ----------------------------------------------------------------- | |
| // 5. Customize axes titles (optional but improves readability). | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "X Axis"; | |
| chart.ValueAxis.Title.Text = "Y Axis"; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "ScatterChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and get the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with X?Y data. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| cells["A1"].PutValue("X Value"); | |
| cells["B1"].PutValue("Y Value"); | |
| // Sample data points | |
| double[] xValues = { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
| double[] yValues = { 3, 5, 2, 8, 6, 9, 4, 7 }; | |
| for (int i = 0; i < xValues.Length; i++) | |
| { | |
| // Row index is i+1 because row 0 holds the headers. | |
| cells[i + 1, 0].PutValue(xValues[i]); // Column A (X) | |
| cells[i + 1, 1].PutValue(yValues[i]); // Column B (Y) | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a chart of type ScatterConnectedByCurvesWithoutMarker. | |
| // ----------------------------------------------------------------- | |
| // Parameters: type, upper left row, upper left column, lower right row, lower right column | |
| int chartIdx = sheet.Charts.Add(ChartType.ScatterConnectedByCurvesWithoutDataMarker, | |
| 10, 0, 30, 10); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Smooth Scatter Curve (No Markers)"; | |
| // ----------------------------------------------------------------- | |
| // 4. Bind the data series to the X?Y ranges. | |
| // ----------------------------------------------------------------- | |
| // The first series automatically uses the X values from column A and Y values from column B. | |
| // We specify the full range ¨C the first row contains the headers. | |
| chart.NSeries.Add("=Sheet1!$A$2:$B$9", true); | |
| chart.NSeries[0].Name = "Sample Trend"; | |
| // ----------------------------------------------------------------- | |
| // 5. Hide the data markers ¨C they are shown by default. | |
| // ----------------------------------------------------------------- | |
| // Set MarkerStyle to None (or remove the marker collection entirely). | |
| chart.NSeries[0].Marker.MarkerStyle = ChartMarkerType.None; | |
| // ----------------------------------------------------------------- | |
| // 6. Optional: customize axes titles and scaling. | |
| // ----------------------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "X Axis"; | |
| chart.ValueAxis.Title.Text = "Y Axis"; | |
| // Ensure the axes start at zero for better visual balance. | |
| chart.CategoryAxis.MinValue = 0; | |
| chart.ValueAxis.MinValue = 0; | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "ScatterConnectedByCurvesWithoutDataMarker.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to \"{outputPath}\""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Data"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate worksheet with sample X?Y data. | |
| // Column A ¨C X values, Column B ¨C Y values. | |
| // ----------------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].Value = "X Value"; | |
| sheet.Cells["B1"].Value = "Y Value"; | |
| // Sample data (you can replace this with your own dataset) | |
| double[] xValues = { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
| double[] yValues = { 2.5, 3.8, 4.1, 5.6, 5.0, 6.3, 7.2, 8.1 }; | |
| for (int i = 0; i < xValues.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = xValues[i]; // Column A (zero?based index) | |
| sheet.Cells[i + 1, 1].Value = yValues[i]; // Column B | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a ScatterConnectedByLinesWithDataMarker chart. | |
| // The chart is placed from row 12, column 0 to row 30, column 9. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add( | |
| ChartType.ScatterConnectedByLinesWithDataMarker, | |
| 12, 0, 30, 9); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set a meaningful title. | |
| chart.Title.Text = "Scatter Chart ¨C Points Connected by Lines"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define the data series. | |
| // The series uses column B (Y values) and column A (X values) as | |
| // category (X) data. | |
| // ----------------------------------------------------------------- | |
| // Create a series that references Y values. | |
| int seriesIndex = chart.NSeries.Add("=Data!$B$2:$B$9", true); | |
| chart.NSeries[seriesIndex].Name = "Sample Series"; | |
| // Associate the X values with the series. | |
| chart.NSeries.CategoryData = "=Data!$A$2:$A$9"; | |
| // Optionally, explicitly set the series type (redundant here because the chart base type already defines it). | |
| chart.NSeries[seriesIndex].Type = ChartType.ScatterConnectedByLinesWithDataMarker; | |
| // ----------------------------------------------------------------- | |
| // 5. Customize the appearance. | |
| // ----------------------------------------------------------------- | |
| // Marker style ¨C use a circle with a solid fill. | |
| chart.NSeries[seriesIndex].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[seriesIndex].Marker.MarkerSize = 12; | |
| chart.NSeries[seriesIndex].Marker.Area.ForegroundColor = Color.Crimson; | |
| chart.NSeries[seriesIndex].Marker.Border.Color = Color.Black; | |
| chart.NSeries[seriesIndex].Marker.Border.IsVisible = true; | |
| // Line style ¨C solid line with a moderate thickness. | |
| chart.NSeries[seriesIndex].Border.Weight = WeightType.HairLine; | |
| chart.NSeries[seriesIndex].Border.Style = LineType.Solid; | |
| chart.NSeries[seriesIndex].Border.Color = Color.SteelBlue; | |
| // Axis titles. | |
| chart.CategoryAxis.Title.Text = "X Axis (Independent Variable)"; | |
| chart.ValueAxis.Title.Text = "Y Axis (Dependent Variable)"; | |
| // Show data labels if you want the exact values displayed. | |
| // Uncomment the following line to enable them: | |
| // chart.NSeries[seriesIndex].HasDataLabels = true; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "ScatterConnectedByLinesWithDataMarker.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "AdvancedData"; | |
| // ----------------------- Sample Data ----------------------- | |
| // A B C | |
| // X1 Y1 Y2 (secondary) | |
| // 1 3 6 | |
| // 2 5 9 | |
| // 3 7 12 | |
| // 4 9 15 | |
| // 5 11 18 | |
| // --------------------------------------------------------- | |
| sheet.Cells["A1"].PutValue("X"); | |
| sheet.Cells["B1"].PutValue("Y Primary"); | |
| sheet.Cells["C1"].PutValue("Y Secondary"); | |
| double[] x = { 1, 2, 3, 4, 5 }; | |
| double[] yPrimary = { 3, 5, 7, 9, 11 }; | |
| double[] ySecondary = { 6, 9, 12, 15, 18 }; | |
| for (int i = 0; i < x.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(x[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(yPrimary[i]); // Column B | |
| sheet.Cells[i + 1, 2].PutValue(ySecondary[i]); // Column C | |
| } | |
| // ----------------------- Chart Creation ----------------------- | |
| int chartIdx = sheet.Charts.Add(ChartType.ScatterConnectedByLinesWithoutDataMarker, | |
| 7, 0, 24, 8); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Dual?Axis Scatter Chart (No Markers)"; | |
| chart.NSeries.CategoryData = "=AdvancedData!$A$2:$A$6"; | |
| // Primary series (Y Primary) | |
| chart.NSeries.Add("=AdvancedData!$B$2:$B$6", true); | |
| chart.NSeries[0].Name = "Primary Series"; | |
| // Apply a thick blue line. | |
| chart.NSeries[0].Border.Color = Color.Blue; | |
| chart.NSeries[0].Border.Weight = WeightType.SingleLine; | |
| // Secondary series (Y Secondary) ¨C plotted on second axis. | |
| chart.NSeries.Add("=AdvancedData!$C$2:$C$6", true); | |
| chart.NSeries[1].Name = "Secondary Series"; | |
| chart.NSeries[1].PlotOnSecondAxis = true; | |
| // Red dashed line. | |
| chart.NSeries[1].Border.Color = Color.Red; | |
| chart.NSeries[1].Border.Style = LineType.Dash; | |
| chart.NSeries[1].Border.Weight = WeightType.HairLine; | |
| // ----------------------- Axis Customization ----------------------- | |
| chart.CategoryAxis.Title.Text = "X Axis"; | |
| chart.ValueAxis.Title.Text = "Primary Y Axis"; | |
| chart.SecondValueAxis.Title.Text = "Secondary Y Axis"; | |
| // Show grid lines only on primary axis. | |
| chart.ValueAxis.MajorGridLines.Color = Color.Red; | |
| chart.SecondValueAxis.MajorGridLines.Color = Color.Blue; | |
| // ----------------------- Export Options ----------------------- | |
| // Save workbook. | |
| workbook.Save("ScatterDualAxis.xlsx"); | |
| // Export chart as PNG. | |
| chart.ToImage("ScatterDualAxis.png", ImageType.Png); | |
| Console.WriteLine("Workbook and chart image have been saved."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and get the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Data"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample X¨CY data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 X Y | |
| // 2 1 2 | |
| // 3 2 4 | |
| // 4 3 6 | |
| // 5 4 8 | |
| // 6 5 10 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("X"); | |
| sheet.Cells["B1"].PutValue("Y"); | |
| double[] xValues = { 1, 2, 3, 4, 5 }; | |
| double[] yValues = { 2, 4, 6, 8, 10 }; | |
| for (int i = 0; i < xValues.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(xValues[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(yValues[i]); // Column B | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a ScatterConnectedByLinesWithoutDataMarker chart. | |
| // ------------------------------------------------------------ | |
| // Chart placed from row 8, column 0 to row 25, column 8. | |
| int chartIndex = sheet.Charts.Add( | |
| ChartType.ScatterConnectedByLinesWithoutDataMarker, | |
| 7, 0, 24, 8); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title. | |
| chart.Title.Text = "Linear Trend (No Markers)"; | |
| // ------------------------------------------------------------ | |
| // 4. Define the series ¨C use column B as Y values and column A as X values. | |
| // ------------------------------------------------------------ | |
| // The series data range should be in the format: | |
| // =SheetName!$A$2:$A$6 for X (CategoryData) and | |
| // =SheetName!$B$2:$B$6 for Y (NSeries[0]). | |
| chart.NSeries.Add("=Data!$B$2:$B$6", true); | |
| chart.NSeries[0].Name = "Y = 2X"; | |
| // Link X values. | |
| chart.NSeries.CategoryData = "=Data!$A$2:$A$6"; | |
| // ------------------------------------------------------------ | |
| // 5. Customize axes (optional but recommended). | |
| // ------------------------------------------------------------ | |
| chart.ValueAxis.Title.Text = "Y Axis"; | |
| chart.CategoryAxis.Title.Text = "X Axis"; | |
| // Set major grid lines for better readability. | |
| chart.ValueAxis.MajorGridLines.Color = Color.Red; | |
| chart.CategoryAxis.MajorGridLines.Color = Color.Blue; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook to XLSX format. | |
| // ------------------------------------------------------------ | |
| string outputPath = "ScatterConnectedByLinesWithoutDataMarker.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and obtain the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Month Sales | |
| // 2 Jan 120 | |
| // 3 Feb 150 | |
| // 4 Mar 180 | |
| // 5 Apr 210 | |
| // ------------------------------------------------------------ | |
| string[] months = { "Jan", "Feb", "Mar", "Apr" }; | |
| double[] sales = { 120, 150, 180, 210 }; | |
| sheet.Cells["A1"].PutValue("Month"); | |
| sheet.Cells["B1"].PutValue("Sales"); | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(months[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(sales[i]); // Column B | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Line chart object. | |
| // ------------------------------------------------------------ | |
| // Parameters: (type, upper left row, upper left column, lower right row, lower right column) | |
| int chartIndex = sheet.Charts.Add(ChartType.Line, 6, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Monthly Sales Trend"; | |
| // ------------------------------------------------------------ | |
| // 4. Add the series ¨C data range refers to the Sales column. | |
| // ------------------------------------------------------------ | |
| int seriesIndex = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries[seriesIndex].Name = "Sales"; | |
| // ------------------------------------------------------------ | |
| // 5. Set category (X?axis) data ¨C months. | |
| // ------------------------------------------------------------ | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // ------------------------------------------------------------ | |
| // 6. Optional: customize axes titles. | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Sales"; | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save("SimpleLineChart.xlsx"); | |
| Console.WriteLine("Workbook with Line chart saved as SimpleLineChart.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and get the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Product Share | |
| // 2 A 40 | |
| // 3 B 25 | |
| // 4 C 20 | |
| // 5 D 15 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Product"; | |
| sheet.Cells["B1"].Value = "Share"; | |
| string[] products = { "A", "B", "C", "D" }; | |
| double[] shares = { 40, 25, 20, 15 }; | |
| for (int i = 0; i < products.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = products[i]; // Column A | |
| sheet.Cells[i + 1, 1].Value = shares[i]; // Column B | |
| } | |
| // 3. Add a Pie chart (base type) to the worksheet. | |
| int chartIndex = sheet.Charts.Add(ChartType.Pie, 6, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // 4. Set chart title. | |
| chart.Title.Text = "Market Share"; | |
| // 5. Define the data range for the series. | |
| // Series data: B2:B5 (Share values) | |
| // Category data: A2:A5 (Product names) | |
| chart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
| // 6. Optional: Show data labels (percentage). | |
| chart.NSeries[0].DataLabels.ShowPercentage = true; | |
| chart.NSeries[0].DataLabels.ShowCategoryName = true; | |
| // 7. Save the workbook to an XLSX file. | |
| workbook.Save("SimplePieChart_Output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B C | |
| // 1 Category Series1 Series2 | |
| // 2 A 5 4 | |
| // 3 B 3 6 | |
| // 4 C 4 5 | |
| // 5 D 7 3 | |
| // 6 E 2 4 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Series1"); | |
| sheet.Cells["C1"].PutValue("Series2"); | |
| string[] categories = { "A", "B", "C", "D", "E" }; | |
| double[] series1 = { 5, 3, 4, 7, 2 }; | |
| double[] series2 = { 4, 6, 5, 3, 4 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(categories[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(series1[i]); // Column B | |
| sheet.Cells[i + 1, 2].PutValue(series2[i]); // Column C | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Radar chart object. | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.Radar, 7, 0, 25, 12); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Simple Radar Chart"; | |
| // ------------------------------------------------------------ | |
| // 4. Add the first series (Series1) ¨C Radar type is inherited. | |
| // ------------------------------------------------------------ | |
| int s1 = chart.NSeries.Add("=Sheet1!$B$2:$B$6", true); | |
| chart.NSeries[s1].Name = "Series1"; | |
| // ------------------------------------------------------------ | |
| // 5. Add the second series (Series2). | |
| // ------------------------------------------------------------ | |
| int s2 = chart.NSeries.Add("=Sheet1!$C$2:$C$6", true); | |
| chart.NSeries[s2].Name = "Series2"; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save(filePath + "SimpleRadarChart.xlsx"); | |
| Console.WriteLine("SimpleRadarChart.xlsx created successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1? Initialize workbook and worksheet. | |
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| // 2? Populate data ¨C three quarters for three regions. | |
| ws.Cells["A1"].Value = "Quarter"; | |
| ws.Cells["B1"].Value = "North"; | |
| ws.Cells["C1"].Value = "South"; | |
| ws.Cells["D1"].Value = "East"; | |
| string[] quarters = { "Q1", "Q2", "Q3", "Q4" }; | |
| double[] north = { 200, 250, 300, 350 }; | |
| double[] south = { 180, 230, 280, 330 }; | |
| double[] east = { 150, 200, 260, 310 }; | |
| for (int i = 0; i < quarters.Length; i++) | |
| { | |
| ws.Cells[i + 1, 0].Value = quarters[i]; | |
| ws.Cells[i + 1, 1].Value = north[i]; | |
| ws.Cells[i + 1, 2].Value = south[i]; | |
| ws.Cells[i + 1, 3].Value = east[i]; | |
| } | |
| // 3? Add a Stacked Area chart. | |
| int idx = ws.Charts.Add(ChartType.AreaStacked, 6, 0, 22, 12); | |
| Chart stackedChart = ws.Charts[idx]; | |
| stackedChart.Title.Text = "Quarterly Revenue ¨C Stacked Area"; | |
| // 4? Add three series ¨C one per region. | |
| int index = stackedChart.NSeries.Add("=Sheet1!$B$2:$B$5", true); | |
| stackedChart.NSeries[index].Name = "North"; | |
| index = stackedChart.NSeries.Add("=Sheet1!$C$2:$C$5", true); | |
| stackedChart.NSeries[index].Name = "South"; | |
| index = stackedChart.NSeries.Add("=Sheet1!$D$2:$D$5", true); | |
| stackedChart.NSeries[index].Name = "East"; | |
| // 5? Axis titles. | |
| stackedChart.CategoryAxis.Title.Text = "Quarter"; | |
| stackedChart.ValueAxis.Title.Text = "Revenue (k$)"; | |
| // 6? Optional: Set a semi?transparent fill for better readability. | |
| stackedChart.PlotArea.Area.Transparency = 0.15; | |
| stackedChart.PlotArea.Area.ForegroundColor = Color.LightBlue; | |
| // 7? Save the file. | |
| string outFile = "StackedAreaChart_Output.xlsx"; | |
| wb.Save(outFile); | |
| Console.WriteLine($"Workbook saved to {outFile}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -------------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // -------------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| sheet.Name = "FinancialData"; | |
| // -------------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample data. | |
| // Column A ¨C Date | |
| // Column B ¨C Open | |
| // Column C ¨C High | |
| // Column D ¨C Low | |
| // Column E ¨C Close | |
| // -------------------------------------------------------------------- | |
| string[] dates = { "2025-10-01", "2025-10-02", "2025-10-03", "2025-10-04", "2025-10-05" }; | |
| double[] opens = { 150.2, 152.5, 151.0, 153.3, 154.8 }; | |
| double[] highs = { 155.0, 156.2, 154.0, 156.5, 158.0 }; | |
| double[] lows = { 148.5, 149.8, 149.0, 151.2, 152.1 }; | |
| double[] closes = { 152.0, 154.0, 150.5, 155.8, 157.3 }; | |
| // Write headers | |
| sheet.Cells["A1"].PutValue("Date"); | |
| sheet.Cells["B1"].PutValue("Open"); | |
| sheet.Cells["C1"].PutValue("High"); | |
| sheet.Cells["D1"].PutValue("Low"); | |
| sheet.Cells["E1"].PutValue("Close"); | |
| // Fill data rows | |
| for (int i = 0; i < dates.Length; i++) | |
| { | |
| int row = i + 1; // 0?based index; first data row is row 1 (Excel row 2) | |
| sheet.Cells[row, 0].PutValue(DateTime.Parse(dates[i])); // Date | |
| sheet.Cells[row, 1].PutValue(opens[i]); // Open | |
| sheet.Cells[row, 2].PutValue(highs[i]); // High | |
| sheet.Cells[row, 3].PutValue(lows[i]); // Low | |
| sheet.Cells[row, 4].PutValue(closes[i]); // Close | |
| } | |
| // Format the date column (optional, makes Excel display dates nicely) | |
| Style dateStyle = sheet.Cells["A2"].GetStyle(); | |
| dateStyle.Number = 14; // Built?in date format | |
| sheet.Cells.CreateRange("A2", "A" + (dates.Length + 1)).SetStyle(dateStyle); | |
| // -------------------------------------------------------------------- | |
| // 3. Add a StockHighLowClose chart. | |
| // -------------------------------------------------------------------- | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column (all zero?based indexes) | |
| int chartIndex = sheet.Charts.Add(ChartType.StockHighLowClose, 7, 0, 26, 10); | |
| Chart stockChart = sheet.Charts[chartIndex]; | |
| stockChart.Title.Text = "Sample Stock High?Low?Close Chart"; | |
| // -------------------------------------------------------------------- | |
| // 4. Set the data range for the chart. | |
| // A2:A6 -> Category axis (dates) | |
| // B2:E6 -> Series data (Open, High, Low, Close) | |
| // -------------------------------------------------------------------- | |
| stockChart.SetChartDataRange("A1:E6", true); | |
| stockChart.NSeries.CategoryData = "A2:A6"; // Dates | |
| // Add individual series. The order must match the chart type (Open, High, Low, Close) | |
| int index = stockChart.NSeries.Add("=FinancialData!$B$2:$B$6", true); | |
| stockChart.NSeries[index].Name = "Open"; | |
| index = stockChart.NSeries.Add("=FinancialData!$C$2:$C$6", true); | |
| stockChart.NSeries[index].Name = "High"; | |
| index = stockChart.NSeries.Add("=FinancialData!$D$2:$D$6", true); | |
| stockChart.NSeries[index].Name = "Low"; | |
| index = stockChart.NSeries.Add("=FinancialData!$E$2:$E$6", true); | |
| stockChart.NSeries[index].Name = "Close"; | |
| // -------------------------------------------------------------------- | |
| // 5. Customize axes (optional but recommended for financial charts) | |
| // -------------------------------------------------------------------- | |
| stockChart.CategoryAxis.Title.Text = "Date"; | |
| stockChart.ValueAxis.Title.Text = "Price"; | |
| // Display major grid lines on the value axis | |
| stockChart.ValueAxis.MajorGridLines.IsVisible = true; | |
| stockChart.ValueAxis.MajorGridLines.Weight = WeightType.SingleLine; | |
| stockChart.ValueAxis.MajorGridLines.Color = Color.LightGray; | |
| // -------------------------------------------------------------------- | |
| // 6. Adjust legend position and marker style. | |
| // -------------------------------------------------------------------- | |
| stockChart.ShowLegend = true; | |
| stockChart.Legend.Position = LegendPositionType.Right; | |
| // Set marker style for better visibility of data points | |
| foreach (Series series in stockChart.NSeries) | |
| { | |
| series.Marker.MarkerStyle = ChartMarkerType.Circle; | |
| series.Marker.MarkerSize = 8; | |
| series.Marker.Area.Formatting = FormattingType.Custom; | |
| series.Marker.Area.ForegroundColor = Color.White; | |
| series.Marker.Border.IsVisible = true; | |
| series.Marker.Border.Color = Color.DarkBlue; | |
| } | |
| // -------------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // -------------------------------------------------------------------- | |
| string outputPath = "StockChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "OHLCData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample Open?High?Low?Close data. | |
| // Column A ¨C Date (as string for simplicity) | |
| // Column B ¨C Open | |
| // Column C ¨C High | |
| // Column D ¨C Low | |
| // Column E ¨C Close | |
| // ----------------------------------------------------------------- | |
| string[] dates = { "2025?01?01", "2025?01?02", "2025?01?03", "2025?01?04", "2025?01?05" }; | |
| double[] opens = { 150.25, 152.10, 149.80, 151.00, 152.75 }; | |
| double[] highs = { 153.40, 153.50, 151.20, 152.80, 154.10 }; | |
| double[] lows = { 149.90, 150.70, 148.60, 149.50, 151.90 }; | |
| double[] closes = { 152.80, 151.20, 150.10, 152.45, 153.55 }; | |
| // Header row | |
| sheet.Cells["A1"].PutValue("Date"); | |
| sheet.Cells["B1"].PutValue("Open"); | |
| sheet.Cells["C1"].PutValue("High"); | |
| sheet.Cells["D1"].PutValue("Low"); | |
| sheet.Cells["E1"].PutValue("Close"); | |
| // Data rows | |
| for (int i = 0; i < dates.Length; i++) | |
| { | |
| int row = i + 1; // +1 because rows are zero?based | |
| sheet.Cells[row, 0].PutValue(dates[i]); | |
| sheet.Cells[row, 1].PutValue(opens[i]); | |
| sheet.Cells[row, 2].PutValue(highs[i]); | |
| sheet.Cells[row, 3].PutValue(lows[i]); | |
| sheet.Cells[row, 4].PutValue(closes[i]); | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a StockOpenHighLowClose chart. | |
| // The chart will be placed from rows 8?30 and columns 0?10. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.StockOpenHighLowClose, 7, 0, 30, 10); | |
| Chart ohlcChart = sheet.Charts[chartIndex]; | |
| ohlcChart.Title.Text = "Sample OHLC Chart"; | |
| // ----------------------------------------------------------------- | |
| // 4. Bind data series to the chart. | |
| // Aspose.Cells expects a range that includes Open, High, Low, | |
| // and Close values in separate columns. | |
| // ----------------------------------------------------------------- | |
| // Series 0 ¨C Open?High?Low?Close (the entire data block) | |
| int seriesIndex = ohlcChart.NSeries.Add("=OHLCData!$B$2:$E$6", true); | |
| ohlcChart.NSeries[seriesIndex].Name = "Price"; | |
| // Category (X?axis) data ¨C the dates column. | |
| ohlcChart.NSeries.CategoryData = "=OHLC Data!$A$2:$A$6"; | |
| // ----------------------------------------------------------------- | |
| // 5. Optional: Customize chart appearance. | |
| // ----------------------------------------------------------------- | |
| // Set the chart background to light gray. | |
| ohlcChart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| ohlcChart.PlotArea.Area.ForegroundColor = Color.FromArgb(245, 245, 245); | |
| // Show grid lines on the value axis. | |
| ohlcChart.ValueAxis.MajorGridLines.IsVisible = true; | |
| ohlcChart.ValueAxis.MajorGridLines.Weight = WeightType.SingleLine; | |
| ohlcChart.ValueAxis.MajorGridLines.Color = Color.LightGray; | |
| // Set axis titles. | |
| ohlcChart.CategoryAxis.Title.Text = "Date"; | |
| ohlcChart.ValueAxis.Title.Text = "Price (USD)"; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook to XLSX (or any other supported format). | |
| // ----------------------------------------------------------------- | |
| string outputPath = "StockOHLCChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------- | |
| // 1. Initialise workbook & sheet. | |
| // ------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "AdvancedData"; | |
| // ------------------------------------------------- | |
| // 2. Insert sample data ¨C Month, Open, High, Low, | |
| // Close, Volume (optional for secondary axis). | |
| // ------------------------------------------------- | |
| // A B C D E F | |
| // 1 Month Open High Low Close Volume | |
| // 2 Jan 124 130 120 128 5000 | |
| // 3 Feb 128 135 125 132 6200 | |
| // 4 Mar 132 140 130 138 5400 | |
| // 5 Apr 138 145 136 142 7100 | |
| // ------------------------------------------------- | |
| sheet.Cells["A1"].PutValue("Month"); | |
| sheet.Cells["B1"].PutValue("Open"); | |
| sheet.Cells["C1"].PutValue("High"); | |
| sheet.Cells["D1"].PutValue("Low"); | |
| sheet.Cells["E1"].PutValue("Close"); | |
| sheet.Cells["F1"].PutValue("Volume"); | |
| string[] months = { "Jan", "Feb", "Mar", "Apr" }; | |
| double[] open = { 124, 128, 132, 138 }; | |
| double[] high = { 130, 135, 140, 145 }; | |
| double[] low = { 120, 125, 130, 136 }; | |
| double[] close = { 128, 132, 138, 142 }; | |
| double[] volume = { 5000, 6200, 5400, 7100 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| int row = i + 1; | |
| sheet.Cells[row, 0].PutValue(months[i]); | |
| sheet.Cells[row, 1].PutValue(open[i]); | |
| sheet.Cells[row, 2].PutValue(high[i]); | |
| sheet.Cells[row, 3].PutValue(low[i]); | |
| sheet.Cells[row, 4].PutValue(close[i]); | |
| sheet.Cells[row, 5].PutValue(volume[i]); | |
| } | |
| // ------------------------------------------------- | |
| // 3. Add the StockVolumeHighLowClose chart. | |
| // ------------------------------------------------- | |
| int chartIdx = sheet.Charts.Add( | |
| ChartType.StockVolumeHighLowClose, 7, 0, 27, 12); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "Advanced Stock Chart with Volume"; | |
| // ------------------------------------------------- | |
| // 4. Bind data ranges. | |
| // ------------------------------------------------- | |
| chart.NSeries.CategoryData = "=AdvancedData!$A$2:$A$5"; | |
| // Series order: Open, High, Low, Close, Volume | |
| chart.NSeries.Add("=AdvancedData!$B$2:$B$5", true); // Open | |
| chart.NSeries.Add("=AdvancedData!$C$2:$C$5", true); // High | |
| chart.NSeries.Add("=AdvancedData!$D$2:$D$5", true); // Low | |
| chart.NSeries.Add("=AdvancedData!$E$2:$E$5", true); // Close | |
| chart.NSeries.Add("=AdvancedData!$F$2:$F$5", true); // Volume | |
| // ------------------------------------------------- | |
| // 5. Customise each series. | |
| // ------------------------------------------------- | |
| // Open ¨C Blue line, square markers. | |
| chart.NSeries[0].Border.Color = Color.Blue; | |
| chart.NSeries[0].Border.Style = LineType.Solid; | |
| chart.NSeries[0].Marker.MarkerStyle = ChartMarkerType.Square; | |
| chart.NSeries[0].Marker.MarkerSize = 12; | |
| chart.NSeries[0].Marker.Area.ForegroundColor = Color.LightBlue; | |
| // High ¨C Dark green line, circle markers. | |
| chart.NSeries[1].Border.Color = Color.DarkGreen; | |
| chart.NSeries[1].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[1].Marker.MarkerSize = 10; | |
| chart.NSeries[1].Marker.Area.ForegroundColor = Color.YellowGreen; | |
| // Low ¨C Red line, triangle markers. | |
| chart.NSeries[2].Border.Color = Color.Red; | |
| chart.NSeries[2].Marker.MarkerStyle = ChartMarkerType.Triangle; | |
| chart.NSeries[2].Marker.MarkerSize = 10; | |
| chart.NSeries[2].Marker.Area.ForegroundColor = Color.Pink; | |
| // Close ¨C Purple line, diamond markers. | |
| chart.NSeries[3].Border.Color = Color.Purple; | |
| chart.NSeries[3].Marker.MarkerStyle = ChartMarkerType.Diamond; | |
| chart.NSeries[3].Marker.MarkerSize = 12; | |
| chart.NSeries[3].Marker.Area.ForegroundColor = Color.Plum; | |
| // Volume ¨C Use a secondary axis (column type) to show trading volume. | |
| chart.NSeries[4].Type = ChartType.Column; // Change series type to Column. | |
| chart.NSeries[4].PlotOnSecondAxis = true; // Move to secondary axis. | |
| chart.NSeries[4].Border.IsVisible = false; | |
| chart.NSeries[4].Area.ForegroundColor = Color.Gray; | |
| chart.NSeries[4].Area.Formatting = FormattingType.Custom; | |
| // ------------------------------------------------- | |
| // 6. Axis titles and secondary axis formatting. | |
| // ------------------------------------------------- | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Price"; | |
| chart.SecondValueAxis.Title.Text = "Volume"; | |
| chart.SecondValueAxis.IsAutomaticMaxValue = false; | |
| chart.SecondValueAxis.MaxValue = 8000; // Example max for volume. | |
| // ------------------------------------------------- | |
| // 7. Legend and plot area style. | |
| // ------------------------------------------------- | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| chart.PlotArea.Area.Formatting = FormattingType.None; // Transparent background. | |
| // ------------------------------------------------- | |
| // 8. Save the workbook. | |
| // ------------------------------------------------- | |
| workbook.Save("StockVolumeHighLowClose_Customized.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------- | |
| // 1. Create a new workbook. | |
| // ------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "MarketData"; | |
| // ------------------------------------------------- | |
| // 2. Fill the worksheet with sample OHLC data. | |
| // ------------------------------------------------- | |
| // A B C D E | |
| // 1 Month Open High Low Close | |
| // 2 Jan 100 110 95 105 | |
| // 3 Feb 105 115 102 110 | |
| // 4 Mar 110 120 108 118 | |
| // 5 Apr 118 125 115 123 | |
| // ------------------------------------------------- | |
| sheet.Cells["A1"].PutValue("Month"); | |
| sheet.Cells["B1"].PutValue("Open"); | |
| sheet.Cells["C1"].PutValue("High"); | |
| sheet.Cells["D1"].PutValue("Low"); | |
| sheet.Cells["E1"].PutValue("Close"); | |
| string[] months = { "Jan", "Feb", "Mar", "Apr" }; | |
| double[] open = { 100, 105, 110, 118 }; | |
| double[] high = { 110, 115, 120, 125 }; | |
| double[] low = { 95, 102, 108, 115 }; | |
| double[] close = { 105, 110, 118, 123 }; | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| int row = i + 1; // Excel rows are 0?based in the API | |
| sheet.Cells[row, 0].PutValue(months[i]); // Column A | |
| sheet.Cells[row, 1].PutValue(open[i]); // Column B | |
| sheet.Cells[row, 2].PutValue(high[i]); // Column C | |
| sheet.Cells[row, 3].PutValue(low[i]); // Column D | |
| sheet.Cells[row, 4].PutValue(close[i]); // Column E | |
| } | |
| // ------------------------------------------------- | |
| // 3. Add a StockVolumeHighLowClose chart. | |
| // ------------------------------------------------- | |
| // Parameters: chart type, upper?left row, upper?left column, | |
| // lower?right row, lower?right column (all zero?based indexes). | |
| int chartIndex = sheet.Charts.Add( | |
| ChartType.StockVolumeHighLowClose, 6, 0, 26, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Monthly Stock Prices (VHLC)"; | |
| // ------------------------------------------------- | |
| // 4. Define the data source for the chart. | |
| // ------------------------------------------------- | |
| // Category (X?axis) ¨C Month names. | |
| chart.NSeries.CategoryData = "=MarketData!$A$2:$A$5"; | |
| // Series ¨C Open, High, Low, Close. | |
| chart.NSeries.Add("=MarketData!$B$2:$B$5", true); // Open | |
| chart.NSeries.Add("=MarketData!$C$2:$C$5", true); // High | |
| chart.NSeries.Add("=MarketData!$D$2:$D$5", true); // Low | |
| chart.NSeries.Add("=MarketData!$E$2:$E$5", true); // Close | |
| // ------------------------------------------------- | |
| // 5. Optional visual tweaks. | |
| // ------------------------------------------------- | |
| chart.Legend.Position = LegendPositionType.Right; | |
| chart.PlotArea.Area.Formatting = FormattingType.None; // Transparent background | |
| chart.ShowDataTable = false; | |
| // ------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ------------------------------------------------- | |
| workbook.Save("StockVolumeHighLowClose_Basic.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------- | |
| // 2. Create a new workbook and get the first worksheet | |
| // ----------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "MarketData"; | |
| // ----------------------------------------------------------- | |
| // 3. Fill worksheet with sample Open?High?Low?Close?Volume data | |
| // ----------------------------------------------------------- | |
| // Header row | |
| sheet.Cells["A1"].Value = "Date"; | |
| sheet.Cells["B1"].Value = "Open"; | |
| sheet.Cells["C1"].Value = "High"; | |
| sheet.Cells["D1"].Value = "Low"; | |
| sheet.Cells["E1"].Value = "Close"; | |
| sheet.Cells["F1"].Value = "Volume"; | |
| // Sample data (Date, O, H, L, C, Volume) | |
| object[,] data = new object[,] | |
| { | |
| { new DateTime(2025, 9, 1), 132.5, 135.0, 131.2, 134.1, 1_200_000 }, | |
| { new DateTime(2025, 9, 2), 134.1, 136.5, 133.0, 135.8, 1_350_000 }, | |
| { new DateTime(2025, 9, 3), 135.8, 138.0, 135.0, 137.5, 1_500_000 }, | |
| { new DateTime(2025, 9, 4), 137.5, 139.2, 136.8, 138.9, 1_450_000 }, | |
| { new DateTime(2025, 9, 5), 138.9, 140.5, 138.0, 139.7, 1_600_000 } | |
| }; | |
| // Write data to sheet starting at A2 | |
| sheet.Cells.ImportTwoDimensionArray(data, 1, 0); | |
| // ----------------------------------------------------------- | |
| // 4. Add a StockVolumeOpenHighLowClose chart | |
| // ----------------------------------------------------------- | |
| // Parameters: chart type, first row, first column, last row, last column | |
| int chartIndex = sheet.Charts.Add(ChartType.StockVolumeOpenHighLowClose, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "OHLCV ¨C Sample Stock Chart"; | |
| // ----------------------------------------------------------- | |
| // 5. Define series ranges | |
| // ----------------------------------------------------------- | |
| // The chart automatically recognizes that the first series is OHLC, | |
| // and the second series is Volume. | |
| // Explicitly set the data range for clarity. | |
| chart.NSeries.Add("=MarketData!$B$2:$E$6", true); | |
| chart.NSeries[0].Name = "Price"; | |
| chart.NSeries.Add("=MarketData!$F$2:$F$6", true); | |
| chart.NSeries[1].Name = "Volume"; | |
| // ----------------------------------------------------------- | |
| // 6. Set category (X?axis) data ¨C the dates column | |
| // ----------------------------------------------------------- | |
| chart.NSeries.CategoryData = "MarketData!$A$2:$A$6"; | |
| // ----------------------------------------------------------- | |
| // 7. Minor optional styling | |
| // ----------------------------------------------------------- | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| chart.CategoryAxis.Title.Text = "Date"; | |
| chart.ValueAxis.Title.Text = "Price"; | |
| chart.SecondValueAxis.Title.Text = "Volume"; | |
| chart.SecondValueAxis.Title.TextHorizontalAlignment = TextAlignmentType.Center; | |
| // ----------------------------------------------------------- | |
| // 8. Save the workbook | |
| // ----------------------------------------------------------- | |
| string outputPath = "StockVolumeOpenHighLowClose_Basic.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------- | |
| // 1. Initialise workbook and worksheet | |
| // ----------------------------------------------------------- | |
| var wb = new Workbook(); | |
| var ws = wb.Worksheets[0]; | |
| ws.Name = "FinancialData"; | |
| // ----------------------------------------------------------- | |
| // 2. Populate sample OHLCV data (Date, O, H, L, C, V) | |
| // ----------------------------------------------------------- | |
| ws.Cells["A1"].Value = "Date"; | |
| ws.Cells["B1"].Value = "Open"; | |
| ws.Cells["C1"].Value = "High"; | |
| ws.Cells["D1"].Value = "Low"; | |
| ws.Cells["E1"].Value = "Close"; | |
| ws.Cells["F1"].Value = "Volume"; | |
| var rnd = new Random(); | |
| DateTime start = new DateTime(2025, 8, 1); | |
| for (int i = 0; i < 15; i++) | |
| { | |
| DateTime d = start.AddDays(i); | |
| double open = 100 + rnd.NextDouble() * 20; | |
| double high = open + rnd.NextDouble() * 5; | |
| double low = open - rnd.NextDouble() * 5; | |
| double close = low + rnd.NextDouble() * (high - low); | |
| long volume = 1_000_000 + rnd.Next(0, 500_000); | |
| ws.Cells[i + 1, 0].Value = d; | |
| ws.Cells[i + 1, 1].Value = Math.Round(open, 2); | |
| ws.Cells[i + 1, 2].Value = Math.Round(high, 2); | |
| ws.Cells[i + 1, 3].Value = Math.Round(low, 2); | |
| ws.Cells[i + 1, 4].Value = Math.Round(close, 2); | |
| ws.Cells[i + 1, 5].Value = volume; | |
| } | |
| // ----------------------------------------------------------- | |
| // 3. Add the StockVolumeOpenHighLowClose chart | |
| // ----------------------------------------------------------- | |
| int idx = ws.Charts.Add(ChartType.StockVolumeOpenHighLowClose, 18, 0, 40, 12); | |
| Chart chart = ws.Charts[idx]; | |
| chart.Title.Text = "15?Day OHLCV Overview"; | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Right; | |
| // ----------------------------------------------------------- | |
| // 4. Configure series | |
| // ----------------------------------------------------------- | |
| // OHLC series (first series) | |
| chart.NSeries.Add("=FinancialData!$B$2:$E$16", true); | |
| chart.NSeries[0].Name = "Price"; | |
| // Volume series (second series) ¨C plotted on secondary axis | |
| chart.NSeries.Add("=FinancialData!$F$2:$F$16", true); | |
| chart.NSeries[1].Name = "Volume"; | |
| chart.NSeries[1].PlotOnSecondAxis = true; | |
| // Category (X) axis data ¨C dates | |
| chart.NSeries.CategoryData = "FinancialData!$A$2:$A$16"; | |
| // ----------------------------------------------------------- | |
| // 5. Styling ¨C colors, markers, axes | |
| // ----------------------------------------------------------- | |
| // Stock series ¨C use candlestick style with red/green filling | |
| chart.NSeries[0].UpBars.Area.ForegroundColor = Color.Green; // Close > Open | |
| chart.NSeries[0].DownBars.Area.ForegroundColor = Color.Red; // Close < Open | |
| chart.NSeries[0].Border.Color = Color.Black; | |
| chart.NSeries[0].Border.IsVisible = true; | |
| // Volume series ¨C keep default column type; customize fill | |
| chart.NSeries[1].Type = ChartType.Column; | |
| chart.NSeries[1].Area.ForegroundColor = Color.FromArgb(70, Color.Blue); // semi?transparent | |
| // Axes titles | |
| chart.CategoryAxis.Title.Text = "Date"; | |
| chart.ValueAxis.Title.Text = "Price (USD)"; | |
| chart.SecondValueAxis.Title.Text = "Volume"; | |
| // Axis formatting ¨C date format on category axis | |
| chart.CategoryAxis.TickLabels.NumberFormat = "mmm dd"; | |
| // Plot area ¨C remove background fill for a cleaner look | |
| chart.PlotArea.Area.Formatting = FormattingType.None; | |
| // ----------------------------------------------------------- | |
| // 6. Save the workbook | |
| // ----------------------------------------------------------- | |
| string outFile = "StockVolumeOpenHighLowClose_Styled.xlsx"; | |
| wb.Save(outFile); | |
| Console.WriteLine($"Styled chart workbook saved to {outFile}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and get the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------------------ | |
| // 2. Fill the worksheet with sample hierarchical data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Category Amount | |
| // 2 Lead 5000 | |
| // 3 Prospect 3500 | |
| // 4 Qualified 2000 | |
| // 5 Proposal 1200 | |
| // 6 Closed 800 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Amount"); | |
| string[] categories = { "Lead", "Prospect", "Qualified", "Proposal", "Closed" }; | |
| double[] amounts = { 5000, 3500, 2000, 1200, 800 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(categories[i]); // Column A | |
| sheet.Cells[i + 1, 1].PutValue(amounts[i]); // Column B | |
| } | |
| // Add the pyramid chart. | |
| int chartIdx = sheet.Charts.Add(ChartType.Pyramid, 7, 0, 25, 10); | |
| Chart pyramidChart = sheet.Charts[chartIdx]; | |
| pyramidChart.Title.Text = "Sales Funnel ¨C Styled Pyramid"; | |
| // Add series (same as before) | |
| pyramidChart.NSeries.Add("=SalesData!$B$2:$B$6", true); | |
| pyramidChart.NSeries.CategoryData = "=SalesData!$A$2:$A$6"; | |
| pyramidChart.NSeries[0].Name = "Amount"; | |
| pyramidChart.NSeries[0].DataLabels.ShowValue = true; | |
| // ------------------------------------------------------------ | |
| // 4. Apply custom colors to each pyramid level. | |
| // ------------------------------------------------------------ | |
| Color[] levelColors = { | |
| Color.FromArgb(0x4CAF50), // Lead ¨C Green | |
| Color.FromArgb(0x2196F3), // Prospect ¨C Blue | |
| Color.FromArgb(0xFF9800), // Qualified ¨C Orange | |
| Color.FromArgb(0xF44336), // Proposal ¨C Red | |
| Color.FromArgb(0x9C27B0) // Closed ¨C Purple | |
| }; | |
| for (int i = 0; i < pyramidChart.NSeries[0].Points.Count; i++) | |
| { | |
| pyramidChart.NSeries[0].Points[i].Area.ForegroundColor = levelColors[i]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 5. Show legend at the bottom. | |
| // ------------------------------------------------------------ | |
| pyramidChart.ShowLegend = true; | |
| pyramidChart.Legend.Position = LegendPositionType.Bottom; | |
| // Save workbook. | |
| workbook.Save("Styled_PyramidChart.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialize workbook and worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Insert data (same layout as the simple example). | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Product A"); | |
| sheet.Cells["C1"].PutValue("Product B"); | |
| string[] cats = { "Quality", "Speed", "Cost", "Flexibility", "Reliability" }; | |
| double[] prodA = { 8, 6, 7, 9, 5 }; | |
| double[] prodB = { 7, 8, 6, 7, 8 }; | |
| for (int i = 0; i < cats.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].PutValue(cats[i]); // Category column | |
| sheet.Cells[i + 1, 1].PutValue(prodA[i]); // Product A values | |
| sheet.Cells[i + 1, 2].PutValue(prodB[i]); // Product B values | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Radar chart. | |
| // ------------------------------------------------------------ | |
| int chartIdx = sheet.Charts.Add(ChartType.Radar, 7, 0, 26, 15); | |
| Chart radarChart = sheet.Charts[chartIdx]; | |
| radarChart.Title.Text = "Product Comparison Radar"; | |
| // ------------------------------------------------------------ | |
| // 4. Add series and apply custom marker styles. | |
| // ------------------------------------------------------------ | |
| int serA = radarChart.NSeries.Add("=Sheet1!$B$2:$B$6", true); | |
| radarChart.NSeries[serA].Name = "Product A"; | |
| radarChart.NSeries[serA].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| radarChart.NSeries[serA].Marker.MarkerSize = 9; | |
| radarChart.NSeries[serA].Marker.Area.ForegroundColor = Color.DarkBlue; | |
| radarChart.NSeries[serA].Border.Color = Color.DarkBlue; | |
| radarChart.NSeries[serA].Border.Style = LineType.Solid; | |
| int serB = radarChart.NSeries.Add("=Sheet1!$C$2:$C$6", true); | |
| radarChart.NSeries[serB].Name = "Product B"; | |
| radarChart.NSeries[serB].Marker.MarkerStyle = ChartMarkerType.Diamond; | |
| radarChart.NSeries[serB].Marker.MarkerSize = 9; | |
| radarChart.NSeries[serB].Marker.Area.ForegroundColor = Color.DarkRed; | |
| radarChart.NSeries[serB].Border.Color = Color.DarkRed; | |
| radarChart.NSeries[serB].Border.Style = LineType.Solid; | |
| // ------------------------------------------------------------ | |
| // 5. Fill the area of each series with a semi?transparent color. | |
| // ------------------------------------------------------------ | |
| radarChart.NSeries[serA].Area.Formatting = FormattingType.Custom; | |
| radarChart.NSeries[serA].Area.ForegroundColor = Color.FromArgb(80, Color.LightBlue); | |
| radarChart.NSeries[serB].Area.Formatting = FormattingType.Custom; | |
| radarChart.NSeries[serB].Area.ForegroundColor = Color.FromArgb(80, Color.LightCoral); | |
| // ------------------------------------------------------------ | |
| // 6. Optional: hide the PlotArea border to give a cleaner look. | |
| // ------------------------------------------------------------ | |
| radarChart.PlotArea.Area.Formatting = FormattingType.None; | |
| // ------------------------------------------------------------ | |
| // 7. Save the workbook. | |
| // ------------------------------------------------------------ | |
| workbook.Save("StyledRadarChart.xlsx"); | |
| Console.WriteLine("StyledRadarChart.xlsx created successfully."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "Hierarchy"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate hierarchical data. | |
| // The data layout follows the structure required by Sunburst: | |
| // Column A ¨C Category (Level 1) | |
| // Column B ¨C Sub?Category (Level 2) | |
| // Column C ¨C Item (Level 3) | |
| // Column D ¨C Value (numeric) | |
| // ------------------------------------------------------------ | |
| string[,] data = new string[,] | |
| { | |
| // Category, Sub?Category, Item, Value | |
| { "Technology", "Hardware", "Laptop", "120" }, | |
| { "Technology", "Hardware", "Desktop", "80" }, | |
| { "Technology", "Software", "OS", "150" }, | |
| { "Technology", "Software", "Office", "100" }, | |
| { "Finance", "Banking", "Retail", "200" }, | |
| { "Finance", "Banking", "Corporate", "180" }, | |
| { "Finance", "Investments","Equity", "130" }, | |
| { "Finance", "Investments","Bonds", "90" } | |
| }; | |
| // Write the header row | |
| sheet.Cells["A1"].PutValue("Category"); | |
| sheet.Cells["B1"].PutValue("Sub?Category"); | |
| sheet.Cells["C1"].PutValue("Item"); | |
| sheet.Cells["D1"].PutValue("Value"); | |
| // Fill the data rows | |
| for (int r = 0; r < data.GetLength(0); r++) | |
| { | |
| for (int c = 0; c < data.GetLength(1); c++) | |
| { | |
| sheet.Cells[r + 1, c].PutValue(data[r, c]); | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Sunburst chart. | |
| // ------------------------------------------------------------ | |
| // The chart will be placed starting at row 12, column 0 and | |
| // will span 20 rows and 10 columns. | |
| int chartIdx = sheet.Charts.Add(ChartType.Sunburst, 12, 0, 32, 10); | |
| Chart sunburstChart = sheet.Charts[chartIdx]; | |
| sunburstChart.Title.Text = "Company Revenue by Category"; | |
| // ------------------------------------------------------------ | |
| // 4. Set the data range for the chart. | |
| // Sunburst expects the first column to contain the innermost | |
| // level (Category), and the last column to hold the numeric | |
| // values. | |
| // ------------------------------------------------------------ | |
| // A2:D9 contains the hierarchy + values. | |
| sunburstChart.SetChartDataRange("=Hierarchy!$A$2:$D$9", true); | |
| // ------------------------------------------------------------ | |
| // 5. (Optional) Customize the appearance. | |
| // ------------------------------------------------------------ | |
| // Example: Set a pastel background for the PlotArea. | |
| sunburstChart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| sunburstChart.PlotArea.Area.ForegroundColor = Color.FromArgb(247, 250, 255); | |
| // Set the legend to the right side. | |
| sunburstChart.Legend.Position = LegendPositionType.Right; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "SunburstChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Sunburst chart created successfully: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new Workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SurfaceData"; | |
| // ----------------------------------------------------------------- | |
| // 2. Populate the worksheet with sample 3?dimensional data. | |
| // The data matrix represents Z?values; rows = X?axis, columns = Y?axis. | |
| // ----------------------------------------------------------------- | |
| // Header row (Y?axis labels) | |
| sheet.Cells["B1"].PutValue("Y1"); | |
| sheet.Cells["C1"].PutValue("Y2"); | |
| sheet.Cells["D1"].PutValue("Y3"); | |
| sheet.Cells["E1"].PutValue("Y4"); | |
| // First column (X?axis labels) and Z?values | |
| double[,] zValues = new double[,] | |
| { | |
| { 10, 20, 30, 40 }, | |
| { 15, 25, 35, 45 }, | |
| { 20, 30, 40, 50 }, | |
| { 25, 35, 45, 55 } | |
| }; | |
| string[] xLabels = { "X1", "X2", "X3", "X4" }; | |
| for (int i = 0; i < xLabels.Length; i++) | |
| { | |
| // X?axis label | |
| sheet.Cells[i + 2, 0].PutValue(xLabels[i]); | |
| // Z?values for the current X | |
| for (int j = 0; j < zValues.GetLength(1); j++) | |
| { | |
| sheet.Cells[i + 2, j + 1].PutValue(zValues[i, j]); | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a Surface3D chart (base type). Position it below the data table. | |
| // ----------------------------------------------------------------- | |
| int chartIndex = sheet.Charts.Add(ChartType.Surface3D, 7, 0, 30, 12); | |
| Chart surfaceChart = sheet.Charts[chartIndex]; | |
| surfaceChart.Title.Text = "3?D Surface Chart Example"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define the data range for the chart. | |
| // The range includes both axis labels and Z?values. | |
| // ----------------------------------------------------------------- | |
| // Data range: B1:E5 (Y?labels + Z?values) | |
| // Category (X) range: A2:A5 | |
| // Series (Y) range: B1:E1 | |
| surfaceChart.SetChartDataRange("A1:E5", true); | |
| surfaceChart.NSeries.CategoryData = "A2:A5"; // X?axis | |
| // ----------------------------------------------------------------- | |
| // 5. Optional customisations. | |
| // ----------------------------------------------------------------- | |
| // Set the chart style (pre?defined style number 5) | |
| surfaceChart.Style = 5; | |
| // Set the surface colors ¨C use a gradient for better visual effect. | |
| surfaceChart.PlotArea.Area.Formatting = FormattingType.Custom; | |
| surfaceChart.PlotArea.Area.ForegroundColor = Color.White; | |
| // Turn on the legend and position it at the right. | |
| surfaceChart.ShowLegend = true; | |
| surfaceChart.Legend.Position = LegendPositionType.Right; | |
| // Customize axis titles. | |
| surfaceChart.CategoryAxis.Title.Text = "X Axis"; | |
| surfaceChart.ValueAxis.Title.Text = "Z Value"; | |
| surfaceChart.SecondValueAxis.Title.Text = "Y Axis"; | |
| // ----------------------------------------------------------------- | |
| // 6. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "Surface3DChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved successfully to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------------------------------------------------------- | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ----------------------------------------------------------------- | |
| var workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| // ----------------------------------------------------------------- | |
| // 2. Fill the worksheet with sample data. | |
| // The data represents a 5x5 grid (X?axis: columns, Y?axis: rows). | |
| // ----------------------------------------------------------------- | |
| // Header row (X?axis labels) | |
| cells["B1"].PutValue("X1"); | |
| cells["C1"].PutValue("X2"); | |
| cells["D1"].PutValue("X3"); | |
| cells["E1"].PutValue("X4"); | |
| cells["F1"].PutValue("X5"); | |
| // Header column (Y?axis labels) and sample Z?values | |
| double[,] zValues = new double[5, 5] | |
| { | |
| { 30, 20, 10, 20, 30 }, | |
| { 20, 10, 0, 10, 20 }, | |
| { 10, 0, -10, 0, 10 }, | |
| { 20, 10, 0, 10, 20 }, | |
| { 30, 20, 10, 20, 30 } | |
| }; | |
| for (int row = 0; row < 5; row++) | |
| { | |
| // Y?axis label | |
| cells[row + 2, 0].PutValue($"Y{row + 1}"); | |
| for (int col = 0; col < 5; col++) | |
| { | |
| // Z?value at (Y, X) | |
| cells[row + 2, col + 1].PutValue(zValues[row, col]); | |
| } | |
| } | |
| // ----------------------------------------------------------------- | |
| // 3. Add a SurfaceContour chart. | |
| // Parameters: type, upper?left row/col, lower?right row/col. | |
| // ----------------------------------------------------------------- | |
| int chartIdx = sheet.Charts.Add(ChartType.SurfaceContour, 8, 0, 28, 12); | |
| Chart chart = sheet.Charts[chartIdx]; | |
| chart.Title.Text = "SurfaceContour Chart Example"; | |
| // ----------------------------------------------------------------- | |
| // 4. Define the data range for the chart. | |
| // The range includes both X and Y headers plus the Z values. | |
| // ----------------------------------------------------------------- | |
| chart.SetChartDataRange("A1:F6", true); // true = series are plotted on rows | |
| // ----------------------------------------------------------------- | |
| // 5. Set the category (X) and series (Y) data. | |
| // CategoryData ¨C X?axis labels (first row, excluding corner cell A1) | |
| // NSeries[0].CategoryData ¨C Y?axis labels (first column, excluding A1) | |
| // ----------------------------------------------------------------- | |
| chart.NSeries.CategoryData = "B1:F1"; // X labels | |
| chart.NSeries.CategoryData = "A2:A6"; // Y labels | |
| // ----------------------------------------------------------------- | |
| // 6. Customize the chart appearance (optional). | |
| // ----------------------------------------------------------------- | |
| // Fill the plot area with a light gradient for better visual effect. | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.Gradient; | |
| chart.PlotArea.Area.FillFormat.SetTwoColorGradient(Color.Blue, Color.Red, GradientStyleType.DiagonalDown, 1); | |
| // Set the legend to the bottom. | |
| chart.ShowLegend = true; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Add a soft edge to the chart border. | |
| chart.ChartArea.Border.IsVisible = true; | |
| chart.ChartArea.Border.Color = Color.Gray; | |
| chart.ChartArea.Border.Weight = WeightType.HairLine; | |
| // ----------------------------------------------------------------- | |
| // 7. Save the workbook. | |
| // ----------------------------------------------------------------- | |
| string outputPath = "SurfaceContourChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to '{outputPath}'."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Create a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with 3?D surface data. | |
| // The data layout follows Excel's requirement: | |
| // - First row : X?axis values (categories) | |
| // - First column: Y?axis values (categories) | |
| // - Remaining cells: Z?values (surface height) | |
| // ------------------------------------------------------------ | |
| // Header row (X?axis) | |
| string[] xLabels = { "Jan", "Feb", "Mar", "Apr", "May" }; | |
| for (int col = 1; col <= xLabels.Length; col++) | |
| sheet.Cells[0, col].Value = xLabels[col - 1]; | |
| // Header column (Y?axis) and Z?values | |
| double[,] zValues = { | |
| { 10, 15, 20, 25, 30 }, // Row for "Product A" | |
| { 12, 18, 24, 30, 36 }, // Row for "Product B" | |
| { 14, 21, 28, 35, 42 }, // Row for "Product C" | |
| { 16, 24, 32, 40, 48 } // Row for "Product D" | |
| }; | |
| string[] yLabels = { "Product A", "Product B", "Product C", "Product D" }; | |
| for (int row = 1; row <= yLabels.Length; row++) | |
| { | |
| sheet.Cells[row, 0].Value = yLabels[row - 1]; // Y?axis label | |
| for (int col = 1; col <= xLabels.Length; col++) | |
| { | |
| sheet.Cells[row, col].Value = zValues[row - 1, col - 1]; | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a SurfaceContourWireframe chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: type, upper left row, upper left column, lower right row, lower right column | |
| int chartIndex = sheet.Charts.Add(ChartType.SurfaceContourWireframe, 7, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| chart.Title.Text = "Quarterly Sales Surface"; | |
| // Bind the chart to the data range (including row & column headers) | |
| // A1:E5 covers the whole table (headers + data) | |
| chart.SetChartDataRange("A1:E5", true); | |
| // Optional: set explicit series and category ranges (Aspose.Cells auto?detects them, | |
| // but showing them makes the code clearer) | |
| chart.NSeries.CategoryData = "A2:A5"; // Y?axis labels | |
| chart.NSeries[0].Values = "B2:E5"; // Z?values | |
| // ------------------------------------------------------------ | |
| // 4. Customize axes titles (optional but improves readability) | |
| // ------------------------------------------------------------ | |
| chart.CategoryAxis.Title.Text = "Month"; | |
| chart.ValueAxis.Title.Text = "Sales Volume"; | |
| chart.SecondValueAxis.Title.Text = "Height (Z)"; | |
| // ------------------------------------------------------------ | |
| // 5. Apply a built?in style to make the chart look polished | |
| // ------------------------------------------------------------ | |
| chart.ChartArea.Area.BackgroundColor = Color.White; | |
| chart.PlotArea.Area.BackgroundColor = Color.LightYellow; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outFile = "SurfaceContourWireframeChart_Output.xlsx"; | |
| workbook.Save(outFile); | |
| Console.WriteLine($"Workbook saved to {outFile}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------------------------------ | |
| // 1. Initialize a new workbook and obtain the first worksheet. | |
| // ------------------------------------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "3DSurfaceData"; | |
| // ------------------------------------------------------------ | |
| // 2. Populate the worksheet with sample data. | |
| // The data matrix represents Z?values for X (columns) and Y (rows). | |
| // ------------------------------------------------------------ | |
| // Header row (X?axis values) | |
| sheet.Cells["B1"].Value = 0; | |
| sheet.Cells["C1"].Value = 1; | |
| sheet.Cells["D1"].Value = 2; | |
| sheet.Cells["E1"].Value = 3; | |
| sheet.Cells["F1"].Value = 4; | |
| // Y?axis labels and Z?values | |
| string[] yLabels = { "0", "1", "2", "3", "4" }; | |
| double[,] zValues = { | |
| { 10, 12, 14, 16, 18 }, | |
| { 12, 15, 18, 21, 24 }, | |
| { 14, 18, 22, 26, 30 }, | |
| { 16, 21, 26, 31, 36 }, | |
| { 18, 24, 30, 36, 42 } | |
| }; | |
| for (int i = 0; i < yLabels.Length; i++) | |
| { | |
| // Y?axis label (first column) | |
| sheet.Cells[i + 2, 0].Value = yLabels[i]; | |
| // Z?values (starting from column B) | |
| for (int j = 0; j < zValues.GetLength(1); j++) | |
| { | |
| sheet.Cells[i + 2, j + 1].Value = zValues[i, j]; | |
| } | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a SurfaceWireframe3D chart. | |
| // ------------------------------------------------------------ | |
| int chartIndex = sheet.Charts.Add(ChartType.SurfaceWireframe3D, 8, 0, 27, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "3?D Surface Wireframe Chart"; | |
| // ------------------------------------------------------------ | |
| // 4. Define the data range for the chart. | |
| // The range includes the header row, Y?labels, and Z?values. | |
| // ------------------------------------------------------------ | |
| // Note: The first cell (A1) is left empty intentionally. | |
| chart.SetChartDataRange("A1:F6", true); | |
| chart.NSeries.CategoryData = "A2:A6"; // Y?axis labels | |
| chart.NSeries[0].Name = "Z?Values"; | |
| // ------------------------------------------------------------ | |
| // 5. Optional customizations | |
| // ------------------------------------------------------------ | |
| chart.Style = 8; | |
| // Hide the legend (not needed for a single series) | |
| chart.ShowLegend = false; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook | |
| // ------------------------------------------------------------ | |
| string outputPath = "SurfaceWireframe3D_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------ | |
| // 1. Create a new workbook | |
| // ------------------------------ | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| sheet.Name = "SalesData"; | |
| // ------------------------------------------------- | |
| // 2. Fill the worksheet with hierarchical sample data | |
| // ------------------------------------------------- | |
| // A B C D E | |
| // ------------------------------------------------- | |
| // Region Country Category Subcategory Sales | |
| // Europe Germany Electronics Phones 120000 | |
| // Europe Germany Electronics Laptops 85000 | |
| // Europe France Furniture Chairs 45000 | |
| // Asia China Electronics Phones 200000 | |
| // Asia China Furniture Tables 95000 | |
| // America USA Electronics TVs 175000 | |
| // ------------------------------------------------- | |
| string[,] data = new string[,] | |
| { | |
| { "Region", "Country", "Category", "Subcategory", "Sales" }, | |
| { "Europe", "Germany", "Electronics", "Phones", "120000" }, | |
| { "Europe", "Germany", "Electronics", "Laptops", "85000" }, | |
| { "Europe", "France", "Furniture", "Chairs", "45000" }, | |
| { "Asia", "China", "Electronics", "Phones", "200000" }, | |
| { "Asia", "China", "Furniture", "Tables", "95000" }, | |
| { "America", "USA", "Electronics", "TVs", "175000" } | |
| }; | |
| for (int row = 0; row < data.GetLength(0); row++) | |
| { | |
| for (int col = 0; col < data.GetLength(1); col++) | |
| { | |
| sheet.Cells[row, col].PutValue(data[row, col]); | |
| } | |
| } | |
| // ------------------------------------------------- | |
| // 3. Add a Treemap chart | |
| // ------------------------------------------------- | |
| // The chart will be placed starting at row 9, column 0 | |
| // and will occupy rows 9?30 and columns 0?10. | |
| int chartIndex = sheet.Charts.Add(ChartType.Treemap, 9, 0, 30, 10); | |
| Chart treemap = sheet.Charts[chartIndex]; | |
| // Set chart title | |
| treemap.Title.Text = "Global Sales Treemap"; | |
| // ------------------------------------------------- | |
| // 4. Define the series for the Treemap | |
| // ------------------------------------------------- | |
| // The data range includes columns A?E (rows 2?7) without the header. | |
| // Category data (hierarchy) is taken from columns A?D. | |
| // Values are taken from column E. | |
| treemap.NSeries.Add("=SalesData!$E$2:$E$7", true); | |
| treemap.NSeries.CategoryData = "=SalesData!$A$2:$D$7"; | |
| // ------------------------------------------------- | |
| // 5. Customize the appearance (optional) | |
| // ------------------------------------------------- | |
| // Example: set a graduated fill based on sales values. | |
| treemap.NSeries[0].IsColorVaried = true; // Vary color by value | |
| // ------------------------------------------------- | |
| // 6. Save the workbook | |
| // ------------------------------------------------- | |
| string outputPath = "TreemapChart_Output.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Treemap chart created successfully. File saved to: {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Create a new workbook and obtain the first worksheet. | |
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 2. Populate worksheet with sample data. | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Category Amount | |
| // 2 Start 5000 | |
| // 3 Q1 Sales 1200 | |
| // 4 Q2 Returns -300 | |
| // 5 Q3 Sales 900 | |
| // 6 Q4 Sales 800 | |
| // 7 End 7600 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Category"; | |
| sheet.Cells["B1"].Value = "Amount"; | |
| string[] categories = { "Start", "Q1 Sales", "Q2 Returns", "Q3 Sales", "Q4 Sales", "End" }; | |
| double[] amounts = { 5000, 1200, -300, 900, 800, 7600 }; | |
| for (int i = 0; i < categories.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = categories[i]; | |
| sheet.Cells[i + 1, 1].Value = amounts[i]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 3. Add a Waterfall chart. | |
| // ------------------------------------------------------------ | |
| // Parameters: (type, upper-left row, upper-left column, lower-right row, lower-right column) | |
| int chartIndex = sheet.Charts.Add(ChartType.Waterfall, 8, 0, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.Title.Text = "Quarterly Profit Waterfall"; | |
| // ------------------------------------------------------------ | |
| // 4. Set the data range for the chart. | |
| // ------------------------------------------------------------ | |
| // Category (X) data: column A (excluding header) | |
| // Values (Y) data: column B (excluding header) | |
| chart.NSeries.Add("=Sheet1!$B$2:$B$7", true); | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$7"; | |
| // ------------------------------------------------------------ | |
| // 5. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string outputPath = "WaterfallChart_Basic.xlsx"; | |
| workbook.Save(outputPath); | |
| Console.WriteLine($"Workbook saved to {outputPath}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var workbook = new Workbook(); | |
| var sheet = workbook.Worksheets[0]; | |
| // ------------------------------------------------------------ | |
| // 1. Fill worksheet with sample data (including totals). | |
| // ------------------------------------------------------------ | |
| // A B | |
| // 1 Item Amount | |
| // 2 Opening 20000 | |
| // 3 Sales 8500 | |
| // 4 Returns -1500 | |
| // 5 Expenses -3000 | |
| // 6 Net Change 0 <-- This row will be treated as a total later | |
| // 7 Closing 24000 | |
| // ------------------------------------------------------------ | |
| sheet.Cells["A1"].Value = "Item"; | |
| sheet.Cells["B1"].Value = "Amount"; | |
| string[] items = { "Opening", "Sales", "Returns", "Expenses", "Net Change", "Closing" }; | |
| double[] values = { 20000, 8500, -1500, -3000, 0, 24000 }; | |
| for (int i = 0; i < items.Length; i++) | |
| { | |
| sheet.Cells[i + 1, 0].Value = items[i]; | |
| sheet.Cells[i + 1, 1].Value = values[i]; | |
| } | |
| // ------------------------------------------------------------ | |
| // 2. Add Waterfall chart. | |
| // ------------------------------------------------------------ | |
| int idx = sheet.Charts.Add(ChartType.Waterfall, 9, 0, 26, 12); | |
| Chart chart = sheet.Charts[idx]; | |
| chart.Title.Text = "Profit & Loss Waterfall"; | |
| // ------------------------------------------------------------ | |
| // 3. Bind data to the chart. | |
| // ------------------------------------------------------------ | |
| chart.NSeries.Add("=Sheet1!$B$2:$B$7", true); | |
| chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$7"; | |
| // ------------------------------------------------------------ | |
| // 4. Customize series appearance. | |
| // ------------------------------------------------------------ | |
| var series = chart.NSeries[0]; | |
| // Enable data labels. | |
| series.DataLabels.Position = LabelPositionType.OutsideEnd; | |
| series.DataLabels.ShowValue = true; | |
| // ------------------------------------------------------------ | |
| // 5. Fine?tune axes (optional). | |
| // ------------------------------------------------------------ | |
| chart.ValueAxis.Title.Text = "Amount ($)"; | |
| chart.CategoryAxis.Title.Text = "Item"; | |
| // ------------------------------------------------------------ | |
| // 6. Save the workbook. | |
| // ------------------------------------------------------------ | |
| string output = "WaterfallChart_Customized.xlsx"; | |
| workbook.Save(output); | |
| Console.WriteLine($"Customized Waterfall chart saved to {output}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyCalculationMonitor : AbstractCalculationMonitor | |
| { | |
| public override void BeforeCalculate(int sheetIndex, int rowIndex, int colIndex) | |
| { | |
| if(new StackTrace(false).FrameCount > 2000) | |
| { | |
| throw new Exception("Stop the formula calculation because risk of StackOverflowException"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create the workbook | |
| Workbook workbook = new Workbook(); | |
| //Get the first worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| Aspose.Cells.Cells cells = ws.Cells; | |
| //Setting the value to the cells | |
| Aspose.Cells.Cell cell = cells["A1"]; | |
| cell.PutValue("Fruit"); | |
| cell = cells["B1"]; | |
| cell.PutValue("Count"); | |
| cell = cells["C1"]; | |
| cell.PutValue("Price"); | |
| cell = cells["A2"]; | |
| cell.PutValue("Apple"); | |
| cell = cells["A3"]; | |
| cell.PutValue("Mango"); | |
| cell = cells["A4"]; | |
| cell.PutValue("Blackberry"); | |
| cell = cells["A5"]; | |
| cell.PutValue("Cherry"); | |
| cell = cells["B2"]; | |
| cell.PutValue(5); | |
| cell = cells["B3"]; | |
| cell.PutValue(3); | |
| cell = cells["B4"]; | |
| cell.PutValue(6); | |
| cell = cells["B5"]; | |
| cell.PutValue(4); | |
| cell = cells["C2"]; | |
| cell.PutValue(5); | |
| cell = cells["C3"]; | |
| cell.PutValue(20); | |
| cell = cells["C4"]; | |
| cell.PutValue(30); | |
| cell = cells["C5"]; | |
| cell.PutValue(60); | |
| // Access the worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| Cell a2 = worksheet.Cells["A2"]; | |
| // Get style of A2 | |
| Style style = a2.GetStyle(); | |
| // Change the format | |
| style.Font.Color = Color.Red; | |
| style.Font.IsBold = true; | |
| StyleFlag flag = new StyleFlag(); | |
| flag.FontColor = true; | |
| a2.SetStyle(style, flag); | |
| Cell b3 = worksheet.Cells["B3"]; | |
| // Get style of B3 | |
| Style style2 = b3.GetStyle(); | |
| // Change the format | |
| style2.Font.Color = Color.Blue; | |
| style2.Font.IsItalic = true; | |
| b3.SetStyle(style2); | |
| // Save the modified workbook | |
| workbook.Save("output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new workbook | |
| Workbook workbook = new Workbook(); | |
| WorksheetCollection sheets = workbook.Worksheets; | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // Add text to a cell | |
| Cell cell = sheet.Cells["A1"]; | |
| cell.PutValue("Aspose.Cells Formatting"); | |
| FontSetting settting1 = new FontSetting(0, 6, sheets); | |
| settting1.Font.IsBold = true; | |
| settting1.Font.Color = Color.Red; | |
| FontSetting settting2 = new FontSetting(7, 5, sheets); | |
| settting2.Font.IsItalic = true; | |
| settting2.Font.Color = Color.Blue; | |
| // Get the existing text characters | |
| FontSetting[] fontSettings = new FontSetting[] { settting1, settting2 }; | |
| // Apply partial formatting | |
| cell.SetCharacters(fontSettings); | |
| // Save the workbook | |
| workbook.Save("FormattedText.xlsx"); | |
| Console.WriteLine("Partial formatting applied successfully!"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new workbook and access the first worksheet | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // Add text to a cell | |
| Cell a1 = sheet.Cells["A1"]; | |
| a1.PutValue("Welcome to Aspose.Cells!"); | |
| // Replace part of the text | |
| string originalText = a1.StringValue; | |
| string newText = originalText.Replace("Aspose.Cells", "Excel Automation"); | |
| // Update the cell with the modified text | |
| a1.PutValue(newText); | |
| // Add text to a cell | |
| Cell a2 = sheet.Cells["A2"]; | |
| a2.PutValue("Welcome to Aspose.Cells!"); | |
| ReplaceOptions options = new ReplaceOptions(); | |
| options.MatchEntireCellContents = false; | |
| string str = "Excel Automation"; | |
| FontSetting setting = new FontSetting(0, str.Length, workbook.Worksheets); | |
| setting.Font.Name = "Calbri"; | |
| setting.Font.Color = System.Drawing.Color.Red; | |
| options.FontSettings = new FontSetting[] { setting }; | |
| // Update the cell with the modified text | |
| a2.Replace("Aspose.Cells", str, options); | |
| // Save the workbook | |
| workbook.Save("UpdatedText.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| workbook.Settings.Date1904 = false; | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| Cells cells = ws.Cells; | |
| DateTime dateData = new DateTime(2023, 11, 23); | |
| //Setting the DateTime value to the cells | |
| Cell a1 = cells["A1"]; | |
| a1.PutValue(dateData); | |
| // Check if the cell contains a numeric value | |
| if (a1.Type == CellValueType.IsNumeric) | |
| { | |
| Console.WriteLine("A1 is Numeric Value: " + a1.DoubleValue); | |
| } | |
| workbook.Settings.Date1904 = true; | |
| Console.WriteLine("use The 1904 date system===================="); | |
| //Setting the DateTime value to the cells | |
| Cell a2 = cells["A2"]; | |
| a2.Value = dateData; | |
| // Check if the cell contains a numeric value | |
| if (a2.Type == CellValueType.IsNumeric) | |
| { | |
| Console.WriteLine("A2 is Numeric Value: " + a2.DoubleValue); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void TestAboveAverage() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| AddAboveAverage(_sheet); | |
| AddAboveAverage2(_sheet); | |
| AddAboveAverage3(_sheet); | |
| book.Save(filePath + "AboveAverage.xlsx"); | |
| } | |
| // This method implements the AboveAverage conditional formatting type. | |
| private void AddAboveAverage(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A11:C12", Color.Tomato, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.AboveAverage); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| } | |
| // This method implements an AboveAverage conditional formatting type with some custom attributes. | |
| private void AddAboveAverage2(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A13:C14", Color.Empty, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.AboveAverage); | |
| FormatCondition cond = conds[idx]; | |
| cond.AboveAverage.IsAboveAverage = false; | |
| cond.AboveAverage.IsEqualAverage = true; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| } | |
| // This method implements an AboveAverage conditional formatting type with some custom attributes. | |
| private void AddAboveAverage3(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A15:C16", Color.Empty, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.AboveAverage); | |
| FormatCondition cond = conds[idx]; | |
| cond.AboveAverage.IsAboveAverage = false; | |
| cond.AboveAverage.IsEqualAverage = true; | |
| cond.AboveAverage.StdDev = 3; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| } | |
| // This method adds formatted conditions. | |
| private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| // Adds an empty conditional formattings | |
| int index = _sheet.ConditionalFormattings.Add(); | |
| // Get the formatted conditions | |
| FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
| // Get the cell area calling the custom GetCellAreaByName method | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| // Add the formatted conditions cell area. | |
| formatConditions.AddArea(area); | |
| // Call the custom FillCell method | |
| FillCell(cellAreaName, color, _sheet); | |
| // Return the formatted conditions | |
| return formatConditions; | |
| } | |
| // This method specifies the cell shading color for the conditional formattings cellarea range. | |
| private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| int k = 0; | |
| for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
| { | |
| for (int j = area.StartRow; j <= area.EndRow; j++) | |
| { | |
| Cell c = _sheet.Cells[j, i]; | |
| if (!color.IsEmpty) | |
| { | |
| Style s = c.GetStyle(); | |
| s.ForegroundColor = color; | |
| s.Pattern = BackgroundType.Solid; | |
| c.SetStyle(s); | |
| } | |
| // Set some random values to the cells in the cellarea range | |
| int value = j + i + k; | |
| c.PutValue(value); | |
| k++; | |
| } | |
| } | |
| } | |
| // This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
| // For the conditional formatting | |
| internal static CellArea GetCellAreaByName(string s) | |
| { | |
| CellArea area = new CellArea(); | |
| string[] strCellRange = s.Replace("$", "").Split(':'); | |
| int column; | |
| CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
| area.StartColumn = column; | |
| if (strCellRange.Length == 1) | |
| { | |
| area.EndRow = area.StartRow; | |
| area.EndColumn = area.StartColumn; | |
| } | |
| else | |
| { | |
| CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
| area.EndColumn = column; | |
| } | |
| return area; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void TestDataBar() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| AddDataBar1(_sheet); | |
| AddDataBar2(_sheet); | |
| book.Save(filePath + "DataBar.xlsx"); | |
| } | |
| // This method implements the DataBars conditional formatting type with Percentile attribute. | |
| private void AddDataBar2(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("E3:G4", Color.LightGreen, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.DataBar); | |
| FormatCondition cond = conds[idx]; | |
| cond.DataBar.Color = Color.Orange; | |
| cond.DataBar.MinCfvo.Type = FormatConditionValueType.Percentile; | |
| cond.DataBar.MinCfvo.Value = 30.78; | |
| cond.DataBar.ShowValue = false; | |
| } | |
| // This method implements the DataBars conditional formatting type. | |
| private void AddDataBar1(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("E1:G2", Color.YellowGreen, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.DataBar); | |
| FormatCondition cond = conds[idx]; | |
| } | |
| // This method adds formatted conditions. | |
| private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| // Adds an empty conditional formattings | |
| int index = _sheet.ConditionalFormattings.Add(); | |
| // Get the formatted conditions | |
| FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
| // Get the cell area calling the custom GetCellAreaByName method | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| // Add the formatted conditions cell area. | |
| formatConditions.AddArea(area); | |
| // Call the custom FillCell method | |
| FillCell(cellAreaName, color, _sheet); | |
| // Return the formatted conditions | |
| return formatConditions; | |
| } | |
| // This method specifies the cell shading color for the conditional formattings cellarea range. | |
| private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| int k = 0; | |
| for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
| { | |
| for (int j = area.StartRow; j <= area.EndRow; j++) | |
| { | |
| Cell c = _sheet.Cells[j, i]; | |
| if (!color.IsEmpty) | |
| { | |
| Style s = c.GetStyle(); | |
| s.ForegroundColor = color; | |
| s.Pattern = BackgroundType.Solid; | |
| c.SetStyle(s); | |
| } | |
| // Set some random values to the cells in the cellarea range | |
| int value = j + i + k; | |
| c.PutValue(value); | |
| k++; | |
| } | |
| } | |
| } | |
| // This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
| // For the conditional formatting | |
| internal static CellArea GetCellAreaByName(string s) | |
| { | |
| CellArea area = new CellArea(); | |
| string[] strCellRange = s.Replace("$", "").Split(':'); | |
| int column; | |
| CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
| area.StartColumn = column; | |
| if (strCellRange.Length == 1) | |
| { | |
| area.EndRow = area.StartRow; | |
| area.EndColumn = area.StartColumn; | |
| } | |
| else | |
| { | |
| CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
| area.EndColumn = column; | |
| } | |
| return area; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void TestIconSets() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| AddDefaultIconSet(_sheet); | |
| AddIconSet2(_sheet); | |
| AddIconSet3(_sheet); | |
| AddIconSet4(_sheet); | |
| AddIconSet5(_sheet); | |
| AddIconSet6(_sheet); | |
| AddIconSet7(_sheet); | |
| AddIconSet8(_sheet); | |
| AddIconSet9(_sheet); | |
| AddIconSet10(_sheet); | |
| AddIconSet11(_sheet); | |
| AddIconSet12(_sheet); | |
| AddIconSet13(_sheet); | |
| AddIconSet14(_sheet); | |
| AddIconSet15(_sheet); | |
| AddIconSet16(_sheet); | |
| AddIconSet17(_sheet); | |
| AddIconSet18(_sheet); | |
| book.Save(filePath + "IconSets.xlsx"); | |
| } | |
| // This method implements the IconSet conditional formatting type. | |
| private void AddDefaultIconSet(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A1:C2", Color.Yellow, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| } | |
| // This method implements the IconSet conditional formatting type with 3 Arrows Colored attribute. | |
| private void AddIconSet2(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M1:O2", Color.AliceBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Arrows3; | |
| Cell c = _sheet.Cells["M1"]; | |
| c.PutValue("Arrows3"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 4 Arrows Colored attribute. | |
| private void AddIconSet3(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M3:O4", Color.AntiqueWhite, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Arrows4; | |
| Cell c = _sheet.Cells["M3"]; | |
| c.PutValue("Arrows4"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 5 Arrows Colored attribute. | |
| private void AddIconSet4(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M5:O6", Color.Aqua, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Arrows5; | |
| Cell c = _sheet.Cells["M5"]; | |
| c.PutValue("Arrows5"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 3 Arrows Gray attribute. | |
| private void AddIconSet5(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M7:O8", Color.Aquamarine, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.ArrowsGray3; | |
| Cell c = _sheet.Cells["M7"]; | |
| c.PutValue("ArrowsGray3"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 4 Arrows Gray attribute. | |
| private void AddIconSet6(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M9:O10", Color.Azure, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.ArrowsGray4; | |
| Cell c = _sheet.Cells["M9"]; | |
| c.PutValue("ArrowsGray4"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 5 Arrows Gray attribute. | |
| private void AddIconSet7(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M11:O12", Color.Beige, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.ArrowsGray5; | |
| Cell c = _sheet.Cells["M11"]; | |
| c.PutValue("ArrowsGray5"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 3 Flags attribute. | |
| private void AddIconSet8(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M13:O14", Color.Bisque, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Flags3; | |
| Cell c = _sheet.Cells["M13"]; | |
| c.PutValue("Flags3"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 5 Quarters attribute. | |
| private void AddIconSet9(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M15:O16", Color.BlanchedAlmond, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Quarters5; | |
| Cell c = _sheet.Cells["M15"]; | |
| c.PutValue("Quarters5"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 4 Ratings attribute. | |
| private void AddIconSet10(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M17:O18", Color.Blue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Rating4; | |
| Cell c = _sheet.Cells["M17"]; | |
| c.PutValue("Rating4"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 5 Ratings attribute. | |
| private void AddIconSet11(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M19:O20", Color.BlueViolet, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Rating5; | |
| Cell c = _sheet.Cells["M19"]; | |
| c.PutValue("Rating5"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 4 Red To Black attribute. | |
| private void AddIconSet12(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M21:O22", Color.Brown, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.RedToBlack4; | |
| Cell c = _sheet.Cells["M21"]; | |
| c.PutValue("RedToBlack4"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 3 Signs attribute. | |
| private void AddIconSet13(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M23:O24", Color.BurlyWood, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Signs3; | |
| Cell c = _sheet.Cells["M23"]; | |
| c.PutValue("Signs3"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 3 Symbols attribute. | |
| private void AddIconSet14(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M25:O26", Color.CadetBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Symbols3; | |
| Cell c = _sheet.Cells["M25"]; | |
| c.PutValue("Symbols3"); | |
| } | |
| // This method implements the IconSet conditional formatting type with another 3 Symbols attribute. | |
| private void AddIconSet15(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M27:O28", Color.Chartreuse, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.Symbols32; | |
| Cell c = _sheet.Cells["M27"]; | |
| c.PutValue("Symbols32"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 3 Traffic Lights attribute. | |
| private void AddIconSet16(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M29:O30", Color.Chocolate, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.TrafficLights31; | |
| Cell c = _sheet.Cells["M29"]; | |
| c.PutValue("TrafficLights31"); | |
| } | |
| // This method implements the IconSet conditional formatting type with another 3 Traffic Lights attribute. | |
| private void AddIconSet17(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M31:O32", Color.Coral, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.TrafficLights32; | |
| Cell c = _sheet.Cells["M31"]; | |
| c.PutValue("TrafficLights32"); | |
| } | |
| // This method implements the IconSet conditional formatting type with 4 Traffic Lights attribute. | |
| private void AddIconSet18(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("M33:O35", Color.CornflowerBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.IconSet); | |
| FormatCondition cond = conds[idx]; | |
| cond.IconSet.Type = IconSetType.TrafficLights4; | |
| Cell c = _sheet.Cells["M33"]; | |
| c.PutValue("TrafficLights4"); | |
| } | |
| // This method adds formatted conditions. | |
| private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| // Adds an empty conditional formattings | |
| int index = _sheet.ConditionalFormattings.Add(); | |
| // Get the formatted conditions | |
| FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
| // Get the cell area calling the custom GetCellAreaByName method | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| // Add the formatted conditions cell area. | |
| formatConditions.AddArea(area); | |
| // Call the custom FillCell method | |
| FillCell(cellAreaName, color, _sheet); | |
| // Return the formatted conditions | |
| return formatConditions; | |
| } | |
| // This method specifies the cell shading color for the conditional formattings cellarea range. | |
| private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| int k = 0; | |
| for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
| { | |
| for (int j = area.StartRow; j <= area.EndRow; j++) | |
| { | |
| Cell c = _sheet.Cells[j, i]; | |
| if (!color.IsEmpty) | |
| { | |
| Style s = c.GetStyle(); | |
| s.ForegroundColor = color; | |
| s.Pattern = BackgroundType.Solid; | |
| c.SetStyle(s); | |
| } | |
| // Set some random values to the cells in the cellarea range | |
| int value = j + i + k; | |
| c.PutValue(value); | |
| k++; | |
| } | |
| } | |
| } | |
| // This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
| // For the conditional formatting | |
| internal static CellArea GetCellAreaByName(string s) | |
| { | |
| CellArea area = new CellArea(); | |
| string[] strCellRange = s.Replace("$", "").Split(':'); | |
| int column; | |
| CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
| area.StartColumn = column; | |
| if (strCellRange.Length == 1) | |
| { | |
| area.EndRow = area.StartRow; | |
| area.EndColumn = area.StartColumn; | |
| } | |
| else | |
| { | |
| CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
| area.EndColumn = column; | |
| } | |
| return area; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the BeginsWith conditional formatting type. | |
| private void AddBeginWith() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E15:G16", Color.LightGoldenrodYellow, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.BeginsWith); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.Text = "ab"; | |
| Cell c = _sheet.Cells["E15"]; | |
| c.PutValue("abc"); | |
| c = _sheet.Cells["G16"]; | |
| c.PutValue("babx"); | |
| book.Save("BeginsWith.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the ContainsBlank conditional formatting type. | |
| private void AddContainsBlank() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E9:G10", Color.LightBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.ContainsBlanks); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Yellow; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| Cell c = _sheet.Cells["E9"]; | |
| c.PutValue(" "); | |
| c = _sheet.Cells["G10"]; | |
| c.PutValue(" "); | |
| book.Save("ContainsBlank.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the ContainsErrors conditional formatting type. | |
| private void AddContainsErrors() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E17:G18", Color.LightSkyBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.ContainsErrors); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Yellow; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| Cell c = _sheet.Cells["E17"]; | |
| c.PutValue(" "); | |
| c = _sheet.Cells["G18"]; | |
| c.PutValue(" "); | |
| book.Save("ContainsErrors.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the ContainsText conditional formatting type. | |
| private void AddContainsText() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E5:G6", Color.LightBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.ContainsText); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Yellow; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.Text = "1"; | |
| book.Save("ContainsText.xlsx"); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the DuplicateValues conditional formatting type. | |
| private void AddDuplicateValues() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E23:G24", Color.LightSlateGray, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.DuplicateValues); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| Cell c = _sheet.Cells["E23"]; | |
| c.PutValue("bb"); | |
| c = _sheet.Cells["G24"]; | |
| c.PutValue("bb"); | |
| book.Save("DuplicateValues.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the EndsWith conditional formatting type. | |
| private void AddEndWith() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E13:G14", Color.LightGray, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.EndsWith); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Yellow; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.Text = "ab"; | |
| Cell c = _sheet.Cells["E13"]; | |
| c.PutValue("nnnab"); | |
| c = _sheet.Cells["G14"]; | |
| c.PutValue("mmmabc"); | |
| book.Save("EndsWith.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the NotContainsBlank conditional formatting type. | |
| private void AddNotContainsBlank() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E11:G12", Color.LightCoral, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.NotContainsBlanks); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| Cell c = _sheet.Cells["E11"]; | |
| c.PutValue("abc"); | |
| c = _sheet.Cells["G12"]; | |
| c.PutValue(" "); | |
| book.Save("NotContainsBlank.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the NotContainsErrors conditional formatting type. | |
| private void AddNotContainsErrors() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E19:G20", Color.LightSeaGreen, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.NotContainsErrors); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| Cell c = _sheet.Cells["E19"]; | |
| c.PutValue(" "); | |
| c = _sheet.Cells["G20"]; | |
| c.PutValue(" "); | |
| book.Save("NotContainsErrors.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the NotContainsText conditional formatting type. | |
| private void AddNotContainsText() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E7:G8", Color.LightCoral, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.NotContainsText); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.Text = "3"; | |
| book.Save("NotContainsText.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method implements the UniqueValues conditional formatting type. | |
| private void AddUniqueValues() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| FormatConditionCollection conds = GetFormatCondition("E21:G22", Color.LightSalmon, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.UniqueValues); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Yellow; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| Cell c = _sheet.Cells["E21"]; | |
| c.PutValue("aa"); | |
| c = _sheet.Cells["G22"]; | |
| c.PutValue("aa"); | |
| book.Save("UniqueValues.xlsx"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This method adds formatted conditions. | |
| private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| // Adds an empty conditional formattings | |
| int index = _sheet.ConditionalFormattings.Add(); | |
| // Get the formatted conditions | |
| FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
| // Get the cell area calling the custom GetCellAreaByName method | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| // Add the formatted conditions cell area. | |
| formatConditions.AddArea(area); | |
| // Call the custom FillCell method | |
| FillCell(cellAreaName, color, _sheet); | |
| // Return the formatted conditions | |
| return formatConditions; | |
| } | |
| // This method specifies the cell shading color for the conditional formattings cellarea range. | |
| private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| int k = 0; | |
| for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
| { | |
| for (int j = area.StartRow; j <= area.EndRow; j++) | |
| { | |
| Cell c = _sheet.Cells[j, i]; | |
| if (!color.IsEmpty) | |
| { | |
| Style s = c.GetStyle(); | |
| s.ForegroundColor = color; | |
| s.Pattern = BackgroundType.Solid; | |
| c.SetStyle(s); | |
| } | |
| // Set some random values to the cells in the cellarea range | |
| int value = j + i + k; | |
| c.PutValue(value); | |
| k++; | |
| } | |
| } | |
| } | |
| // This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
| // For the conditional formatting | |
| internal static CellArea GetCellAreaByName(string s) | |
| { | |
| CellArea area = new CellArea(); | |
| string[] strCellRange = s.Replace("$", "").Split(':'); | |
| int column; | |
| CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
| area.StartColumn = column; | |
| if (strCellRange.Length == 1) | |
| { | |
| area.EndRow = area.StartRow; | |
| area.EndColumn = area.StartColumn; | |
| } | |
| else | |
| { | |
| CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
| area.EndColumn = column; | |
| } | |
| return area; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void TestTimePeriod() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| AddTimePeriod_1(_sheet); | |
| AddTimePeriod_2(_sheet); | |
| AddTimePeriod_3(_sheet); | |
| AddTimePeriod_4(_sheet); | |
| AddTimePeriod_5(_sheet); | |
| AddTimePeriod_6(_sheet); | |
| AddTimePeriod_7(_sheet); | |
| AddTimePeriod_8(_sheet); | |
| AddTimePeriod_9(_sheet); | |
| AddTimePeriod_10(_sheet); | |
| book.Save(filePath + "TimePeriodType.xlsx"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with Yesterday attribute. | |
| private void AddTimePeriod_10(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I19:K20", Color.MediumSeaGreen, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.Yesterday; | |
| Cell c = _sheet.Cells["I19"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/07/30")); | |
| c = _sheet.Cells["K20"]; | |
| c.PutValue(DateTime.Parse("2008/08/03")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I20"]; | |
| c.PutValue("Yesterday"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with Tomorrow attribute. | |
| private void AddTimePeriod_9(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I17:K18", Color.MediumPurple, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.Tomorrow; | |
| Cell c = _sheet.Cells["I17"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/08/01")); | |
| c = _sheet.Cells["K18"]; | |
| c.PutValue(DateTime.Parse("2008/08/03")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I18"]; | |
| c.PutValue("Tomorrow"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with ThisWeek attribute. | |
| private void AddTimePeriod_8(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I15:K16", Color.MediumOrchid, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.ThisWeek; | |
| Cell c = _sheet.Cells["I15"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/07/28")); | |
| c = _sheet.Cells["K16"]; | |
| c.PutValue(DateTime.Parse("2008/08/03")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I16"]; | |
| c.PutValue("ThisWeek"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with ThisMonth attribute. | |
| private void AddTimePeriod_7(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I13:K14", Color.MediumBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.ThisMonth; | |
| Cell c = _sheet.Cells["I13"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/07/5")); | |
| c = _sheet.Cells["K14"]; | |
| c.PutValue(DateTime.Parse("2008/05/30")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I14"]; | |
| c.PutValue("ThisMonth"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with NextWeek attribute. | |
| private void AddTimePeriod_6(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I11:K12", Color.MediumAquamarine, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.NextWeek; | |
| Cell c = _sheet.Cells["I11"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/08/5")); | |
| c = _sheet.Cells["K12"]; | |
| c.PutValue(DateTime.Parse("2008/07/30")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I12"]; | |
| c.PutValue("NextWeek"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with NextMonth attribute. | |
| private void AddTimePeriod_5(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I9:K10", Color.Maroon, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.NextMonth; | |
| Cell c = _sheet.Cells["I9"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/08/25")); | |
| c = _sheet.Cells["K10"]; | |
| c.PutValue(DateTime.Parse("2008/07/30")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I10"]; | |
| c.PutValue("NextMonth"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with LastWeek attribute. | |
| private void AddTimePeriod_4(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I7:K8", Color.Linen, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.LastWeek; | |
| Cell c = _sheet.Cells["I7"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/07/25")); | |
| c = _sheet.Cells["K8"]; | |
| c.PutValue(DateTime.Parse("2008/07/30")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I8"]; | |
| c.PutValue("LastWeek"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with LastMonth attribute. | |
| private void AddTimePeriod_3(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I5:K6", Color.Linen, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.LastMonth; | |
| Cell c = _sheet.Cells["I5"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/06/26")); | |
| c = _sheet.Cells["K6"]; | |
| c.PutValue(DateTime.Parse("2008/07/30")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I6"]; | |
| c.PutValue("LastMonth"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with Last7Days attribute. | |
| private void AddTimePeriod_2(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I3:K4", Color.LightSteelBlue, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.Last7Days; | |
| Cell c = _sheet.Cells["I3"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Parse("2008/07/26")); | |
| c = _sheet.Cells["K4"]; | |
| c.PutValue(DateTime.Parse("2008/08/30")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I4"]; | |
| c.PutValue("Last7Days"); | |
| } | |
| // This method implements the TimePeriod conditional formatting type with Today attribute. | |
| private void AddTimePeriod_1(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("I1:K2", Color.LightSlateGray, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.TimePeriod); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.TimePeriod = TimePeriodType.Today; | |
| Cell c = _sheet.Cells["I1"]; | |
| Style style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c.PutValue(DateTime.Today); | |
| c = _sheet.Cells["K2"]; | |
| c.PutValue(DateTime.Parse("2008/07/30")); | |
| style = c.GetStyle(); | |
| style.Number = 30; | |
| c.SetStyle(style); | |
| c = _sheet.Cells["I2"]; | |
| c.PutValue("Today"); | |
| } | |
| // This method adds formatted conditions. | |
| private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| // Adds an empty conditional formattings | |
| int index = _sheet.ConditionalFormattings.Add(); | |
| // Get the formatted conditions | |
| FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
| // Get the cell area calling the custom GetCellAreaByName method | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| // Add the formatted conditions cell area. | |
| formatConditions.AddArea(area); | |
| // Call the custom FillCell method | |
| FillCell(cellAreaName, color, _sheet); | |
| // Return the formatted conditions | |
| return formatConditions; | |
| } | |
| // This method specifies the cell shading color for the conditional formattings cellarea range. | |
| private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| int k = 0; | |
| for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
| { | |
| for (int j = area.StartRow; j <= area.EndRow; j++) | |
| { | |
| Cell c = _sheet.Cells[j, i]; | |
| if (!color.IsEmpty) | |
| { | |
| Style s = c.GetStyle(); | |
| s.ForegroundColor = color; | |
| s.Pattern = BackgroundType.Solid; | |
| c.SetStyle(s); | |
| } | |
| // Set some random values to the cells in the cellarea range | |
| int value = j + i + k; | |
| c.PutValue(value); | |
| k++; | |
| } | |
| } | |
| } | |
| // This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
| // For the conditional formatting | |
| internal static CellArea GetCellAreaByName(string s) | |
| { | |
| CellArea area = new CellArea(); | |
| string[] strCellRange = s.Replace("$", "").Split(':'); | |
| int column; | |
| CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
| area.StartColumn = column; | |
| if (strCellRange.Length == 1) | |
| { | |
| area.EndRow = area.StartRow; | |
| area.EndColumn = area.StartColumn; | |
| } | |
| else | |
| { | |
| CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
| area.EndColumn = column; | |
| } | |
| return area; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void TestTop10() | |
| { | |
| // Instantiate a workbook object | |
| Workbook book = new Workbook(); | |
| // Create a worksheet object and get the first worksheet | |
| Worksheet _sheet = book.Worksheets[0]; | |
| AddTop10_1(_sheet); | |
| AddTop10_2(_sheet); | |
| AddTop10_3(_sheet); | |
| AddTop10_4(_sheet); | |
| book.Save(filePath + "Top10.xlsx"); | |
| } | |
| // This method implements a simple Top10 conditional formatting type. | |
| private void AddTop10_1(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A17:C20", Color.Gray, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.Top10); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Yellow; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| } | |
| // This method implements another Top10 conditional formatting type. | |
| private void AddTop10_2(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A21:C24", Color.Green, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.Top10); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Pink; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.Top10.IsBottom = true; | |
| } | |
| // This method implements another Top10 conditional formatting type with some custom attributes. | |
| private void AddTop10_3(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A25:C28", Color.Orange, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.Top10); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Blue; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.Top10.IsPercent = true; | |
| } | |
| // This method implements another Top10 conditional formatting type with some custom attributes. | |
| private void AddTop10_4(Worksheet _sheet) | |
| { | |
| FormatConditionCollection conds = GetFormatCondition("A29:C32", Color.Gold, _sheet); | |
| int idx = conds.AddCondition(FormatConditionType.Top10); | |
| FormatCondition cond = conds[idx]; | |
| cond.Style.BackgroundColor = Color.Green; | |
| cond.Style.Pattern = BackgroundType.Solid; | |
| cond.Top10.Rank = 3; | |
| } | |
| // This method adds formatted conditions. | |
| private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| // Adds an empty conditional formattings | |
| int index = _sheet.ConditionalFormattings.Add(); | |
| // Get the formatted conditions | |
| FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
| // Get the cell area calling the custom GetCellAreaByName method | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| // Add the formatted conditions cell area. | |
| formatConditions.AddArea(area); | |
| // Call the custom FillCell method | |
| FillCell(cellAreaName, color, _sheet); | |
| // Return the formatted conditions | |
| return formatConditions; | |
| } | |
| // This method specifies the cell shading color for the conditional formattings cellarea range. | |
| private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
| { | |
| CellArea area = GetCellAreaByName(cellAreaName); | |
| int k = 0; | |
| for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
| { | |
| for (int j = area.StartRow; j <= area.EndRow; j++) | |
| { | |
| Cell c = _sheet.Cells[j, i]; | |
| if (!color.IsEmpty) | |
| { | |
| Style s = c.GetStyle(); | |
| s.ForegroundColor = color; | |
| s.Pattern = BackgroundType.Solid; | |
| c.SetStyle(s); | |
| } | |
| // Set some random values to the cells in the cellarea range | |
| int value = j + i + k; | |
| c.PutValue(value); | |
| k++; | |
| } | |
| } | |
| } | |
| // This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
| // For the conditional formatting | |
| internal static CellArea GetCellAreaByName(string s) | |
| { | |
| CellArea area = new CellArea(); | |
| string[] strCellRange = s.Replace("$", "").Split(':'); | |
| int column; | |
| CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
| area.StartColumn = column; | |
| if (strCellRange.Length == 1) | |
| { | |
| area.EndRow = area.StartRow; | |
| area.EndColumn = area.StartColumn; | |
| } | |
| else | |
| { | |
| CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
| area.EndColumn = column; | |
| } | |
| return area; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Workbook wb = new Workbook(); | |
| Worksheet sheet = wb.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| Cell a1 = sheet.Cells["A1"]; | |
| Cell a2 = sheet.Cells["A2"]; | |
| a1.PutValue(123456.89); | |
| a2.PutValue(665588.88); | |
| Style style = wb.CreateStyle(); | |
| style.Custom = "[DBNum2][$RMB]General;[Red][DBNum2][$RMB]General"; | |
| a1.SetStyle(style); | |
| a2.SetStyle(style); | |
| wb.Settings.Region = CountryCode.China; | |
| sheet.AutoFitColumns(); | |
| wb.Save(filePath + "out_net.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new Workbook or load an existing one | |
| var workbook = new Aspose.Cells.Workbook(); | |
| // Access the first worksheet in the workbook | |
| var worksheet = workbook.Worksheets[0]; | |
| // Access a specific cell, for example, cell A1 | |
| var cell = worksheet.Cells["A1"]; | |
| // Put some numeric value in the cell | |
| cell.PutValue(1234.56); | |
| // Get the style of the cell | |
| var style = cell.GetStyle(); | |
| // Set the number format to accounting. | |
| // The format code "_(\$* #,##0.00_);_(\$* (#,##0.00);_(\$* \"-\"??_);_(@_)" is an example for US currency. | |
| // You might need to adjust the format code according to your specific requirements and locale. | |
| style.Custom = "_(\\$* #,##0.00_);_(\\$* (#,##0.00);_(\\$* \"-\"??_);_(@_)"; | |
| // Apply the style to the cell | |
| cell.SetStyle(style); | |
| // Save the workbook | |
| workbook.Save("FormattedWorkbook.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new workbook | |
| var workbook = new Aspose.Cells.Workbook(); | |
| // Get the first worksheet | |
| var worksheet = workbook.Worksheets[0]; | |
| // Access the cell you want to format | |
| var cell = worksheet.Cells["A1"]; | |
| // Set the cell value | |
| cell.PutValue(0.5); | |
| // Get the style of the cell | |
| var style = cell.GetStyle(); | |
| // Set the number format to fraction (e.g., "# ?/?") | |
| style.Custom = "# ?/?"; | |
| // Apply the style to the cell | |
| cell.SetStyle(style); | |
| // Save the workbook | |
| workbook.Save("output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new workbook | |
| var workbook = new Aspose.Cells.Workbook(); | |
| // Access the first worksheet | |
| var worksheet = workbook.Worksheets[0]; | |
| // Access a specific cell, for example, cell "A1" | |
| var cell = worksheet.Cells["A1"]; | |
| // Set the cell value | |
| cell.PutValue(0.25); | |
| // Get the cell's style | |
| var style = cell.GetStyle(); | |
| // Set the number format to percentage | |
| style.Number = 9; // Number 9 corresponds to the percentage format | |
| // Apply the style to the cell | |
| cell.SetStyle(style); | |
| // Save the workbook to a file | |
| workbook.Save("output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new workbook | |
| var workbook = new Aspose.Cells.Workbook(); | |
| // Access the first worksheet | |
| var worksheet = workbook.Worksheets[0]; | |
| // Access the cell you want to format, for example, cell "A1" | |
| var cell = worksheet.Cells["A1"]; | |
| // Set the value of the cell | |
| cell.PutValue(12345.6789); // The second parameter ensures the value is set as a number | |
| // Get the cell's style | |
| var style = cell.GetStyle(); | |
| // Set the custom format of the cell to scientific notation | |
| style.Custom = "0.00E+00"; | |
| // Apply the style to the cell | |
| cell.SetStyle(style); | |
| // Save the workbook to a file | |
| workbook.Save("output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new workbook | |
| var workbook = new Aspose.Cells.Workbook(); | |
| // Access the first worksheet | |
| var worksheet = workbook.Worksheets[0]; | |
| // Access the cell at the first row and first column (A1) | |
| var cell = worksheet.Cells["A1"]; | |
| // Set the value of the cell | |
| cell.PutValue(1234567890); // Example value | |
| // Get the style of the cell | |
| var style = cell.GetStyle(); | |
| // Set the custom number format | |
| // For example, format as a phone number | |
| style.Custom = "(###) ###-####"; | |
| // Apply the style to the cell | |
| cell.SetStyle(style); | |
| // Save the workbook | |
| workbook.Save("output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create a new workbook | |
| var workbook = new Aspose.Cells.Workbook(); | |
| // Access the first worksheet | |
| var worksheet = workbook.Worksheets[0]; | |
| // Access the cell you want to format (e.g., "A1") | |
| var cell = worksheet.Cells["A1"]; | |
| // Put a numeric value representing time in the cell | |
| // For example, 0.5 represents 12 hours (half a day) | |
| cell.PutValue(0.5); | |
| // Get the style of the cell | |
| var style = cell.GetStyle(); | |
| // Set the custom number format for time (HH:MM) | |
| style.Custom = "HH:MM"; | |
| // Apply the style to the cell | |
| cell.SetStyle(style); | |
| workbook.Save("output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| Cells cells = ws.Cells; | |
| //Setting the DateTime value to the cells | |
| Cell a1 = cells["A1"]; | |
| a1.PutValue(DateTime.Now); | |
| // Check if the cell contains a numeric value | |
| if (a1.Type == CellValueType.IsNumeric) | |
| { | |
| Console.WriteLine("A1 is Numeric Value: " + a1.IsNumericValue); | |
| } | |
| Style a1Style = a1.GetStyle(); | |
| // Set custom Datetime style | |
| a1Style.Custom = "mm-dd-yy hh:mm:ss"; | |
| a1.SetStyle(a1Style); | |
| // Check if the cell contains a DateTime value | |
| if (a1.Type == CellValueType.IsDateTime) | |
| { | |
| Console.WriteLine("Cell A1 contains a DateTime value."); | |
| // Get the DateTime value | |
| DateTime dateTimeValue = a1.DateTimeValue; | |
| // Now, you can use dateTimeValue as needed | |
| Console.WriteLine("A1 DateTime Value: " + dateTimeValue); | |
| // Output date formatted string | |
| Console.WriteLine("A1 DateTime String Value: " + a1.StringValue); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Cell A1 does not contain a DateTime value."); | |
| } | |
| //Setting the DateTime value to the cells | |
| Cell a2 = cells["A2"]; | |
| a2.Value = DateTime.Now; | |
| // Check if the cell contains a numeric value | |
| if (a2.Type == CellValueType.IsNumeric) | |
| { | |
| Console.WriteLine("A2 is Numeric Value: " + a2.IsNumericValue); | |
| } | |
| Style a2Style = a2.GetStyle(); | |
| // Set the display format of numbers and dates. | |
| a2Style.Number = 22; | |
| a2.SetStyle(a2Style); | |
| // Check if the cell contains a DateTime value | |
| if (a2.Type == CellValueType.IsDateTime) | |
| { | |
| Console.WriteLine("Cell A2 contains a DateTime value."); | |
| // Get the DateTime value | |
| DateTime dateTimeValue = a2.DateTimeValue; | |
| // Now, you can use dateTimeValue as needed | |
| Console.WriteLine("A2 DateTime Value: " + dateTimeValue); | |
| // Output date formatted string | |
| Console.WriteLine("A2 DateTime String Value: " + a2.StringValue); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Cell A2 does not contain a DateTime value."); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| Cells cells = ws.Cells; | |
| //Setting the value to the cells | |
| Cell cell = cells["A1"]; | |
| cell.PutValue("Fruit"); | |
| cell = cells["B1"]; | |
| cell.PutValue("Count"); | |
| cell = cells["C1"]; | |
| cell.PutValue("Price"); | |
| cell = cells["A2"]; | |
| cell.PutValue("Apple"); | |
| cell = cells["A3"]; | |
| cell.PutValue("Mango"); | |
| cell = cells["A4"]; | |
| cell.PutValue("Blackberry"); | |
| cell = cells["A5"]; | |
| cell.PutValue("Cherry"); | |
| Cell c3 = cells["C3"]; | |
| //set html string for C3 cell. | |
| c3.HtmlString = "<b>test bold</b>"; | |
| Cell c4 = cells["C4"]; | |
| //set html string for C4 cell. | |
| c4.HtmlString = "<i>test italic</i>"; | |
| //get the html string of specific cell. | |
| Console.WriteLine(c3.HtmlString); | |
| Console.WriteLine(c4.HtmlString); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| Cells cells = ws.Cells; | |
| //Setting the value to the cells | |
| Cell cell = cells["A1"]; | |
| cell.PutValue("Fruit"); | |
| cell = cells["B1"]; | |
| cell.PutValue("Count"); | |
| cell = cells["C1"]; | |
| cell.PutValue("Price"); | |
| cell = cells["A2"]; | |
| cell.PutValue("Apple"); | |
| cell = cells["A3"]; | |
| cell.PutValue("Mango"); | |
| cell = cells["A4"]; | |
| cell.PutValue("Blackberry"); | |
| cell = cells["A5"]; | |
| cell.PutValue("Cherry"); | |
| cell = cells["B2"]; | |
| cell.PutValue(5); | |
| cell = cells["B3"]; | |
| cell.PutValue(3); | |
| cell = cells["B4"]; | |
| cell.PutValue(6); | |
| cell = cells["B5"]; | |
| cell.PutValue(4); | |
| cell = cells["C2"]; | |
| cell.PutValue(5); | |
| cell = cells["C3"]; | |
| cell.PutValue(20); | |
| cell = cells["C4"]; | |
| cell.PutValue(30); | |
| cell = cells["C5"]; | |
| cell.PutValue(60); | |
| Cell curr = cells.Find("Blackberry", null); | |
| int currRow; | |
| int currCol; | |
| //get row and column index of current cell | |
| CellsHelper.CellNameToIndex(curr.Name, out currRow, out currCol); | |
| Console.WriteLine("Row Index: " + currRow + " Column Index: " + currCol); | |
| //get column name by column index | |
| string columnName = CellsHelper.ColumnIndexToName(currCol); | |
| //get row name by row index | |
| string rowName = CellsHelper.RowIndexToName(currRow); | |
| Console.WriteLine("Column Name: " + columnName + " Row Name: " + rowName); | |
| //get column index by column name | |
| int columnIndex = CellsHelper.ColumnNameToIndex(columnName); | |
| //get row index by row name | |
| int rowIndex = CellsHelper.RowNameToIndex(rowName); | |
| Console.WriteLine("Column Index: " + columnIndex + " Row Index: " + rowIndex); | |
| Assert.AreEqual(columnIndex, currCol); | |
| Assert.AreEqual(rowIndex, currRow); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| Aspose.Cells.Cells cells = ws.Cells; | |
| //Setting the value to the cells | |
| Aspose.Cells.Cell cell = cells["A1"]; | |
| cell.PutValue("Fruit"); | |
| cell = cells["B1"]; | |
| cell.PutValue("Count"); | |
| cell = cells["C1"]; | |
| cell.PutValue("Price"); | |
| cell = cells["A2"]; | |
| cell.PutValue("Apple"); | |
| cell = cells["A3"]; | |
| cell.PutValue("Mango"); | |
| cell = cells["A4"]; | |
| cell.PutValue("Blackberry"); | |
| cell = cells["A5"]; | |
| cell.PutValue("Cherry"); | |
| cell = cells["B2"]; | |
| cell.PutValue(5); | |
| cell = cells["B3"]; | |
| cell.PutValue(3); | |
| cell = cells["B4"]; | |
| cell.PutValue(6); | |
| cell = cells["B5"]; | |
| cell.PutValue(4); | |
| cell = cells["C2"]; | |
| cell.PutValue(5); | |
| cell = cells["C3"]; | |
| cell.PutValue(20); | |
| cell = cells["C4"]; | |
| cell.PutValue(30); | |
| cell = cells["C5"]; | |
| cell.PutValue(60); | |
| cell = cells["E10"]; | |
| Style temp = workbook.CreateStyle(); | |
| temp.Font.Color = Color.Red; | |
| cell.SetStyle(temp); | |
| // Get max display range of worksheet | |
| Range range = cells.MaxDisplayRange; | |
| //get maximum row index of cell which contains data or style. | |
| Console.WriteLine(cells.MaxRow); | |
| //get maximum row index of cell which contains data. | |
| Console.WriteLine(cells.MaxDataRow); | |
| //get maximum column index of cell which contains data or style. | |
| Console.WriteLine(cells.MaxColumn); | |
| //get maximum column index of cell which contains data. | |
| Console.WriteLine(cells.MaxDataColumn); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Workbook workbook = new Workbook(filePath + "sample.xlsx"); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| Cells cells = sheet.Cells; | |
| Row row = cells.CheckRow(1); | |
| if (row != null) | |
| { | |
| //get Maximum column index of Row which contains data or style. | |
| Console.WriteLine("Max column index in row: " + row.LastCell.Column); | |
| //get Maximum column index of Row which contains data. | |
| Console.WriteLine("Max data column index in row: " + row.LastDataCell.Column); | |
| } | |
| // create the range of column B | |
| Range columnRange = cells.CreateRange(1, 1, true); | |
| IEnumerator colIter = columnRange.GetEnumerator(); | |
| int maxRow = 0; | |
| int maxDataRow = 0; | |
| while (colIter.MoveNext()) | |
| { | |
| Cell currCell = (Cell)colIter.Current; | |
| if (!string.IsNullOrEmpty(currCell.StringValue)) | |
| { | |
| maxDataRow = currCell.Row; | |
| } | |
| if (!string.IsNullOrEmpty(currCell.StringValue) || currCell.HasCustomStyle) | |
| { | |
| maxRow = currCell.Row; | |
| } | |
| } | |
| //Maximum row index of Column which contains data or style. | |
| Console.WriteLine("Max row index in Column: " + maxRow); | |
| //Maximum row index of Column which contains data. | |
| Console.WriteLine("Max data row index in Column: " + maxDataRow); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| //Getting D8 cell | |
| Cell d8 = worksheet.Cells["D8"]; | |
| d8.EmbeddedImage = File.ReadAllBytes("aspose.png"); | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| Cells cells = worksheet.Cells; | |
| //Getting F8 cell | |
| Cell f8 = cells["F8"]; | |
| // Adding a picture at the location of a cell whose row and column indices are 7, 5 in the worksheet. It is "F8" cell | |
| int index = worksheet.Pictures.Add(7, 5, "aspose.png"); | |
| double widthPixel = cells.GetColumnWidthPixel(f8.Column); | |
| double heightPixel = cells.GetRowHeightPixel(f8.Row); | |
| Picture picture = worksheet.Pictures[index]; | |
| picture.Width = (int)widthPixel; | |
| picture.Height = (int)heightPixel; | |
| // Saving the Excel file | |
| workbook.Save("out_net.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Row index of the cell | |
| int row = 0; | |
| // Column index of the cell | |
| int column = 0; | |
| Cell a1 = worksheet.Cells[row, column]; | |
| a1.PutValue("a1 rotate text"); | |
| Style a1Style = a1.GetStyle(); | |
| // Set the rotation angle in degrees | |
| a1Style.RotationAngle = 45; | |
| a1.SetStyle(a1Style); | |
| // set Column index of the cell | |
| column = 1; | |
| Cell b1 = worksheet.Cells[row, column]; | |
| b1.PutValue("b1 rotate text"); | |
| Style b1Style = b1.GetStyle(); | |
| // Set the rotation angle in degrees | |
| b1Style.RotationAngle = 255; | |
| b1.SetStyle(b1Style); | |
| // set Column index of the cell | |
| column = 2; | |
| Cell c1 = worksheet.Cells[row, column]; | |
| c1.PutValue("c1 rotate text"); | |
| Style c1Style = c1.GetStyle(); | |
| // Set the rotation angle in degrees | |
| c1Style.RotationAngle = -90; | |
| c1.SetStyle(c1Style); | |
| // set Column index of the cell | |
| column = 3; | |
| Cell d1 = worksheet.Cells[row, column]; | |
| d1.PutValue("d1 rotate text"); | |
| Style d1Style = d1.GetStyle(); | |
| // Set the rotation angle in degrees | |
| d1Style.RotationAngle = -90; | |
| d1.SetStyle(d1Style); | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiating an Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Obtaining the reference of the newly added worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| Cells cells = ws.Cells; | |
| //Setting the DateTime value to the cells | |
| Cell a1 = cells["A1"]; | |
| a1.PutValue(DateTime.Now); | |
| // Check if the cell contains a numeric value | |
| if (a1.Type == CellValueType.IsNumeric) | |
| { | |
| Console.WriteLine("A1 is Numeric Value: " + a1.IsNumericValue); | |
| } | |
| Style a1Style = a1.GetStyle(); | |
| // Set custom Datetime style | |
| a1Style.Custom = "mm-dd-yy hh:mm:ss"; | |
| a1.SetStyle(a1Style); | |
| // Check if the cell contains a DateTime value | |
| if (a1.Type == CellValueType.IsDateTime) | |
| { | |
| Console.WriteLine("Cell A1 contains a DateTime value."); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Cell A1 does not contain a DateTime value."); | |
| } | |
| //Setting the DateTime value to the cells | |
| Cell a2 = cells["A2"]; | |
| a2.Value = DateTime.Now; | |
| // Check if the cell contains a numeric value | |
| if (a2.Type == CellValueType.IsNumeric) | |
| { | |
| Console.WriteLine("A2 is Numeric Value: " + a2.IsNumericValue); | |
| } | |
| Style a2Style = a2.GetStyle(); | |
| // Set the display format of numbers and dates. | |
| a2Style.Number = 22; | |
| a2.SetStyle(a2Style); | |
| // Check if the cell contains a DateTime value | |
| if (a2.Type == CellValueType.IsDateTime) | |
| { | |
| Console.WriteLine("Cell A2 contains a DateTime value."); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Cell A2 does not contain a DateTime value."); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Load the Excel file | |
| Workbook workbook = new Workbook("sample.xlsx"); | |
| // Access the first worksheet | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // Unlock all cells first | |
| Style unlockStyle = workbook.CreateStyle(); | |
| unlockStyle.IsLocked = false; | |
| StyleFlag styleFlag = new StyleFlag(); | |
| styleFlag.Locked = true; | |
| sheet.Cells.ApplyStyle(unlockStyle, styleFlag); | |
| // Lock specific cells (e.g., A1 and B2) | |
| Style lockStyle = workbook.CreateStyle(); | |
| lockStyle.IsLocked = true; | |
| sheet.Cells["A1"].SetStyle(lockStyle); | |
| sheet.Cells["B2"].SetStyle(lockStyle); | |
| // Protect the worksheet to enforce the locking | |
| sheet.Protect(ProtectionType.All); | |
| // Save the modified workbook | |
| workbook.Save("output_locked.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| path = ""; | |
| Workbook wb = new Workbook(path + "Test.xlsx"); | |
| Chart chart1 = wb.Worksheets[0].Charts[0]; | |
| chart1.NSeries.ChangeColors(ChartColorPaletteType.MonochromaticPalette6); | |
| wb.Save(path + "Output.xlsx"); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Obtaining the reference of first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Accessing the "A1" cell from the worksheet | |
| Cell cell = worksheet.Cells["A1"]; | |
| // Adding some value to the "A1" cell | |
| cell.PutValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
| // Gets style in the "A1" cell | |
| Style style = cell.GetStyle(); | |
| // Shrinking the text to fit according to the dimensions of the cell | |
| style.TextDirection = (TextDirectionType.LeftToRight); | |
| cell.SetStyle(style); | |
| // Saving the Excel file | |
| workbook.Save("book1.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //How to Set Category Axis | |
| //Your local test path | |
| String path = @""; | |
| //Create a new workbook | |
| Workbook workbook = new Workbook(); | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| worksheet.Name = ("CHART"); | |
| // Add a chart to the worksheet | |
| int chartIndex = worksheet.Charts.Add(ChartType.Column, 8, 0, 20, 10); | |
| Chart chart = worksheet.Charts[chartIndex]; | |
| //Add some values to cells | |
| worksheet.Cells["A1"].PutValue("Sales"); | |
| worksheet.Cells["A2"].PutValue(100); | |
| worksheet.Cells["A3"].PutValue(150); | |
| worksheet.Cells["A4"].PutValue(130); | |
| worksheet.Cells["A5"].PutValue(160); | |
| worksheet.Cells["A6"].PutValue(150); | |
| worksheet.Cells["B1"].PutValue("Days"); | |
| worksheet.Cells["B2"].PutValue(1); | |
| worksheet.Cells["B3"].PutValue(2); | |
| worksheet.Cells["B4"].PutValue(3); | |
| worksheet.Cells["B5"].PutValue(4); | |
| worksheet.Cells["B6"].PutValue(5); | |
| //Some values in string | |
| String Sales = "100,150,130,160,150"; | |
| String Days = "1,2,3,4,5"; | |
| //Set Category Axis Data with string | |
| chart.NSeries.CategoryData = "{" + Days + "}"; | |
| //Or you can set Category Axis Data with data in cells, try it! | |
| //chart.NSeries.CategoryData = "B2:B6"; | |
| //Add Series to the chart | |
| chart.NSeries.Add("Demand1", true); | |
| //Set value axis with string | |
| chart.NSeries[0].Values = "{" + Sales + "}"; | |
| chart.NSeries.Add("Demand2", true); | |
| //Set value axis with data in cells | |
| chart.NSeries[1].Values = "A2:A6"; | |
| //Set some Category Axis properties | |
| chart.CategoryAxis.TickLabels.RotationAngle = 45; | |
| chart.CategoryAxis.TickLabels.Font.Size = 8; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| //Save the workbook to view the result file | |
| workbook.Save(path + "Output.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Adding a new worksheet to the Workbook object | |
| int sheetIndex = workbook.Worksheets.Add(); | |
| // Obtaining the reference of the newly added worksheet by passing its sheet index | |
| Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
| // Adding sample values to cells | |
| worksheet.Cells["A1"].PutValue("Series1"); | |
| worksheet.Cells["A2"].PutValue(50); | |
| worksheet.Cells["A3"].PutValue(100); | |
| worksheet.Cells["A4"].PutValue(150); | |
| worksheet.Cells["B1"].PutValue("Series2"); | |
| worksheet.Cells["B2"].PutValue(60); | |
| worksheet.Cells["B3"].PutValue(32); | |
| worksheet.Cells["B4"].PutValue(50); | |
| // Adding a chart to the worksheet | |
| int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
| // Accessing the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
| // Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
| chart.SetChartDataRange("A1:B4",true); | |
| //hiding X axis | |
| chart.CategoryAxis.IsVisible = false; | |
| // Setting max value of Y axis. | |
| chart.ValueAxis.MaxValue = 200; | |
| // Setting major unit. | |
| chart.ValueAxis.MajorUnit = 50; | |
| // Save the file | |
| workbook.Save("chart_axes.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Adding a new worksheet to the Workbook object | |
| int sheetIndex = workbook.Worksheets.Add(); | |
| // Obtaining the reference of the newly added worksheet by passing its sheet index | |
| Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
| // Adding sample values to cells | |
| worksheet.Cells["A1"].PutValue(50); | |
| worksheet.Cells["A2"].PutValue(100); | |
| worksheet.Cells["A3"].PutValue(150); | |
| worksheet.Cells["B1"].PutValue(60); | |
| worksheet.Cells["B2"].PutValue(32); | |
| worksheet.Cells["B3"].PutValue(50); | |
| // Adding a chart to the worksheet | |
| int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
| // Accessing the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
| // Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
| chart.NSeries.Add("A1:B3", true); | |
| //Show value labels | |
| chart.NSeries[0].DataLabels.ShowValue = true; | |
| //Show series name labels | |
| chart.NSeries[1].DataLabels.ShowSeriesName = true; | |
| //Move labels to center | |
| chart.NSeries[1].DataLabels.Position = LabelPositionType.Center; | |
| // Save the file | |
| workbook.Save("chart_datalabels.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Adding a new worksheet to the Workbook object | |
| int sheetIndex = workbook.Worksheets.Add(); | |
| // Obtaining the reference of the newly added worksheet by passing its sheet index | |
| Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
| // Adding sample values to cells | |
| worksheet.Cells["A1"].PutValue(50); | |
| worksheet.Cells["A2"].PutValue(100); | |
| worksheet.Cells["A3"].PutValue(150); | |
| worksheet.Cells["B1"].PutValue(60); | |
| worksheet.Cells["B2"].PutValue(32); | |
| worksheet.Cells["B3"].PutValue(50); | |
| // Adding a chart to the worksheet | |
| int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
| // Accessing the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
| // Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
| chart.NSeries.Add("A1:B3", true); | |
| // Setting the title of a chart | |
| chart.Title.Text = "Title"; | |
| // Setting the font color of the chart title to blue | |
| chart.Title.Font.Color = Color.Blue; | |
| // Move the legend to left | |
| chart.Legend.Position = LegendPositionType.Left; | |
| // Set font color of the legend | |
| chart.Legend.Font.Color = Color.Blue; | |
| // Save the file | |
| workbook.Save("chart_legend.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using Aspose.Cells.Charts; | |
| namespace ChartGlobalizationSettingsTest | |
| { | |
| public class ChartChineseSetttings: ChartGlobalizationSettings | |
| { | |
| public override string GetAxisTitleName() | |
| { | |
| return "坐标轴标题"; | |
| } | |
| public override string GetAxisUnitName(DisplayUnitType type) | |
| { | |
| switch (type) | |
| { | |
| case DisplayUnitType.None: | |
| return string.Empty; | |
| case DisplayUnitType.Hundreds: | |
| return "百"; | |
| case DisplayUnitType.Thousands: | |
| return "千"; | |
| case DisplayUnitType.TenThousands: | |
| return "万"; | |
| case DisplayUnitType.HundredThousands: | |
| return "十万"; | |
| case DisplayUnitType.Millions: | |
| return "百万"; | |
| case DisplayUnitType.TenMillions: | |
| return "千万"; | |
| case DisplayUnitType.HundredMillions: | |
| return "亿"; | |
| case DisplayUnitType.Billions: | |
| return "十亿"; | |
| case DisplayUnitType.Trillions: | |
| return "兆"; | |
| default: | |
| return string.Empty; | |
| } | |
| } | |
| public override string GetChartTitleName() | |
| { | |
| return "图表标题"; | |
| } | |
| public override string GetLegendDecreaseName() | |
| { | |
| return "减少"; | |
| } | |
| public override string GetLegendIncreaseName() | |
| { | |
| return "增加"; | |
| } | |
| public override string GetLegendTotalName() | |
| { | |
| return "汇总"; | |
| } | |
| public override string GetOtherName() | |
| { | |
| return "其他"; | |
| } | |
| public override string GetSeriesName() | |
| { | |
| return "系列"; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Create a custom language class for chart component,here take Turkey for example, | |
| //which will translate the chart element to specific language | |
| public class TurkeyChartGlobalizationSettings : ChartGlobalizationSettings | |
| { | |
| public override string GetChartTitleName() | |
| { | |
| return "Grafik Başlığı";//Chart Title | |
| } | |
| public override string GetLegendIncreaseName() | |
| { | |
| return "Artış";//Increase | |
| } | |
| public override string GetLegendDecreaseName() | |
| { | |
| return "Düşüş";//Decrease; | |
| } | |
| public override string GetLegendTotalName() | |
| { | |
| return "Toplam";//Total | |
| } | |
| public override string GetAxisTitleName() | |
| { | |
| return "Eksen Başlığı";//Axis Title | |
| } | |
| } | |
| public static void ChartGlobalizationSettingsTest() | |
| { | |
| //Create an instance of existing Workbook | |
| string pathName = "input.xlsx"; | |
| Workbook workbook = new Workbook(pathName); | |
| //Set custom chartGlobalizationSettings, here is TurkeyChartGlobalizationSettings | |
| workbook.Settings.GlobalizationSettings.ChartSettings = new TurkeyChartGlobalizationSettings(); | |
| //Get the worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| ChartCollection chartCollection = worksheet.Charts; | |
| //Load the chart from source worksheet | |
| Chart chart = chartCollection[0]; | |
| //Chart Calculate | |
| chart.Calculate(); | |
| //Get the chart title | |
| Title title = chart.Title; | |
| //Output the name of the Chart title | |
| Console.WriteLine("\nWorkbook chart title: " + title.Text); | |
| string[] legendEntriesLabels = chart.Legend.GetLegendLabels(); | |
| //Output the name of the Legend | |
| for (int i = 0; i < legendEntriesLabels.Length; i++) | |
| { | |
| Console.WriteLine("\nWorkbook chart legend: " + legendEntriesLabels[i]); | |
| } | |
| //Output the name of the Axis title | |
| Title categoryAxisTitle = chart.CategoryAxis.Title; | |
| Console.WriteLine("\nWorkbook category axis title: " + categoryAxisTitle.Text); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using Aspose.Cells.Charts; | |
| namespace ChartGlobalizationSettingsTest | |
| { | |
| public class ChartJapaneseSetttings: ChartGlobalizationSettings | |
| { | |
| public override string GetAxisTitleName() | |
| { | |
| return "軸タイトル"; | |
| } | |
| public override string GetAxisUnitName(DisplayUnitType type) | |
| { | |
| switch (type) | |
| { | |
| case DisplayUnitType.None: | |
| return string.Empty; | |
| case DisplayUnitType.Hundreds: | |
| return "百"; | |
| case DisplayUnitType.Thousands: | |
| return "千"; | |
| case DisplayUnitType.TenThousands: | |
| return "万"; | |
| case DisplayUnitType.HundredThousands: | |
| return "10万"; | |
| case DisplayUnitType.Millions: | |
| return "百万"; | |
| case DisplayUnitType.TenMillions: | |
| return "千万"; | |
| case DisplayUnitType.HundredMillions: | |
| return "億"; | |
| case DisplayUnitType.Billions: | |
| return "10億"; | |
| case DisplayUnitType.Trillions: | |
| return "兆"; | |
| default: | |
| return string.Empty; | |
| } | |
| } | |
| public override string GetChartTitleName() | |
| { | |
| return "グラフ タイトル"; | |
| } | |
| public override string GetLegendDecreaseName() | |
| { | |
| return "削減"; | |
| } | |
| public override string GetLegendIncreaseName() | |
| { | |
| return "ぞうか"; | |
| } | |
| public override string GetLegendTotalName() | |
| { | |
| return "すべての"; | |
| } | |
| public override string GetOtherName() | |
| { | |
| return "その他"; | |
| } | |
| public override string GetSeriesName() | |
| { | |
| return "シリーズ"; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Load the Excel file containing chart | |
| Workbook wb = new Workbook("ReadAxisLabels.xlsx"); | |
| //Access first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Access the chart | |
| Chart ch = ws.Charts[0]; | |
| //Calculate the chart | |
| ch.Calculate(); | |
| //Read axis labels of category axis | |
| string[] lstLabels = ch.CategoryAxis.GetAxisTexts(); | |
| //Print axis labels on console | |
| System.Console.WriteLine("Category Axis Labels: "); | |
| System.Console.WriteLine("---------------------"); | |
| //Iterate axis labels and print them one by one | |
| for (int i = 0; i < lstLabels.Length; i++) | |
| { | |
| System.Console.WriteLine(lstLabels[i]); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| string inputJson = @"[ | |
| { BEFORE: 'before cell', TEST: 'asd1', AFTER: 'after cell' }, | |
| { BEFORE: 'before cell', TEST: 'asd2', AFTER: 'after cell' }, | |
| { BEFORE: 'before cell', TEST: 'asd3', AFTER: 'after cell' }, | |
| { BEFORE: 'before cell', TEST: 'asd4', AFTER: 'after cell' } | |
| ]"; | |
| string sheetName = "Sheet1"; | |
| int row = 3; | |
| int column = 2; | |
| // create a Workbook object | |
| Workbook book = new Workbook(); | |
| Worksheet worksheet = book.Worksheets[sheetName]; | |
| // set JsonLayoutOptions to treat Arrays as Table | |
| JsonLayoutOptions jsonLayoutOptions = new JsonLayoutOptions(); | |
| jsonLayoutOptions.ArrayAsTable = true; | |
| JsonUtility.ImportData(inputJson, worksheet.Cells, row, column, jsonLayoutOptions); | |
| //save file to xlsx format | |
| book.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook wkb = new Workbook(); | |
| // Obtaining the reference of the first worksheet | |
| Worksheet wks = wkb.Worksheets[0]; | |
| // Adding sample values to cells | |
| wks.Cells["A2"].PutValue("Category1"); | |
| wks.Cells["A3"].PutValue("Category2"); | |
| wks.Cells["A4"].PutValue("Category3"); | |
| wks.Cells["B1"].PutValue("Column1"); | |
| wks.Cells["B2"].PutValue(4); | |
| wks.Cells["B3"].PutValue(20); | |
| wks.Cells["B4"].PutValue(50); | |
| wks.Cells["C1"].PutValue("Column2"); | |
| wks.Cells["C2"].PutValue(50); | |
| wks.Cells["C3"].PutValue(100); | |
| wks.Cells["C4"].PutValue(150); | |
| // Adding a chart to the worksheet | |
| int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
| // Accessing the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
| // Setting chart data source as the range "A1:C4" | |
| chart.SetChartDataRange("A1:C4", true); | |
| // Converting to the BMP file | |
| chart.ToImage("output.bmp"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook wkb = new Workbook(); | |
| // Obtaining the reference of the first worksheet | |
| Worksheet wks = wkb.Worksheets[0]; | |
| // Adding sample values to cells | |
| wks.Cells["A2"].PutValue("Category1"); | |
| wks.Cells["A3"].PutValue("Category2"); | |
| wks.Cells["A4"].PutValue("Category3"); | |
| wks.Cells["B1"].PutValue("Column1"); | |
| wks.Cells["B2"].PutValue(4); | |
| wks.Cells["B3"].PutValue(20); | |
| wks.Cells["B4"].PutValue(50); | |
| wks.Cells["C1"].PutValue("Column2"); | |
| wks.Cells["C2"].PutValue(50); | |
| wks.Cells["C3"].PutValue(100); | |
| wks.Cells["C4"].PutValue(150); | |
| // Adding a chart to the worksheet | |
| int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
| // Accessing the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
| // Setting chart data source as the range "A1:C4" | |
| chart.SetChartDataRange("A1:C4", true); | |
| // Converting to the SVG file | |
| chart.ToImage("output.emf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook wkb = new Workbook(); | |
| // Obtaining the reference of the first worksheet | |
| Worksheet wks = wkb.Worksheets[0]; | |
| // Adding sample values to cells | |
| wks.Cells["A2"].PutValue("Category1"); | |
| wks.Cells["A3"].PutValue("Category2"); | |
| wks.Cells["A4"].PutValue("Category3"); | |
| wks.Cells["B1"].PutValue("Column1"); | |
| wks.Cells["B2"].PutValue(4); | |
| wks.Cells["B3"].PutValue(20); | |
| wks.Cells["B4"].PutValue(50); | |
| wks.Cells["C1"].PutValue("Column2"); | |
| wks.Cells["C2"].PutValue(50); | |
| wks.Cells["C3"].PutValue(100); | |
| wks.Cells["C4"].PutValue(150); | |
| // Adding a chart to the worksheet | |
| int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
| // Accessing the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
| // Setting chart data source as the range "A1:C4" | |
| chart.SetChartDataRange("A1:C4", true); | |
| // Converting to the JPG file | |
| chart.ToImage("output.jpg"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook wkb = new Workbook(); | |
| // Obtaining the reference of the first worksheet | |
| Worksheet wks = wkb.Worksheets[0]; | |
| // Adding sample values to cells | |
| wks.Cells["A2"].PutValue("Category1"); | |
| wks.Cells["A3"].PutValue("Category2"); | |
| wks.Cells["A4"].PutValue("Category3"); | |
| wks.Cells["B1"].PutValue("Column1"); | |
| wks.Cells["B2"].PutValue(4); | |
| wks.Cells["B3"].PutValue(20); | |
| wks.Cells["B4"].PutValue(50); | |
| wks.Cells["C1"].PutValue("Column2"); | |
| wks.Cells["C2"].PutValue(50); | |
| wks.Cells["C3"].PutValue(100); | |
| wks.Cells["C4"].PutValue(150); | |
| // Adding a chart to the worksheet | |
| int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
| // Accessing the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
| // Setting chart data source as the range "A1:C4" | |
| chart.SetChartDataRange("A1:C4", true); | |
| // Converting to the SVG file | |
| chart.ToImage("output.svg"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Get the first worksheet in the worksheets collection. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Create a range of cells. | |
| Range sourceRange = worksheet.Cells.CreateRange("A1", "A2"); | |
| // Input some data with some formattings into | |
| // A few cells in the range. | |
| sourceRange[0, 0].PutValue("Test"); | |
| sourceRange[1, 0].PutValue("123"); | |
| // Create target range of cells. | |
| Range targetRange = worksheet.Cells.CreateRange("B1", "B2"); | |
| // Copy the data of source range to target range | |
| targetRange.CopyData(sourceRange); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Get the first worksheet in the worksheets collection. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Create a range of cells. | |
| Range sourceRange = worksheet.Cells.CreateRange("A1", "A2"); | |
| // Input some data with some formattings into | |
| // A few cells in the range. | |
| sourceRange[0, 0].PutValue("Test"); | |
| sourceRange[1, 0].PutValue("123"); | |
| // Create target range of cells. | |
| Range targetRange = worksheet.Cells.CreateRange("B1", "B2"); | |
| // Copy source range to target range in the same workhseet | |
| targetRange.Copy(sourceRange); | |
| // Create target range of cells. | |
| workbook.Worksheets.Add(); | |
| worksheet = workbook.Worksheets[1]; | |
| targetRange = worksheet.Cells.CreateRange("A1", "A2"); | |
| // Copy source range to target range in another workhseet | |
| targetRange.Copy(sourceRange); | |
| //Copy to another workbook | |
| Workbook anotherWorkbook = new Workbook(); | |
| worksheet = workbook.Worksheets[0]; | |
| targetRange = worksheet.Cells.CreateRange("A1", "A2"); | |
| // Copy source range to target range in another workbook | |
| targetRange.Copy(sourceRange); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create the workbook | |
| Workbook workbook = new Workbook("combo.xlsx"); | |
| // Access the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Add a Column chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.Column, 15, 0, 34, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "Column and Line Combo Chart"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:C13", true); | |
| // Set category data | |
| chart.NSeries.CategoryData = "A2:A13"; | |
| //Set the second Series to Line type | |
| chart.NSeries[1].Type = ChartType.Line; | |
| workbook.Save("comlum_line_combo_chart.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string filepath = ""; | |
| // Create the workbook | |
| Workbook workbook = new Workbook(filepath + "combo.xlsx"); | |
| // Access the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Add a Column chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.Column, 5, 0, 20, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "Combo Chart"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:D3", true); | |
| // Change the chart type for Series[0] , so it will be a Combo Chart | |
| chart.NSeries[0].Type = ChartType.Line; | |
| // Set style for the border of first series | |
| chart.NSeries[0].Border.Style = LineType.Solid; | |
| // Set Color for the first series | |
| chart.NSeries[0].Border.Color = Color.DarkBlue; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.Formatting = FormattingType.None; | |
| // Save the Excel file | |
| workbook.Save(filepath + "out.xlsx"); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //How to Create a Dynamic Chart with Dropdownlist | |
| //Your local test path | |
| string LocalPath = @""; | |
| //Create a new workbook and access the first worksheet. | |
| Workbook workbook = new Workbook(); | |
| WorksheetCollection sheets = workbook.Worksheets; | |
| Worksheet sheet = sheets[0]; | |
| //Populate the data for the chart. Add values to cells and set series names. | |
| sheet.Cells["A3"].PutValue("Tea"); | |
| sheet.Cells["A4"].PutValue("Coffee"); | |
| sheet.Cells["A5"].PutValue("Sugar"); | |
| //In this example, we will add 12 months of data | |
| sheet.Cells["B2"].PutValue("Jan"); | |
| sheet.Cells["C2"].PutValue("Feb"); | |
| sheet.Cells["D2"].PutValue("Mar"); | |
| sheet.Cells["E2"].PutValue("Apr"); | |
| sheet.Cells["F2"].PutValue("May"); | |
| sheet.Cells["G2"].PutValue("Jun"); | |
| sheet.Cells["H2"].PutValue("Jul"); | |
| sheet.Cells["I2"].PutValue("Aug"); | |
| sheet.Cells["J2"].PutValue("Sep"); | |
| sheet.Cells["K2"].PutValue("Oct"); | |
| sheet.Cells["L2"].PutValue("Nov"); | |
| sheet.Cells["M2"].PutValue("Dec"); | |
| int allMonths = 12; | |
| int iCount = 3; | |
| for (int i = 0; i < iCount; i++) | |
| { | |
| for (int j = 0; j < allMonths; j++) | |
| { | |
| int _row = i + 2; | |
| int _column = j + 1; | |
| sheet.Cells[_row, _column].PutValue(50 * (i % 2) + 20 * (j % 3) + 10 * (i / 3) + 10); | |
| } | |
| } | |
| //This is the Dropdownlist for Dynamic Data | |
| CellArea ca = new CellArea(); | |
| ca.StartRow = 9; | |
| ca.EndRow = 9; | |
| ca.StartColumn = 0; | |
| ca.EndColumn = 0; | |
| int _index = sheet.Validations.Add(ca); | |
| Validation _va = sheet.Validations[_index]; | |
| _va.Type = Aspose.Cells.ValidationType.List; | |
| _va.InCellDropDown = true; | |
| _va.Formula1 = "=$B$2:$M$2"; | |
| sheet.Cells["A9"].PutValue("Current Month"); | |
| sheet.Cells["A10"].PutValue("Jan"); | |
| Style _style = sheet.Cells["A10"].GetStyle(); | |
| _style.Font.IsBold = true; | |
| _style.Pattern = BackgroundType.Solid; | |
| _style.ForegroundColor = Color.Yellow; | |
| sheet.Cells["A10"].SetStyle(_style); | |
| //Set the dynamic range for the chart's data source. | |
| int index = sheets.Names.Add("Sheet1!ChtMonthData"); | |
| sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$A$3,0,MATCH($A$10, $B$2:$M$2, 0),3,1)"; | |
| //Set the dynamic range for the chart's data labels. | |
| index = sheets.Names.Add("Sheet1!ChtXLabels"); | |
| sheets.Names[index].RefersTo = "=Sheet1!$A$3:$A$5"; | |
| //Create a chart object and set its data source. | |
| int chartIndex = sheet.Charts.Add(ChartType.Column, 8, 2, 20, 8); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.NSeries.Add("month", true); | |
| chart.NSeries[0].Name = "=Sheet1!$A$10"; | |
| chart.NSeries[0].Values = "Sheet1!ChtMonthData"; | |
| chart.NSeries[0].XValues = "Sheet1!ChtXLabels"; | |
| //Save the workbook as an Excel file. | |
| workbook.Save(LocalPath + "DynamicChartWithDropdownlist.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //How to Create a Dynamic Rolling Chart | |
| //Your local test path | |
| string LocalPath = @""; | |
| //Create a new workbook and access the first worksheet. | |
| Workbook workbook = new Workbook(); | |
| WorksheetCollection sheets = workbook.Worksheets; | |
| Worksheet sheet = sheets[0]; | |
| //Populate the data for the chart. Add values to cells and set series names. | |
| sheet.Cells["A1"].PutValue("Month"); | |
| sheet.Cells["A2"].PutValue(1); | |
| sheet.Cells["A3"].PutValue(2); | |
| sheet.Cells["A4"].PutValue(3); | |
| sheet.Cells["A5"].PutValue(4); | |
| sheet.Cells["A6"].PutValue(5); | |
| sheet.Cells["A7"].PutValue(6); | |
| sheet.Cells["A8"].PutValue(7); | |
| sheet.Cells["B1"].PutValue("Sales"); | |
| sheet.Cells["B2"].PutValue(50); | |
| sheet.Cells["B3"].PutValue(45); | |
| sheet.Cells["B4"].PutValue(55); | |
| sheet.Cells["B5"].PutValue(60); | |
| sheet.Cells["B6"].PutValue(55); | |
| sheet.Cells["B7"].PutValue(65); | |
| sheet.Cells["B8"].PutValue(70); | |
| //Set the dynamic range for the chart's data source. | |
| int index = sheets.Names.Add("Sheet1!ChtData"); | |
| sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$B$1,COUNT(Sheet1!$B:$B),0,-5,1)"; | |
| //Set the dynamic range for the chart's data labels. | |
| index = sheets.Names.Add("Sheet1!ChtLabels"); | |
| sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$A$1,COUNT(Sheet1!$A:$A),0,-5,1)"; | |
| //Create a chart object and set its data source. | |
| int chartIndex = sheet.Charts.Add(ChartType.Line, 10, 3, 25, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.NSeries.Add("Sales", true); | |
| chart.NSeries[0].Values = "Sheet1!ChtData"; | |
| chart.NSeries[0].XValues = "Sheet1!ChtLabels"; | |
| //Save the workbook as an Excel file. | |
| workbook.Save(LocalPath + "DynamicRollingChart.xlsx"); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // How to Create a Dynamic Scrolling Chart | |
| // Your local test path | |
| string LocalPath = @""; | |
| //Create a new workbook and access the first worksheet. | |
| Workbook workbook = new Workbook(); | |
| WorksheetCollection sheets = workbook.Worksheets; | |
| Worksheet sheet = sheets[0]; | |
| //Populate the data for the chart. Add values to cells and set series names. | |
| sheet.Cells["A1"].PutValue("Day"); | |
| sheet.Cells["B1"].PutValue("Sales"); | |
| //In this example, we will add 30 days of data | |
| int allDays = 30; | |
| int showDays = 10; | |
| int currentDay = 1; | |
| for (int i = 0; i < allDays; i++) | |
| { | |
| string _cellA = String.Format("A{0}", i + 2); | |
| string _cellB = String.Format("B{0}", i + 2); | |
| sheet.Cells[_cellA].PutValue(i + 1); | |
| sheet.Cells[_cellB].PutValue(50 * (i % 2) + 20 * (i % 3) + 10 * (i/3)); | |
| } | |
| //This is the Dynamic Scrolling Control Data | |
| sheet.Cells["G19"].PutValue("Start Day"); | |
| sheet.Cells["G20"].PutValue(currentDay); | |
| sheet.Cells["H19"].PutValue("Show Days"); | |
| sheet.Cells["H20"].PutValue(showDays); | |
| //Set the dynamic range for the chart's data source. | |
| int index = sheets.Names.Add("Sheet1!ChtScrollData"); | |
| sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$B$2,Sheet1!$G$20,0,Sheet1!$H$20,1)"; | |
| //Set the dynamic range for the chart's data labels. | |
| index = sheets.Names.Add("Sheet1!ChtScrollLabels"); | |
| sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$A$2,Sheet1!$G$20,0,Sheet1!$H$20,1)"; | |
| //Add a ScrollBar for the Dynamic Scrolling Chart | |
| ScrollBar bar = sheet.Shapes.AddScrollBar(2, 0, 3, 0, 200, 30); | |
| bar.Min = 0; | |
| bar.Max = allDays - showDays; | |
| bar.CurrentValue = currentDay; | |
| bar.LinkedCell = "$G$20"; | |
| //Create a chart object and set its data source. | |
| int chartIndex = sheet.Charts.Add(ChartType.Line, 2, 4, 15, 10); | |
| Chart chart = sheet.Charts[chartIndex]; | |
| chart.NSeries.Add("Sales", true); | |
| chart.NSeries[0].Values = "Sheet1!ChtScrollData"; | |
| chart.NSeries[0].XValues = "Sheet1!ChtScrollLabels"; | |
| //Save the workbook as an Excel file. | |
| workbook.Save(LocalPath + "DynamicScrollingChart.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook("sample.xlsx"); | |
| // Access the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| //Create BarStacked Chart | |
| int i = worksheet.Charts.Add(ChartType.BarStacked, 5, 6, 20, 15); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[i]; | |
| // Set the chart title name | |
| chart.Title.Text = "Gantt Chart"; | |
| // Set the chart title is Visible | |
| chart.Title.IsVisible = true; | |
| // Set data range | |
| chart.SetChartDataRange("B1:B6", true); | |
| // Add series data range | |
| chart.NSeries.Add("C2:C6", true); | |
| // No fill for one serie | |
| chart.NSeries[0].Area.FillFormat.FillType = FillType.None; | |
| // Set the Horizontal(Category) Axis | |
| chart.NSeries.CategoryData = "A2:A6"; | |
| // Reverse the Horizontal(Category) Axis | |
| chart.CategoryAxis.IsPlotOrderReversed = true; | |
| //Set the value axis's MinValue and MaxValue | |
| chart.ValueAxis.MinValue = worksheet.Cells["B2"].Value; | |
| chart.ValueAxis.MaxValue = worksheet.Cells["D6"].Value; | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| //Show the DataLabels | |
| chart.NSeries[1].DataLabels.ShowValue = true; | |
| //Disable the Legend | |
| chart.ShowLegend = false; | |
| //Save the result | |
| workbook.Save("result.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook("High-Low-Close.xlsx"); | |
| // Access the first worksheet. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| //Create High-Low-Close-Stock Chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.StockHighLowClose, 5, 6, 20, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "High-Low-Close Stock"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:D9", true); | |
| // Set category data | |
| chart.NSeries.CategoryData = "A2:A9"; | |
| // Set the marker with the built-in data | |
| chart.NSeries[2].Marker.MarkerStyle = ChartMarkerType.Dash; | |
| chart.NSeries[2].Marker.MarkerSize = 20; | |
| chart.NSeries[2].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[2].Marker.Area.ForegroundColor = Color.Maroon; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook("Open-High-Low-Close.xlsx"); | |
| // Access the first worksheet. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| //Create High-Low-Close-Stock Chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.StockOpenHighLowClose, 5, 6, 20, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "OPen-High-Low-Close Stock"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:E9", true); | |
| // Set category data | |
| chart.NSeries.CategoryData = "A2:A9"; | |
| // Set the DownBars and UpBars with different color | |
| chart.NSeries[0].DownBars.Area.ForegroundColor = Color.Green; | |
| chart.NSeries[0].UpBars.Area.ForegroundColor = Color.Red; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create the workbook | |
| Workbook workbook = new Workbook("combo.xlsx"); | |
| // Access the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Add a stock vloume(VHLC) | |
| int pieIdx = worksheet.Charts.Add(ChartType.StockVolumeHighLowClose, 15, 0, 34, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "Combo Chart"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:E12", true); | |
| // Set category data | |
| chart.NSeries.CategoryData = "A2:A12"; | |
| // Set the Series[1] Series[2] and Series[3] to different Marker Style | |
| for (int j = 0; j < chart.NSeries.Count; j++) | |
| { | |
| switch (j) | |
| { | |
| case 1: | |
| chart.NSeries[j].Marker.MarkerStyle = ChartMarkerType.Circle; | |
| chart.NSeries[j].Marker.MarkerSize = 15; | |
| chart.NSeries[j].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[j].Marker.Area.ForegroundColor = Color.Pink; | |
| chart.NSeries[j].Border.IsVisible = false; | |
| break; | |
| case 2: | |
| chart.NSeries[j].Marker.MarkerStyle = ChartMarkerType.Dash; | |
| chart.NSeries[j].Marker.MarkerSize = 15; | |
| chart.NSeries[j].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[j].Marker.Area.ForegroundColor = Color.Orange; | |
| chart.NSeries[j].Border.IsVisible = false; | |
| break; | |
| case 3: | |
| chart.NSeries[j].Marker.MarkerStyle = ChartMarkerType.Square; | |
| chart.NSeries[j].Marker.MarkerSize = 15; | |
| chart.NSeries[j].Marker.Area.Formatting = FormattingType.Custom; | |
| chart.NSeries[j].Marker.Area.ForegroundColor = Color.LightBlue; | |
| chart.NSeries[j].Border.IsVisible = false; | |
| break; | |
| } | |
| } | |
| // Set the chart type for Series[0] | |
| chart.NSeries[0].Type = ChartType.Line; | |
| // Set style for the border of first series | |
| chart.NSeries[0].Border.Style = LineType.Solid; | |
| // Set Color for the first series | |
| chart.NSeries[0].Border.Color = Color.DarkBlue; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.Formatting = FormattingType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook("sunburst.xlsx"); | |
| // Access the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Add a Treemap chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.Sunburst, 5, 6, 25, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "Sunburst Chart"; | |
| // Add series data range | |
| chart.NSeries.Add("D2:D16", true); | |
| // Set category data(A2:A16 is incorrect,as hierarchical catogory) | |
| chart.NSeries.CategoryData = "A2:C16"; | |
| // Show the DataLabels with category names | |
| chart.NSeries[0].DataLabels.ShowCategoryName = true; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx"); ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Workbook wb = new Workbook("sample.xlsx"); | |
| Worksheet sheet = wb.Worksheets[0]; | |
| ChartCollection charts = sheet.Charts; | |
| // Add bar chart | |
| int index = charts.Add(ChartType.BarStacked, 8, 1, 24, 8); | |
| Chart chart = charts[index]; | |
| // Set data for bar chart | |
| chart.SetChartDataRange("A1:C7", true); | |
| // Set properties for bar chart | |
| chart.Title.Text = "Tornado chart"; | |
| chart.Style = 2; | |
| chart.PlotArea.Area.ForegroundColor = Color.White; | |
| chart.PlotArea.Border.Color = Color.White; | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.Low; | |
| chart.CategoryAxis.IsPlotOrderReversed = true; | |
| chart.GapWidth = 10; | |
| Axis valueAxis = chart.ValueAxis; | |
| valueAxis.TickLabels.NumberFormat = "#,##0;#,##0"; | |
| wb.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook("treemap.xlsx"); | |
| // Access the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Add a Treemap chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.Treemap, 5, 6, 20, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "TreeMap Chart"; | |
| // Add series data range(D2:D13,actually) | |
| chart.NSeries.Add("D2:F13", true); | |
| // Set category data(A2:A13 is incorrect ) | |
| chart.NSeries.CategoryData = "A2:C13"; | |
| // Show the DataLabels with category names | |
| chart.NSeries[0].DataLabels.ShowCategoryName = true; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx");; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook("Volume-High-Low-Close.xlsx"); | |
| // Access the first worksheet. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| //Create High-Low-Close-Stock Chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.StockVolumeHighLowClose, 5, 6, 20, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "Volume-High-Low-Close Stock"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:E9", true); | |
| // Set category data | |
| chart.NSeries.CategoryData = "A2:A9"; | |
| // Set Color for the first series(Volume) data | |
| chart.NSeries[0].Area.ForegroundColor = Color.FromArgb(79, 129,189); | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook("Volume-Open-High-Low-Close.xlsx"); | |
| // Access the first worksheet. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| //Create High-Low-Close-Stock Chart | |
| int pieIdx = worksheet.Charts.Add(ChartType.StockVolumeOpenHighLowClose, 5, 6, 20, 12); | |
| // Retrieve the Chart object | |
| Chart chart = worksheet.Charts[pieIdx]; | |
| // Set the legend can be showed | |
| chart.ShowLegend = true; | |
| // Set the chart title name | |
| chart.Title.Text = "Volume-Open-High-Low-Close Stock"; | |
| // Set the Legend at the bottom of the chart area | |
| chart.Legend.Position = LegendPositionType.Bottom; | |
| // Set data range | |
| chart.SetChartDataRange("A1:F9", true); | |
| // Set category data | |
| chart.NSeries.CategoryData = "A2:A9"; | |
| // Set Color for the first series(Volume) data | |
| chart.NSeries[0].Area.ForegroundColor = Color.FromArgb(79, 129,189); | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| // Save the Excel file | |
| workbook.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Create union range | |
| UnionRange unionRange = workbook.Worksheets.CreateUnionRange("sheet1!A1:A10,sheet1!C1:C10", 0); | |
| // Put value "ABCD" in the range | |
| unionRange.Value = "ABCD"; | |
| // Save the output workbook. | |
| workbook.Save("CreateUnionRange_out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook(); | |
| // Get the First sheet. | |
| Worksheet sheet = workbook.Worksheets["Sheet1"]; | |
| // Add data into details cells. | |
| sheet.Cells[0, 0].PutValue("Fruits Name"); | |
| sheet.Cells[0, 1].PutValue("Fruits Price"); | |
| sheet.Cells[1, 0].PutValue("Apples"); | |
| sheet.Cells[2, 0].PutValue("Bananas"); | |
| sheet.Cells[3, 0].PutValue("Grapes"); | |
| sheet.Cells[4, 0].PutValue("Oranges"); | |
| sheet.Cells[1, 1].PutValue(5); | |
| sheet.Cells[2, 1].PutValue(2); | |
| sheet.Cells[3, 1].PutValue(1); | |
| sheet.Cells[4, 1].PutValue(4); | |
| // Add a chart to the worksheet | |
| int chartIndex = sheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 7, 7, 15, 15); | |
| // Access the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = sheet.Charts[chartIndex]; | |
| // Set data range | |
| chart.SetChartDataRange("A1:B5", true); | |
| // Set AutoFilter range | |
| sheet.AutoFilter.Range = "A1:B5"; | |
| // Add filters for a filter column. | |
| sheet.AutoFilter.AddFilter(0, "Bananas"); | |
| sheet.AutoFilter.AddFilter(0, "Oranges"); | |
| // Apply the filters | |
| sheet.AutoFilter.Refresh(); | |
| chart.ToImage("Autofilter.png"); | |
| workbook.Save("Autofilter.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an instance of Workbook | |
| Workbook workbook = new Workbook(); | |
| // Get the first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Add the sample values to cells | |
| worksheet.Cells["A1"].PutValue("Date"); | |
| // 14 means datetime format | |
| Style style = worksheet.Cells.Style; | |
| style.Number = 14; | |
| // Put values to cells for creating chart | |
| worksheet.Cells["A2"].SetStyle(style); | |
| worksheet.Cells["A2"].PutValue(new DateTime(2022, 6, 26)); | |
| worksheet.Cells["A3"].SetStyle(style); | |
| worksheet.Cells["A3"].PutValue(new DateTime(2022, 5, 22)); | |
| worksheet.Cells["A4"].SetStyle(style); | |
| worksheet.Cells["A4"].PutValue(new DateTime(2022, 8, 3)); | |
| worksheet.Cells["B1"].PutValue("Price"); | |
| worksheet.Cells["B2"].PutValue(40); | |
| worksheet.Cells["B3"].PutValue(50); | |
| worksheet.Cells["B4"].PutValue(60); | |
| // Adda chart to the worksheet | |
| int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 9, 6, 21, 13); | |
| // Access the instance of the newly added chart | |
| Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
| // Add SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B4" | |
| chart.SetChartDataRange("A1:B4", true); | |
| // Set the Axis type to Date time | |
| chart.CategoryAxis.CategoryType = CategoryType.TimeScale; | |
| // Set the base unit for CategoryAxis to days | |
| chart.CategoryAxis.BaseUnitScale = TimeUnit.Days; | |
| // Set the direction for the axis text to be vertical | |
| chart.CategoryAxis.TickLabels.DirectionType = ChartTextDirectionType.Vertical; | |
| // Fill the PlotArea area with nothing | |
| chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
| // Set max value of Y axis. | |
| chart.ValueAxis.MaxValue = 70; | |
| // Set major unit. | |
| chart.ValueAxis.MajorUnit = 10; | |
| // Save the file | |
| workbook.Save("DateAxis.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Deleted some defined names. | |
| worksheets.Names.RemoveDuplicateNames(); | |
| //Save the workbook to retain the changes. | |
| workbook.Save("Book2.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Deleted a named range by text. | |
| worksheets.Names.Remove("NamedRange"); | |
| // Deleted a defined name by index. | |
| worksheets.Names.RemoveAt(0); | |
| //Save the workbook to retain the changes. | |
| workbook.Save("Book2.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Deleted some defined names. | |
| worksheets.Names.Remove(new string[] { "NamedRange1", "NamedRange2" }); | |
| //Save the workbook to retain the changes. | |
| workbook.Save("Book2.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Get the first worksheet in the worksheets collection. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Gets cells. | |
| Cells cells = worksheet.Cells; | |
| // Input some data with some formattings into | |
| // A few cells in the range. | |
| cells["C2"].PutValue("C2"); | |
| cells["C3"].PutValue("C3"); | |
| CellArea ca = CellArea.CreateCellArea("B2", "B3"); | |
| cells.DeleteRange(ca.StartRow, ca.StartColumn, ca.EndRow, ca.EndColumn, ShiftType.Left); | |
| Console.WriteLine(worksheet.Cells["B2"].StringValue == "C2"); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook(); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Get the first worksheet in the worksheets collection. | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Gets cells. | |
| Cells cells = worksheet.Cells; | |
| // Input some data with some formattings into | |
| // A few cells in the range. | |
| cells["B4"].PutValue("B4"); | |
| cells["B5"].PutValue("B5"); | |
| CellArea ca = CellArea.CreateCellArea("B2", "B3"); | |
| cells.DeleteRange(ca.StartRow, ca.StartColumn, ca.EndRow, ca.EndColumn, ShiftType.Up); | |
| Console.WriteLine(worksheet.Cells["B2"].StringValue == "B4"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Init workbook instance. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| //Set autofit options for rendering. | |
| AutoFitterOptions autoFitterOptions = new AutoFitterOptions(); | |
| autoFitterOptions.ForRendering = true; | |
| //Autofit rows with options. | |
| workbook.Worksheets[0].AutoFitRows(autoFitterOptions); | |
| //Save to pdf. | |
| workbook.Save("output.pdf", SaveFormat.Pdf); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Load the Excel file | |
| Workbook workbook = new Workbook("input.xlsx"); | |
| // Create an instance of ImageOrPrintOptions | |
| ImageOrPrintOptions options = new ImageOrPrintOptions | |
| { | |
| // Set horizontal and vertical resolution to 300 DPI | |
| HorizontalResolution = 300, | |
| VerticalResolution = 300, | |
| // Set the image type | |
| ImageType = ImageType.Png, | |
| }; | |
| // Get the worksheet | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // Create a SheetRender instance | |
| SheetRender render = new SheetRender(sheet, options); | |
| // Generate and save the image | |
| render.ToImage(0, "output.png"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Open the template excel file | |
| Workbook wb = new Workbook("sheetset-example.xlsx"); | |
| // Set active sheet to output | |
| PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
| pdfSaveOptions.SheetSet = SheetSet.Active; | |
| // Save the pdf file with PdfSaveOptions | |
| wb.Save("output.pdf", pdfSaveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //prepare a workbook with 3 pages. | |
| Workbook wb = new Workbook(); | |
| wb.Worksheets[0].Cells["A1"].PutValue("Page1"); | |
| int index = wb.Worksheets.Add(); | |
| wb.Worksheets[index].Cells["A1"].PutValue("Page2"); | |
| index = wb.Worksheets.Add(); | |
| wb.Worksheets[index].Cells["A1"].PutValue("Page3"); | |
| wb.Worksheets[index].PageSetup.PaperSize = PaperSizeType.PaperA3; | |
| //create a watermark from image(you need to prepare image bytes). | |
| byte[] imageBytes = null; | |
| RenderingWatermark watermark = new RenderingWatermark(imageBytes); | |
| //specify offset to alignment. | |
| watermark.OffsetX = 100; | |
| watermark.OffsetY = 200; | |
| //specify rotation | |
| watermark.Rotation = 30; | |
| //specify watermark to background. | |
| watermark.IsBackground= true; | |
| //specify opacity | |
| watermark.Opacity = 0.6f; | |
| //specify the scale to page(e.g. 100, 50) in percent. | |
| watermark.ScaleToPagePercent = 50; | |
| //spcify watermark for rendering to pdf. | |
| PdfSaveOptions options = new PdfSaveOptions(); | |
| options.Watermark = watermark; | |
| wb.Save("oputput_image_watermark.pdf", options); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //prepare a workbook with 3 pages. | |
| Workbook wb = new Workbook(); | |
| wb.Worksheets[0].Cells["A1"].PutValue("Page1"); | |
| int index = wb.Worksheets.Add(); | |
| wb.Worksheets[index].Cells["A1"].PutValue("Page2"); | |
| index = wb.Worksheets.Add(); | |
| wb.Worksheets[index].Cells["A1"].PutValue("Page3"); | |
| wb.Worksheets[index].PageSetup.PaperSize = PaperSizeType.PaperA3; | |
| //create a font for watermark, and specify bold, italic, color. | |
| RenderingFont font = new RenderingFont("Calibri", 68); | |
| font.Italic = true; | |
| font.Bold = true; | |
| font.Color = Color.Blue; | |
| //create a watermark from text and the specified font. | |
| RenderingWatermark watermark = new RenderingWatermark("Watermark", font); | |
| //specify horizontal and vertical alignment | |
| watermark.HAlignment = TextAlignmentType.Center; | |
| watermark.VAlignment = TextAlignmentType.Center; | |
| //specify rotation | |
| watermark.Rotation = 30; | |
| //specify opacity | |
| watermark.Opacity = 0.6f; | |
| //specify the scale to page(e.g. 100, 50) in percent. | |
| watermark.ScaleToPagePercent = 50; | |
| //spcify watermark for rendering to pdf. | |
| PdfSaveOptions options = new PdfSaveOptions(); | |
| options.Watermark = watermark; | |
| wb.Save("output_text_watermark.pdf", options); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Open the template excel file | |
| Workbook wb = new Workbook("sheetset-example.xlsx"); | |
| // Set all sheets to output | |
| PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
| pdfSaveOptions.SheetSet = SheetSet.All; | |
| // Save the pdf file with PdfSaveOptions | |
| wb.Save("output.pdf", pdfSaveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| try | |
| { | |
| // Get the template excel file path. | |
| string designerFile = dataDir + "SampleInput.xlsx"; | |
| // Specify the pdf file path. | |
| string pdfFile = dataDir + "Output.out.pdf"; | |
| // Open the template excel file | |
| Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(designerFile); | |
| // Save the pdf file. | |
| wb.Save(pdfFile, SaveFormat.Pdf); | |
| } | |
| catch (Exception e) | |
| { | |
| Console.WriteLine(e.Message); | |
| Console.ReadLine(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Open the template file | |
| Workbook wb = new Workbook("embedded-attachments-example.xlsx"); | |
| // Set to embed Ole Object attachment. | |
| PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
| pdfSaveOptions.EmbedAttachments= true; | |
| // Save the pdf file with PdfSaveOptions | |
| wb.Save("output.pdf", pdfSaveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Open the excel file with image, shape, chart, etc. | |
| Workbook wb = new Workbook("document-structure-example.xlsx"); | |
| // Set to export document structure. | |
| PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
| pdfSaveOptions.ExportDocumentStructure= true; | |
| // Save the pdf file with PdfSaveOptions | |
| wb.Save("output.pdf", pdfSaveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Open the template excel file | |
| Workbook wb = new Workbook("sheetset-example.xlsx"); | |
| // Set custom multiple sheets(Sheet1, Sheet3) to output | |
| SheetSet sheetSet = new SheetSet(new int[] { 0, 2 }); | |
| PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
| pdfSaveOptions.SheetSet = sheetSet; | |
| // Save the pdf file with PdfSaveOptions | |
| wb.Save("output.pdf", pdfSaveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Open the template excel file | |
| Workbook wb = new Workbook("sheetset-example.xlsx"); | |
| // Reorder sheets(Sheet1, Sheet2, Sheet3, Sheet4) to sheets(Sheet4, Sheet3, Sheet2, Sheet1) | |
| SheetSet sheetSet = new SheetSet(new int[] { 3, 2, 1, 0 }); | |
| PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
| pdfSaveOptions.SheetSet = sheetSet; | |
| // Save the pdf file with PdfSaveOptions | |
| wb.Save("output.pdf", pdfSaveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Create an empty Workbook | |
| Workbook wb = new Workbook(); | |
| //Prepare data | |
| wb.Worksheets[0].Cells["D9"].PutValue("gridline"); | |
| //Enable to print gridline | |
| wb.Worksheets[0].PageSetup.PrintGridlines = true; | |
| //Set to render gridline as solid line | |
| PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
| pdfSaveOptions.GridlineType = GridlineType.Hair; | |
| // Save the pdf file with PdfSaveOptions | |
| wb.Save("test_Cs.pdf", pdfSaveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| public class StopConversionOrLoadingUsingInterruptMonitor | |
| { | |
| //Output directory | |
| static string outputDir = "./"; | |
| //Create InterruptMonitor object | |
| InterruptMonitor im = new InterruptMonitor(); | |
| //This function will create workbook and convert it to Pdf format | |
| void CreateWorkbookAndConvertItToPdfFormat(object threadObj) | |
| { | |
| Thread monitorThread = (Thread)threadObj; | |
| //Create a workbook object | |
| Workbook wb = new Workbook(); | |
| //Assign it InterruptMonitor object | |
| wb.InterruptMonitor = im; | |
| //Access first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Access cell J1000000 and add some text inside it. | |
| Cell cell = ws.Cells["J1000000"]; | |
| cell.PutValue("This is text."); | |
| try | |
| { | |
| //Save the workbook to Pdf format | |
| wb.Save(outputDir + "output_InterruptMonitor.pdf"); | |
| //Show successful message | |
| Console.WriteLine("Excel to PDF - Successful Conversion"); | |
| //stop monitor thread | |
| monitorThread.Interrupt(); | |
| } | |
| catch (Aspose.Cells.CellsException ex) | |
| { | |
| if (ex.Code == ExceptionType.Interrupted) | |
| { | |
| Console.WriteLine("Conversion process is interrupted - Message: " + ex.Message); | |
| } | |
| else | |
| { | |
| throw ex; | |
| } | |
| } | |
| } | |
| //This function will interrupt the conversion process after 10s | |
| void WaitForWhileAndThenInterrupt() | |
| { | |
| try | |
| { | |
| Thread.Sleep(1000 * 10); | |
| im.Interrupt(); | |
| } | |
| catch(ThreadInterruptedException e) | |
| { | |
| Console.WriteLine("Monitor thread is interrupted - Message: " + e.Message); | |
| } | |
| } | |
| public void TestRun() | |
| { | |
| Thread monitorThread = new Thread(WaitForWhileAndThenInterrupt); | |
| Thread conversionThread = new Thread(CreateWorkbookAndConvertItToPdfFormat); | |
| monitorThread.Start(); | |
| conversionThread.Start(monitorThread); | |
| monitorThread.Join(); | |
| conversionThread.Join(); | |
| } | |
| public static void Run() | |
| { | |
| new StopConversionOrLoadingUsingInterruptMonitor().TestRun(); | |
| Console.WriteLine("StopConversionOrLoadingUsingInterruptMonitor executed successfully."); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Open an Excel file which workbook structure is protected. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| //Unprotect workbook structure. | |
| workbook.Unprotect("password"); | |
| //Save Excel file. | |
| workbook.Save("Book1.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Create a new file. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| //Gets the first worksheet. | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| //Protect contents of the worksheet. | |
| sheet.Unprotect("password"); | |
| //Save Excel file. | |
| workbook.Save("Book1.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string filePath =@""; | |
| // Create a new workbook | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // Add a RoundedRectangularCallout to the worksheet | |
| Shape polygonShape = sheet.Shapes.AddAutoShape(AutoShapeType.RoundedRectangularCallout, 0, 0, 0, 0, 0, 0); | |
| polygonShape.Y = 200; //Shape Top properties | |
| polygonShape.X = 500; //Shape Left properties | |
| polygonShape.Width = 200; // Shape Width | |
| polygonShape.Height = 100; // Shape Height | |
| ShapeGuideCollection shapeGuides = polygonShape.Geometry.ShapeAdjustValues; | |
| shapeGuides.Add("adj1", 1.02167d); //The distance between the tip point and the center point, with negative values on the left and positive values on the right. Its value is usually the ratio of the half-width. | |
| shapeGuides.Add("adj2", -0.295d); //The distance between the tip point and the center point, negative for upwards and positive for downwards. Its value is usually a ratio of the half-height. | |
| shapeGuides.Add("adj3", 0.16667d); //Usually the default value | |
| // Save the workbook | |
| workbook.Save(filePath + "res.xlsx", SaveFormat.Xlsx); | |
| // Read a new workbook | |
| workbook = new Workbook(filePath + "res.xlsx"); | |
| sheet = workbook.Worksheets[0]; | |
| // Get a RoundedRectangularCallout from the worksheet | |
| polygonShape = sheet.Shapes[0]; | |
| shapeGuides = polygonShape.Geometry.ShapeAdjustValues; | |
| shapeGuides[0].Value = 0.7d; | |
| // Save the workbook | |
| workbook.Save(filePath + "res-resave.xlsx", SaveFormat.Xlsx); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
| // Create workbook object from source Excel file | |
| Workbook workbook = new Workbook(dataDir + "sample.xlsm"); | |
| // Change the VBA Module Code | |
| foreach (VbaModule module in workbook.VbaProject.Modules) | |
| { | |
| string code = module.Codes; | |
| // Replace the original message with the modified message | |
| if (code.Contains("This is test message.")) | |
| { | |
| code = code.Replace("This is test message.", "This is Aspose.Cells message."); | |
| module.Codes = code; | |
| } | |
| } | |
| // Save the output Excel file | |
| workbook.Save(dataDir + "output_out.xlsm"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
| // Load excel file into workbook object | |
| Workbook workbook = new Workbook(dataDir + "SampleBook.xlsx"); | |
| // Save into Pdf with Minimum size | |
| PdfSaveOptions opts = new PdfSaveOptions(); | |
| opts.OptimizationType = Aspose.Cells.Rendering.PdfOptimizationType.MinimumSize; | |
| workbook.Save(dataDir + "OptimizedOutput_out.pdf", opts); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
| // Create workbook | |
| Workbook wb = new Workbook(); | |
| Cells cells = wb.Worksheets[0].Cells; | |
| // Set formula | |
| Cell cell = cells[0, 0]; | |
| cell.SetArrayFormula("=MYFUNC()", 2, 2); | |
| Style style = cell.GetStyle(); | |
| style.Number = 14; | |
| cell.SetStyle(style); | |
| // Set calculation options for formula | |
| CalculationOptions copt = new CalculationOptions(); | |
| copt.CustomFunction = new CustomFunctionStaticValue(); | |
| wb.CalculateFormula(copt); | |
| // Save to xlsx by setting the calc mode to manual | |
| wb.Settings.CalcMode = CalcModeType.Manual; | |
| wb.Save(dataDir + "output_out.xlsx"); | |
| // Save to pdf | |
| wb.Save(dataDir + "output_out.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| public class CustomFunctionStaticValue : ICustomFunction | |
| { | |
| public object CalculateCustomFunction(string functionName, ArrayList paramsList, ArrayList contextObjects) | |
| { | |
| return new object[][] { | |
| new object[]{new DateTime(2015, 6, 12, 10, 6, 30), 2}, | |
| new object[]{3.0, "Test"} | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Load the source Excel file | |
| Workbook wb = new Workbook("sampleSeries_ValuesFormatCode.xlsx"); | |
| //Access first worksheet | |
| Worksheet worksheet = wb.Worksheets[0]; | |
| //Access first chart | |
| Chart ch = worksheet.Charts[0]; | |
| //Add series using an array of values | |
| ch.NSeries.Add("{10000, 20000, 30000, 40000}", true); | |
| //Access the series and set its values format code | |
| Series srs = ch.NSeries[0]; | |
| srs.ValuesFormatCode = "$#,##0"; | |
| //Save the output Excel file | |
| wb.Save("outputSeries_ValuesFormatCode.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SystemTimeInterruptMonitor monitor = new SystemTimeInterruptMonitor(false); | |
| LoadOptions lopts = new LoadOptions(); | |
| lopts.InterruptMonitor = monitor; | |
| monitor.StartMonitor(1000); //time limit is 1 second | |
| Workbook wb = new Workbook("Large.xlsx", lopts); | |
| //if the time cost of loading the template file exceeds one second, interruption will be required and exception will be thrown here | |
| //otherwise starts to monitor the save procedure | |
| monitor.StartMonitor(1500); //time limit is 1.5 seconds | |
| wb.Save("result.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Create workbook | |
| Workbook wb = new Workbook(); | |
| //Add two test worksheets | |
| wb.Worksheets.Add("TestSheet1"); | |
| wb.Worksheets.Add("TestSheet2"); | |
| //Access both worksheets as TestSheet1 and TestSheet2 | |
| Worksheet TestSheet1 = wb.Worksheets["TestSheet1"]; | |
| Worksheet TestSheet2 = wb.Worksheets["TestSheet2"]; | |
| //Set the Paper Size of TestSheet1 to PaperA3ExtraTransverse | |
| TestSheet1.PageSetup.PaperSize = PaperSizeType.PaperA3ExtraTransverse; | |
| //Print the Paper Size of both worksheets | |
| Console.WriteLine("Before Paper Size: " + TestSheet1.PageSetup.PaperSize); | |
| Console.WriteLine("Before Paper Size: " + TestSheet2.PageSetup.PaperSize); | |
| Console.WriteLine(); | |
| //Copy the PageSetup from TestSheet1 to TestSheet2 | |
| TestSheet2.PageSetup.Copy(TestSheet1.PageSetup, new CopyOptions()); | |
| //Print the Paper Size of both worksheets | |
| Console.WriteLine("After Paper Size: " + TestSheet1.PageSetup.PaperSize); | |
| Console.WriteLine("After Paper Size: " + TestSheet2.PageSetup.PaperSize); | |
| Console.WriteLine(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| string sourceDir = "./"; | |
| //Load the first workbook having automatic paper size false | |
| Workbook wb1 = new Workbook(sourceDir + "samplePageSetupIsAutomaticPaperSize-False.xlsx"); | |
| //Load the second workbook having automatic paper size true | |
| Workbook wb2 = new Workbook(sourceDir + "samplePageSetupIsAutomaticPaperSize-True.xlsx"); | |
| //Access first worksheet of both workbooks | |
| Worksheet ws11 = wb1.Worksheets[0]; | |
| Worksheet ws12 = wb2.Worksheets[0]; | |
| //Print the PageSetup.IsAutomaticPaperSize property of both worksheets | |
| Console.WriteLine("First Worksheet of First Workbook - IsAutomaticPaperSize: " + ws11.PageSetup.IsAutomaticPaperSize); | |
| Console.WriteLine("First Worksheet of Second Workbook - IsAutomaticPaperSize: " + ws12.PageSetup.IsAutomaticPaperSize); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Setting the number of pages to which the length of the worksheet will be spanned | |
| worksheet.PageSetup.FitToPagesTall = 1; | |
| // Setting the number of pages to which the width of the worksheet will be spanned | |
| worksheet.PageSetup.FitToPagesWide = 1; | |
| // Save the workbook. | |
| workbook.Save(dataDir + "FitToPagesOptions_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Create an instance of Workbook class | |
| Workbook book = new Workbook(); | |
| // Access first worksheet | |
| Worksheet sheet = book.Worksheets[0]; | |
| // Set paper size to A2 and print paper width and height in inches | |
| sheet.PageSetup.PaperSize = PaperSizeType.PaperA2; | |
| Console.WriteLine("PaperA2: " + sheet.PageSetup.PaperWidth + "x" + sheet.PageSetup.PaperHeight); | |
| // Set paper size to A3 and print paper width and height in inches | |
| sheet.PageSetup.PaperSize = PaperSizeType.PaperA3; | |
| Console.WriteLine("PaperA3: " + sheet.PageSetup.PaperWidth + "x" + sheet.PageSetup.PaperHeight); | |
| // Set paper size to A4 and print paper width and height in inches | |
| sheet.PageSetup.PaperSize = PaperSizeType.PaperA4; | |
| Console.WriteLine("PaperA4: " + sheet.PageSetup.PaperWidth + "x" + sheet.PageSetup.PaperHeight); | |
| // Set paper size to Letter and print paper width and height in inches | |
| sheet.PageSetup.PaperSize = PaperSizeType.PaperLetter; | |
| Console.WriteLine("PaperLetter: " + sheet.PageSetup.PaperWidth + "x" + sheet.PageSetup.PaperHeight); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| string outputDir = "./"; | |
| //Create workbook object | |
| Workbook wb = new Workbook(); | |
| //Access first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Set custom paper size in unit of inches | |
| ws.PageSetup.CustomPaperSize(6, 4); | |
| //Access cell B4 | |
| Cell b4 = ws.Cells["B4"]; | |
| //Add the message in cell B4 | |
| b4.PutValue("Pdf Page Dimensions: 6.00 x 4.00 in"); | |
| //Save the workbook in pdf format | |
| wb.Save(outputDir + "outputCustomPaperSize.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Setting the paper size to A4 | |
| worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4; | |
| // Save the Workbook. | |
| workbook.Save(dataDir + "ManagePaperSize_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Obtaining the reference of the PageSetup of the worksheet | |
| PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
| // Allowing to print gridlines | |
| pageSetup.PrintGridlines = true; | |
| // Allowing to print row/column headings | |
| pageSetup.PrintHeadings = true; | |
| // Allowing to print worksheet in black & white mode | |
| pageSetup.BlackAndWhite = true; | |
| // Allowing to print comments as displayed on worksheet | |
| pageSetup.PrintComments = PrintCommentsType.PrintInPlace; | |
| // Allowing to print worksheet with draft quality | |
| pageSetup.PrintDraft = true; | |
| // Allowing to print cell errors as N/A | |
| pageSetup.PrintErrors = PrintErrorsType.PrintErrorsNA; | |
| // Save the workbook. | |
| workbook.Save(dataDir + "OtherPrintOptions_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Setting the orientation to Portrait | |
| worksheet.PageSetup.Orientation = PageOrientationType.Portrait; | |
| // Save the Workbook. | |
| workbook.Save(dataDir + "PageOrientation_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Source directory | |
| string sourceDir = "./"; | |
| //Output directory | |
| string outputDir = "./"; | |
| //Load source Excel file | |
| Workbook wb = new Workbook(sourceDir + "sampleRemoveExistingPrinterSettingsOfWorksheets.xlsx"); | |
| //Get the sheet counts of the workbook | |
| int sheetCount = wb.Worksheets.Count; | |
| //Iterate all sheets | |
| for (int i = 0; i < sheetCount; i++) | |
| { | |
| //Access the i-th worksheet | |
| Worksheet ws = wb.Worksheets[i]; | |
| //Access worksheet page setup | |
| PageSetup ps = ws.PageSetup; | |
| //Check if printer settings for this worksheet exist | |
| if (ps.PrinterSettings != null) | |
| { | |
| //Print the following message | |
| Console.WriteLine("PrinterSettings of this worksheet exist."); | |
| //Print sheet name and its paper size | |
| Console.WriteLine("Sheet Name: " + ws.Name); | |
| Console.WriteLine("Paper Size: " + ps.PaperSize); | |
| //Remove the printer settings by setting them null | |
| ps.PrinterSettings = null; | |
| Console.WriteLine("Printer settings of this worksheet are now removed by setting it null."); | |
| Console.WriteLine(""); | |
| }//if | |
| }//for | |
| //Save the workbook | |
| wb.Save(outputDir + "outputRemoveExistingPrinterSettingsOfWorksheets.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Setting the scaling factor to 100 | |
| worksheet.PageSetup.Zoom = 100; | |
| // Save the workbook. | |
| workbook.Save(dataDir + "ScalingFactor_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Setting the first page number of the worksheet pages | |
| worksheet.PageSetup.FirstPageNumber = 2; | |
| // Save the Workbook. | |
| workbook.Save(dataDir + "SetFirstPageNumber_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Create a workbook object | |
| Workbook workbook = new Workbook(); | |
| // Get the worksheets in the workbook | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Get the first (default) worksheet | |
| Worksheet worksheet = worksheets[0]; | |
| // Get the pagesetup object | |
| PageSetup pageSetup = worksheet.PageSetup; | |
| // Set bottom,left,right and top page margins | |
| pageSetup.BottomMargin = 2; | |
| pageSetup.LeftMargin = 1; | |
| pageSetup.RightMargin = 1; | |
| pageSetup.TopMargin = 3; | |
| // Save the Workbook. | |
| workbook.Save(dataDir + "SetMargins_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Create a workbook object | |
| Workbook workbook = new Workbook(); | |
| // Get the worksheets in the workbook | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| // Get the first (default) worksheet | |
| Worksheet worksheet = worksheets[0]; | |
| // Get the pagesetup object | |
| PageSetup pageSetup = worksheet.PageSetup; | |
| // Specify Center on page Horizontally and Vertically | |
| pageSetup.CenterHorizontally = true; | |
| pageSetup.CenterVertically = true; | |
| // Save the Workbook. | |
| workbook.Save(dataDir + "CenterOnPage_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Obtaining the reference of the PageSetup of the worksheet | |
| PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
| // Setting the printing order of the pages to over then down | |
| pageSetup.Order = PrintOrderType.OverThenDown; | |
| // Save the workbook. | |
| workbook.Save(dataDir + "SetPageOrder_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Obtaining the reference of the PageSetup of the worksheet | |
| PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
| // Specifying the cells range (from A1 cell to T35 cell) of the print area | |
| pageSetup.PrintArea = "A1:T35"; | |
| // Save the workbook. | |
| workbook.Save(dataDir + "SetPrintArea_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Setting the print quality of the worksheet to 180 dpi | |
| worksheet.PageSetup.PrintQuality = 180; | |
| // Save the Workbook. | |
| workbook.Save(dataDir + "SetPrintQuality_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Obtaining the reference of the PageSetup of the worksheet | |
| Aspose.Cells.PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
| // Defining column numbers A & B as title columns | |
| pageSetup.PrintTitleColumns = "$A:$B"; | |
| // Defining row numbers 1 & 2 as title rows | |
| pageSetup.PrintTitleRows = "$1:$2"; | |
| // Save the workbook. | |
| workbook.Save(dataDir + "SetPrintTitle_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Source directory | |
| string sourceDir = "./"; | |
| //Load source Excel file | |
| Workbook workbook = new Workbook(sourceDir + "SheetRenderSample.xlsx"); | |
| ImageOrPrintOptions imgOpt = new ImageOrPrintOptions(); | |
| //Access first worksheet | |
| Worksheet worksheet = workbook.Worksheets[1]; | |
| SheetRender sheetRender = new SheetRender(worksheet, imgOpt); | |
| PrinterSettings printerSettings = new PrinterSettings(); | |
| printerSettings.PrinterName = "<PRINTER NAME>"; | |
| printerSettings.Copies = 2; | |
| sheetRender.ToPrinter(printerSettings); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Source directory | |
| string sourceDir = "./"; | |
| //Output directory | |
| string outputDir = "./"; | |
| //Load source Excel file | |
| Workbook workbook = new Workbook(sourceDir + "GraphicBackground.ods"); | |
| //Access first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| OdsPageBackground background = worksheet.PageSetup.ODSPageBackground; | |
| Console.WriteLine("Background Type: " + background.Type.ToString()); | |
| Console.WriteLine("Backgorund Position: " + background.GraphicPositionType.ToString()); | |
| //Save background image | |
| Bitmap image = new Bitmap(new MemoryStream(background.GraphicData)); | |
| image.Save(outputDir + "background.jpg"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Source directory | |
| string sourceDir = "./"; | |
| Workbook workbook = new Workbook(sourceDir + "ThreadedCommentsSample.xlsx"); | |
| //Access first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Get Threaded Comments | |
| ThreadedCommentCollection threadedComments = worksheet.Comments.GetThreadedComments("A1"); | |
| foreach (ThreadedComment comment in threadedComments) | |
| { | |
| Console.WriteLine("Comment: " + comment.Notes); | |
| Console.WriteLine("Author: " + comment.Author.Name); | |
| Console.WriteLine("Created Time: " + comment.CreatedTime); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Source directory | |
| string sourceDir = "./"; | |
| Workbook workbook = new Workbook(sourceDir + "ThreadedCommentsSample.xlsx"); | |
| //Access first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Get Threaded Comments | |
| ThreadedCommentCollection threadedComments = worksheet.Comments.GetThreadedComments("A1"); | |
| foreach (ThreadedComment comment in threadedComments) | |
| { | |
| Console.WriteLine("Comment: " + comment.Notes); | |
| Console.WriteLine("Author: " + comment.Author.Name); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Source directory | |
| string sourceDir = "./"; | |
| string outDir = "./"; | |
| Workbook workbook = new Workbook(sourceDir + "ThreadedCommentsSample.xlsx"); | |
| //Access first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| CommentCollection comments = worksheet.Comments; | |
| // Get Author of first comment in A1 | |
| ThreadedCommentAuthor author = worksheet.Comments.GetThreadedComments("A1")[0].Author; | |
| // Remove Comments | |
| comments.RemoveAt("A1"); | |
| ThreadedCommentAuthorCollection authors = workbook.Worksheets.ThreadedCommentAuthors; | |
| // Remove Author of first comment in A1 | |
| authors.RemoveAt(authors.IndexOf(author)); | |
| workbook.Save(outDir + "ThreadedCommentsSample_Out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Creating a file stream containing the Excel file to be opened | |
| FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
| // Instantiating a Workbook object | |
| // Opening the Excel file through the file stream | |
| Workbook excel = new Workbook(fstream); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = excel.Worksheets[0]; | |
| // Restricting users to delete columns of the worksheet | |
| worksheet.Protection.AllowDeletingColumn = false; | |
| // Restricting users to delete row of the worksheet | |
| worksheet.Protection.AllowDeletingRow = false; | |
| // Restricting users to edit contents of the worksheet | |
| worksheet.Protection.AllowEditingContent = false; | |
| // Restricting users to edit objects of the worksheet | |
| worksheet.Protection.AllowEditingObject = false; | |
| // Restricting users to edit scenarios of the worksheet | |
| worksheet.Protection.AllowEditingScenario = false; | |
| // Restricting users to filter | |
| worksheet.Protection.AllowFiltering = false; | |
| // Allowing users to format cells of the worksheet | |
| worksheet.Protection.AllowFormattingCell = true; | |
| // Allowing users to format rows of the worksheet | |
| worksheet.Protection.AllowFormattingRow = true; | |
| // Allowing users to insert columns in the worksheet | |
| worksheet.Protection.AllowFormattingColumn = true; | |
| // Allowing users to insert hyperlinks in the worksheet | |
| worksheet.Protection.AllowInsertingHyperlink = true; | |
| // Allowing users to insert rows in the worksheet | |
| worksheet.Protection.AllowInsertingRow = true; | |
| // Allowing users to select locked cells of the worksheet | |
| worksheet.Protection.AllowSelectingLockedCell = true; | |
| // Allowing users to select unlocked cells of the worksheet | |
| worksheet.Protection.AllowSelectingUnlockedCell = true; | |
| // Allowing users to sort | |
| worksheet.Protection.AllowSorting = true; | |
| // Allowing users to use pivot tables in the worksheet | |
| worksheet.Protection.AllowUsingPivotTable = true; | |
| // Saving the modified Excel file | |
| excel.Save(dataDir + "output.xls", SaveFormat.Excel97To2003); | |
| // Closing the file stream to free all resources | |
| fstream.Close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| worksheet.Cells["A1"].GetStyle().IsLocked = true; | |
| // Finally, Protect the sheet now. | |
| worksheet.Protect(ProtectionType.All); | |
| workbook.Save(dataDir + "output.xlsx"); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Create directory if it is not already present. | |
| bool IsExists = System.IO.Directory.Exists(dataDir); | |
| if (!IsExists) | |
| System.IO.Directory.CreateDirectory(dataDir); | |
| // Instantiate a new Workbook | |
| Workbook book = new Workbook(); | |
| // Get the first (default) worksheet | |
| Worksheet sheet = book.Worksheets[0]; | |
| // Get the Allow Edit Ranges | |
| ProtectedRangeCollection allowRanges = sheet.AllowEditRanges; | |
| // Define ProtectedRange | |
| ProtectedRange proteced_range; | |
| // Create the range | |
| int idx = allowRanges.Add("r2", 1, 1, 3, 3); | |
| proteced_range = allowRanges[idx]; | |
| // Specify the passoword | |
| proteced_range.Password = "123"; | |
| // Protect the sheet | |
| sheet.Protect(ProtectionType.All); | |
| // Save the Excel file | |
| book.Save(dataDir + "protectedrange.out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Create directory if it is not already present. | |
| bool IsExists = System.IO.Directory.Exists(dataDir); | |
| if (!IsExists) | |
| System.IO.Directory.CreateDirectory(dataDir); | |
| // Create a new workbook. | |
| Workbook wb = new Workbook(); | |
| // Create a worksheet object and obtain the first sheet. | |
| Worksheet sheet = wb.Worksheets[0]; | |
| // Define the style object. | |
| Style style; | |
| // Define the styleflag object. | |
| StyleFlag flag; | |
| // Loop through all the columns in the worksheet and unlock them. | |
| for (int i = 0; i <= 255; i++) | |
| { | |
| style = sheet.Cells.Columns[(byte)i].GetStyle(); | |
| style.IsLocked = false; | |
| flag = new StyleFlag(); | |
| flag.Locked = true; | |
| sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag); | |
| } | |
| // Get the first column style. | |
| style = sheet.Cells.Columns[0].GetStyle(); | |
| // Lock it. | |
| style.IsLocked = true; | |
| // Instantiate the flag. | |
| flag = new StyleFlag(); | |
| // Set the lock setting. | |
| flag.Locked = true; | |
| // Apply the style to the first column. | |
| sheet.Cells.Columns[0].ApplyStyle(style, flag); | |
| // Protect the sheet. | |
| sheet.Protect(ProtectionType.All); | |
| // Save the excel file. | |
| wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Create directory if it is not already present. | |
| bool IsExists = System.IO.Directory.Exists(dataDir); | |
| if (!IsExists) | |
| System.IO.Directory.CreateDirectory(dataDir); | |
| // Create a new workbook. | |
| Workbook wb = new Workbook(); | |
| // Create a worksheet object and obtain the first sheet. | |
| Worksheet sheet = wb.Worksheets[0]; | |
| // Define the style object. | |
| Style style; | |
| // Define the styleflag object | |
| StyleFlag styleflag; | |
| // Loop through all the columns in the worksheet and unlock them. | |
| for (int i = 0; i <= 255; i++) | |
| { | |
| style = sheet.Cells.Columns[(byte)i].GetStyle(); | |
| style.IsLocked = false; | |
| styleflag = new StyleFlag(); | |
| styleflag.Locked = true; | |
| sheet.Cells.Columns[(byte)i].ApplyStyle(style, styleflag); | |
| } | |
| // Lock the three cells...i.e. A1, B1, C1. | |
| style = sheet.Cells["A1"].GetStyle(); | |
| style.IsLocked = true; | |
| sheet.Cells["A1"].SetStyle(style); | |
| style = sheet.Cells["B1"].GetStyle(); | |
| style.IsLocked = true; | |
| sheet.Cells["B1"].SetStyle(style); | |
| style = sheet.Cells["C1"].GetStyle(); | |
| style.IsLocked = true; | |
| sheet.Cells["C1"].SetStyle(style); | |
| // Finally, Protect the sheet now. | |
| sheet.Protect(ProtectionType.All); | |
| // Save the excel file. | |
| wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Create directory if it is not already present. | |
| bool IsExists = System.IO.Directory.Exists(dataDir); | |
| if (!IsExists) | |
| System.IO.Directory.CreateDirectory(dataDir); | |
| // Create a new workbook. | |
| Workbook wb = new Workbook(); | |
| // Create a worksheet object and obtain the first sheet. | |
| Worksheet sheet = wb.Worksheets[0]; | |
| // Define the style object. | |
| Style style; | |
| // Define the styleflag object. | |
| StyleFlag flag; | |
| // Loop through all the columns in the worksheet and unlock them. | |
| for (int i = 0; i <= 255; i++) | |
| { | |
| style = sheet.Cells.Columns[(byte)i].GetStyle(); | |
| style.IsLocked = false; | |
| flag = new StyleFlag(); | |
| flag.Locked = true; | |
| sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag); | |
| } | |
| // Get the first row style. | |
| style = sheet.Cells.Rows[0].GetStyle(); | |
| // Lock it. | |
| style.IsLocked = true; | |
| // Instantiate the flag. | |
| flag = new StyleFlag(); | |
| // Set the lock setting. | |
| flag.Locked = true; | |
| // Apply the style to the first row. | |
| sheet.Cells.ApplyRowStyle(0, style, flag); | |
| // Protect the sheet. | |
| sheet.Protect(ProtectionType.All); | |
| // Save the excel file. | |
| wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Creating a file stream containing the Excel file to be opened | |
| FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
| // Instantiating a Workbook object | |
| // Opening the Excel file through the file stream | |
| Workbook excel = new Workbook(fstream); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = excel.Worksheets[0]; | |
| // Protecting the worksheet with a password | |
| worksheet.Protect(ProtectionType.All, "aspose", null); | |
| // Saving the modified Excel file in default format | |
| excel.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003); | |
| // Closing the file stream to free all resources | |
| fstream.Close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Unprotecting the worksheet with a password | |
| worksheet.Unprotect(""); | |
| // Save Workbook | |
| workbook.Save(dataDir + "output.out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Unprotecting the worksheet without a password | |
| worksheet.Unprotect(); | |
| // Saving the Workbook | |
| workbook.Save(dataDir + "output.xls", SaveFormat.Excel97To2003); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Output directory | |
| string outputDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Access first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| worksheet.Cells[0, 0].Value = 1; | |
| worksheet.Cells[1, 0].Value = 2; | |
| worksheet.Cells[2, 0].Value = 3; | |
| worksheet.Cells[3, 0].Value = 4; | |
| worksheet.Cells[4, 0].Value = 5; | |
| worksheet.Cells[5, 0].Value = 6; | |
| worksheet.Cells[0, 1].Value = 7; | |
| worksheet.Cells[1, 1].Value = 8; | |
| worksheet.Cells[2, 1].Value = 9; | |
| worksheet.Cells[3, 1].Value = 10; | |
| worksheet.Cells[4, 1].Value = 11; | |
| worksheet.Cells[5, 1].Value = 12; | |
| OdsPageBackground background = worksheet.PageSetup.ODSPageBackground; | |
| background.Color = Color.Azure; | |
| background.Type = OdsPageBackgroundType.Color; | |
| workbook.Save(outputDir + "ColoredBackground.ods", SaveFormat.Ods); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Source directory | |
| string sourceDir = "./"; | |
| //Output directory | |
| string outputDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| //Access first worksheet | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| worksheet.Cells[0, 0].Value = 1; | |
| worksheet.Cells[1, 0].Value = 2; | |
| worksheet.Cells[2, 0].Value = 3; | |
| worksheet.Cells[3, 0].Value = 4; | |
| worksheet.Cells[4, 0].Value = 5; | |
| worksheet.Cells[5, 0].Value = 6; | |
| worksheet.Cells[0, 1].Value = 7; | |
| worksheet.Cells[1, 1].Value = 8; | |
| worksheet.Cells[2, 1].Value = 9; | |
| worksheet.Cells[3, 1].Value = 10; | |
| worksheet.Cells[4, 1].Value = 11; | |
| worksheet.Cells[5, 1].Value = 12; | |
| OdsPageBackground background = worksheet.PageSetup.ODSPageBackground; | |
| background.Type = OdsPageBackgroundType.Graphic; | |
| background.GraphicData = File.ReadAllBytes(sourceDir + "background.jpg"); | |
| background.GraphicType = OdsPageBackgroundGraphicType.Area; | |
| workbook.Save(outputDir + "GraphicBackground.ods", SaveFormat.Ods); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Load source Excel file | |
| Workbook wb = new Workbook("sampleSheetId.xlsx"); | |
| //Access first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Print its Sheet or Tab Id on console | |
| Console.WriteLine("Sheet or Tab Id: " + ws.TabId); | |
| //Change Sheet or Tab Id | |
| ws.TabId = 358; | |
| //Save the workbook | |
| wb.Save("outputSheetId.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Add a page break at cell Y30 | |
| workbook.Worksheets[0].HorizontalPageBreaks.Add("Y30"); | |
| workbook.Worksheets[0].VerticalPageBreaks.Add("Y30"); | |
| // Save the Excel file. | |
| workbook.Save(dataDir + "AddingPageBreaks_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Clearing all page breaks | |
| workbook.Worksheets[0].HorizontalPageBreaks.Clear(); | |
| workbook.Worksheets[0].VerticalPageBreaks.Clear(); | |
| // Save the Excel file. | |
| workbook.Save(dataDir + "ClearAllPageBreaks_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| string InputPath = dataDir + "book1.xls"; | |
| // Open an existing Excel file. | |
| Workbook wb = new Workbook(InputPath); | |
| // Create a Worksheets object with reference to | |
| // the sheets of the Workbook. | |
| WorksheetCollection sheets = wb.Worksheets; | |
| // Copy data to a new sheet from an existing | |
| // sheet within the Workbook. | |
| sheets.AddCopy("Sheet1"); | |
| // Save the Excel file. | |
| wb.Save(dataDir + "CopyWithinWorkbook_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Create a new Workbook. | |
| Workbook excelWorkbook0 = new Workbook(); | |
| // Get the first worksheet in the book. | |
| Worksheet ws0 = excelWorkbook0.Worksheets[0]; | |
| // Put some data into header rows (A1:A4) | |
| for (int i = 0; i < 5; i++) | |
| { | |
| ws0.Cells[i, 0].PutValue(string.Format("Header Row {0}", i)); | |
| } | |
| // Put some detail data (A5:A999) | |
| for (int i = 5; i < 1000; i++) | |
| { | |
| ws0.Cells[i, 0].PutValue(string.Format("Detail Row {0}", i)); | |
| } | |
| // Define a pagesetup object based on the first worksheet. | |
| PageSetup pagesetup = ws0.PageSetup; | |
| // The first five rows are repeated in each page... | |
| // It can be seen in print preview. | |
| pagesetup.PrintTitleRows = "$1:$5"; | |
| // Create another Workbook. | |
| Workbook excelWorkbook1 = new Workbook(); | |
| // Get the first worksheet in the book. | |
| Worksheet ws1 = excelWorkbook1.Worksheets[0]; | |
| // Name the worksheet. | |
| ws1.Name = "MySheet"; | |
| // Copy data from the first worksheet of the first workbook into the | |
| // first worksheet of the second workbook. | |
| ws1.Copy(ws0); | |
| // Save the excel file. | |
| excelWorkbook1.Save(dataDir + "CopyWorksheetFromWorkbookToOther_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| string InputPath = dataDir + "book1.xls"; | |
| // Create a Workbook. | |
| // Open a file into the first book. | |
| Workbook excelWorkbook0 = new Workbook(InputPath); | |
| // Create another Workbook. | |
| Workbook excelWorkbook1 = new Workbook(); | |
| // Copy the first sheet of the first book into second book. | |
| excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]); | |
| // Save the file. | |
| excelWorkbook1.Save(dataDir + "CopyWorksheetsBetweenWorkbooks_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| string InputPath = dataDir + "book1.xls"; | |
| // Open an existing excel file. | |
| Workbook wb = new Workbook(InputPath); | |
| // Create a Worksheets object with reference to | |
| // the sheets of the Workbook. | |
| WorksheetCollection sheets = wb.Worksheets; | |
| // Get the first worksheet. | |
| Worksheet worksheet = sheets[0]; | |
| // Move the first sheet to the third position in the workbook. | |
| worksheet.MoveTo(2); | |
| // Save the excel file. | |
| wb.Save(dataDir + "MoveWorksheet_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // The path to the documents directory. | |
| string dataDir = "./"; | |
| // Instantiating a Workbook object | |
| Workbook workbook = new Workbook(dataDir + "PageBreaks.xls"); | |
| // Removing a specific page break | |
| workbook.Worksheets[0].HorizontalPageBreaks.RemoveAt(0); | |
| workbook.Worksheets[0].VerticalPageBreaks.RemoveAt(0); | |
| // Save the Excel file. | |
| workbook.Save(dataDir + "RemoveSpecificPageBreak_out.xls"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Load sample Excel file having Xml Map | |
| Workbook wb = new Workbook("sampleRootElementNameOfXmlMap.xlsx"); | |
| //Access first Xml Map inside the Workbook | |
| XmlMap xmap = wb.Worksheets.XmlMaps[0]; | |
| //Print Root Element Name of Xml Map on Console | |
| Console.WriteLine("Root Element Name Of Xml Map: " + xmap.RootElementName); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Load XLSX file containing data from XML file | |
| Workbook workbook = new Workbook("XML Data.xlsx"); | |
| // Access the first worksheet | |
| Worksheet ws = workbook.Worksheets[0]; | |
| // Access ListObject from the first sheet | |
| Aspose.Cells.Tables.ListObject listObject = ws.ListObjects[0]; | |
| // Get the url of the list object's xml map data binding | |
| string url = listObject.XmlMap.DataBinding.Url; | |
| // Display XML file name | |
| Console.WriteLine(url); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Load sample Excel file having Xml Map | |
| Workbook wb = new Workbook("sampleXmlMapQuery.xlsx"); | |
| //Access first XML Map | |
| XmlMap xmap = wb.Worksheets.XmlMaps[0]; | |
| //Access first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Query Xml Map from Path - /MiscData | |
| Console.WriteLine("Query Xml Map from Path - /MiscData"); | |
| ArrayList ret = ws.XmlMapQuery("/MiscData", xmap); | |
| //Print returned ArrayList values | |
| for (int i = 0; i < ret.Count; i++) | |
| { | |
| Console.WriteLine(ret[i]); | |
| } | |
| Console.WriteLine(""); | |
| //Query Xml Map from Path - /MiscData/row/Color | |
| Console.WriteLine("Query Xml Map from Path - /MiscData/row/Color"); | |
| ret = ws.XmlMapQuery("/MiscData/row/Color", xmap); | |
| //Print returned ArrayList values | |
| for (int i = 0; i < ret.Count; i++) | |
| { | |
| Console.WriteLine(ret[i]); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string dirPath = @"./"; | |
| Workbook workbook = new Workbook(dirPath + "Sample.xlsx"); | |
| StringBuilder sb = new StringBuilder(); | |
| sb.Append("<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"UTF-8\">\r\n <title>Title</title>\r\n <script type=\"text/javascript\" async src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML\"></script>\r\n <script type=\"text/x-mathjax-config\">\r\n MathJax.Hub.Config({\r\n\t tex2jax: {\r\n\t inlineMath: [['$','$'], ['\\\\(','\\\\)']],\r\n\t processEscapes: true\r\n\t }\r\n\t});\r\n </script>\r\n</head>\r\n<body>"); | |
| ShapeCollection shapes = workbook.Worksheets[0].Shapes; | |
| TextBox textBox = (TextBox)shapes[0]; | |
| EquationNode mathNode = textBox.GetEquationParagraph().GetChild(0); | |
| string s = mathNode.ToLaTeX(); | |
| sb.AppendLine("<p>$" + s + "$</p>"); | |
| sb.Append("</body>\r\n</html>"); | |
| File.WriteAllText(@"result.html", sb.ToString()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string dirPath = @"./"; | |
| Workbook workbook = new Workbook(dirPath + "Sample.xlsx"); | |
| StringBuilder sb = new StringBuilder(); | |
| sb.Append("<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"UTF-8\">\r\n <title>Title</title>\r\n <script type=\"text/javascript\" async src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML\"></script>\r\n</head>\r\n<body>"); | |
| ShapeCollection shapes = workbook.Worksheets[0].Shapes; | |
| TextBox textBox = (TextBox)shapes[0]; | |
| EquationNode mathNode = textBox.GetEquationParagraph().GetChild(0); | |
| sb.AppendLine(mathNode.ToMathML()); | |
| sb.Append("</body>\r\n</html>"); | |
| File.WriteAllText(@"result.html", sb.ToString()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| // Opening the Excel file through the file stream | |
| Workbook workbook = new Workbook("sample.xlsx"); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| //Method 1: Call MatchBlanks function to apply the filter | |
| //worksheet.AutoFilter.MatchBlanks(1); | |
| //Method 2: Call AddFilter function and set criteria to "" | |
| //worksheet.AutoFilter.AddFilter(1, ""); | |
| //Method 3: Call AddFilter function and set criteria to null | |
| worksheet.AutoFilter.AddFilter(1, null); | |
| // Call refresh function to update the worksheet | |
| worksheet.AutoFilter.Refresh(); | |
| // Saving the modified Excel file | |
| workbook.Save("FilteredBlanks.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiating a Workbook object | |
| // Opening the Excel file through the file stream | |
| Workbook workbook = new Workbook("sample.xlsx"); | |
| // Accessing the first worksheet in the Excel file | |
| Worksheet worksheet = workbook.Worksheets[0]; | |
| // Call MatchBlanks function to apply the filter | |
| worksheet.AutoFilter.MatchNonBlanks(1); | |
| // Call refresh function to update the worksheet | |
| worksheet.AutoFilter.Refresh(); | |
| // Saving the modified Excel file | |
| workbook.Save("FilteredNonBlanks.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Path of your custom font directory. | |
| string customFontsDir = "C:\\TempDir\\CustomFonts"; | |
| // Specify individual font configs custom font directory. | |
| IndividualFontConfigs fontConfigs = new IndividualFontConfigs(); | |
| // If you comment this line or if custom font directory is wrong or | |
| // if it does not contain required font then output pdf will not be rendered correctly. | |
| fontConfigs.SetFontFolder(customFontsDir, false); | |
| // Specify load options with font configs. | |
| LoadOptions opts = new LoadOptions(LoadFormat.Xlsx); | |
| opts.FontConfigs = fontConfigs; | |
| // Load the sample Excel file with individual font configs. | |
| Workbook wb = new Workbook("sampleSpecifyIndividualOrPrivateSetOfFontsForWorkbookRendering.xlsx", opts); | |
| // Save to PDF format. | |
| wb.Save("outputSpecifyIndividualOrPrivateSetOfFontsForWorkbookRendering.pdf", SaveFormat.Pdf); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Create empty workbook. | |
| Workbook wb = new Workbook(); | |
| // Access first worksheet. | |
| Worksheet ws = wb.Worksheets[0]; | |
| // Put some integer values in cell A1 and A2. | |
| ws.Cells["A1"].PutValue(10); | |
| ws.Cells["A2"].PutValue(30); | |
| // Access cell C1 and set its formula. | |
| Cell c1 = ws.Cells["C1"]; ; | |
| c1.Formula = "=Sum(A1,A2)"; | |
| // Add cell C1 into cell watches by name. | |
| ws.CellWatches.Add(c1.Name); | |
| // Access cell E1 and set its formula. | |
| Cell e1 = ws.Cells["E1"]; ; | |
| e1.Formula = "=A2*A1"; | |
| // Add cell E1 into cell watches by its row and column indices. | |
| ws.CellWatches.Add(e1.Row, e1.Column); | |
| // Save workbook to output XLSX format. | |
| wb.Save("outputAddCellsToMicrosoftExcelFormulaWatchWindow.xlsx", SaveFormat.Xlsx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Instantiate an Workbook object | |
| Workbook wb = new Workbook("dynamicArrayFormula.xlsx"); | |
| //Get the first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Getting the F16 | |
| Cell f16 = ws.Cells["F16"]; | |
| //Set dynamic array formula | |
| f16.SetDynamicArrayFormula("=FILTER(A2:C15,(A2:A15=F4)*(C2:C15=25),\"\")", new FormulaParseOptions(), false); | |
| //Refresh the dynamic array formulas | |
| wb.RefreshDynamicArrayFormulas(true); | |
| wb.CalculateFormula(); | |
| wb.Save("out.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Freeze.xlsx"); | |
| // Freezing panes at the scond column | |
| workbook.Worksheets[0].FreezePanes("B1", 0 , 1); | |
| // Saving the file | |
| workbook.Save("frozen.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Freeze.xlsx"); | |
| // Freezing panes at th cell B2 | |
| workbook.Worksheets[0].FreezePanes("B2", 1, 1); | |
| // Saving the file | |
| workbook.Save("frozen.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Freeze.xlsx"); | |
| // Freezing panes at th cell B2 | |
| workbook.Worksheets[0].FreezePanes("A2", 1, 0); | |
| // Saving the file | |
| workbook.Save("frozen.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| Worksheet sheet = worksheets[0]; | |
| //Gets the max data range. | |
| int maxRow = sheet.Cells.MaxDataRow; | |
| int maxColumn = sheet.Cells.MaxDataColumn; | |
| //The range is A1:B3. | |
| Range range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1); | |
| sheet.Cells["A10"].PutValue(null); | |
| maxRow = sheet.Cells.MaxDataRow; | |
| maxColumn = sheet.Cells.MaxDataColumn; | |
| //The range is still A1:B3. | |
| range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| //Gets the max display range. | |
| Range range = worksheets[0].Cells.MaxDisplayRange; | |
| //Save the range to html | |
| HtmlSaveOptions saveOptions = new HtmlSaveOptions(); | |
| saveOptions.ExportActiveWorksheetOnly = true; | |
| saveOptions.ExportArea = CellArea.CreateCellArea(range.FirstRow, range.FirstColumn, range.FirstRow + range.RowCount - 1, range.FirstColumn + range.ColumnCount - 1); | |
| //Save the range. | |
| workbook.Save("html.html", saveOptions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| // Get all the worksheets in the book. | |
| WorksheetCollection worksheets = workbook.Worksheets; | |
| Worksheet sheet = worksheets[0]; | |
| //Gets the max data range. | |
| int maxRow = sheet.Cells.MaxRow; | |
| int maxColumn = sheet.Cells.MaxColumn; | |
| //The range is A1:B3. | |
| Range range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1); | |
| sheet.Cells["A10"].PutValue(null); | |
| maxRow = sheet.Cells.MaxRow; | |
| maxColumn = sheet.Cells.MaxColumn; | |
| //The range is udpated to A1:B10. | |
| range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string filepath = ""; | |
| Workbook wb = new Workbook(filepath + "TestFile.xlsx"); | |
| Chart chart = wb.Worksheets[0].Charts[0]; | |
| chart.Calculate(); | |
| Shape shapeToMove1 = chart.Shapes[0]; | |
| Shape shapeToMove2 = chart.Shapes[1]; | |
| //Get the postion of PlotArea, the unit is in units of ratio of the chart area. | |
| double PlotAreaLeft = chart.PlotArea.InnerXRatioToChart; | |
| double PlotAreaTop = chart.PlotArea.InnerYRatioToChart; | |
| // move the text box to the left edge of the plot area, in unit of 1/4000 of width of the parent chart. | |
| shapeToMove1.LeftInShape = (int)(PlotAreaLeft * 4000); | |
| shapeToMove1.TopInShape = (int)(PlotAreaTop * 4000); | |
| //Get the postion of Chart title, the unit is in units of ratio of the chart area. | |
| double TitleLeft = chart.Title.XRatioToChart; | |
| double TitleBottom = chart.Title.YRatioToChart + chart.Title.HeightRatioToChart; | |
| // move the text box to the left edge of the plot area, in unit of 1/4000 of width of the parent chart. | |
| shapeToMove2.LeftInShape = (int)(TitleLeft * 4000); | |
| shapeToMove2.TopInShape = (int)(TitleBottom * 4000); | |
| wb.Save(filepath + "Output.xlsx"); | |
| //Convert to pixel units | |
| double XInPixels = chart.Title.XRatioToChart * chart.ChartObject.Width; | |
| double WidthInPixels = chart.Title.WidthRatioToChart * chart.ChartObject.Width; | |
| double YInPixels = chart.Title.YRatioToChart * chart.ChartObject.Height; | |
| double HeightInPixels = chart.Title.HeightRatioToChart * chart.ChartObject.Height; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Loading a Workbook object | |
| string path = "your file"; | |
| Workbook workbook = new Workbook(path); | |
| // Access first worksheet from the collection | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // Select picture object via its index from 'Pictures' attribute of Worksheet. | |
| Picture pic = sheet.Pictures[0]; | |
| //print display size | |
| Console.WriteLine("display size: w=" + pic.Width + " h=" + pic.Height); | |
| //print original size | |
| Console.WriteLine("original size: w=" + pic.OriginalWidth + " h=" + pic.OriginalHeight); | |
| //print scaling | |
| Console.WriteLine("scaling: sw=" + pic.WidthScale + "% sh=" + pic.HeightScale + "%"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AcwController : Controller | |
| { | |
| public IActionResult Operation(string type, string id) | |
| { | |
| return Aspose.Cells.GridWeb.AcwController.DoAcwAction(this, type, id); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //set a session store path | |
| GridWeb.SessionStorePath = @"D:\Test\tmp\"; | |
| GridWeb mw = new GridWeb(); | |
| mw.ID = "gid"; | |
| mw.SetSession(HttpContext.Session); | |
| //set acw_client path | |
| mw.ResourceFilePath = "/js/acw_client/"; | |
| //load workbook | |
| mw.ImportExcelFile("D:\\Book1.xlsx"); | |
| //set width height | |
| mw.Width = Unit.Pixel(800); | |
| mw.Height = Unit.Pixel(500); | |
| return View(mw); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @model GridWeb | |
| <script src="~/js/acw_client/acwmain.js" asp-append-version="true"></script> | |
| <script src="~/js/acw_client/lang_en.js" asp-append-version="true"></script> | |
| <link href="~/js/acw_client/menu.css" rel="stylesheet" type="text/css"> | |
| <div class="text-center"> | |
| <GridWebDiv mw=Model></GridWebDiv> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| services.AddSession(options => | |
| { | |
| // Set a short timeout for easy testing. | |
| options.IdleTimeout = TimeSpan.FromSeconds(3600); | |
| options.Cookie.HttpOnly = true; | |
| // Make the session cookie essential | |
| options.Cookie.IsEssential = true; | |
| }); | |
| services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, GridScheduedService>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app.UseSession(); | |
| app.UseMvc(routes => | |
| { | |
| routes.MapRoute("acw", "acw/{type}/{id}", | |
| defaults: new { controller = "Acw", action = "Operation" }); | |
| routes.MapRoute( | |
| name: "default", | |
| template: "{controller=Home}/{action=Index}/{id?}"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @using Aspose.Cells.GridWeb | |
| @addTagHelper *, Aspose.Cells.GridWeb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string path = @"YourPath\"; | |
| string name = "sample"; | |
| // Create the workbook | |
| Workbook workbook = new Workbook(path + name + ".xlsx"); | |
| //Get shapes, images, and objects | |
| ShapeCollection shapes = workbook.Worksheets[0].Shapes; | |
| //group 1 | |
| Shape[] shapeArr1 = new Shape[] { shapes[0], shapes[2] }; | |
| //group 2 | |
| Shape[] shapeArr2 = new Shape[] { shapes[1], shapes[3] }; | |
| shapes.Group(shapeArr1); | |
| shapes.Group(shapeArr2); | |
| // Save the modified workbook | |
| workbook.Save(path + "outputGroup.xlsx", SaveFormat.Xlsx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string path = @"YourPath\"; | |
| string name = "sample"; | |
| // Create the workbook | |
| Workbook workbook = new Workbook(path + name + ".xlsx"); | |
| //Get shapes, images, and objects | |
| ShapeCollection shapes = workbook.Worksheets[0].Shapes; | |
| //Get the group | |
| GroupShape groupShape = (GroupShape)shapes[0]; | |
| //ungroup | |
| shapes.Ungroup(groupShape); | |
| // Save the modified workbook | |
| workbook.Save(path + "outputUngroup.xlsx", SaveFormat.Xlsx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // Loading a Workbook object | |
| Workbook workbook = new Workbook(); | |
| // Access first worksheet from the collection | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| // | |
| ShapeCollection shapes = sheet.Shapes; | |
| //add first shape | |
| Shape shape1 = shapes.AddAutoShape(AutoShapeType.RightArrow, 2, 0, 1, 0, 100, 50); | |
| //add second shape | |
| Shape shape2 = shapes.AddRectangle(6, 0, 2, 0, 30, 30); | |
| //add three shape | |
| shapes.AddAutoShape(AutoShapeType.RightArrow, 2, 0, 5, 0, 100, 50); | |
| //add four shape | |
| shapes.AddRectangle(6, 0, 6, 0, 30, 30); | |
| //group1 | |
| Shape[] shapesArr = new Shape[] { shape1, shape2 }; | |
| shapes.Group(shapesArr); | |
| //group2 | |
| shapesArr = new Shape[] { shapes[2], shapes[3] }; | |
| GroupShape groupShape = shapes.Group(shapesArr); | |
| //ungroup | |
| shapes.Ungroup(groupShape); | |
| //Save | |
| workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| Style defaultStyle = workbook.DefaultStyle; | |
| FontSchemeType schemeType = defaultStyle.Font.SchemeType; | |
| if (schemeType == FontSchemeType.Major //headings | |
| || schemeType == FontSchemeType.Minor //body | |
| ) | |
| { | |
| Console.WriteLine("It's theme font"); | |
| } | |
| //Change theme font to mormal font | |
| defaultStyle.Font.SchemeType = FontSchemeType.None; | |
| workbook.DefaultStyle = defaultStyle; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Instantiate a new Workbook. | |
| Workbook workbook = new Workbook("Book1.xlsx"); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| //Sets A6 cell in the left column as the active cell. | |
| sheet.ActiveCell = "A6"; | |
| //Split worksheet horizontally on rows | |
| sheet.Split(); | |
| workbook.Save("dest.xlsx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Create workbook | |
| Workbook wb = new Workbook(); | |
| //Create an unused named style | |
| wb.CreateStyle().Name = "UnusedStyle_XXXXXXXXXXXXXX"; | |
| //Access first worksheet | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Put some value in cell C7 | |
| ws.Cells["C7"].PutValue("This is sample text."); | |
| //Specify html save options, we want to exclude unused styles | |
| HtmlSaveOptions opts = new HtmlSaveOptions(); | |
| //Comment this line to include unused styles | |
| opts.ExcludeUnusedStyles = true; | |
| //Save the workbook in html format | |
| wb.Save("outputExcludeUnusedStylesInExcelToHTML.html", opts); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Load the sample Excel file | |
| Workbook workbook = new Workbook("sampleExportDocumentWorkbookAndWorksheetPropertiesInHTML.xlsx"); | |
| //Specify Html Save Options | |
| HtmlSaveOptions options = new HtmlSaveOptions(); | |
| //We do not want to export document, workbook and worksheet properties | |
| options.ExportDocumentProperties = false; | |
| options.ExportWorkbookProperties = false; | |
| options.ExportWorksheetProperties = false; | |
| //Export the Excel file to Html with Html Save Options | |
| workbook.Save("outputExportDocumentWorkbookAndWorksheetPropertiesInHTML.html", options); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Load the sample Excel file | |
| Workbook wb = new Workbook("sampleExportSimilarBorderStyle.xlsx"); | |
| //Specify Html Save Options - Export Similar Border Style | |
| HtmlSaveOptions opts = new HtmlSaveOptions(); | |
| opts.ExportSimilarBorderStyle = true; | |
| //Save the workbook in Html format with specified Html Save Options | |
| wb.Save("outputExportSimilarBorderStyle.html", opts); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| //Create workbook. | |
| Workbook wb = new Workbook(); | |
| //Access first worksheet. | |
| Worksheet ws = wb.Worksheets[0]; | |
| //Access cell A1 and put some text inside it. | |
| Cell cell = ws.Cells["A1"]; | |
| cell.PutValue("This is some text."); | |
| //Get the Normal and Html5 strings. | |
| string strNormal = cell.GetHtmlString(false); | |
| string strHtml5 = cell.GetHtmlString(true); | |
| //Print the Normal and Html5 strings on console. | |
| Console.WriteLine("Normal:\r\n" + strNormal); | |
| Console.WriteLine(); | |
| Console.WriteLine("Html5:\r\n" + strHtml5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| string sourceDir = "./"; | |
| string outputDir = "./"; | |
| //Load sample Excel file | |
| Workbook wb = new Workbook(sourceDir + "sampleHidingOverlaidContentWithCrossHideRightWhileSavingToHtml.xlsx"); | |
| //Specify HtmlSaveOptions - Hide Overlaid Content with CrossHideRight while saving to Html | |
| HtmlSaveOptions opts = new HtmlSaveOptions(); | |
| opts.HtmlCrossStringType = HtmlCrossType.CrossHideRight; | |
| //Save to HTML with HtmlSaveOptions | |
| wb.Save(outputDir + "outputHidingOverlaidContentWithCrossHideRightWhileSavingToHtml.html", opts); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HtmlLoadOptions options = new HtmlLoadOptions(LoadFormat.Html); | |
| Workbook book = new Workbook("sample.html", options); | |
| book.Save("out.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
| // load an existing file | |
| Workbook workbook = new Workbook("Test.ods"); | |
| // add the comment object to cell A1 of first worksheet | |
| int commentIndex = workbook.Worksheets[0].Comments.Add("A1"); | |
| Comment comment = workbook.Worksheets[0].Comments[commentIndex]; | |
| //add comments, with the author object | |
| int author1Index = workbook.Worksheets.ThreadedCommentAuthors.Add("Mr.Yang", "author1", "OV1"); | |
| int author2Index = workbook.Worksheets.ThreadedCommentAuthors.Add("Johnson", "author2", "OV2"); | |
| ThreadedCommentAuthor author1 = workbook.Worksheets.ThreadedCommentAuthors[0]; | |
| ThreadedCommentAuthor author2 = workbook.Worksheets.ThreadedCommentAuthors[1]; | |
| comment.ThreadedComments.Add("Where did we get this ods file?", author1); | |
| comment.ThreadedComments.Add("It came from Contoso.", author2); | |
| // save file with comment (check with office 365). | |
| workbook.Save("comment-added.ods"); | |
| // remove one comment from cell A1 | |
| comment.ThreadedComments.RemoveAt(1); | |
| // save the book again to compare (check with office 365). | |
| workbook.Save("comment-removed.ods"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment