.net导入excel和word 类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private DataSet myDS = new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
Data_Load();
}
#region Web 重写 使用gridView控件时必须重写
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
#endregion
private void ExportDataGrid(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Data_Load()
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("select top 20 * from dbo.spt_values", myConnection);
cmd.CommandType = CommandType.Text;
myConnection.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
DataView CreateDataSource()
{
string nowDSN = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection myConnection = new SqlConnection(nowDSN);
SqlCommand cmd = new SqlCommand("select top 20 * from dbo.spt_values", myConnection);
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
myConnection.Close();
Page.DataBind();
return ds.Tables[0].DefaultView;
}
protected void BtnImportWord_Click1(object sender, EventArgs e)
{
ExportDataGrid("application/ms-word", "Word.doc");
}
}
//前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
<table width="120%"><tr><td>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="729px">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td></tr></table>
<p>
<asp:button id="BtnImportWord" runat="server" Text="转到WORD" OnClick="BtnImportWord_Click1"></asp:button></p>
</div>
</form>
</body>
</html>