【问题标题】:How to fetch a excel file from a folder?如何从文件夹中获取excel文件?
【发布时间】:2014-01-18 04:31:47
【问题描述】:

我的文件夹中有一个 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文件的fileName(varchar(256)),Data(varbinary(max)),Path(varchar(256))保存到sql数据库中。

请帮帮我

【问题讨论】:

  • 您所要做的就是将文件转换为流,然后转换为字节数组,并使用任何标准的ADO.NET db 类将其保存在 db 中,您这样做有没有遇到任何问题?你试过了吗?
  • 将文件转换为byte[] (stackoverflow.com/questions/2030847/…) 并获取名称和路径并保存。如果可以保存很多文件,我个人建议只保存路径和名称。

标签: c# asp.net


【解决方案1】:

试试这个来获取和读取一个 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);

}

【讨论】:

  • 嗯?我认为您误读了这个问题-“如何将文件作为字节数组保存到 SQL 的 varbinary 文件中”。您的答案是完全不同的“如何从 Excel 文件中读取数据”。
【解决方案2】:

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

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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多