After I bind a DataTable to an Excel template sheet, how can I build an Excel scatter chart on top of the DataTable placeholders?

StackOverflow https://stackoverflow.com/questions/16241148

  •  13-04-2022
  •  | 
  •  

Question

Here my problem in short. I'm using a demo version of OfficeWriter to figure out whether it is suitable for our porpuses or not. In particular i've prepared an Excel template composed of 8 placeholders, representing the X-Y columns of 4 DataTables, and an Excel Scatter-Chart in which i add 4 series with reference to the respecive columns pairs. enter image description here In my code i copy this template several times according items in a list and naming those copied sheets after my items' names (i use wb.CopySheet(...) method for this). The main issue here is that when i call for "xlt.process()" method, it correctly updates the Y-range datamarkers with the right sheet name, but it doesn't seem to update the X-range datamarker. enter image description here Can someone give me a hand?

Was it helpful?

Solution

This appears to be a bug. When OfficeWriter is reading in the values for the chart, the property .ScatterValues should have been set to the correct range value (e.g. =Sheet2!$R$2:$R$2), and therefore causing CopySheet to fail. I have filed a bug for development to fix this bug.

To work around this issue, you can set the .ScatterValues (see http://wiki.softartisans.com/display/EW8/Series.ScatterValues) property in the code above:

string templatePath = "input.xlsx";

/* Open the template workbook */
ExcelApplication xla = new ExcelApplication();
var wb = xla.Open(templatePath);

/* Select the template worksheet to copy */
var origWS = wb.Worksheets["Sheet1"];

/* Make a copy of the worksheet with the given name */
var wsName = "Sheet2";
wb.Worksheets.CopySheet(origWS, 1, wsName);

/* For the new worksheet, update the ScatterValues to point to this sheet */
var newWS = wb.Worksheets[wsName];
newWS.Charts[0].SeriesCollection[0].ScatterValues = "=" + wsName + "!$B$27";

/* Create an instance of ExcelTemplate */
ExcelTemplate xlt = new ExcelTemplate();

/* Open the workbook from the ExcelApplication object above */
xlt.Open(xla, wb);

Disclaimer: I work for SoftArtisans, makers of OfficeWriter.

OTHER TIPS

I love OfficeWriter (Disclaimer: I am a developer on OfficeWriter) and I was able to setup a quick sample to accomplished this:

Using the following code:

using System; using System.Data; using SoftArtisans.OfficeWriter.ExcelWriter;

namespace ExcelTemplateScatterChart {
    class Program
    {
        static void Main(string[] args)
        {
            /* Create an instance of ExcelTemplate */
            ExcelTemplate xlt = new ExcelTemplate();

            /* Open the template workbook */
            string templatePath = "input.xlsx";
            xlt.Open(templatePath);

            /* Query the database for report data */
            DataTable dt1 = GetTable1();
            DataTable dt2 = GetTable2();

            /* Pass the DataTable to ExcelTemplate */
            xlt.BindData(dt1, "DataTable1", xlt.CreateDataBindingProperties());
            xlt.BindData(dt2, "DataTable2", xlt.CreateDataBindingProperties());

            /* Call the process() method to populate the 
            * template with the data source values
            */
            xlt.Process();

            /* Save the report by streaming it
             * to the client's browser */
            xlt.Save("output.xlsx");
        }

        /// <summary>
        /// This example method generates a DataTable.
        /// </summary>
        static DataTable GetTable1()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("x", typeof(int));
            table.Columns.Add("y", typeof(int));

            //
            // Here we add three DataRows.
            //
            table.Rows.Add(1, 10);
            table.Rows.Add(2, 20);
            table.Rows.Add(3, 30);
            return table;
        }

        /// <summary>
        /// This example method generates a DataTable.
        /// </summary>
        static DataTable GetTable2()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("x", typeof(int));
            table.Columns.Add("y", typeof(int));

            //
            // Here we add three DataRows.
            //
            table.Rows.Add(1, 30);
            table.Rows.Add(2, 20);
            table.Rows.Add(3, 10);
            return table;
        }
    } }

I setup my template like:

enter image description here

And configured the chart to point to the datamarkers/place holders like

enter image description here

The resulting output is what follows after I run my code:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top