Click here to Skip to main content
14,641,302 members
Rate this:
Please Sign up or sign in to vote.
Hi All,

I am developing an application in SSIS with C# as Code behind. My requirement is to generate MS Charts and save as Word Document in a shared path daily and send the path link as Email to Users on daily basis. Based on Certain data 'n' number of Word document needs to be generated. In each Document again based on data 'n' number of Charts to be pouplated(Bar and Pie Chart). All this should happen on scheduler basis(In my case daily 9PM).

I tried to convert Chart to Byte array and using FileStream writing it to word document. But in word document it is not displaying as chart. Rather displaying as some binary data. I tried saving Chart as Image in local disk and included the path to img src in HTML tag to write in Word document and it is displaying. But If I either remove or rename the file then 'x' red mark is showing but not the image. Saving image separately and using it in Word Document is not right option since there may be huge number of Charts to be generated daily and user wants to keep all the documents for so many months which leads to huge amount of disk space requirement.

using System.IO;
using System.Web.UI.DataVisualization.Charting;

public void Main()
{
Chart CT = new Chart();
CT.Height = 250;
CT.Width = 500;
// And assigned attributes & Data points to the Chart CT.
byte[] byt;
            
MemoryStream imgStream = new MemoryStream();                
CT.SaveImage("D:\\MyChart.png", ChartImageFormat.Png);
CT.SaveImage(imgStream,ChartImageFormat.Png);
byt = imgStream.ToArray();

StringBuilder objLiteral = new StringBuilder();
objLiteral.Append("<html><head><title></title></head><body>");

// The below code block generating Sample.doc with some scrap data(Binary or encrypted data)
using (FileStream fs = File.Create("D:\\Sample.doc")) 
{
byte[] info = new UTF8Encoding(true).GetBytes(objLiteral.ToString());
                    
fs.Write(info, 0, info.Length);
fs.Write(byt, 0, byt.Length);
info = new UTF8Encoding(true).GetBytes("</body></html>");
fs.Write(info, 0, info.Length);
}
fs.flush();

// The below code block generating Sample1.doc with Chart as Image but when MyChart.png is removed or renamed it is not displaying in document also.
using (FileStream fs1 = File.Create("D:\\Sample1.doc"))
{
byte[] info = new UTF8Encoding(true).GetBytes(objLiteral.ToString());
                    
fs1.Write(info, 0, info.Length);
info = new UTF8Encoding(true).GetBytes("<img Src = 'D:\\MyChart.png'></img></body></html>");
fs1.Write(info, 0, info.Length);
}
fs1.flush();
}


Can anybody point me what I am doing wrong. I am not using any dll for Word parsing and is there anything I have to include. If there is any, pls help me how to modify the code after referencing the dll.
Posted
Updated 13-Aug-12 16:59pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Add one button for export
Add on page Export.aspx.

in button click
CT.SaveImage("D:\\MyChart.png", ChartImageFormat.Png);
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Export" + DateTime.Now.ToString(), "<script>window.open('Export.aspx?Image=" +MyChart  + "',target='_blank','toolbar=no,location=no,height=800px,width=900px,resizable=no,scrollbars=no');</script>");



In Export page design
<asp:Panel ID="pnlExport" runat="server" >

 <asp:image id="imgTest" runat="server" xmlns:asp="#unknown" />


In Export page code behind
protected void Page_Load(object sender, EventArgs e)
  {
if (Request.QueryString["Image"] != null
{
imgTest.ImageUrl = Request.QueryString["Image"].Tostring() + ".png";
 Response.Clear(); 
       Response.AddHeader("content-disposition", "attachment; filename=MyChart.doc");
Response.Charset = "";  
Response.ContentType = "application/vnd.ms-word";  
StringWriter sw= new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);   
pnlExport.RenderControl(hw);
  Response.Output.Write(sw.ToString());

 FileInfo fiDelete = new FileInfo(Request.QueryString["Image"].Tostring() + ".png");
        if (fiDelete.Exists)
        {
            fiDelete.Delete();
        }
    Response.Flush();
    Response.End();  

}
}
   
Comments
allav_arunlml 10-Aug-12 1:21am
   
Hi Pradiprenushe, Thanks for your immmediate response. Since this application is been developed in SSIS, I couldn't able to add .aspx page to the package. Also this package have to be scheduled daily 9 PM. Kind of service in Server. So there will not be any interaction with user and so nobody to click button. Is there any way to trigger Word to Parse the receiving Byte Array. My problem here is Word is not displaying proper image when it receives appropriate Image as Byte Array. I think we can achieve something in that way. Please Correct me if I am wrong. Thanks for your time.
pradiprenushe 10-Aug-12 3:29am
   
Ok then after save image create panel object, crate image object, add that image in the panel and render that panel. Code will be same but you have just collect all code at single page, And panel & image will have to be create dynamically.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100