ASP.NET使用OWC組件繪圖、列印
1.添加引用:using Microsoft.Office.Interop.Owc11;
如果引用失敗,需從Microsoft下載並安裝Owc.exe.
2.繪圖:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="OWCdrawing.aspx.cs" Inherits="OWCdrawing" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>繪圖</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div style="text-align: left">
- <table style="width: 600px">
- <tr>
- <td colspan="3" style="height: 20px">
- <strong>ASP.NET2.0中使用OWC給件繪圖</strong>
- </td>
- </tr>
- <tr>
- <td colspan="3" rowspan="2" style="height: 21px">
- <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
- </td>
- </tr>
- <tr>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Microsoft.Office.Interop.Owc11;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- public partial class OWCdrawing : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- //连接数据库并获取特定字符串
- string strSeriesName = "图例1";
- string ConnectString = ConfigurationManager.ConnectionStrings["db"].ConnectionString ;
- string Sql = "SELECT month,Allcount FROM Chart";
- SqlConnection myConn = new SqlConnection(ConnectString);
- myConn.Open();
- SqlDataAdapter Da = new SqlDataAdapter(Sql, myConn);
- DataSet ds = new DataSet();
- Da.Fill(ds);
- //存放月
- string[] MonNum = new string[12];
- //存放数据
- string[] MonCount = new string[12];
- //为数组赋值
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- MonNum[i] = ds.Tables[0].Rows[i][0].ToString();
- MonCount[i] = ds.Tables[0].Rows[i][1].ToString();
- }
- //为x轴指定特定字符串,以便显示数据
- string strXdata = String.Empty;
- foreach (string strData in MonNum)
- {
- strXdata += strData + "/t";
- }
- string strYdata = String.Empty;
- //为y轴指定特定的字符串,以便与x轴相对应
- foreach (string strValue in MonCount)
- {
- strYdata += strValue + "/t";
- }
- //创建ChartSpace对象来放置图表
- ChartSpace laySpace = new ChartSpaceClass();
- //在ChartSpace对象中添加图表
- ChChart InsertChart = laySpace.Charts.Add(0);
- //指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
- //InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
- //InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
- //InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
- InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图
- //指定图表是否需要图例标注
- InsertChart.HasLegend = false;
- InsertChart.HasTitle = true;//为图表添加标题
- InsertChart.Title.Caption = "XX公司2008年每月利潤";//标题名称
- //为x,y轴添加图示说明
- InsertChart.Axes[0].HasTitle = true;
- InsertChart.Axes[0].Title.Caption = "";//月份
- InsertChart.Axes[1].HasTitle = true;
- //InsertChart.Axes[1].Scaling.SplitMinimum = 200;
- InsertChart.Axes[1].Title.Caption = "金額(萬元)";
- //添加一个series系列
- InsertChart.SeriesCollection.Add(0);
- //给定series系列的名字
- InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
- //给定分类
- InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
- //给定值
- InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
- //输出文件.
- string urlName = Session.SessionID.ToString().ToUpper() + ".gif";
- string strAbsolutePath = (Server.MapPath(".")) + @"/Export/" + urlName;
- laySpace.ExportPicture(strAbsolutePath, "GIF", 600, 450);
- //创建GIF文件的相对路径.
- string strRelativePath = "./Export/" + urlName;
- //把图片添加到placeholder中,并在页面上显示
- string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
- this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
- }
- }
3.列印
- <%@ Page Language="C#" validateRequest=false EnableEventValidation=false AutoEventWireup="true" CodeFile="OWCPrint.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>OWC Print</title>
- <script type="text/javascript">
- function btnSave_Click()
- {
- var spreadsheet = document.getElementById("sp");
- document.getElementById('<%= hdnXML.ClientID %>').value=spreadsheet.XMLData;
- return false;
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>OWC Print Functionality </h1>
- <hr width=1px />
- <object classid="clsid:0002E559-0000-0000-C000-000000000046" id="sp" width="100%"
- height="80%"><param name="XMLData" value="<?xml version='1.0'?>
- <?mso-application progid='Excel.Sheet'?>
- <Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
- xmlns:o='urn:schemas-microsoft-com:office:office'
- xmlns:x='urn:schemas-microsoft-com:office:excel'
- xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
- xmlns:html='http://www.w3.org/TR/REC-html40'>
- <DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>
- <Author></Author>
- <LastAuthor></LastAuthor>
- <Created></Created>
- <Company></Company>
- <Version></Version>
- </DocumentProperties>
- <ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'>
- <WindowHeight>8955</WindowHeight>
- <WindowWidth>15195</WindowWidth>
- <WindowTopX>120</WindowTopX>
- <WindowTopY>60</WindowTopY>
- <ProtectStructure>False</ProtectStructure>
- <ProtectWindows>False</ProtectWindows>
- </ExcelWorkbook>
- <Styles>
- <Style ss:ID='Default'
- ss:Name='Normal'>
- <Alignment ss:Vertical='Bottom'/>
- <Borders/>
- <Font/>
- <Interior/>
- <NumberFormat/>
- <Protection/>
- </Style>
- </Styles>
- <Worksheet ss:Name='Sheet1'>
- <WorksheetOptions xmlns='urn:schemas-microsoft-com:office:excel'>
- <Selected/>
- <ProtectObjects>False</ProtectObjects>
- <ProtectScenarios>False</ProtectScenarios>
- </WorksheetOptions>
- </Worksheet>
- </Workbook>"/></object>
- <asp:Button ID="btnPrint" OnClick="btnPrint_Click" runat="server" Text="Print" />
- <asp:Button ID="btnsave" OnClientClick="javascript: return btnSave_Click();" runat="server" Text="Save" />
- </div>
- <input type=hidden runat=server id="hdnXML" />
- </form>
- </body>
- </html>
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using Microsoft.Office.Interop.Owc11;
- using System.IO;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- //No Code
- }
- /// <summary>
- /// Print Functionality using OWC.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnPrint_Click(object sender, EventArgs e)
- {
- try
- {
- //Export and save the OWC in HTM format.
- SpreadsheetClass spreadSheetClass = new SpreadsheetClass();
- spreadSheetClass.XMLData = hdnXML.Value;
- spreadSheetClass.Export(Server.MapPath("OWCXML.htm"), Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone, SheetExportFormat.ssExportHTML);
- //Inject Print function into HTM file
- StreamWriter streamWriter;
- streamWriter = System.IO.File.AppendText(Server.MapPath("OWCXML.htm"));
- streamWriter.WriteLine("<html><body οnlοad='window.print()'><table><tr><td></td></tr></table></body></html>");
- streamWriter.Flush();
- streamWriter.Close();
- //Call Javascript to popup 'OWCXML.htm'
- ClientScript.RegisterClientScriptBlock(this.GetType(), "Key1", "<script>window.showModalDialog('OWCXML.htm','popup'); </script>");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }