asp.Netはexeclにデータをエクスポートしてローカルに保存するOfficeコンポーネントを呼び出す必要はありません
4756 ワード
public static string ExportTable(DataSet ds)
{
StringBuilder sb = new StringBuilder();
int count = 0;
foreach (DataTable tb in ds.Tables)
{
sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"10\" rules=\"all\" border=\"1\">");
// ,
sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
foreach (DataColumn column in tb.Columns)
{
if (column.ColumnName == " " || column.ColumnName == " ")
sb.AppendLine("<td style=\"width:500px;\">" + column.ColumnName + "</td>");
else
sb.AppendLine("<td>" + column.ColumnName + "</td>");
}
sb.AppendLine("</tr>");
//
foreach (DataRow row in tb.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in tb.Columns)
{
sb.Append("<td>" + row[column].ToString() + "</td>");
}
sb.AppendLine("</tr>");
count++;
}
sb.AppendLine("</table>");
}
return sb.ToString();
}
//
protected void btnSaveWord_Click(object sender, EventArgs e)
{
System.Web.UI.Page page = this.Page;
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "gbk";
string name = tname.Text + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + name);
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//
page.Response.ContentType = "application/ms-excel";// excel 。
page.EnableViewState = false;
//
string[] id = Request.Form["ck"].ToString().Split(',');
good = new DAL.GoodMessage();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add(" ", typeof(string));
dt.Columns.Add(" ", typeof(string));
dt.Columns.Add(" ", typeof(string));
foreach (string idstr in id)
{
foreach (DataRow dr in good.GetDataById(Convert.ToInt32(idstr)).Tables[0].Rows)
{
DataRow dr1 = dt.NewRow();
dr1[" "] = dr["title"];
dr1[" "] = dr["url"];
dr1[" "] = dr["froms"];
dt.Rows.Add(dr1);
}
}
ds.Tables.Add(dt);
page.Response.Write(ExportTable(ds));
page.Response.End();
Response.Redirect("DownloadWord.aspx?id=" + name);
}
//
protected void Page_Load(object sender, EventArgs e)
{
string FullFileName = Request.QueryString["id"];
// , C IIS , , URI 。 web , 。
FileInfo DownloadFile = new FileInfo(HostingEnvironment.ApplicationPhysicalPath + FullFileName);
// , 。
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}