怎么把DataGrid数据导出到word

fhuibo 2008-05-26 08:48:46
怎么把DataGrid数据导出到word
Datagrid数据如下:
部门 计划 是否完成
人力资源部 方法 完成
人力资源部 试试 未完成
财务部 中国 完成
财务部 历史 完成
财务部 事实上 未完成

我要导出到WORD里如下:


...全文
216 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeweimp3 2011-03-17
  • 打赏
  • 举报
回复
wrqwrqwrqwrq
live_7sky 2008-06-10
  • 打赏
  • 举报
回复
后台代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace managemis.ExcelTemplate
{

public class excel_col_acc : System.Web.UI.Page
{
public string retstr="";
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Bind();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
private void Bind()
{
if(Session["Table"]!=null)
{
DataView dv=(DataView)Session["Table"];
for(int i=0;i<dv.Count;i++)
{
retstr+=" <tr height=\"37\" style='HEIGHT:27.75pt;mso-height-source:userset'>"+
"<td height=\"37\" class=\"xl24\" width=\"145\">"+dv[i][0].ToString()+
"</td>"+
"<td class=\"xl24\" width=\"144\">"+dv[i][1].ToString()+
"</td>"+
"<td class=\"xl24\" width=\"144\">"+dv[i][2].ToString()+
"</td>"+
"<td class=\"xl24\" width=\"144\">"+dv[i][3].ToString()+
"</td>"+
"<td class=\"xl24\" width=\"144\">"+dv[i][4].ToString()+
"</td>"+
"</tr>";
}
}

}
}
}
live_7sky 2008-06-10
  • 打赏
  • 举报
回复
HTML 代码:

<%@ Page language="c#" Codebehind="excel_col_acc.aspx.cs" AutoEventWireup="false" Inherits="managemis.ExcelTemplate.excel_col_acc" %>
<HTML xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel">
<HEAD>
<%Response.AddHeader("Content-Disposition", "attachment; filename="+HttpUtility.UrlEncode("设备档案.xls"));%>
<%Response.ContentType="application/vnd.ms-excel";%>
<%Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta content="Excel.Sheet" name="ProgId">
<meta content="Microsoft Excel 11" name="Generator">
<LINK href="Book2.files/filelist.xml" rel="File-List">
<LINK href="Book2.files/editdata.mso" rel="Edit-Time-Data">
<LINK href="Book2.files/oledata.mso" rel="OLE-Object-Data"> <!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>aotuMan</o:Author>
<o:LastAuthor>aotuMan</o:LastAuthor>
<o:Created>2008-04-09T06:02:53Z</o:Created>
<o:LastSaved>2008-04-09T06:06:19Z</o:LastSaved>
<o:Company>Y.B</o:Company>
<o:Version>11.8122</o:Version>
</o:DocumentProperties>
</xml><![endif]-->
<style> <!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";}
@page {margin:1.0in .75in 1.0in .75in; mso-header-margin:.5in; mso-footer-margin:.5in;}
tr {mso-height-source:auto; mso-ruby-visibility:none;}
col {mso-width-source:auto; mso-ruby-visibility:none;}
br {mso-data-placement:same-cell;}
.style0 {mso-number-format:General; text-align:general; vertical-align:middle; white-space:nowrap; mso-rotate:0; mso-background-source:auto; mso-pattern:auto; color:windowtext; font-size:12.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:宋体; mso-generic-font-family:auto; mso-font-charset:134; border:none; mso-protection:locked visible; mso-style-name:常规; mso-style-id:0;}
td {mso-style-parent:style0; padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:windowtext; font-size:12.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:宋体; mso-generic-font-family:auto; mso-font-charset:134; mso-number-format:General; text-align:general; vertical-align:middle; border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible; white-space:nowrap; mso-rotate:0;}
.xl24 {mso-style-parent:style0; vertical-align:top; border:.5pt solid windowtext; white-space:normal;}
.xl25 {mso-style-parent:style0; font-weight:700; text-align:center; border-top:.5pt solid windowtext; border-right:.5pt solid windowtext; border-bottom:.5pt solid windowtext; border-left:.5pt solid windowtext; white-space:normal;}
.xl26 {mso-style-parent:style0; font-weight:700; text-align:center; border-top:.5pt solid windowtext; border-right:.5pt solid windowtext; border-bottom:.5pt solid windowtext; border-left:none; white-space:normal;}
.xl27 {mso-style-parent:style0; font-weight:700; text-align:center; border-top:.5pt solid windowtext; border-right:.5pt solid windowtext; border-bottom:.5pt solid windowtext; border-left:none; white-space:normal;}
.xl28 {mso-style-parent:style0; border:.5pt solid windowtext; white-space:normal;}
ruby {ruby-align:left;}
rt {color:windowtext; font-size:9.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:宋体; mso-generic-font-family:auto; mso-font-charset:134; mso-char-type:none; display:none;}
--></style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Sheet1</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>285</x:DefaultRowHeight>
<x:Selected/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
<x:RangeSelection>$A$1:$G$1</x:RangeSelection>
</x:Pane>
</x:Panes>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
<x:ExcelWorksheet>
<x:Name>Sheet2</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>285</x:DefaultRowHeight>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
<x:ExcelWorksheet>
<x:Name>Sheet3</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>285</x:DefaultRowHeight>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>12375</x:WindowHeight>
<x:WindowWidth>16020</x:WindowWidth>
<x:WindowTopX>0</x:WindowTopX>
<x:WindowTopY>45</x:WindowTopY>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><![endif]-->
</HEAD>
<body vLink="purple" link="blue">
<![if supportMisalignedColumns]>
<tr height="0" style='display:none'>
<td width="145" style='width:109pt'></td>
<td width="144" style='width:108pt'></td>
<td width="144" style='width:108pt'></td>
<td width="144" style='width:108pt'></td>
<td width="144" style='width:108pt'></td>
<td width="144" style='width:108pt'></td>
<td width="144" style='width:108pt'></td>
<td width="145" style='width:109pt'></td>
</tr>
<![endif]>
<table style="TABLE-LAYOUT: fixed; BORDER-COLLAPSE: collapse" height="74" cellSpacing="0"
cellPadding="0" width="784" border="0" x:str>
<COLGROUP>
<col style="WIDTH: 109pt; mso-width-source: userset; mso-width-alt: 4640" width="145">
<col style="WIDTH: 108pt; mso-width-source: userset; mso-width-alt: 4608" span="2" width="144">
<col style="WIDTH: 109pt; mso-width-source: userset; mso-width-alt: 4640" width="145">
<tr style="HEIGHT: 27.75pt; mso-height-source: userset" height="37">
<td class="xl25" width="1011" colSpan="5" height="37">设备档案
</td>
</tr>
<tr style="HEIGHT: 27.75pt; mso-height-source: userset" height="37">
<td class="xl28" width="144" height="36">设备名称</td>
<td class="xl28" width="144" height="36">规格型号</td>
<td class="xl28" width="144" height="36">性能特点</td>
<td class="xl28" width="144" height="36">设备类别</td>
<td class="xl28" width="144" height="36">所属系统</td>
</tr>
<%=retstr%>
</table>
</body>
</HTML>


