前台
http://www.cnblogs.com/TNSSTAR
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
function SetVal() {
var link = "";
var myval = "";
var num = "<%=ListNum %>";
for (m = 0; m < num; m++) {
$("[height='550']:eq(" + m + ") table tr td a").each(function (i, k) {
link += $(this).attr("href") + "|";
myval += $(this).text() + "|";
})
link += "&";
myval += "&";
}
$("#<%=listlink.ClientID %>").val(link);
$("#<%=listval.ClientID %>").val(myval);
}
</script>
<style type="text/css">
#Button2
{
width: 96px;
}
.style_1{ margin:30px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="feed">
<div style=" text-align:center;">
<asp:Button ID="Button1"
runat="server" Text="导出Word" Width="622px" OnClientClick="SetVal()" Font-Size="Larger" CssClass="style_1"
οnclick="Button1_Click" Height="186px"/>
<div id="ListStr" style=" display:none;">
<%=GetListStr() %>
</div>
<asp:HiddenField ID="listlink" runat="server" />
<asp:HiddenField ID="listval" runat="server" />
</div>
</div>
</form>
</body>
</html>
后台:
http://www.cnblogs.com/TNSSTAR
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Configuration;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
public int ListNum
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
listval.Value = "";
listlink.Value = "";
ListNum = ConfigurationManager.AppSettings["LinkUrl"].ToString().Split('|').Length;
}
}
protected string GetListStr()
{
string importHtml = "";
string[] str = ConfigurationManager.AppSettings["LinkUrl"].ToString().Split('|');
for (int i = 0; i < str.Length; i++)
{
if (str[i] != "")
{
importHtml += GetListUrl(str[i]);
}
}
return importHtml;
}
protected string GetListUrl(string url)
{
if (url != "")
{
string strList = GetHttpWebRequest(url);
int startindex = strList.IndexOf("<table");
int endindex = strList.LastIndexOf("/table>");
return strList.Substring(startindex, endindex - startindex + 7);
}
else
{
return "";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int k=0;
string[] listLinks = ConfigurationManager.AppSettings["LinkUrl"].ToString().Split('|');
string[] foldeName = ConfigurationManager.AppSettings["SaveFolder"].ToString().Split('|');
for (int j = 0; j < listLinks.Length; j++)
{
string bigLinks = listlink.Value.Split('&')[j];
string bigTexts = listval.Value.Split('&')[j];
for (int i = 0; i < bigLinks.Split('|').Length; i++)
{
if (bigLinks.Split('|')[i] != "")
{
string wordHtml = "";
k++;
string title = bigTexts.Split('|')[i];
if (listLinks[j].EndsWith(".html") || listLinks[j].EndsWith(".HTML"))
{
string words = listLinks[j].Substring(0, listLinks[j].LastIndexOf("/") + 1);
wordHtml = GetWordHtmlFromLink(words + bigLinks.Split('|')[i].Replace("./", ""));
}
else
{
wordHtml = GetWordHtmlFromLink(listLinks[j] + bigLinks.Split('|')[i].Replace("./", ""));
}
string[] str = { "<", ">", "~", "!", "@", "#", "$", "%", "^", "*",@"\=" };
for (int s = 0; s < str.Length; s++)
{
if (title.Contains(str[s]))
{
title = title.Replace(str[s], "");
}
}
string filePath = ConfigurationManager.AppSettings["SavePath"].ToString() + foldeName[j] + "\\" + title + ".doc";
if (File.Exists(filePath))
{
title += "副本" + k.ToString();
}
WriteLog(k.ToString() + "---" + title);
CreatWord(title, foldeName[j], wordHtml);
}
}
}
Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "message", "<script language='javascript'>alert('生成成功!共生成文件" + k.ToString() + "个文件');</script>");
}
private string GetWordHtmlFromLink(string linkUrl)
{
string str = GetHttpWebRequest(linkUrl);
int startindex = str.LastIndexOf("<!--startprint-->");
int endindex = str.LastIndexOf("<!--endprint-->");
return str.Substring(startindex, endindex - startindex + 18);
}
protected string GetHttpWebRequest(string url)
{
Uri uri = new Uri(url);
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
myReq.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
myReq.Accept = "*/*";
myReq.KeepAlive = true;
myReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
Stream receviceStream = result.GetResponseStream();
StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("GB2312"));
string strHTML = readerOfStream.ReadToEnd();
readerOfStream.Close();
receviceStream.Close();
result.Close();
return strHTML;
}
/// <summary>
/// 把页面生成Word,在最后执行
/// </summary>
/// <param name="docName">Word名字,不能是中文</param>
private void CreatWord(string docName,string folderName,string wordHtml)
{
DirectoryIsExsite(ConfigurationManager.AppSettings["SavePath"].ToString() + folderName + "\\");
string filePath = ConfigurationManager.AppSettings["SavePath"].ToString() + folderName + "\\" + docName + ".doc";
StreamWriter writer = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
writer.WriteLine("<html><head><meta http-equiv='Content-Type' content='application/msword; charset=gb2312'><title>" + docName + "</title>");
writer.WriteLine("<style type='text/css'>table{ padding:0; border:0;}.tr{ background:#B3D9FF}</style>");
writer.WriteLine("</head>");
writer.WriteLine("<body>");
//以下可以从数据库里面读取
string builder = wordHtml;
writer.WriteLine(builder);
writer.WriteLine("</body></html>");
writer.Close();
//Process.Start(filePath);
}
private void DirectoryIsExsite(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
/// <summary>
/// 日志记录函数
/// </summary>
/// <param name="strPath"></param>
/// <param name="strInfo"></param>
private void WriteLog(string strInfo)
{
try
{
string pthLog = @"C:\logClient " + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
FileInfo fleLog = new FileInfo(pthLog);
StreamWriter steWrt = fleLog.AppendText();
steWrt.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]: " + strInfo);
steWrt.Close();
}
catch
{
}
}
}
}