当前位置:首页 > 实用技巧 >

asp读取excel数据并导出到新表(asp导出到excel的简单方法)

来源:原点资讯(www.yd166.com)时间:2023-10-27 09:16:25作者:YD166手机阅读>>

前台拖一个Gridview,在拖一个导出excel的按钮,给这个按钮添加事件
后台代码:

using BLL; using Model; namespace Web { public partial class ExcelOperate : System.Web.UI.Page { private StudentBLL bll = new StudentBLL(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); } } //绑定数据 private void Bind() { GridView1.DataSource = bll.GetAllStu(null); GridView1.DataBind(); } #regIOn 导出到Excel //导出excel protected void btnExcelout_Click(object sender, EventArgs e) { string style = @"<style> .text { mso-number-format:\@; } </script> "; //设置格式 Response.ClearContent(); Response.ContentEncoding = Encoding.GetEncoding("gbk"); Response.AddHeader("content-disposition", "attachment;filename=ouput.xls"); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView1.RenderControl(htw); Response.Write(style);//注意 Response.Write(sw.ToString()); Response.End(); } //注意:必须覆盖此方法 public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } //解决数字字符串显示不完全 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { // e.Row.Cells[3].Attributes.Add("class", "text");//在数据绑定中设置格式 //哪一列需要显示文本的,加上下面这句话即可 e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); } #endregion } }

页面效果:

asp读取excel数据并导出到新表,asp导出到excel的简单方法(1)

asp读取excel数据并导出到新表,asp导出到excel的简单方法(2)

导入Excel并保存到数据库:
前台需要拖一个FileUpload上传控件,一个导入excel按钮,给其添加事件:

//导入excel数据 protected void btnExcelIn_Click(object sender, EventArgs e) { string filepath = string.Empty; string getErrormg = string.Empty; DataTable dt=new DataTable(); if (!fuFile.HasFile) { Response.Write("<script>alert('请选择你要导入的Excel文件');</script>"); return; } //获取文件的后缀名 string fileExt = System.IO.Path.GetExtension(fuFile.FileName); if (fileExt != ".xls") { Response.Write("<script>alert('文件类型错误!');</script>"); return; } //获取绝对路径 filepath = fuFile.PostedFile.FileName; string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";Data Source=" filepath; OleDbConnection excelCon = new OleDbConnection(conn); //Excel文件里面工作表名 默认为Sheet1,后面需要加上$符号[工作表名称$]切记,不然会报错 OleDbDataAdapter odda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", excelCon); try { odda.Fill(dt); } catch (Exception ex) { Response.Write(ex.Message); Response.Write("<script>alert('" ex.Message "!')</script>"); } finally { excelCon.Close(); excelCon.Dispose(); } //将数据写到数据库里面 try { for (int i = 0; i < dt.Rows.Count; i ) { Studnet stu=new Studnet(); stu.C_id = Convert.ToInt32(dt.Rows[i]["c_id"]); stu.No = dt.Rows[i]["no"].ToString(); stu.Name = dt.Rows[i]["name"].ToString(); stu.Gender = dt.Rows[i]["gender"].ToString() == "男" ? true : false; stu.Age = Convert.ToInt32(dt.Rows[i]["age"].ToString()); bll.InsertStu(stu); } } catch (Exception ex) { getErrormg = ex.Message; Response.Write(ex.Message); } if (getErrormg == "") { Response.Write("<script>alert('导入Excel文件成功!')</script>"); Bind(); } else { Response.Write("<script>alert('导入Excel文件失败!')</script>"); } }

Excel和导入后的页面效果:

asp读取excel数据并导出到新表,asp导出到excel的简单方法(3)

asp读取excel数据并导出到新表,asp导出到excel的简单方法(4)

数据库在导入excel数据之前和时候的效果:

asp读取excel数据并导出到新表,asp导出到excel的简单方法(5)

这里要注意几个地方,一般导出excel的时候,数字文本会把前面的0都省略掉了,这里需要注意:红色代码片段,导入的时候,也有个***红红红色***标记码块要注意
以下是前台完整代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExcelOperate.aspx.cs" Inherits="Web.ExcelOperate" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView> <br /> <asp:Button ID="btnExcelout" runat="server" OnClick="btnExcelout_Click" Text="导出到Excel" /> <br /> <asp:FileUpload ID="fuFile" runat="server" /> <asp:Button ID="btnExcelIn" runat="server" OnClick="btnExcelIn_Click" Text="导入Excel数据" /> </div> </form> </body> </html>

以下是后台完整代码:

