【问题标题】:Remove need of creating backup file while importing Excel spreadsheet to SQL Server database将 Excel 电子表格导入 SQL Server 数据库时无需创建备份文件
【发布时间】:2011-12-08 01:46:19
【问题描述】:

我是 ASP.NET 和 C# 的新手,我正在使用 VS2005 C# 和 SQL Server 2005。

我有一个 Web 应用程序,其中包含一个从导入数据库的电子表格中上传数据的功能。

但是,我当前的函数将上传的电子表格复制到一个目录中,并使用目录中的上传文件来读取内容。

我想更改它,使其不会创建上传的 excel 文件的备份副本,而是直接从上传的文件中读取文件内容,而不是创建的备份。

下面是我为.xls.xlsx 电子表格导入功能的代码sn-p:

if (FileImport.HasFile)
        {

            // Get the name of the Excel spreadsheet to upload. 
            string strFileName = Server.HtmlEncode(FileImport.FileName);

            // Get the extension of the Excel spreadsheet. 
            string strExtension = Path.GetExtension(strFileName);

            // Validate the file extension. 
            if (strExtension == ".xls" || strExtension == ".xlsx")
            {
                    // Generate the file name to save. 
                    string strUploadFileName = "C:/Documents and Settings/user01/My Documents/Visual Studio 2005/WebSites/MajorProject/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;

                    // Save the Excel spreadsheet on server. 
                    FileImport.SaveAs(strUploadFileName);

                    // Create Connection to Excel Workbook
                    string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Excel 8.0;";

                    using (OleDbConnection connection =
                                 new OleDbConnection(connStr))
                    {
                        string selectStmt = string.Format("Select [COLUMNS]  FROM [userlist$]");

                        OleDbCommand command = new OleDbCommand(selectStmt, connection);

                        connection.Open();
                        Console.WriteLine("Connection Opened");
                        // Create DbDataReader to Data Worksheet
                        using (DbDataReader dr = command.ExecuteReader())
                        {
                            // SQL Server Connection String
                            string sqlConnectionString = "Data Source=<datasource>";

                            // Bulk Copy to SQL Server
                            using (SqlBulkCopy bulkCopy =
                                       new SqlBulkCopy(sqlConnectionString))
                            {
                                bulkCopy.DestinationTableName = "UserDB";
                                bulkCopy.WriteToServer(dr);
                                return;
                            }
                        }
                    }

下面是我为.csv 电子表格导入功能的代码sn-p:

if (strExtension == ".csv")
{
        // Generate the file name to save. 
        string dir = @"C:\Documents and Settings\user01\My Documents\Visual Studio 2005\WebSites\MajorProject\UploadFiles\";
        string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
        // Save the Excel spreadsheet on server. 
        BaanImport.SaveAs(dir + mycsv);

        // Create Connection to Excel Workbook
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;";
        using (OleDbConnection ExcelConnection = new OleDbConnection(connStr))
        {
            string selectStmt = string.Format("SELECT [COLUMNS] FROM " + mycsv);

            OleDbCommand ExcelCommand = new OleDbCommand(selectStmt, ExcelConnection);

            OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);

            ExcelConnection.Open();

            using (DbDataReader dr = ExcelCommand.ExecuteReader())
            {
                // SQL Server Connection String
                string sqlConnectionString = "Data Source=<datasource>";

                // Bulk Copy to SQL Server
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(sqlConnectionString))
                {
                    bulkCopy.DestinationTableName = "UserDB";
                    bulkCopy.WriteToServer(dr);
                    return;
                }
            }
        }
}

我可以知道如何更改它,使其不会在dir:@"C:\Documents and Settings\user01\My Documents\Visual Studio 2005\WebSites\MajorProject\UploadFiles\ 中创建副本,而是直接从导入的文件中读取数据?

提前感谢您的帮助。

【问题讨论】:

    标签: c# asp.net sql visual-studio


    【解决方案1】:

    假设您正在处理导入文件的对象是 HttpPostedFile,那么您可以使用其 InputStream 直接从其上传位置读取文件。

    有关更多信息和示例代码,请参阅this MSDN documentation

    【讨论】:

      猜你喜欢
      • 2010-09-27
      • 2014-03-11
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多