保存路径?保存excel到sql?

发布于 2024-09-14 10:11:01 字数 510 浏览 1 评论 0原文

我真的很困惑如何去做这件事。

  • 我希望能够使用以下命令从我的网络应用程序上传 Excel 工作表 文件上传控制。
  • 接下来,我想读取第一行下的每一行。 (因此从第 2 行开始,第 1 行将是列标题)。
  • 最后,我想将我读到的字符串传递到另一个方法中,该方法将用它执行我想要的操作,然后发布到网格视图。

我打算如何这样做...
由于我的网络应用程序发布在网络上,托管在我的本地盒子上...我上传的文件的常用保存路径(桌面)不起作用。
所以我想将它保存到 SQL Server 上,该 SQL Server 也托管在我的本地机器上。

因此,我想我正在尝试:

  • 将上传的 Excel 保存到 SQL 数据库中。
  • 读取 Excel 中的每一行并将其传递给预期的方法。

好吧,这太令人困惑了。必须有一种更简单的方法来做到这一点! (我真的需要 SQL 数据库吗?)
哦,对我的 savePath 有什么好的想法吗?

I'm really confused on how to go about doing this.

  • I want to be able to upload an excel sheet from my web app using the
    file upload control.
  • Next, I want to read each individual row under the first row. (So starting from row 2, row 1 will be the column title).
  • Finally, I want to pass the Strings I've read into another method that'll do what I want with it and then post to a gridview.

How I intend to do so...
Since I have my web app published on the network, hosted on my local box... my usual savepath for the file uploaded (desktop) doesn't work.
So I thought to save it onto a SQL Server that is also hosted on my local box.

Therefore, I guess I'm trying to:

  • Save the uploaded excel into a SQL database.
  • Read each line from the excel and pass it through the intended method.

Okay, that was super confusing. There must be an easier way to do this! (Do I really need a SQL database?)
Oh, and any good ideas for my savePath?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2024-09-21 10:11:01

还没有真正解决这个问题。但使用 OleDBConnection 读取 excel 文件上的内容会更容易,如下所示:

            try
            {
                using (OleDbConnection olcon = new OleDbConnection())
                {
                        olcon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileupFile.FileName) + ";Extended Properties=Excel 12.0";
                        OleDbCommand olcmd = new OleDbCommand("SELECT * FROM [Sheet1$]", olcon);
                        olcon.Open();
                        OleDbDataReader olread = olcmd.ExecuteReader();
                        while (olread.Read())
                        {
                            line = (String)(olread.GetString(0));
                            Verify(line); //calls the datatosql method
                        }
                        olcon.Close();
                        lblFiup.Text = "Data Inserted Sucessfully";   
                        lblFiup.ForeColor = System.Drawing.Color.Green; 
                }
            }

然后,传递到另一个方法,该方法使用 SqlConnection 写入 SQL Server。像这样:

        protected void datatosql(String url, String stat)
        {
            try
            {
                using (SqlConnection sqlcon = new SqlConnection("Server=(local);Database=URLs;Trusted_Connection=True"))
                {
                    using (SqlCommand sqlcom = sqlcon.CreateCommand())
                    {
                        sqlcom.CommandText = "INSERT INTO WEVRecordsTest (URL, Status) VALUES ('" + url + "','" + stat + "')";
                        sqlcon.Open();
                        sqlcom.ExecuteNonQuery();
                        sqlcon.Close();
                    }
                }
            }
            catch (SqlException se)
            {
                lblHist.Text = se.Message;
                lblHist.ForeColor = System.Drawing.Color.Red;
            }
        }

Haven't really solved this. But it's easier to read whatever's on the excel file using the OleDBConnection like this:

            try
            {
                using (OleDbConnection olcon = new OleDbConnection())
                {
                        olcon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileupFile.FileName) + ";Extended Properties=Excel 12.0";
                        OleDbCommand olcmd = new OleDbCommand("SELECT * FROM [Sheet1$]", olcon);
                        olcon.Open();
                        OleDbDataReader olread = olcmd.ExecuteReader();
                        while (olread.Read())
                        {
                            line = (String)(olread.GetString(0));
                            Verify(line); //calls the datatosql method
                        }
                        olcon.Close();
                        lblFiup.Text = "Data Inserted Sucessfully";   
                        lblFiup.ForeColor = System.Drawing.Color.Green; 
                }
            }

Then, pass into another method, which uses SqlConnection to write into SQL Server. Like this:

        protected void datatosql(String url, String stat)
        {
            try
            {
                using (SqlConnection sqlcon = new SqlConnection("Server=(local);Database=URLs;Trusted_Connection=True"))
                {
                    using (SqlCommand sqlcom = sqlcon.CreateCommand())
                    {
                        sqlcom.CommandText = "INSERT INTO WEVRecordsTest (URL, Status) VALUES ('" + url + "','" + stat + "')";
                        sqlcon.Open();
                        sqlcom.ExecuteNonQuery();
                        sqlcon.Close();
                    }
                }
            }
            catch (SqlException se)
            {
                lblHist.Text = se.Message;
                lblHist.ForeColor = System.Drawing.Color.Red;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文