`
isiqi
  • 浏览: 15990187 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

DataGrid 导出 word 和 excel

阅读更多

<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="Web_zwt.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 128px"
runat="server"></asp:datagrid><asp:button id="Button1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 24px" runat="server"
Text="导出word"></asp:button><asp:button id="Button2" style="Z-INDEX: 103; LEFT: 120px; POSITION: absolute; TOP: 24px" runat="server"
Text="导出excel"></asp:button><asp:label id="Label1" style="Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 88px" runat="server">这个在我这里好用.在"引用"填加引用 -> com -> Microsoft Excel 10.0 Object Library.</asp:label></FONT></form>
</body>
</HTML>

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;
using System.IO;
using System.Windows.Forms;


namespace Web_zwt
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label1;
config con=new config();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//在"引用"填加引用 -> com -> Microsoft Excel 10.0 Object Library.

DataSet ds=con.ds("select * from tab1");
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}

/// <summary>
/// excel
/// </summary>
/// <param name="ds">DataSet</param>
/// <param name="typeid">// typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件</param>
/// <param name="FileName">导出的名字</param>
public void CreateExcel(DataSet ds,string typeid,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.Clear();
resp.Buffer= true;
resp.Charset="GB2312"; //设置了类型为中文防止乱码的出现
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
resp.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
resp.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword

string colHeaders= "", ls_item="";
//int i=0;

//定义表对象与行对像,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select("");
// typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
if(typeid=="1")
{
for (int i=0;i<dt.Columns.Count-1;i++)
{
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
colHeaders+=dt.Columns[i].Caption.ToString()+"\t";
}
colHeaders +=dt.Columns[dt.Columns.Count-1].Caption.ToString() +"\n";
//向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//逐行处理数据
foreach(DataRow row in myRow)
{
for (int i=0;i<dt.Columns.Count-1;i++)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
ls_item +=row[i].ToString() + "\t";
}
ls_item += row[dt.Columns.Count-1].ToString() +"\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item="";
}
}
else
{
if(typeid=="2")
{
//从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();


}
/// <summary>
/// word
/// </summary>
/// <param name="ds">DataSet</param>
/// <param name="typeid">// typeid=="1"时导出为word格式文件;typeid=="2"没有</param>
/// <param name="FileName">导出的名字</param>
public void CreateWord(DataSet ds,string typeid,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.Clear();
resp.Buffer= true;
resp.Charset="GB2312"; //设置了类型为中文防止乱码的出现
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
resp.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
resp.ContentType = "application/ms-excel";

DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select("");
string str=string.Empty;
str+="<table>";
if(typeid=="1")
{
str+="<tr>";
for (int i=0;i<dt.Columns.Count;i++)
{
str+="<td>"+dt.Columns[i].Caption.ToString()+"</td>";
}
str+="</tr>";
foreach(DataRow row in myRow)
{
str+="<tr>";
for (int i=0;i<dt.Columns.Count;i++)
{
str+="<td>"+row[i].ToString()+"</td>";
}
str+="</tr>";

}
str+="</table>";
resp.Write(str);
}
else
{
if(typeid=="2")
{
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();

}


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

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

}
#endregion

private void Button2_Click(object sender, System.EventArgs e)
{
DataSet ds=con.ds("select * from tab1");
CreateExcel(ds,"1","aaa.xls");
}

private void Button1_Click(object sender, System.EventArgs e)
{
DataSet ds=con.ds("select * from tab1");
CreateWord(ds,"1","aaa.doc");
}
}
}

分享到:
评论

相关推荐

    WPF中DataGrid导出Excel和Word示例源码

    本程序可以实现以下功能: 1.DataGrid数据的显示、修改与保存 2.DataGrid数据导出Excel 3.DataGrid数据导出Word

    将datagrid数据导出到excel或者word

    将datagrid数据导出到excel或者word

    WPF中DataGrid导入至Excel和Word示例程序

    本程序实现功能: 1.DataGrid数据的显示、修改与保存 2.DataGrid数据导入至Excel 3.DataGrid数据导入至Word

    DataGrid导出Excel

    DataGrid导出Excel,导出word,数据修改

    多功能DataGrid(C#)

    一个关于c#控件DataGrid的文件导入导出,有导出为word,xml,excel等等。大家下载看看吧

    asp.net c# 开发笔记2

    请去下载不要分数的(asp.net c# 开发笔记3) 和前面发的内容一样,只添加的介绍导数据到excel的介绍,纯属个人编写的编程经验总结,谨献给需要...5.12 DataGrid数据导出成word文档 5.13 DataGrid数据导出成Excel文档

    多功能DataGrid .rar

    行头数字,自动列宽,设置文字变红,打印预览,直接打印,导出Excel,导出Word,导出Xml,导出Html等等功能,所有这些功能都封装到一个Dll中,使用方便.里面有源代码和使用例子,希望对大家有用,呵呵. &lt;br&gt;

    多功能DataGrid

    自己写的一个DataGrid,支持...行头数字,自动列宽,设置文字变红,打印预览,直接打印,导出Excel,导出Word,导出Xml,导出Html等等功能,所有这些功能都封装到一个Dll中,使用方便.里面有源代码和使用例子,希望对大家有用,呵呵.

    asp.net c# 开发笔记3

    谢谢大家的支持,以前发的好像修改不了,我发个不要分的,免费分享 以下是笔记的内容: ...5.2 树型控件的应用 5.3 日期控件的应用 5.4 在DataGrid中添加多选框 5.5 在DataGrid中添加...5.13 DataGrid数据导出成Excel文档

    C# gridview 控件实例

    一个DataGrid,支持列样式有...行头数字,自动列宽,设置文字变红,打印预览,直接打印,导出Excel,导出Word,导出Xml,导出Html等等功能,所有这些功能都封装到一个Dll中,使用方便.里面有源代码和使用例子,希望对大家有用,呵呵.

    react-datagrid-getting-started:Flexicious React DataGrid的基本入门项目

    打印,Word / Excel导出,服务器/客户端分页和筛选,可自定义的筛选器控件和摘要页脚,用户设置,首选项持久性 平滑滚动,嵌套层次树/子网格,左/右锁定列,延迟加载/虚拟滚动 指示 开始 npm install npm start ...

    .net技术资料大全(语言规范 源码教程 学习笔记 技术资料 .net代码生成器)

    ASP导出Excel数据的四种方法.txt C#调用存储过程.txt CheckBox控件.txt datagrid排序_选择_分页.txt DataSet对象.txt DotNET WinForm FAQ 16个.txt excel打印.txt EXCEL导出.txt EXCEL中合并单元格.txt ...

    ASP升级.net资料大全(c#入门 语言规范 源码教程 学习笔记 技术资料 面试题 asp与.net代码生成器)

    ASP导出Excel数据的四种方法.txt C#调用存储过程.txt CheckBox控件.txt datagrid排序_选择_分页.txt DataSet对象.txt DotNET WinForm FAQ 16个.txt excel打印.txt EXCEL导出.txt EXCEL中合并单元格.txt ...

    asp.net知识库

    动态调用对象的属性和方法——性能和灵活性兼备的方法 消除由try/catch语句带来的warning 微软的应试题完整版(附答案) 一个时间转换的问题,顺便谈谈搜索技巧 .net中的正则表达式使用高级技巧 (一) C#静态成员和...

    功能强大的免费DataGridView打印控件

    暂未提供该功能的函数接口,只在打印参数设置窗口中增加了一个数据导出的按钮,可以将当前要打印的DataGridView的内容导出成Excel文件。该功能以后会进一步完善。 6、在进行页眉页脚文本设置时,可以用 [页码] 代表...

    delphi 开发经验技巧宝典源码

    0222 把Excel中的数据保存到数据库中 147 0223 怎样弹出ConnectionString设置页 148 0224 利用ADO获取DELETE后所影响的记录数 148 7.3 业务实现数据处理技术 149 0225 随机产生中奖号码 149 0226 使用...

Global site tag (gtag.js) - Google Analytics