protected void lnkBtnCreateWorkbook_Click(object sender, EventArgs e)
{
// get the Excel workbook format
ExcelWorkbookFormat workbookFormat = radioXlsFormat.Checked ? ExcelWorkbookFormat.Xls_2003 :
ExcelWorkbookFormat.Xlsx_2007;
// create the workbook in the desired format with a single worksheet
ExcelWorkbook workbook = new ExcelWorkbook(workbookFormat);
// set the license key before saving the workbook
workbook.LicenseKey = "RW51ZXZ0ZXVldGt1ZXZ0a3R3a3x8fHw=";
// set workbook description properties
workbook.DocumentProperties.Subject = "Adding charts to a worksheet";
workbook.DocumentProperties.Comments =
"Adding Excel charts with Winnovative Excel library for .NET";
#region CREATE CUSTOM WORKBOOK STYLES
#region Add a style used for the cells in the worksheet title area
ExcelCellStyle titleStyle = workbook.Styles.AddStyle("WorksheetTitleStyle");
// center the text in the title area
titleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
titleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
// set the title area borders
titleStyle.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
titleStyle.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
titleStyle.Borders[ExcelCellBorderIndex.Left].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
titleStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
// set the solid fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.SolidFill;
titleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
// set the gradient fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.GradientFill;
titleStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
titleStyle.Fill.GradientFillOptions.Color2 = Color.White;
}
// set the title area font
titleStyle.Font.Size = 14;
titleStyle.Font.Bold = true;
titleStyle.Font.UnderlineType = ExcelCellUnderlineType.Single;
#endregion
#region Add a style used for all the subtitles in the workbook
ExcelCellStyle subtitleStyle = workbook.Styles.AddStyle("WorksheetSubtitlesStyle");
subtitleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left;
subtitleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
subtitleStyle.Font.Size = 12;
subtitleStyle.Font.Bold = true;
subtitleStyle.Font.Color = Color.Green;
subtitleStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Dot;
subtitleStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Dot;
subtitleStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Dot;
subtitleStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Dot;
subtitleStyle.Fill.FillType = ExcelCellFillType.SolidFill;
subtitleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
#endregion
#endregion
// get the first worksheet in the workbook
ExcelWorksheet worksheet = workbook.Worksheets[0];
// set the default worksheet name
worksheet.Name = "Charts Demo";
#region WORKSHEET PAGE SETUP
// set worksheet paper size and orientation, margins, header and footer
worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4;
worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape;
worksheet.PageSetup.LeftMargin = 1;
worksheet.PageSetup.RightMargin = 1;
worksheet.PageSetup.TopMargin = 1;
worksheet.PageSetup.BottomMargin = 1;
// add header and footer
string imagesPath = System.IO.Path.Combine(Server.MapPath("~"), @"Images");
System.Drawing.Image logoImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "logo.jpg"));
//display a logo image in the left part of the header
worksheet.PageSetup.LeftHeaderFormat = "&G";
worksheet.PageSetup.LeftHeaderPicture = logoImg;
// display worksheet name in the right part of the header
worksheet.PageSetup.RightHeaderFormat = "&A";
// add worksheet header and footer
// display the page number in the center part of the footer
worksheet.PageSetup.CenterFooterFormat = "&P";
// display the workbook file name in the left part of the footer
worksheet.PageSetup.LeftFooterFormat = "&F";
// display the current date in the right part of the footer
worksheet.PageSetup.RightFooterFormat = "&D";
#endregion
#region WRITE THE WORKSHEET TOP TITLE
// merge the cells in the range to create the title area
worksheet["A2:G3"].Merge();
// gets the merged range containing the top left cell of the range
ExcelRange titleRange = worksheet["A2"].MergeArea;
// set the text of title area
worksheet["A2"].Text = "Excel Charts Demo";
// set a row height of 18 points for each row in the range
titleRange.RowHeightInPoints = 18;
// set the worksheet top title style
titleRange.Style = titleStyle;
#endregion
#region CREATE DATA TABLE FOR THE CHART
ExcelCellStyle chartValuesStyle = workbook.Styles.AddStyle("ChartValuesStyle");
chartValuesStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
chartValuesStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
chartValuesStyle.Font.Color = Color.Black;
chartValuesStyle.Font.Bold = true;
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
chartValuesStyle.Fill.FillType = ExcelCellFillType.PatternFill;
chartValuesStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_6_25_Percent;
chartValuesStyle.Fill.PatternFillOptions.BackColor = Color.White;
chartValuesStyle.Fill.PatternFillOptions.PatternColor = Color.Green;
}
else
{
chartValuesStyle.Fill.FillType = ExcelCellFillType.GradientFill;
chartValuesStyle.Fill.GradientFillOptions.Color1 = Color.LightGreen;
chartValuesStyle.Fill.GradientFillOptions.Color2 = Color.White;
chartValuesStyle.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown;
}
// set the products tile row text and style
worksheet["C6:G6"].Merge();
worksheet["C6"].Text = "Analyzed Products";
ExcelRange productsTitle = worksheet["C6"].MergeArea;
productsTitle.RowHeightInPoints = 21;
productsTitle.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
productsTitle.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
productsTitle.Style.Font.Size = 12;
productsTitle.Style.Font.Bold = true;
productsTitle.Style.Fill.FillType = ExcelCellFillType.PatternFill;
productsTitle.Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_25_Percent;
productsTitle.Style.Fill.PatternFillOptions.BackColor = Color.White;
productsTitle.Style.Fill.PatternFillOptions.PatternColor = Color.Orange;
// set the quarters title column text and style
worksheet["A8:A11"].Merge();
worksheet["A8"].Text = "Units Sold per Quarter";
ExcelRange quartersTitle = worksheet["A8"].MergeArea;
// set vertical orientation for the text from bottom to top
quartersTitle.Style.Alignment.Orientation = 90;
// wrap text inside the merged range
quartersTitle.Style.Alignment.WrapText = true;
quartersTitle.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
quartersTitle.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
quartersTitle.Style.Font.Size = 12;
quartersTitle.Style.Font.Bold = true;
quartersTitle.Style.Fill.FillType = ExcelCellFillType.PatternFill;
quartersTitle.Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_25_Percent;
quartersTitle.Style.Fill.PatternFillOptions.BackColor = Color.White;
quartersTitle.Style.Fill.PatternFillOptions.PatternColor = Color.Green;
// set the style for the product names row
ExcelRange productNamesRange = worksheet["B7:G7"];
productNamesRange.RowHeightInPoints = 21;
productNamesRange.ColumnWidthInChars = 16;
productNamesRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
productNamesRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
productNamesRange.Style.Font.Bold = true;
productNamesRange.Style.Fill.FillType = ExcelCellFillType.SolidFill;
productNamesRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153);
// set the styles for the quarter names range
ExcelRange quarterNamesRange = worksheet["B8:B11"];
quarterNamesRange.RowHeightInPoints = 16.5;
quarterNamesRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
quarterNamesRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
quarterNamesRange.Style.Font.Bold = true;
quarterNamesRange.Style.Fill.FillType = ExcelCellFillType.SolidFill;
quarterNamesRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153);
// set the row height for the Yearly Total row
ExcelRange yearlyTotalRange = worksheet["B12:G12"];
yearlyTotalRange.RowHeightInPoints = 21;
yearlyTotalRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
yearlyTotalRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
yearlyTotalRange.Style.Font.Color = Color.Blue;
yearlyTotalRange.Style.Font.Bold = true;
yearlyTotalRange.Style.Fill.FillType = ExcelCellFillType.SolidFill;
yearlyTotalRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(204, 255, 255);
// set the series name column
worksheet["B8"].Text = "First Quarter";
worksheet["B9"].Text = "Second Quarter";
worksheet["B10"].Text = "Third Quarter";
worksheet["B11"].Text = "Fourth Quarter";
worksheet["B12"].Text = "Yearly Total";
// set the categories name row
worksheet["C7"].Text = "HTML to PDF";
worksheet["D7"].Text = "PDF Merge";
worksheet["E7"].Text = "PDF Security";
worksheet["F7"].Text = "Web Chart";
worksheet["G7"].Text = "Excel Library";
// set the chart value style
ExcelRange chartValuesRange = worksheet["C8:G11"];
chartValuesRange.Style = chartValuesStyle;
// set the chart values
worksheet["C8"].Value = 1000;
worksheet["D8"].Value = 500;
worksheet["E8"].Value = 200;
worksheet["F8"].Value = 400;
worksheet["G8"].Value = 800;
worksheet["C9"].Value = 850;
worksheet["D9"].Value = 680;
worksheet["E9"].Value = 350;
worksheet["F9"].Value = 230;
worksheet["G9"].Value = 640;
worksheet["C10"].Value = 950;
worksheet["D10"].Value = 450;
worksheet["E10"].Value = 175;
worksheet["F10"].Value = 350;
worksheet["G10"].Value = 520;
worksheet["C11"].Value = 500;
worksheet["D11"].Value = 700;
worksheet["E11"].Value = 250;
worksheet["F11"].Value = 460;
worksheet["G11"].Value = 320;
worksheet["C12"].Formula = "=SUM(C8:C11)";
worksheet["D12"].Formula = "=SUM(D8:D11)";
worksheet["E12"].Formula = "=SUM(E8:E11)";
worksheet["F12"].Formula = "=SUM(F8:F11)";
worksheet["G12"].Formula = "=SUM(G8:G11)";
// auto fit the width of the quarter names column
worksheet["B7"].AutofitColumns();
#endregion
ExcelChartType chartType =
((ExcelChartItem)((rb2D.Checked ? Charts2D : Charts3D)[ddlChartTypes.SelectedIndex])).ChartType;
#region ADD CHART SUBTITLE
// merge the cells in the range to create the subtitle area
worksheet["B14:G14"].Merge();
// gets the merged range containing the top left cell of the range
ExcelRange subtitleRange = worksheet["B14"].MergeArea;
// set the text of title area
string chartDescription =
((ExcelChartItem)((rb2D.Checked ? Charts2D : Charts3D)[ddlChartTypes.SelectedIndex])).Description;
worksheet["B14"].Text = String.Format("{0}
Chart - Automatically Generated Series", chartDescription);
// set a row height of 18 points for each row in the range
subtitleRange.RowHeightInPoints = 18;
// set the worksheet top title style
subtitleRange.Style = subtitleStyle;
#endregion
#region ADD A CHART WITH AUTOMATICALLY GENERATED SERIES TO THE WORKSHEET
ExcelRange dataSourceRange = worksheet["B7:G12"];
bool seriesDataByRows = rbSeriesByRows.Checked;
ExcelChart chart = worksheet.Charts.AddChart(chartType, dataSourceRange, seriesDataByRows, 2, 16, 8, 33);
// set chart title
chart.Title.Text = "Product Units Sold per Quarter - Auto Generated
Series";
chart.Title.Font.Size = 12;
chart.Title.Font.Color = Color.DarkBlue;
// set chart area style
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
chart.ChartArea.Interior.FillType = ExcelShapeFillType.SolidFill;
chart.ChartArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153);
chart.PlotArea.Interior.FillType = ExcelShapeFillType.SolidFill;
chart.PlotArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
chart.ChartArea.Interior.FillType = ExcelShapeFillType.GradientFill;
chart.ChartArea.Interior.GradientFillOptions.GradientColorsMode = ExcelGradientColors.Preset;
chart.ChartArea.Interior.GradientFillOptions.PresetGradientType = ExcelShapeFillPresetGradientType.Wheat;
chart.PlotArea.Interior.FillType = ExcelShapeFillType.TextureFill;
chart.PlotArea.Interior.TextureFillOptions.TextureType = ExcelShapeFillTextureType.Stationery;
}
// set value axis title
chart.ValueAxis.Title.Text = "Units sold";
chart.ValueAxis.Title.Font.Size = 10;
chart.ValueAxis.Title.Font.Bold = true;
// set value axis text style
chart.ValueAxis.Font.Size = 8;
chart.ValueAxis.Font.Bold = false;
chart.ValueAxis.Font.Italic = true;
chart.ValueAxis.ShowVerticalTitleText();
// set category axis title
chart.CategoryAxis.Title.Text = "Analyzed products";
chart.CategoryAxis.Title.Font.Size = 10;
chart.CategoryAxis.Title.Font.Bold = true;
// set category axis text style
chart.CategoryAxis.Font.Size = 8;
chart.CategoryAxis.Font.Bold = false;
chart.CategoryAxis.Font.Italic = true;
// set chart legend style
chart.Legend.Interior.FillType = ExcelShapeFillType.SolidFill;
chart.Legend.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
chart.Legend.Font.Size = 8;
chart.Legend.Font.Bold = true;
if (seriesDataByRows)
{
// show a label with total number of units sold in a year
chart.Series["Yearly Total"].DataPoints.All.Label.ContainsValue = true;
chart.Series["Yearly Total"].DataPoints.All.Label.LabelFormat.Font.Size = 8;
chart.Series["Yearly Total"].DataPoints.All.Label.LabelFormat.Font.Italic = true;
}
else
{
// show a label with total number of units sold in a year
foreach (ExcelChartSeries series in chart.Series)
{
int lastDataPointIndex = series.DataPoints.Count - 1;
series.DataPoints[lastDataPointIndex].Label.ContainsValue = true;
series.DataPoints[lastDataPointIndex].Label.LabelFormat.Font.Size = 8;
series.DataPoints[lastDataPointIndex].Label.LabelFormat.Font.Italic = true;
}
}
#endregion
#region ADD CHART SUBTITLE
// merge the cells in the range to create the subtitle area
worksheet["B34:G34"].Merge();
// gets the merged range containing the top left cell of the range
subtitleRange = worksheet["B34"].MergeArea;
// set the text of title area
chartDescription =
((ExcelChartItem)((rb2D.Checked ? Charts2D : Charts3D)[ddlChartTypes.SelectedIndex])).Description;
worksheet["B34"].Text = String.Format("{0}
Chart - Custom Series", chartDescription);
// set a row height of 18 points for each row in the range
subtitleRange.RowHeightInPoints = 18;
// set the worksheet top title style
subtitleRange.Style = subtitleStyle;
#endregion
#region ADD A CHART WITH CUSTOM SERIES TO THE WORKSHEET
ExcelChart customSeriesChart = worksheet.Charts.AddChart(chartType, 2, 36, 8, 59);
// set chart data table
customSeriesChart.ShowDataTable = true;
customSeriesChart.DataTable.ShowLegendKey = true;
// set chart title
customSeriesChart.Title.Text = "Product Units Sold per Quarter - Custom
Series";
customSeriesChart.Title.Font.Size = 12;
customSeriesChart.Title.Font.Color = Color.DarkBlue;
// create the category names range
ExcelRange categoryNamesRange = worksheet["C7:E7"];
// Add chart series
// add first series for the first quarter sales
ExcelChartSeries firstQuarterSeries = customSeriesChart.Series.AddSeries("First
Quarter Sales");
firstQuarterSeries.ChartType = chartType;
firstQuarterSeries.CategoryNamesRange = worksheet["C7:E7"];
firstQuarterSeries.ValuesRange = worksheet["C8:E8"];
firstQuarterSeries.DataPoints.All.Label.ContainsValue = true;
firstQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Size = 8;
firstQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Italic = true;
//add second series for second quarter sales
ExcelChartSeries secondQuarterSeries = customSeriesChart.Series.AddSeries("Second
Quarter Sales");
secondQuarterSeries.ChartType = chartType;
secondQuarterSeries.CategoryNamesRange = categoryNamesRange;
secondQuarterSeries.ValuesRange = worksheet["C9:E9"];
secondQuarterSeries.DataPoints.All.Label.ContainsValue = true;
secondQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Size = 8;
secondQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Italic = true;
// add third series for fourth quarter sales
ExcelChartSeries fourthQuarterSeries = customSeriesChart.Series.AddSeries("Fourth
Quarter Sales");
fourthQuarterSeries.ChartType = chartType;
fourthQuarterSeries.CategoryNamesRange = categoryNamesRange;
fourthQuarterSeries.ValuesRange = worksheet["C11:E11"];
fourthQuarterSeries.DataPoints.All.Label.ContainsValue = true;
fourthQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Size = 8;
fourthQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Italic = true;
// set chart area style
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
customSeriesChart.ChartArea.Interior.FillType = ExcelShapeFillType.SolidFill;
customSeriesChart.ChartArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153);
customSeriesChart.PlotArea.Interior.FillType = ExcelShapeFillType.SolidFill;
customSeriesChart.PlotArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
customSeriesChart.ChartArea.Interior.FillType = ExcelShapeFillType.GradientFill;
customSeriesChart.ChartArea.Interior.GradientFillOptions.GradientColorsMode = ExcelGradientColors.Preset;
customSeriesChart.ChartArea.Interior.GradientFillOptions.PresetGradientType =
ExcelShapeFillPresetGradientType.Wheat;
customSeriesChart.PlotArea.Interior.FillType = ExcelShapeFillType.TextureFill;
customSeriesChart.PlotArea.Interior.TextureFillOptions.TextureType = ExcelShapeFillTextureType.Stationery;
}
// set value axis title
customSeriesChart.ValueAxis.Title.Text = "Units sold";
customSeriesChart.ValueAxis.Title.Font.Size = 10;
customSeriesChart.ValueAxis.Title.Font.Bold = true;
// set value axis text style
customSeriesChart.ValueAxis.Font.Size = 8;
customSeriesChart.ValueAxis.Font.Bold = false;
customSeriesChart.ValueAxis.Font.Italic = true;
customSeriesChart.ValueAxis.ShowVerticalTitleText();
// set category axis text style
customSeriesChart.CategoryAxis.Font.Size = 8;
customSeriesChart.CategoryAxis.Font.Bold = false;
customSeriesChart.CategoryAxis.Font.Italic = true;
// set chart legend style
customSeriesChart.Legend.Interior.FillType = ExcelShapeFillType.SolidFill;
customSeriesChart.Legend.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
customSeriesChart.Legend.Font.Size = 8;
customSeriesChart.Legend.Font.Bold = true;
#endregion
#region ADD A CHART WORKSHEET
ExcelChartWorksheet chartWorksheet = workbook.ChartWorksheets.AddChartWorksheet(chartType, dataSourceRange,
seriesDataByRows, "Chart Worksheet Demo");
#region CHART WORKSHEET PAGE SETUP
// set worksheet paper size and orientation, margins, header and footer
chartWorksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4;
chartWorksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape;
chartWorksheet.PageSetup.LeftMargin = 0.75;
chartWorksheet.PageSetup.RightMargin = 0.25;
chartWorksheet.PageSetup.TopMargin = 1;
chartWorksheet.PageSetup.BottomMargin = 0.1;
//display a logo image in the left part of the header
chartWorksheet.PageSetup.LeftHeaderFormat = "&G";
chartWorksheet.PageSetup.LeftHeaderPicture = logoImg;
// display worksheet name in the right part of the header
chartWorksheet.PageSetup.RightHeaderFormat = "&A";
#endregion
// set chart title
chartWorksheet.Title.Text = "Product Units Sold per Quarter - Auto
Generated Series";
chartWorksheet.Title.Font.Size = 12;
chartWorksheet.Title.Font.Color = Color.DarkBlue;
// set chart area style
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
chartWorksheet.ChartArea.Interior.FillType = ExcelShapeFillType.SolidFill;
chartWorksheet.ChartArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153);
chartWorksheet.PlotArea.Interior.FillType = ExcelShapeFillType.SolidFill;
chartWorksheet.PlotArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
chartWorksheet.ChartArea.Interior.FillType = ExcelShapeFillType.GradientFill;
chartWorksheet.ChartArea.Interior.GradientFillOptions.GradientColorsMode = ExcelGradientColors.Preset;
chartWorksheet.ChartArea.Interior.GradientFillOptions.PresetGradientType =
ExcelShapeFillPresetGradientType.Wheat;
chartWorksheet.PlotArea.Interior.FillType = ExcelShapeFillType.TextureFill;
chartWorksheet.PlotArea.Interior.TextureFillOptions.TextureType = ExcelShapeFillTextureType.Stationery;
}
// set value axis title
chartWorksheet.ValueAxis.Title.Text = "Units sold";
chartWorksheet.ValueAxis.Title.Font.Size = 10;
chartWorksheet.ValueAxis.Title.Font.Bold = true;
// set value axis text style
chartWorksheet.ValueAxis.Font.Size = 8;
chartWorksheet.ValueAxis.Font.Bold = false;
chartWorksheet.ValueAxis.Font.Italic = true;
chartWorksheet.ValueAxis.ShowVerticalTitleText();
// set category axis title
chartWorksheet.CategoryAxis.Title.Text = "Analyzed products";
chartWorksheet.CategoryAxis.Title.Font.Size = 10;
chartWorksheet.CategoryAxis.Title.Font.Bold = true;
// set category axis text style
chartWorksheet.CategoryAxis.Font.Size = 8;
chartWorksheet.CategoryAxis.Font.Bold = false;
chartWorksheet.CategoryAxis.Font.Italic = true;
// set chart legend style
chartWorksheet.Legend.Interior.FillType = ExcelShapeFillType.SolidFill;
chartWorksheet.Legend.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
chartWorksheet.Legend.Font.Size = 8;
chartWorksheet.Legend.Font.Bold = true;
if (seriesDataByRows)
{
// show a label with total number of units sold in a year
chartWorksheet.Series["Yearly Total"].DataPoints.All.Label.ContainsValue = true;
chartWorksheet.Series["Yearly Total"].DataPoints.All.Label.LabelFormat.Font.Size = 8;
chartWorksheet.Series["Yearly Total"].DataPoints.All.Label.LabelFormat.Font.Italic = true;
}
else
{
// show a label with total number of units sold in a year
foreach (ExcelChartSeries series in chartWorksheet.Series)
{
int lastDataPointIndex = series.DataPoints.Count - 1;
series.DataPoints[lastDataPointIndex].Label.ContainsValue = true;
series.DataPoints[lastDataPointIndex].Label.LabelFormat.Font.Size = 8;
series.DataPoints[lastDataPointIndex].Label.LabelFormat.Font.Italic = true;
}
}
#endregion
// Save the Excel document in the current HTTP response stream
string outFileName = workbookFormat == ExcelWorkbookFormat.Xls_2003 ? "ChartsDemo.xls" : "ChartsDemo.xlsx";
System.Web.HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
// Prepare the HTTP response stream for saving the Excel document
// Clear any data that might have been previously buffered in the
output stream
httpResponse.Clear();
// Set output stream content type for Excel 97-2003 (.xls) or Excel
2007 (.xlsx)
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
httpResponse.ContentType = "Application/x-msexcel";
else
httpResponse.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
// Add the HTTP header to announce the Excel document either as an
attachment or inline
httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", outFileName));
// Save the workbook to the current HTTP response output stream
// and close the workbook after save to release all the allocated
resources
try
{
workbook.Save(httpResponse.OutputStream);
}
catch (Exception ex)
{
// report any error that might occur during save
Session["ErrorMessage"] = ex.Message;
Response.Redirect("ErrorPage.aspx");
}
finally
{
// close the workbook and release the allocated resources
workbook.Close();
#region Dispose the Image object
if (logoImg != null)
logoImg.Dispose();
#endregion
}
// End the response and finish the execution of this page
httpResponse.End();
}
Protected Sub lnkBtnCreateWorkbook_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles lnkBtnCreateWorkbook.Click
' get the Excel workbook format
Dim workbookFormat As ExcelWorkbookFormat = ExcelWorkbookFormat.Xls_2003
If radioXlsxFormat.Checked Then
workbookFormat = ExcelWorkbookFormat.Xlsx_2007
End If
' create the workbook in the desired format with a single worksheet
Dim workbook As ExcelWorkbook = New ExcelWorkbook(workbookFormat)
' set the license key before saving the workbook
workbook.LicenseKey = "RW51ZXZ0ZXVldGt1ZXZ0a3R3a3x8fHw="
' set workbook description properties
workbook.DocumentProperties.Subject = "Adding charts to a worksheet"
workbook.DocumentProperties.Comments = "Adding Excel charts with Winnovative
Excel library for .NET"
' Add a style used for the cells in the worksheet title area
Dim titleStyle As ExcelCellStyle = workbook.Styles.AddStyle("WorksheetTitleStyle")
' center the text in the title area
titleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
titleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
' set the title area borders
titleStyle.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
titleStyle.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
titleStyle.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
titleStyle.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
' set the solid fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.SolidFill
titleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
' set the gradient fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.GradientFill
titleStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
titleStyle.Fill.GradientFillOptions.Color2 = Color.White
End If
' set the title area font
titleStyle.Font.Size = 14
titleStyle.Font.Bold = True
titleStyle.Font.UnderlineType = ExcelCellUnderlineType.Single
' Add a style used for all the subtitles in the workbook
Dim subtitleStyle As ExcelCellStyle = workbook.Styles.AddStyle("WorksheetSubtitlesStyle")
subtitleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
subtitleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
subtitleStyle.Font.Size = 12
subtitleStyle.Font.Bold = True
subtitleStyle.Font.Color = Color.Green
subtitleStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Dot
subtitleStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Dot
subtitleStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Dot
subtitleStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Dot
subtitleStyle.Fill.FillType = ExcelCellFillType.SolidFill
subtitleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
' get the first worksheet in the workbook
Dim worksheet As ExcelWorksheet = workbook.Worksheets.Item(0)
' set the default worksheet name
worksheet.Name = "Charts Demo"
' WORKSHEET PAGE SETUP
' set worksheet paper size and orientation, margins, header and footer
worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4
worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape
worksheet.PageSetup.LeftMargin = 1
worksheet.PageSetup.RightMargin = 1
worksheet.PageSetup.TopMargin = 1
worksheet.PageSetup.BottomMargin = 1
' add header and footer
Dim imagesPath As String = System.IO.Path.Combine(Server.MapPath("~"), "Images")
Dim logoImg As System.Drawing.Image = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "logo.jpg"))
' display a logo image in the left part of the header
worksheet.PageSetup.LeftHeaderFormat = "&G"
worksheet.PageSetup.LeftHeaderPicture = logoImg
' display worksheet name in the right part of the header
worksheet.PageSetup.RightHeaderFormat = "&A"
' add worksheet header and footer
' display the page number in the center part of the footer
worksheet.PageSetup.CenterFooterFormat = "&P"
' display the workbook file name in the left part of the footer
worksheet.PageSetup.LeftFooterFormat = "&F"
' display the current date in the right part of the footer
worksheet.PageSetup.RightFooterFormat = "&D"
' WRITE THE WORKSHEET TOP TITLE
' merge the cells in the range to create the title area
worksheet.Item("A2:G3").Merge()
' gets the merged range containing the top left cell of the range
Dim titleRange As ExcelRange = worksheet.Item("A2").MergeArea
' set the text of title area
worksheet.Item("A2").Text = "Excel Charts
Demo"
' set a row height of 18 points for each row in the range
titleRange.RowHeightInPoints = 18
' set the worksheet top title style
titleRange.Style = titleStyle
' CREATE DATA TABLE FOR THE CHART
Dim chartValuesStyle As ExcelCellStyle = workbook.Styles.AddStyle("ChartValuesStyle")
chartValuesStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
chartValuesStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
chartValuesStyle.Font.Color = Color.Black
chartValuesStyle.Font.Bold = True
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
chartValuesStyle.Fill.FillType = ExcelCellFillType.PatternFill
chartValuesStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_6_25_Percent
chartValuesStyle.Fill.PatternFillOptions.BackColor = Color.White
chartValuesStyle.Fill.PatternFillOptions.PatternColor = Color.Green
Else
chartValuesStyle.Fill.FillType = ExcelCellFillType.GradientFill
chartValuesStyle.Fill.GradientFillOptions.Color1 = Color.LightGreen
chartValuesStyle.Fill.GradientFillOptions.Color2 = Color.White
chartValuesStyle.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown
End If
' set the products tile row text and style
worksheet.Item("C6:G6").Merge()
worksheet.Item("C6").Text = "Analyzed Products"
Dim productsTitle As ExcelRange = worksheet.Item("C6").MergeArea
productsTitle.RowHeightInPoints = 21
productsTitle.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
productsTitle.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
productsTitle.Style.Font.Size = 12
productsTitle.Style.Font.Bold = True
productsTitle.Style.Fill.FillType = ExcelCellFillType.PatternFill
productsTitle.Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_25_Percent
productsTitle.Style.Fill.PatternFillOptions.BackColor = Color.White
productsTitle.Style.Fill.PatternFillOptions.PatternColor = Color.Orange
' set the quarters title column text and style
worksheet.Item("A8:A11").Merge()
worksheet.Item("A8").Text = "Units Sold per
Quarter"
Dim quartersTitle As ExcelRange = worksheet.Item("A8").MergeArea
' set vertical orientation for the text from bottom to top
quartersTitle.Style.Alignment.Orientation = 90
' wrap text inside the merged range
quartersTitle.Style.Alignment.WrapText = True
quartersTitle.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
quartersTitle.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
quartersTitle.Style.Font.Size = 12
quartersTitle.Style.Font.Bold = True
quartersTitle.Style.Fill.FillType = ExcelCellFillType.PatternFill
quartersTitle.Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_25_Percent
quartersTitle.Style.Fill.PatternFillOptions.BackColor = Color.White
quartersTitle.Style.Fill.PatternFillOptions.PatternColor = Color.Green
' set the style for the product names row
Dim productNamesRange As ExcelRange = worksheet.Item("B7:G7")
productNamesRange.RowHeightInPoints = 21
productNamesRange.ColumnWidthInChars = 16
productNamesRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
productNamesRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
productNamesRange.Style.Font.Bold = True
productNamesRange.Style.Fill.FillType = ExcelCellFillType.SolidFill
productNamesRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)
' set the styles for the quarter names range
Dim quarterNamesRange As ExcelRange = worksheet.Item("B8:B11")
quarterNamesRange.RowHeightInPoints = 16.5
quarterNamesRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
quarterNamesRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
quarterNamesRange.Style.Font.Bold = True
quarterNamesRange.Style.Fill.FillType = ExcelCellFillType.SolidFill
quarterNamesRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)
' set the row height for the Yearly Total row
Dim yearlyTotalRange As ExcelRange = worksheet.Item("B12:G12")
yearlyTotalRange.RowHeightInPoints = 21
yearlyTotalRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
yearlyTotalRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
yearlyTotalRange.Style.Font.Color = Color.Blue
yearlyTotalRange.Style.Font.Bold = True
yearlyTotalRange.Style.Fill.FillType = ExcelCellFillType.SolidFill
yearlyTotalRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(204, 255, 255)
' set the series name column
worksheet.Item("B8").Text = "First Quarter"
worksheet.Item("B9").Text = "Second Quarter"
worksheet.Item("B10").Text = "Third Quarter"
worksheet.Item("B11").Text = "Fourth Quarter"
worksheet.Item("B12").Text = "Yearly Total"
' set the categories name row
worksheet.Item("C7").Text = "HTML to PDF"
worksheet.Item("D7").Text = "PDF Merge"
worksheet.Item("E7").Text = "PDF Security"
worksheet.Item("F7").Text = "Web Chart"
worksheet.Item("G7").Text = "Excel Library"
' set the chart value style
Dim chartValuesRange As ExcelRange = worksheet.Item("C8:G11")
chartValuesRange.Style = chartValuesStyle
' set the chart values
worksheet.Item("C8").Value = 1000
worksheet.Item("D8").Value = 500
worksheet.Item("E8").Value = 200
worksheet.Item("F8").Value = 400
worksheet.Item("G8").Value = 800
worksheet.Item("C9").Value = 850
worksheet.Item("D9").Value = 680
worksheet.Item("E9").Value = 350
worksheet.Item("F9").Value = 230
worksheet.Item("G9").Value = 640
worksheet.Item("C10").Value = 950
worksheet.Item("D10").Value = 450
worksheet.Item("E10").Value = 175
worksheet.Item("F10").Value = 350
worksheet.Item("G10").Value = 520
worksheet.Item("C11").Value = 500
worksheet.Item("D11").Value = 700
worksheet.Item("E11").Value = 250
worksheet.Item("F11").Value = 460
worksheet.Item("G11").Value = 320
worksheet.Item("C12").Formula = "=SUM(C8:C11)"
worksheet.Item("D12").Formula = "=SUM(D8:D11)"
worksheet.Item("E12").Formula = "=SUM(E8:E11)"
worksheet.Item("F12").Formula = "=SUM(F8:F11)"
worksheet.Item("G12").Formula = "=SUM(G8:G11)"
' auto fit the width of the quarter names column
worksheet.Item("B7").AutofitColumns()
Dim chartType As ExcelChartType
If (rb2D.Checked) Then
chartType = CType(Charts2DProperty.Item(ddlChartTypes.SelectedIndex), ExcelChartItem).ChartTypeProperty
Else
chartType = CType(Charts3DProperty.Item(ddlChartTypes.SelectedIndex), ExcelChartItem).ChartTypeProperty
End If
' ADD CHART SUBTITLE
' merge the cells in the range to create the subtitle area
worksheet.Item("B14:G14").Merge()
' gets the merged range containing the top left cell of the range
Dim subtitleRange As ExcelRange = worksheet.Item("B14").MergeArea
' set the text of title area
Dim chartDescription As String
If (rb2D.Checked) Then
chartDescription = CType(Charts2DProperty.Item(ddlChartTypes.SelectedIndex), ExcelChartItem).
DescriptionProperty
Else
chartDescription = CType(Charts3DProperty.Item(ddlChartTypes.SelectedIndex), ExcelChartItem).
DescriptionProperty
End If
worksheet.Item("B14").Text = String.Format("{0} Chart - Automatically Generated Series", chartDescription)
' set a row height of 18 points for each row in the range
subtitleRange.RowHeightInPoints = 18
' set the worksheet top title style
subtitleRange.Style = subtitleStyle
'region ADD A CHART WITH AUTOMATICALLY GENERATED SERIES TO THE WORKSHEET
Dim dataSourceRange As ExcelRange = worksheet.Item("B7:G12")
Dim seriesDataByRows As Boolean = rbSeriesByRows.Checked
Dim chart As ExcelChart = worksheet.Charts.AddChart(chartType, dataSourceRange, seriesDataByRows, 2, 16, 8, 33)
' set chart title
chart.Title.Text = "Product Units Sold per Quarter - Auto Generated
Series"
chart.Title.Font.Size = 12
chart.Title.Font.Color = Color.DarkBlue
' set chart area style
If (workbookFormat = ExcelWorkbookFormat.Xls_2003) Then
chart.ChartArea.Interior.FillType = ExcelShapeFillType.SolidFill
chart.ChartArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)
chart.PlotArea.Interior.FillType = ExcelShapeFillType.SolidFill
chart.PlotArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
chart.ChartArea.Interior.FillType = ExcelShapeFillType.GradientFill
chart.ChartArea.Interior.GradientFillOptions.GradientColorsMode = ExcelGradientColors.Preset
chart.ChartArea.Interior.GradientFillOptions.PresetGradientType =
ExcelShapeFillPresetGradientType.Wheat
chart.PlotArea.Interior.FillType = ExcelShapeFillType.TextureFill
chart.PlotArea.Interior.TextureFillOptions.TextureType = ExcelShapeFillTextureType.Stationery
End If
' set value axis title
chart.ValueAxis.Title.Text = "Units sold"
chart.ValueAxis.Title.Font.Size = 10
chart.ValueAxis.Title.Font.Bold = True
' set value axis text style
chart.ValueAxis.Font.Size = 8
chart.ValueAxis.Font.Bold = False
chart.ValueAxis.Font.Italic = True
chart.ValueAxis.ShowVerticalTitleText()
' set category axis title
chart.CategoryAxis.Title.Text = "Analyzed products"
chart.CategoryAxis.Title.Font.Size = 10
chart.CategoryAxis.Title.Font.Bold = True
' set category axis text style
chart.CategoryAxis.Font.Size = 8
chart.CategoryAxis.Font.Bold = False
chart.CategoryAxis.Font.Italic = True
' set chart legend style
chart.Legend.Interior.FillType = ExcelShapeFillType.SolidFill
chart.Legend.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
chart.Legend.Font.Size = 8
chart.Legend.Font.Bold = True
If seriesDataByRows Then
' show a label with total number of units sold in a year
chart.Series.Item("Yearly Total").DataPoints.All.Label.ContainsValue = True
chart.Series.Item("Yearly Total").DataPoints.All.Label.LabelFormat.Font.Size = 8
chart.Series.Item("Yearly Total").DataPoints.All.Label.LabelFormat.Font.Italic = True
Else
' show a label with total number of units sold in a year
For Each series As ExcelChartSeries In chart.Series
Dim lastDataPointIndex As Integer = series.DataPoints.Count - 1
series.DataPoints.Item(lastDataPointIndex).Label.ContainsValue = True
series.DataPoints.Item(lastDataPointIndex).Label.LabelFormat.Font.Size = 8
series.DataPoints.Item(lastDataPointIndex).Label.LabelFormat.Font.Italic = True
Next
End If
' ADD CHART SUBTITLE
' merge the cells in the range to create the subtitle area
worksheet.Item("B34:G34").Merge()
' gets the merged range containing the top left cell of the range
subtitleRange = worksheet.Item("B34").MergeArea
' set the text of title area
worksheet.Item("B34").Text = String.Format("{0} Chart - Custom Series", chartDescription)
' set a row height of 18 points for each row in the range
subtitleRange.RowHeightInPoints = 18
' set the worksheet top title style
subtitleRange.Style = subtitleStyle
' ADD A CHART WITH CUSTOM SERIES TO THE WORKSHEET
Dim customSeriesChart As ExcelChart = worksheet.Charts.AddChart(chartType, 2, 36, 8, 59)
' set chart data table
customSeriesChart.ShowDataTable = True
customSeriesChart.DataTable.ShowLegendKey = True
' set chart title
customSeriesChart.Title.Text = "Product Units Sold per Quarter - Custom
Series"
customSeriesChart.Title.Font.Size = 12
customSeriesChart.Title.Font.Color = Color.DarkBlue
' create the category names range
Dim categoryNamesRange As ExcelRange = worksheet.Item("C7:E7")
' Add chart series
' add first series for the first quarter sales
Dim firstQuarterSeries As ExcelChartSeries = customSeriesChart.Series.AddSeries("First Quarter Sales")
firstQuarterSeries.ChartType = chartType
firstQuarterSeries.CategoryNamesRange = worksheet.Item("C7:E7")
firstQuarterSeries.ValuesRange = worksheet.Item("C8:E8")
firstQuarterSeries.DataPoints.All.Label.ContainsValue = True
firstQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Size = 8
firstQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Italic = True
'add second series for second quarter sales
Dim secondQuarterSeries As ExcelChartSeries = customSeriesChart.Series.AddSeries("Second Quarter Sales")
secondQuarterSeries.ChartType = chartType
secondQuarterSeries.CategoryNamesRange = categoryNamesRange
secondQuarterSeries.ValuesRange = worksheet.Item("C9:E9")
secondQuarterSeries.DataPoints.All.Label.ContainsValue = True
secondQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Size = 8
secondQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Italic = True
' add third series for fourth quarter sales
Dim fourthQuarterSeries As ExcelChartSeries = customSeriesChart.Series.AddSeries("Fourth Quarter Sales")
fourthQuarterSeries.ChartType = chartType
fourthQuarterSeries.CategoryNamesRange = categoryNamesRange
fourthQuarterSeries.ValuesRange = worksheet.Item("C11:E11")
fourthQuarterSeries.DataPoints.All.Label.ContainsValue = True
fourthQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Size = 8
fourthQuarterSeries.DataPoints.All.Label.LabelFormat.Font.Italic = True
' set chart area style
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
customSeriesChart.ChartArea.Interior.FillType = ExcelShapeFillType.SolidFill
customSeriesChart.ChartArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)
customSeriesChart.PlotArea.Interior.FillType = ExcelShapeFillType.SolidFill
customSeriesChart.PlotArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
customSeriesChart.ChartArea.Interior.FillType = ExcelShapeFillType.GradientFill
customSeriesChart.ChartArea.Interior.GradientFillOptions.GradientColorsMode = ExcelGradientColors.Preset
customSeriesChart.ChartArea.Interior.GradientFillOptions.PresetGradientType =
ExcelShapeFillPresetGradientType.Wheat
customSeriesChart.PlotArea.Interior.FillType = ExcelShapeFillType.TextureFill
customSeriesChart.PlotArea.Interior.TextureFillOptions.TextureType = ExcelShapeFillTextureType.Stationery
End If
' set value axis title
customSeriesChart.ValueAxis.Title.Text = "Units sold"
customSeriesChart.ValueAxis.Title.Font.Size = 10
customSeriesChart.ValueAxis.Title.Font.Bold = True
' set value axis text style
customSeriesChart.ValueAxis.Font.Size = 8
customSeriesChart.ValueAxis.Font.Bold = False
customSeriesChart.ValueAxis.Font.Italic = True
customSeriesChart.ValueAxis.ShowVerticalTitleText()
' set category axis text style
customSeriesChart.CategoryAxis.Font.Size = 8
customSeriesChart.CategoryAxis.Font.Bold = False
customSeriesChart.CategoryAxis.Font.Italic = True
' set chart legend style
customSeriesChart.Legend.Interior.FillType = ExcelShapeFillType.SolidFill
customSeriesChart.Legend.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
customSeriesChart.Legend.Font.Size = 8
customSeriesChart.Legend.Font.Bold = True
' ADD A CHART WORKSHEET
Dim chartWorksheet As ExcelChartWorksheet = workbook.ChartWorksheets.AddChartWorksheet(chartType, dataSourceRange,
seriesDataByRows, "Chart Worksheet Demo")
' CHART WORKSHEET PAGE SETUP
' set worksheet paper size and orientation, margins, header and footer
chartWorksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4
chartWorksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape
chartWorksheet.PageSetup.LeftMargin = 0.75
chartWorksheet.PageSetup.RightMargin = 0.25
chartWorksheet.PageSetup.TopMargin = 1
chartWorksheet.PageSetup.BottomMargin = 0.1
'display a logo image in the left part of the header
chartWorksheet.PageSetup.LeftHeaderFormat = "&G"
chartWorksheet.PageSetup.LeftHeaderPicture = logoImg
' display worksheet name in the right part of the header
chartWorksheet.PageSetup.RightHeaderFormat = "&A"
' set chart title
chartWorksheet.Title.Text = "Product Units Sold per Quarter - Auto
Generated Series"
chartWorksheet.Title.Font.Size = 12
chartWorksheet.Title.Font.Color = Color.DarkBlue
' set chart area style
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
chartWorksheet.ChartArea.Interior.FillType = ExcelShapeFillType.SolidFill
chartWorksheet.ChartArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)
chartWorksheet.PlotArea.Interior.FillType = ExcelShapeFillType.SolidFill
chartWorksheet.PlotArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
chartWorksheet.ChartArea.Interior.FillType = ExcelShapeFillType.GradientFill
chartWorksheet.ChartArea.Interior.GradientFillOptions.GradientColorsMode = ExcelGradientColors.Preset
chartWorksheet.ChartArea.Interior.GradientFillOptions.PresetGradientType =
ExcelShapeFillPresetGradientType.Wheat
chartWorksheet.PlotArea.Interior.FillType = ExcelShapeFillType.TextureFill
chartWorksheet.PlotArea.Interior.TextureFillOptions.TextureType = ExcelShapeFillTextureType.Stationery
End If
' set value axis title
chartWorksheet.ValueAxis.Title.Text = "Units sold"
chartWorksheet.ValueAxis.Title.Font.Size = 10
chartWorksheet.ValueAxis.Title.Font.Bold = True
' set value axis text style
chartWorksheet.ValueAxis.Font.Size = 8
chartWorksheet.ValueAxis.Font.Bold = False
chartWorksheet.ValueAxis.Font.Italic = True
chartWorksheet.ValueAxis.ShowVerticalTitleText()
' set category axis title
chartWorksheet.CategoryAxis.Title.Text = "Analyzed products"
chartWorksheet.CategoryAxis.Title.Font.Size = 10
chartWorksheet.CategoryAxis.Title.Font.Bold = True
' set category axis text style
chartWorksheet.CategoryAxis.Font.Size = 8
chartWorksheet.CategoryAxis.Font.Bold = False
chartWorksheet.CategoryAxis.Font.Italic = True
' set chart legend style
chartWorksheet.Legend.Interior.FillType = ExcelShapeFillType.SolidFill
chartWorksheet.Legend.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
chartWorksheet.Legend.Font.Size = 8
chartWorksheet.Legend.Font.Bold = True
If (seriesDataByRows) Then
' show a label with total number of units sold in a year
chartWorksheet.Series.Item("Yearly Total").DataPoints.All.Label.ContainsValue = True
chartWorksheet.Series.Item("Yearly Total").DataPoints.All.Label.LabelFormat.Font.Size = 8
chartWorksheet.Series.Item("Yearly Total").DataPoints.All.Label.LabelFormat.Font.Italic = True
Else
' show a label with total number of units sold in a year
For Each series As ExcelChartSeries In chartWorksheet.Series
Dim lastDataPointIndex As Integer = series.DataPoints.Count - 1
series.DataPoints.Item(lastDataPointIndex).Label.ContainsValue = True
series.DataPoints.Item(lastDataPointIndex).Label.LabelFormat.Font.Size = 8
series.DataPoints.Item(lastDataPointIndex).Label.LabelFormat.Font.Italic = True
Next
End If
' Save the Excel document in the current HTTP response stream
Dim outFileName As String
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
outFileName = "ChartsDemo.xls"
Else
outFileName = "ChartsDemo.xlsx"
End If
Dim httpResponse As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
' Prepare the HTTP response stream for saving the Excel document
' Clear any data that might have been previously buffered in the output
stream
httpResponse.Clear()
' Set output stream content type for Excel 97-2003 (.xls) or Excel
2007 (.xlsx)
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
httpResponse.ContentType = "Application/x-msexcel"
Else
httpResponse.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
End If
' Add the HTTP header to announce the Excel document either as an
attachment or inline
httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", outFileName))
' Save the workbook to the current HTTP response output stream
' and close the workbook after save to release all the allocated resources
Try
workbook.Save(httpResponse.OutputStream)
Catch ex As Exception
' report any error that might occur during save
Session.Item("ErrorMessage") = ex.Message
Response.Redirect("ErrorPage.aspx")
Finally
' close the workbook and release the allocated resources
workbook.Close()
' Dispose the Image object
If Not logoImg Is Nothing Then
logoImg.Dispose()
End If
End Try
' End the response and finish the execution of this page
httpResponse.End()
End Sub
|