using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using BLL; using Model; namespace Web { public partial class ExcelOperate : System.Web.UI.Page { private StudentBLL bll = new StudentBLL(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); } } //绑定数据 private void Bind() { GridView1.DataSource = bll.GetAllStu(null); GridView1.DataBind(); } #region 导出到excel //导出excel protected void btnExcelout_Click(object sender, EventArgs e) { string style = @"<style> .text { mso-number-format:\@; } </script> "; //设置格式 Response.ClearContent(); Response.ContentEncoding = Encoding.GetEncoding("gbk"); Response.AddHeader("content-disposition", "attachment;filename=ouput.xls"); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView1.RenderControl(htw); Response.Write(style);//注意 Response.Write(sw.ToString()); Response.End(); } //注意:必须覆盖此方法 ***红红红色*** public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } //解决数字字符串显示不完全 ***红红红色******红红红色******红红红色*** protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //在数据绑定中设置格式 //哪一列需要显示文本的,加上下面这句话即可 e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); } #endregion //导入excel数据 protected void btnExcelIn_Click(object sender, EventArgs e) { string filepath = string.Empty; string getErrormg = string.Empty; DataTable dt=new DataTable(); if (!fuFile.HasFile) { Response.Write("<script>alert('请选择你要导入的Excel文件');</script>"); return; } //获取文件的后缀名 string fileExt = System.IO.Path.GetExtension(fuFile.FileName); if (fileExt != ".xls") { Response.Write("<script>alert('文件类型错误!');</script>"); return; } //获取绝对路径 filepath = fuFile.PostedFile.FileName; string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";Data Source=" filepath; OleDbConnection excelCon = new OleDbConnection(conn); //默认为Sheet1,后面需要加上$符号表面为什么名称,[表名称$],切记不然会报错 OleDbDataAdapter odda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", excelCon); try { odda.Fill(dt); } catch (Exception ex) { Response.Write(ex.Message); Response.Write("<script>alert('" ex.Message "!')</script>"); } finally { excelCon.Close(); excelCon.Dispose(); } //将数据写到数据库里面 try { for (int i = 0; i < dt.Rows.Count; i ) { Studnet stu=new Studnet(); stu.C_id = Convert.ToInt32(dt.Rows[i]["c_id"]); stu.No = dt.Rows[i]["no"].ToString(); stu.Name = dt.Rows[i]["name"].ToString(); stu.Gender = dt.Rows[i]["gender"].ToString() == "男" ? true : false; stu.Age = Convert.ToInt32(dt.Rows[i]["age"].ToString()); bll.InsertStu(stu); } } catch (Exception ex) { getErrormg = ex.Message; Response.Write(ex.Message); } if (getErrormg == "") { Response.Write("<script>alert('导入Excel文件成功!')</script>"); Bind(); } else { Response.Write("<script>alert('导入Excel文件失败!')</script>"); } } } }

栏目热文

asp中怎么将excel表导入到数据库(asp查询出的数据导出excel)

asp中怎么将excel表导入到数据库(asp查询出的数据导出excel)

Access数据库是微软公司出品的Office办公软件套件,我用的版本号是2003。Access是数据库,库的内部可以...

2023-10-27 09:03:31查看全文 >>

健康证在哪里预约(健康证哪里可以预约办理)

健康证在哪里预约(健康证哪里可以预约办理)

土左旗大小事// 尽在“融媒土左”即日起,呼和浩特市卫健委为杜绝假证,实行办理健康证必须经过“青城医疗”公众号预约,具体...

2023-10-27 09:17:05查看全文 >>

健康证哪里办最快拿到(哪里办健康证很快下来)

健康证哪里办最快拿到(哪里办健康证很快下来)

乌鲁木齐的伙伴们需要办理健康证的看过来啦~南湾街社区卫生服务中心可以办理健康证在这里一站式服务两天就能拿证 来源...

2023-10-27 08:51:47查看全文 >>

健康证好办吗(个人健康证好办吗)

健康证好办吗(个人健康证好办吗)

近日,云南西双版纳州纪委州监委网站发布《关于景洪市健康证“办证难”问题追责问责情况通报》,主要针对部分群众反映健康证“办...

2023-10-27 08:57:55查看全文 >>

健康证几天可以拿到(四种人不能办健康证)

健康证几天可以拿到(四种人不能办健康证)

齐鲁网4月29日讯前段时间办理健康证需要大半夜去排队的新闻闹得沸沸扬扬。而济南市高新区办理健康证却简便省时,一个人仅需2...

2023-10-27 09:15:07查看全文 >>

asp怎么查询输入数据库里的记录(asp查询出来的数据导出)

asp怎么查询输入数据库里的记录(asp查询出来的数据导出)

第一步,我们要建立一个名为db_sample.mdb的数据库(本文以access2000数据库为例),并在其中建立表T_...

2023-10-27 08:56:43查看全文 >>

asp表格导出到excel(asp网页导出excel)

asp表格导出到excel(asp网页导出excel)

asp导出到excel方法一:---------------------------------------------...

2023-10-27 08:56:59查看全文 >>

怎么用asp循环输出表格(asp导入电子表格的方法有哪些)

怎么用asp循环输出表格(asp导入电子表格的方法有哪些)

可能是将一个html 表格变成 Microsoft Excel 格式的最快方法。ContentType 属性通知浏览器数...

2023-10-27 09:00:41查看全文 >>

如何在asp中添加一个表格(asp怎么插入表)

如何在asp中添加一个表格(asp怎么插入表)

1. 前言报表打印通常是管理信息系统中的一个重要模块,而Excel凭借它功能强大、应用灵活、通用性强等的优势在报表打印中...

2023-10-27 08:38:59查看全文 >>

asp修改excel数据(asp怎么读取excel文件)

asp修改excel数据(asp怎么读取excel文件)

在本文中,您将学习如何在ASP.NET MVC 应用程序中创建、读取和编辑 Excel 电子表格。为此,我们将创建一个由...

2023-10-27 09:20:41查看全文 >>

文档排行