ASP.NET使用OWC組件繪圖、列印

ASP.NET使用OWC組件繪圖、列印

 

 

1.添加引用:using Microsoft.Office.Interop.Owc11;

如果引用失敗,需從Microsoft下載並安裝Owc.exe.

 

2.繪圖:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="OWCdrawing.aspx.cs" Inherits="OWCdrawing" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5.     <title>繪圖</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div style="text-align: left">
  10.         <table style="width: 600px">
  11.             <tr>
  12.                 <td colspan="3" style="height: 20px">
  13.                     <strong>ASP.NET2.0中使用OWC給件繪圖</strong>
  14.                 </td>
  15.             </tr>
  16.             <tr>
  17.                 <td colspan="3" rowspan="2" style="height: 21px">
  18.                     <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
  19.                 </td>
  20.             </tr>
  21.             <tr>
  22.             </tr>
  23.         </table>
  24.     </div>
  25.     </form>
  26. </body>
  27. </html>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using Microsoft.Office.Interop.Owc11;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. public partial class OWCdrawing : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         //连接数据库并获取特定字符串
  15.         string strSeriesName = "图例1";
  16.         string ConnectString = ConfigurationManager.ConnectionStrings["db"].ConnectionString ;
  17.         string Sql = "SELECT month,Allcount FROM Chart";
  18.         SqlConnection myConn = new SqlConnection(ConnectString);
  19.         myConn.Open();
  20.         SqlDataAdapter Da = new SqlDataAdapter(Sql, myConn);
  21.         DataSet ds = new DataSet();
  22.         Da.Fill(ds);
  23.         //存放月
  24.         string[] MonNum = new string[12];
  25.         //存放数据
  26.         string[] MonCount = new string[12];
  27.         //为数组赋值
  28.         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  29.         {
  30.             MonNum[i] = ds.Tables[0].Rows[i][0].ToString();
  31.             MonCount[i] = ds.Tables[0].Rows[i][1].ToString();
  32.         }
  33.         //为x轴指定特定字符串,以便显示数据
  34.         string strXdata = String.Empty;
  35.         foreach (string strData in MonNum)
  36.         {
  37.             strXdata += strData + "/t";
  38.         }
  39.         string strYdata = String.Empty;
  40.         //为y轴指定特定的字符串,以便与x轴相对应
  41.         foreach (string strValue in MonCount)
  42.         {
  43.             strYdata += strValue + "/t";
  44.         }
  45.         //创建ChartSpace对象来放置图表
  46.         ChartSpace laySpace = new ChartSpaceClass();
  47.         //在ChartSpace对象中添加图表
  48.         ChChart InsertChart = laySpace.Charts.Add(0);
  49.         //指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
  50.         //InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
  51.         //InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
  52.         //InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
  53.         InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图
  54.         //指定图表是否需要图例标注
  55.         InsertChart.HasLegend = false;
  56.         InsertChart.HasTitle = true;//为图表添加标题
  57.         InsertChart.Title.Caption = "XX公司2008年每月利潤";//标题名称
  58.         //为x,y轴添加图示说明
  59.         InsertChart.Axes[0].HasTitle = true;
  60.         InsertChart.Axes[0].Title.Caption = "";//月份
  61.         InsertChart.Axes[1].HasTitle = true;
  62.         //InsertChart.Axes[1].Scaling.SplitMinimum = 200;
  63.         InsertChart.Axes[1].Title.Caption = "金額(萬元)";
  64.         //添加一个series系列
  65.         InsertChart.SeriesCollection.Add(0);
  66.         //给定series系列的名字
  67.         InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
  68.         //给定分类
  69.         InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
  70.         //给定值
  71.         InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
  72.         //输出文件.
  73.         string urlName = Session.SessionID.ToString().ToUpper() + ".gif";
  74.         string strAbsolutePath = (Server.MapPath(".")) + @"/Export/" + urlName;
  75.         laySpace.ExportPicture(strAbsolutePath, "GIF", 600, 450);
  76.         //创建GIF文件的相对路径.
  77.         string strRelativePath = "./Export/" + urlName;
  78.         //把图片添加到placeholder中,并在页面上显示
  79.         string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
  80.         this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
  81.     }
  82. }

