MS图表-使用C#在SSIS中无法正确显示导出到Word [英] MS Chart - Export to Word not displaying properly in SSIS using C#

查看:163
本文介绍了MS图表-使用C#在SSIS中无法正确显示导出到Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在SSIS中开发一个应用程序,后面带有C#.我的要求是每天生成MS图表并另存为共享路径中的Word文档,并每天将路径链接作为电子邮件发送给用户.根据某些数据,需要生成Word文档的"n"个数字.在每个文档中,再次基于数据"n"个要填充的图表数量(条形图和饼图).所有这些都应该在调度程序的基础上进行(在我的情况下,每天晚上9点).

我试图将Chart转换为Byte数组,并使用FileStream将其写入Word文档.但是在Word文档中,它没有显示为图表.而是显示为一些二进制数据.我尝试将Chart作为Image保存在本地磁盘中,并在HTML标签中包含img src的路径以写入Word文档中,并且它正在显示.但是,如果我删除或重命名文件,则显示"x"红色标记,但不显示图像.单独保存图像并在Word文档中使用它不是正确的选择,因为每天可能会生成大量的图表,并且用户希望将所有文档保存如此长时间,这会导致大量的磁盘空间需求.

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();
}



谁能指出我做错了什么.我没有使用任何dll进行Word解析,因此我必须包含任何内容.如果有的话,请帮助我参考dll后如何修改代码.



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.

推荐答案

添加一个用于导出的按钮
在页面上添加Export.aspx.

在按钮中单击
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();  

}
}


这篇关于MS图表-使用C#在SSIS中无法正确显示导出到Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