live_7sky 2008-06-10
  • 打赏
  • 举报
回复
1, 新建一个 word, 画好格式之后,保存为 HTML,然后 复制 HTML 的源代码 到你的 网页里面去, 在需要绑定的地方 使用

<%=变量%> ,后台给前台的 HTML 传值 。

下面是一个 使用EXCEL 的例子:

w1122333 2008-06-05
  • 打赏
  • 举报
回复
顶下
czk598478 2008-06-05
  • 打赏
  • 举报
回复
帮你顶
live_7sky 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuhaiwuya 的回复:]
你可以这样做 用word建一个模板 这个模板给你的程序(datagrid)有相同的表格 然后你一行行的写入到你的模板就可以 我之前实现过这样
[/Quote]

哈哈,我也这样 实现过 ,

WORD 就是 HTML
deepinnet 2008-05-27
  • 打赏
  • 举报
回复
UP
fhuibo 2008-05-26
  • 打赏
  • 举报
回复
各位大哥,我要的WORD是要带表格的!!!
优途科技 2008-05-26
  • 打赏
  • 举报
回复

private void ExportToWord_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
dgDevice.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

试一下吧,也许可以实现你的功能。
yagebu1983 2008-05-26
  • 打赏
  • 举报
回复
网上例子很多的!!
去搜搜!!
lfywy 2008-05-26
  • 打赏
  • 举报
回复

private void ExportToWord_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
dgDevice.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
net0003 2008-05-26
  • 打赏
  • 举报
回复
ding

czhy 2008-05-26
  • 打赏
  • 举报
回复
/// <summary>
/// 导出Excel文件
/// </summary>
/// <param name="ControlId">要导出控件的Id</param>
/// <param name="Response">Page页的Response对象</param>
/// <param name="FileName">要导出的文件名</param>
public static void ResponseExcel(Control ControlId, HttpResponse Response, string FileName)
{
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ".xls");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append("<html><head>");
sb.Append("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
sb.Append("</head><body>");
ControlId.RenderControl(hw);
sb.Append("</body></html>");
Response.Write(sb.ToString());
Response.End();
}
/// <summary>
/// 导出Word文件
/// </summary>
/// <param name="ControlId">要导出控件的Id</param>
/// <param name="Response">Page页的Response对象</param>
/// <param name="FileName">要导出的文件名</param>
public static void ResponseWord(Control ControlId, HttpResponse Response, string FileName)
{
Response.Clear();
Response.ContentType = "application/ms-word";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(FileName+".doc", Encoding.UTF8));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append("<html><head>");
sb.Append("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
sb.Append("</head><body>");
ControlId.RenderControl(hw);
sb.Append("</body></html>");
Response.Write(sb.ToString());
Response.End();
}
wuhaiwuya 2008-05-26
  • 打赏
  • 举报
回复
你可以这样做 用word建一个模板 这个模板给你的程序(datagrid)有相同的表格 然后你一行行的写入到你的模板就可以 我之前实现过这样

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