C# 如何从文件夹中获取excel文件?

C# 如何从文件夹中获取excel文件?,c#,asp.net,C#,Asp.net,我的文件夹里有一个excel文件。但是我不知道如何从文件夹中获取该文件 但我正在检查文件是否存在 这是我的密码: protected void Page_Load(object sender, EventArgs e) { string filePath = Server.MapPath("~/Upload/Sample.xlsx"); bool fileexists = File.Exists(filePath); //Here fileexists = true

我的文件夹里有一个excel文件。但是我不知道如何从文件夹中获取该文件

但我正在检查文件是否存在

这是我的密码:

protected void Page_Load(object sender, EventArgs e)
{
     string filePath = Server.MapPath("~/Upload/Sample.xlsx");
     bool fileexists = File.Exists(filePath); //Here fileexists = true    
}
我需要将那个excel文件保存在sql数据库中

我需要将该excel文件的文件名(varchar(256))、数据(varbinary(max))、路径(varchar(256))保存到sql数据库中


请帮帮我

试试这个来获取并读取xlsx文件

if (Directory.Exists(Server.MapPath("path")))
{
             string filename = Path.GetFileName("path");// to get filename
 string conStr = string.Empty;
        string extension = Path.GetExtension(filename);// get extension
        if (extension == ".xls" || extension == ".xlsx")
        {
            switch (extension)
            {
                case ".xls": //Excel 1997-2003
                    conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source='" + mappingPath + "';" + "Extended Properties=Excel 8.0;";
                    break;
                case ".xlsx": //Excel 2007
                    conStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + mappingPath + "';" + "Extended Properties=Excel 8.0;";
                    break;
                default:
                    break;
            }

            OleDbConnection connExcel = new OleDbConnection(conStr.ToString());
            OleDbCommand cmdExcel = new OleDbCommand();
            OleDbDataAdapter oda = new OleDbDataAdapter();
            connExcel.Open();

            DataTable dtExcelSchema;
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
            ViewState["SheetName"] = SheetName;
            //Selecting Values from the first sheet
            //Sheet name must be as Sheet1
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * From [" + SheetName + "]", conStr.ToString()); // to fetch data from excel 
            da.Fill(dtExcel);

}

这比你想象的要简单得多,应该是这样的:

if (File.Exists(filePath)) {   
    byte[] data = File.ReadAllBytes(filePath);
    string fileName = Path.GetFileName(filePath);

    const string query = "INSERT INTO Files (FileName, Data, Path) VALUES (@FileName, @Data, @Path)";

    using (var connection = new SqlConnection(connectionString))
    using (var command = new SqlCommand(query, connection)) {
        command.Parameters.AddWithValue("@FileName", fileName);
        command.Parameters.AddWithValue("@Data", data);
        command.Parameters.AddWithValue("@Path", filePath);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

您所要做的就是在流中转换您的文件,然后使用任何标准的
ADO.NET
db类将其保存在db中,您在这样做时遇到过任何问题吗?你试过了吗?将文件转换成字节[](),获取名称和路径并保存它们。就我个人而言,如果可以保存很多文件,我建议只保存路径和名称?我认为您误解了这个问题——“如何将文件作为字节数组保存到SQL的varbinary文件”。您的答案是完全不同的“如何从Excel文件读取数据”。