如何将excel文件从用户的机器保存到数据库中 [英] how to save a excel file from user's machine into database

查看:91
本文介绍了如何将excel文件从用户的机器保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用文件上传控件并使用一些java脚本我已经使它点击功能来做后面的代码也使用另一个相邻的按钮。问题是我想从用户的机器获取文件的完整路径,以便我可以对其执行一些验证,最后将其保存到数据库中。但是,我无法这样做,因为我使用server.mappath()的路径意味着服务器的路径,因此代码在路径中给出了缺陷。您能告诉我如何获取完整的文件路径,而不是将其保存到数据库中。让我们说文件是一个excel文件。

我的代码在这里:



Hi All,

I am using a file upload control and using some java scripts I have made it click function to do the code behind thing too using another adjacent button. The problem is that I want to get the full path of the file from user''s machine so that I can perform some validations over it and than finally save it into database. However, I am not able to do so as the path I am getting using server.mappath() means the path of server and thus code gives a defect in the path. Could you please tell me how to get the full file path and than saving it to database. lets say the file is a excel file.
My code here :

 protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {

            if (FileUpload1.HasFile)
            {
                string strFilepath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
                string filename =(FileUpload1.PostedFile.FileName);
                                if (filename != "")
                {
                    res = ImportExcel2007(filename);
                    if (res != null)
                    {
                        gvErrors.DataSource = res;
                        gvErrors.DataBind();
                    }
                }
            }
        }



        catch (Exception ex)
        {
            throw ex;
        }
    }

private DataSet ImportExcel2007(String strFilePath)
    {
        //if (!File.Exists(strFilePath)) return false;
        String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;"
        + "Data Source=" + strFilePath + ";"
        + "Extended Properties='Excel 8.0;HDR=No'";
        OleDbConnection connExcel = new OleDbConnection(strExcelConn);
        OleDbCommand cmdExcel = new OleDbCommand();
        DataSet ds = new DataSet();
        try
        {
            cmdExcel.Connection = connExcel;

            //Check if the Sheet Exists
            connExcel.Open();
            DataTable dtExcelSchema;
            //Get the Schema of the WorkBook
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            connExcel.Close();

            //Read Data from Sheet1
            connExcel.Open();
            OleDbDataAdapter da = new OleDbDataAdapter();

            Error Comes here ( No row at postion[0]) --> string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
            cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";

            //Range Query
            //cmdExcel.CommandText = "SELECT * From [" + SheetName + "A3:B5]";

            da.SelectCommand = cmdExcel;
            da.Fill(ds);
            connExcel.Close();

        }
        catch(Exception ex)
        {
            throw ex;

        }
        finally
        {
            cmdExcel.Dispose();
            connExcel.Dispose();
        }

        return ds;
    }



请帮忙!


Pls help !

推荐答案

使用
Server.MapPath("~/App_Data/")

并将文件保存到网站App_Data文件夹。然后你就没有问题了吗

and save the file to the website App_Data folder. Then you will have no problem in reading it


试试这个...



Try this...

if (FileUpload1.HasFile)
{
    FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
    string FolderPath = ConfigurationManager.AppSettings["DMSFolder"];
    FilePath = Server.MapPath(FolderPath + FileName);
    FileUpload1.SaveAs(FilePath);
}


这篇关于如何将excel文件从用户的机器保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