1、Excel数据导入到数据库中:
在页面放置1个按钮控件(Button1)和1个文件上传控件(FileUpload1) protected void Page_Load(object sender, EventArgs e) { GridView1.Visible = false; } protected void Button1_Click(object sender, EventArgs e) {try
{ if (FileUpload1.PostedFile.FileName != "") { string filepath = FileUpload1.PostedFile.FileName; string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1); string sheetname = "Sheet1"; string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "$]", conn); DataSet ds = new DataSet(); oada.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); GridView1.Visible = true; } } catch (Exception error) { this.Label1.Text = "上传发生错误!原因:" + error.ToString(); } }注意把Excel文件属性中加入ASPNET用户权限,不然不能导入
3、将数据库中的数据导入Excel中,在这里我讲叙自己实践中的两个方法:
方法一:从最基本的写文件入手,将从数据库中读出来的数据写入到Excel中;
//该方法实现将数据导入到Excel文件中,其中的DataTable dt就是你需要将数据写入到Excel中的数据;
public void ExportExcel( DataTable dt , StreamWriter w )...{ try ...{ for( int i = 0 ; i < dt.Columns.Count ; i ++ ) ...{ w.Write ( dt.Columns[i] ); w.Write( ' ' ); } w.Write ( " " );object[] values = new object [dt.Columns.Count];
foreach ( DataRow dr in dt.Rows ) ...{ values = dr.ItemArray ; for ( int i = 0 ; i < dt.Columns.Count ; i++ ) ...{ w.Write ( values[i] ); w.Write ( ' ' ); } w.Write ( " " ); } w.Flush(); w.Close(); } catch ...{ w.Close(); }}StreamWriter w就是你自己创建的一个流,创建该数据流时,你需要自己指定需要将数据写入到那个文件,即指定文件路径,要实现下载的话,就可以简单的用Response.Redirect
( "指定需要被下载的文件路径" );
方法二:该方法实现的是将数据从DataGrid中导入到Excel中:
//filename为Excel的名字,ToExcelGrid就是数据源,在此为DataGrid数据源;
private void ExportExcelFromDataGrid( string filename , System.Web.UI.WebControls.DataGrid ToExcelGrid )...{ Response.Clear(); Response.Buffer= true; Response.Charset="utf-8"; Response.AppendHeader("Content-Disposition","p_w_upload;filename="+Server.UrlEncode ( filename ) ); Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文 Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true); System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); ToExcelGrid.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End();Excel导出:
页面上放一个GridView控件,ID为GridView1,添加个导出按钮ID为Button2protected void Button2_Click(object sender, EventArgs e)
{ Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "p_w_upload;filename=FileName.xls"); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();}
页面后台必须重写这两个方法,不然出现错误!
public override void VerifyRenderingInServerForm( Control control ) {}
protected override void Render(HtmlTextWriter writer) { if (Page != null) { Page.VerifyRenderingInServerForm(this); } base.Render(writer); }将GridView打印出来,添加个打印按钮,ID为Button4
protected void Button4_Click(object sender, EventArgs e) { Response.Write("<script> window.print()</script>"); }