htmlTable To jpeg
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HtmlTableToJpg._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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table width="500px" border="1" id="DataTable">
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HtmlTableToJpg._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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table width="500px" border="1" id="DataTable">
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
<tr>
<td>
张三
</td>
<td>
李四
</td>
<td>
王五
</td>
</tr>
</table>
</form>
</body>
</html>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Net;
using System.Drawing;
using System.Collections.Generic;
using System.Collections;
using System.Drawing.Drawing2D;
namespace HtmlTableToJpg
{
public partial class TableToJpg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HtmlTableToJpg("");
}
protected void HtmlTableToJpg(string url)
{
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
Byte[] pageData = MyWebClient.DownloadData("http://localhost:4585/Default.aspx");//从指定网站下载数
string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句
int start = pageHtml.IndexOf("<table");
int end = pageHtml.IndexOf("</table>");
string tabledata = pageHtml.Substring(start, end - start+8);
tabledata=tabledata.Replace("</tr>", "|");
tabledata = tabledata.Replace("</td>", ",");
tabledata = tabledata.Replace("\n", string.Empty);
tabledata = tabledata.Replace("\r", string.Empty);
tabledata = ClearHtml(tabledata);
tabledata = tabledata.Trim();
tabledata=System.Text.RegularExpressions.Regex.Replace(tabledata, @"\s", "");
tabledata = tabledata.Substring(0, tabledata.Length - 2);
DrawTable(tabledata);
}
public void DrawTable(string data)
{
System.Drawing.Bitmap img = new System.Drawing.Bitmap(600, 480);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
System.Drawing.Font font = new System.Drawing.Font("宋体", 16); //字体与大小
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.White);
System.Drawing.Brush brush = new SolidBrush(Color.White);
graphics.DrawLine(pen, new PointF(10, 10), new PointF(10, 470));
graphics.DrawLine(pen, new PointF(10, 10), new PointF(590, 10));
graphics.DrawLine(pen, new PointF(10, 470), new PointF(590, 470));
graphics.DrawLine(pen, new PointF(590, 470), new PointF(590, 10));
string[] rows=data.Split( new char[] {'|'});
int r=rows.Length;
int c=0;
float dis=460/6;
float dis2 = 580 / 3;
string coldata = string.Empty;
for (int i = 0; i < rows.Length; i++)
{
string[] arraycoldata = rows[i].Split(new char[]{','});
c = arraycoldata.Length;
graphics.DrawLine(pen, new PointF(590,470 - dis * i), new PointF(10,470 - dis * i));
for (int j = 0; j < arraycoldata.Length; j++)
{
graphics.DrawLine(pen, new PointF(590-dis2*j, 470), new PointF(590-dis2*j, 10));
graphics.DrawString(arraycoldata[j], font, brush, new PointF(10+dis2 * j, 15+dis * i));
}
}
//将图片保存到内存流中
System.IO.MemoryStream stream = new System.IO.MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
//在页面上输出
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(stream.ToArray());
stream.Close();
stream.Dispose();
graphics.Dispose();
img.Dispose();
Response.End();
}
public string ReplaceHtml(string Pattern, string HtmlStr, string replacement)
{
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(Pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (rx.IsMatch(HtmlStr))
return rx.Replace(HtmlStr, replacement);
return HtmlStr;
}
public string ClearHtml(string HtmlStr)
{
string tmpStr = HtmlStr;
tmpStr = ReplaceHtml("&#[^>]*;", tmpStr, "");
tmpStr = ReplaceHtml("</?marquee[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?object[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?param[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?embed[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?table[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml(" ", tmpStr, "");
tmpStr = ReplaceHtml("</?tr[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?th[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?p[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?a[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?img[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?tbody[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?li[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?span[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?div[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?th[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?td[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?script[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("(javascript|jscript|vbscript|vbs):", tmpStr, "");
tmpStr = ReplaceHtml("on(mouse|exit|error|click|key)", tmpStr, "");
tmpStr = ReplaceHtml("<\\?xml[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("<\\/?[a-z]+:[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?font[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?b[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?u[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?i[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?strong[^>]*>", tmpStr, "");
return tmpStr;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Net;
using System.Drawing;
using System.Collections.Generic;
using System.Collections;
using System.Drawing.Drawing2D;
namespace HtmlTableToJpg
{
public partial class TableToJpg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HtmlTableToJpg("");
}
protected void HtmlTableToJpg(string url)
{
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
Byte[] pageData = MyWebClient.DownloadData("http://localhost:4585/Default.aspx");//从指定网站下载数
string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句
int start = pageHtml.IndexOf("<table");
int end = pageHtml.IndexOf("</table>");
string tabledata = pageHtml.Substring(start, end - start+8);
tabledata=tabledata.Replace("</tr>", "|");
tabledata = tabledata.Replace("</td>", ",");
tabledata = tabledata.Replace("\n", string.Empty);
tabledata = tabledata.Replace("\r", string.Empty);
tabledata = ClearHtml(tabledata);
tabledata = tabledata.Trim();
tabledata=System.Text.RegularExpressions.Regex.Replace(tabledata, @"\s", "");
tabledata = tabledata.Substring(0, tabledata.Length - 2);
DrawTable(tabledata);
}
public void DrawTable(string data)
{
System.Drawing.Bitmap img = new System.Drawing.Bitmap(600, 480);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
System.Drawing.Font font = new System.Drawing.Font("宋体", 16); //字体与大小
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.White);
System.Drawing.Brush brush = new SolidBrush(Color.White);
graphics.DrawLine(pen, new PointF(10, 10), new PointF(10, 470));
graphics.DrawLine(pen, new PointF(10, 10), new PointF(590, 10));
graphics.DrawLine(pen, new PointF(10, 470), new PointF(590, 470));
graphics.DrawLine(pen, new PointF(590, 470), new PointF(590, 10));
string[] rows=data.Split( new char[] {'|'});
int r=rows.Length;
int c=0;
float dis=460/6;
float dis2 = 580 / 3;
string coldata = string.Empty;
for (int i = 0; i < rows.Length; i++)
{
string[] arraycoldata = rows[i].Split(new char[]{','});
c = arraycoldata.Length;
graphics.DrawLine(pen, new PointF(590,470 - dis * i), new PointF(10,470 - dis * i));
for (int j = 0; j < arraycoldata.Length; j++)
{
graphics.DrawLine(pen, new PointF(590-dis2*j, 470), new PointF(590-dis2*j, 10));
graphics.DrawString(arraycoldata[j], font, brush, new PointF(10+dis2 * j, 15+dis * i));
}
}
//将图片保存到内存流中
System.IO.MemoryStream stream = new System.IO.MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
//在页面上输出
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(stream.ToArray());
stream.Close();
stream.Dispose();
graphics.Dispose();
img.Dispose();
Response.End();
}
public string ReplaceHtml(string Pattern, string HtmlStr, string replacement)
{
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(Pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (rx.IsMatch(HtmlStr))
return rx.Replace(HtmlStr, replacement);
return HtmlStr;
}
public string ClearHtml(string HtmlStr)
{
string tmpStr = HtmlStr;
tmpStr = ReplaceHtml("&#[^>]*;", tmpStr, "");
tmpStr = ReplaceHtml("</?marquee[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?object[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?param[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?embed[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?table[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml(" ", tmpStr, "");
tmpStr = ReplaceHtml("</?tr[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?th[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?p[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?a[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?img[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?tbody[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?li[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?span[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?div[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?th[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?td[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?script[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("(javascript|jscript|vbscript|vbs):", tmpStr, "");
tmpStr = ReplaceHtml("on(mouse|exit|error|click|key)", tmpStr, "");
tmpStr = ReplaceHtml("<\\?xml[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("<\\/?[a-z]+:[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?font[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?b[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?u[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?i[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?strong[^>]*>", tmpStr, "");
return tmpStr;
}
}
}