将数据从Excel文件导入数据库表

将数据从Excel文件导入数据库表

本文介绍了将数据从Excel文件导入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我最近一直在寻找能够上传excel文件并将数据导入数据库表的代码我发现似乎没有工作。



谢谢,

Leroy

Hi everyone,

I have been looking around recently for some codes that would allow me to upload an excel file and import the data into a database table but those that I have found dont seem to work.

Thanks,
Leroy

推荐答案

protected void btnImport_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile)
            {
                try
                {
                    string path = string.Concat(Server.MapPath("~/Uploaded Folder/" + FileUpload1.FileName));
                    FileUpload1.SaveAs(path);

                    // Connection String to Excel Workbook
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
                    OleDbConnection connection = new OleDbConnection();
                    connection.ConnectionString = excelConnectionString;
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1



这篇关于将数据从Excel文件导入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:06