Excel导出
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
public partial class WExcel : System.Web.UI.Page
{
private readonly string DSExcel = ConfigurationSettings.AppSettings["DSExcel"].ToString();
private readonly string Excel = ConfigurationSettings.AppSettings["Excel"].ToString();
public static string sqlcon = ConfigurationManager.ConnectionStrings["SQLConnetionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();
}
}
private void binddata()
{
this.GridView1.DataSource = GetDataSet();
this.GridView1.DataBind();
}
private DataSet GetDataSet()
{
string sql = "select * from Files";
SqlConnection cn = new SqlConnection(sqlcon);
cn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
cn.Close();
da.Fill(ds);
return ds;
}
//导入到Excel
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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();
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "文件上传.xls");
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
//读取Excel数据
private DataSet CreateDataSource()
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~\\App_Data\\excel.xls") + ";Extended Properties=Excel 8.0 ;";
OleDbConnection olecon = new OleDbConnection(strCon);
// string ExcelDbPath = DSExcel + Server.MapPath(Excel) + ";";
// OleDbConnection olecon = new OleDbConnection(ExcelDbPath);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", olecon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView2.DataSource = CreateDataSource();
GridView2.DataBind();
}
//生成Excel表格
/**//* private void CreateExcelTable()
{
DataSet ds = GetDataSet();
//创建Excel对象
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int collIndex = 0;
//添加Excel对象的WorkBooks
excel.Application.Workbooks.Add(true);
DataTable table = ds.Tables[0];
//将所得到的表的列名赋给Excel单元格
foreach (DataColumn col in table.Columns)
{//添加列名
collIndex++;
excel.Cells[1, collIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{//添加数据
rowIndex++;
collIndex = 0;
foreach (DataColumn col in table.Columns)
{
collIndex++;
excel.Cells[rowIndex, collIndex] = row[col.ColumnName].ToString();
}
}
//不可见即后台处理
excel.Visible = false;
excel.DisplayAlerts = false;
//保存刚才创建的Excel表格
excel.Save(MapPath("App_Data/excel.xls"));
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
//释放使用的Excel对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();//调用垃圾收集内存
}
*/
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
public partial class WExcel : System.Web.UI.Page
{
private readonly string DSExcel = ConfigurationSettings.AppSettings["DSExcel"].ToString();
private readonly string Excel = ConfigurationSettings.AppSettings["Excel"].ToString();
public static string sqlcon = ConfigurationManager.ConnectionStrings["SQLConnetionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();
}
}
private void binddata()
{
this.GridView1.DataSource = GetDataSet();
this.GridView1.DataBind();
}
private DataSet GetDataSet()
{
string sql = "select * from Files";
SqlConnection cn = new SqlConnection(sqlcon);
cn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
cn.Close();
da.Fill(ds);
return ds;
}
//导入到Excel
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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();
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "文件上传.xls");
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
//读取Excel数据
private DataSet CreateDataSource()
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~\\App_Data\\excel.xls") + ";Extended Properties=Excel 8.0 ;";
OleDbConnection olecon = new OleDbConnection(strCon);
// string ExcelDbPath = DSExcel + Server.MapPath(Excel) + ";";
// OleDbConnection olecon = new OleDbConnection(ExcelDbPath);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", olecon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView2.DataSource = CreateDataSource();
GridView2.DataBind();
}
//生成Excel表格
/**//* private void CreateExcelTable()
{
DataSet ds = GetDataSet();
//创建Excel对象
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int collIndex = 0;
//添加Excel对象的WorkBooks
excel.Application.Workbooks.Add(true);
DataTable table = ds.Tables[0];
//将所得到的表的列名赋给Excel单元格
foreach (DataColumn col in table.Columns)
{//添加列名
collIndex++;
excel.Cells[1, collIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{//添加数据
rowIndex++;
collIndex = 0;
foreach (DataColumn col in table.Columns)
{
collIndex++;
excel.Cells[rowIndex, collIndex] = row[col.ColumnName].ToString();
}
}
//不可见即后台处理
excel.Visible = false;
excel.DisplayAlerts = false;
//保存刚才创建的Excel表格
excel.Save(MapPath("App_Data/excel.xls"));
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
//释放使用的Excel对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();//调用垃圾收集内存
}
*/
}