3.列印

  1. <%@ Page Language="C#" validateRequest=false EnableEventValidation=false AutoEventWireup="true"   CodeFile="OWCPrint.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>OWC Print</title>
  6.     <script type="text/javascript">
  7.      function btnSave_Click()
  8.      {
  9.         var spreadsheet = document.getElementById("sp");        
  10.         document.getElementById('<%= hdnXML.ClientID %>').value=spreadsheet.XMLData; 
  11.         return false;
  12.      } 
  13.     </script>
  14. </head>
  15. <body>
  16.     <form id="form1" runat="server">
  17.     <div>
  18.     <h1>OWC Print Functionality </h1>
  19.     <hr width=1px />
  20.     <object classid="clsid:0002E559-0000-0000-C000-000000000046" id="sp" width="100%"
  21.                     height="80%"><param name="XMLData" value="<?xml version='1.0'?>
  22.         <?mso-application progid='Excel.Sheet'?>
  23.         <Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
  24.          xmlns:o='urn:schemas-microsoft-com:office:office'
  25.          xmlns:x='urn:schemas-microsoft-com:office:excel'
  26.          xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
  27.          xmlns:html='http://www.w3.org/TR/REC-html40'>
  28.          <DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>
  29.           <Author></Author>
  30.           <LastAuthor></LastAuthor>
  31.           <Created></Created>
  32.           <Company></Company>
  33.           <Version></Version>
  34.          </DocumentProperties>
  35.          <ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'>
  36.           <WindowHeight>8955</WindowHeight>
  37.           <WindowWidth>15195</WindowWidth>
  38.           <WindowTopX>120</WindowTopX>
  39.           <WindowTopY>60</WindowTopY>
  40.           <ProtectStructure>False</ProtectStructure>
  41.           <ProtectWindows>False</ProtectWindows>
  42.          </ExcelWorkbook>
  43.          <Styles>
  44.           <Style ss:ID='Default' 
  45.           ss:Name='Normal'>
  46.            <Alignment ss:Vertical='Bottom'/>
  47.            <Borders/>
  48.            <Font/>
  49.            <Interior/>
  50.            <NumberFormat/>
  51.            <Protection/>
  52.           </Style>
  53.          </Styles>
  54.          <Worksheet ss:Name='Sheet1'>
  55.           <WorksheetOptions xmlns='urn:schemas-microsoft-com:office:excel'>
  56.            <Selected/>
  57.            <ProtectObjects>False</ProtectObjects>
  58.            <ProtectScenarios>False</ProtectScenarios>
  59.           </WorksheetOptions>
  60.          </Worksheet>
  61.           
  62.         </Workbook>"/></object>
  63.           <asp:Button ID="btnPrint" OnClick="btnPrint_Click" runat="server" Text="Print" /> 
  64.           <asp:Button ID="btnsave" OnClientClick="javascript: return btnSave_Click();" runat="server" Text="Save" /> 
  65.     </div>
  66.     <input type=hidden runat=server id="hdnXML" />
  67.     </form>
  68.     
  69. </body>
  70. </html>
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using Microsoft.Office.Interop.Owc11;
  11. using System.IO;
  12. public partial class _Default : System.Web.UI.Page 
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         //No Code
  17.     }
  18.     /// <summary>
  19.     /// Print Functionality using OWC.
  20.     /// </summary>
  21.     /// <param name="sender"></param>
  22.     /// <param name="e"></param>
  23.     protected void btnPrint_Click(object sender, EventArgs e)
  24.     {
  25.         try
  26.         {
  27.             //Export and save the OWC in HTM format.
  28.             SpreadsheetClass spreadSheetClass = new SpreadsheetClass();
  29.             spreadSheetClass.XMLData = hdnXML.Value;
  30.             spreadSheetClass.Export(Server.MapPath("OWCXML.htm"), Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone, SheetExportFormat.ssExportHTML);
  31.             //Inject Print function into HTM file 
  32.             StreamWriter streamWriter;
  33.             streamWriter = System.IO.File.AppendText(Server.MapPath("OWCXML.htm"));
  34.             streamWriter.WriteLine("<html><body οnlοad='window.print()'><table><tr><td></td></tr></table></body></html>");
  35.             streamWriter.Flush();
  36.             streamWriter.Close();
  37.             //Call Javascript to popup 'OWCXML.htm'
  38.             ClientScript.RegisterClientScriptBlock(this.GetType(), "Key1""<script>window.showModalDialog('OWCXML.htm','popup'); </script>");
  39.         }
  40.         catch (Exception ex)
  41.         {
  42.             throw ex;
  43.         }
  44.     }
  45. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值