简单的将数据插入excel表

这段代码展示了如何使用C#将数据插入到Excel表格中。通过创建一个OleDbConnection对象连接Excel文件,并使用OleDbCommand执行INSERT语句来添加记录。此外,还提供了导出数据到Excel的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;
using System.Data;
namespace CardProject.admin.resource
{
    public partial class test2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void daochu()
        {
            string filePath = Server.MapPath("~/" + Guid.NewGuid().ToString() + ".xls");

            File.Copy(Server.MapPath("~/Book1.xls"), filePath);

            // 使用OleDb驱动程序连接到副本

            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;");

            using (conn)
            {

                conn.Open();

                // 增加记录

            

                OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([年龄], [姓名], [生日]) VALUES(@Id, @Name, @Birthday)", conn);

                cmd.Parameters.AddWithValue("@Id", "1");
              
                cmd.Parameters.AddWithValue("@Name", "Hsu Yencheng");

                cmd.Parameters.AddWithValue("@Birthday", "1981-10-13");
               
                cmd.ExecuteNonQuery();

            }

            // 输出副本的二进制字节流

            Response.ContentType = "application/ms-excel";

            Response.AppendHeader("Content-Disposition", "attachment;filename=info.xls");

            Response.BinaryWrite(File.ReadAllBytes(filePath));

            // 删除副本

            //File.Delete(filePath);
#region
//          //  导出数据:
 
//  //string filePath = Server.MapPath("~/info.xls");
 
//  //OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0");
 
//  //DataTable dt = new DataTable();
 
//  //da.Fill(dt);
 


//        public DataTable  GetDataFromExcel(string filepath) 
//        {         
//            try      
//            {           
//                string ext = Path.GetExtension(filepath).ToLower();           
//                string strConn;             
//                if (ext== ".xls")         
//                {                   
//                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";       
//                }             
//                else if (ext== ".xlsx")     
//                {              
//                    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0 Xml;HDR=YES' ";      
//                }            
//                else        
//                {               
//                    throw new Exception("filepath输入错误!");        
//                }             
//                OleDbDataAdapter da = new OleDbDataAdapter("SELECT 工号,姓名,考核结果 FROM [Sheet1$]", strConn);         
//                DataTable dt = new DataTable();        
//                da.Fill(dt);           
//                return dt;      
//            }    
//            catch       
//            {          
//                throw;    
//            }   
       
#endregion
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            daochu();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值