How to create CSV file from Active Report 6

A common requirement is to have applications share data with other programs. Although there are interfaces available to work with, for example, CSV files. A much simpler way to have your application share data is by reading and writing Comma-Separated Values (CSV) files. CSV files can easily be read and written by many programs, including Microsoft Excel.

Active Report 6 developers sometime needs to export report data to CSV file. Now we describe how to do it. We have Order Details Report. Supose we have some text fields into Details section and need to export the whole report data into a new CSV file named “OrderDetails.csv”. We use Active Report 6 with C# language support.

The “Orders.xml” file structure is:

<?xml version=”1.0″ encoding=”utf-8″?>
<Orders>
   <Order ID=”10248″>
 <Product>
     <Description>Queso Cabrales</Description>
     <Discount>0%</Discount>
     <UnitPrice>$14.00</UnitPrice>
     <Quantity>12</Quantity>
     <ExtendedPrice>$168.00</ExtendedPrice>
        </Product>
 <Product>
     <Description>Singaporean Hokkien Fried Mee</Description>
     <Discount>0%</Discount>
     <UnitPrice>$9.80</UnitPrice>
     <Quantity>10</Quantity>
     <ExtendedPrice>$98.00</ExtendedPrice>
        </Product>
 <Product>
     <Description>Mozzaralla of Giovanni</Description>
     <Discount>0%</Discount>
     <UnitPrice>$34.80</UnitPrice>
     <Quantity>5</Quantity>
     <ExtendedPrice>$174.00</ExtendedPrice>
        </Product>
   </Order>
   <Order ID=”10249″>
 <Product>
     <Description>Manjimup Dried Apples</Description>
     <Discount>0%</Discount>
     <UnitPrice>$42.40</UnitPrice>
     <Quantity>40</Quantity>
     <ExtendedPrice>$1696.00</ExtendedPrice>
        </Product>
 <Product>
     <Description>Tofu</Description>
     <Discount>0%</Discount>
     <UnitPrice>$18.60</UnitPrice>
     <Quantity>9</Quantity>
     <ExtendedPrice>$167.40</ExtendedPrice>
        </Product>
   </Order>
   <Order ID=”10250″>
 <Product>
     <Description>Manjimup Dried Apples</Description>
     <Discount>15%</Discount>
     <UnitPrice>$42.40</UnitPrice>
     <Quantity>35</Quantity>
     <ExtendedPrice>$1261.40</ExtendedPrice>
        </Product>
 <Product>
     <Description>Jack’sNewEnglandClean Chowder</Description>
     <Discount>0%</Discount>
     <UnitPrice>$7.70</UnitPrice>
     <Quantity>10</Quantity>
     <ExtendedPrice>$77.00</ExtendedPrice>
        </Product>
 <Product>
     <Description>Louisiana Fiery Hot Perrer Sauce</Description>
     <Discount>15%</Discount>
     <UnitPrice>$16.80</UnitPrice>
     <Quantity>15</Quantity>
     <ExtendedPrice>$214.20</ExtendedPrice>
        </Product>
   </Order>
   <Order ID=”10251″>
 <Product>
     <Description>Louisiana Fiery Hot Perrer Sauce</Description>
     <Discount>0%</Discount>
     <UnitPrice>$16.80</UnitPrice>
     <Quantity>20</Quantity>
     <ExtendedPrice>$336.00</ExtendedPrice>
        </Product>
 <Product>
     <Description>Gustafs Knackebrod</Description>
     <Discount>5%</Discount>
     <UnitPrice>$16.80</UnitPrice>
     <Quantity>6</Quantity>
     <ExtendedPrice>$95.70</ExtendedPrice>
        </Product>
 <Product>
     <Description>Ravidi Angelo</Description>
     <Discount>5%</Discount>
     <UnitPrice>$15.60</UnitPrice>
     <Quantity>15</Quantity>
     <ExtendedPrice>$222.30</ExtendedPrice>
        </Product>
   </Order>
   <Order ID=”10252″>
 <Product>
     <Description>Getost</Description>
     <Discount>5%</Discount>
     <UnitPrice>$2.00</UnitPrice>
     <Quantity>25</Quantity>
     <ExtendedPrice>$47.50</ExtendedPrice>
        </Product>
   </Order>
</Orders>

The report looks like:

OrderDetailsReport

To create csv file we have to implements the given steps:

1. Add the following line into class scope of the report:


using System.IO;

2. We add code into detail section of the report:

public void Detail_Format()
{
 //csv file OrderDetails
 string columnText = “”;
 string columnHeaderText = “”;
 //Header Text
 columnHeaderText = “Order ID” + ‘,’ + “Product” + ‘,’ + “Discount” + ‘,’ + “Unit Price” + ‘,’ + “Quantity” + ‘,’ + “Extended Price”;
 
 columnText = fldOrderId.Text + ‘,’ + fldProductDescription.Text + ‘,’ + fldDiscount.Text + ‘,’ + fldUnitPrice.Text + ‘,’ + fldQuantity.Text + ‘,’ + fldExtendedPrice.Text;


 string filename = “OrderDetails.csv”;
 StreamWriter sw = new StreamWriter(filename, true);
 //add Header to CSV
 if(bAddHeader == false)
 {
  sw.WriteLine(columnHeaderText);
  bAddHeader = true;
 }
 else
 {
  //Add order details
  sw.WriteLine(columnText);
 }
 sw.Close();
}

Now run the report or click on the “Preview” tab to view reports output. The “OrderDetails.csv” file is created into the application directory. The output file is looks like below:

 You can download source files from here

Advertisements

One Response

  1. i am trying to fetch the datas from the database and trying to implement the datas in excel format according to the datas present in the database so please help me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: