2

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?

  • Can you post the code you are using? – Sam Plus Plus Apr 26 '13 at 19:17
  • You can also checkout the OfficeWriter tutorial for generating charts using Excel Template it also has sample code. This can be found at http://wiki.softartisans.com/display/EW8/Using+Charts+with+ExcelTemplate – Sam Plus Plus Apr 26 '13 at 19:25

2 Answers2

2

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.

Chad Evans
  • 36
  • 3
  • Thanks a lot, i actually had the feeling it was a bug, but i appreciate your answer and the work-around suggested. Do you think you will get it fixed soon? – user2324645 Apr 30 '13 at 07:26
  • Revised my old comment: This issue has been sent to Development to deal with; when they schedule the fix has not been determined. If you would like, you can contact SoftArtisans (http://www.softartisans.com/contact-softartisans) directly, and the team can help you work through this issue directly. Just mention this post and it will get routed correctly. – Chad Evans Jul 17 '13 at 17:02
1

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

Sam Plus Plus
  • 4,381
  • 2
  • 21
  • 43
  • I apologize if i wasn't clear enough on my question, i didn't have time to make some explainatory screenshots. I've made my question more detailed now. – user2324645 Apr 29 '13 at 08:56