将Excel工作表上载到SQL服务器表$时,不会上传数字,而只上传文本

时间:2013-11-14 05:54:14

标签: sql-server excel

我想将excel表格数据上传到sql sever 2008,我的excel表格中的所有数据,即数字和字母表将在数据库表格中上传,但$ sign不会上传,因为我必须使用货币作为列,$ sign将上传文本数据但不上传数字.....请让我知道这个问题

这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.IO;
using log4net.Config;
using log4net; 


namespace ExcelUpload
{
    class Program
    {
        static ILog log = LogManager.GetLogger(typeof(Program));

        //Variable declarations
        public static string strSqlConnection, strExcelDataQry, strSqlTable, strExcelFilePath, sexcelconnectionstring;
        public static int intRows;

        static void Main(string[] args)
        {
            #region GET PARAMS FROM CONFIG FILE
            strSqlConnection        = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString();
            strExcelFilePath        = ConfigurationManager.AppSettings["ExcelFileName"].ToString(); 
            string[] sqlSheets      = ConfigurationManager.AppSettings["Sheets"].Split(',');
            #endregion

            #region SET CONNECTIONS
            sexcelconnectionstring      = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelFilePath + ";Extended Properties='Excel 12.0 xml;HDR=YES;'";
            SqlConnection Sqlconn       = new SqlConnection(strSqlConnection);
            SqlBulkCopy bulkcopy        = new SqlBulkCopy(strSqlConnection);
            OleDbConnection oledbconn   = new OleDbConnection(sexcelconnectionstring);
            #endregion

            XmlConfigurator.Configure();
            log4net.ThreadContext.Properties["Context"] = "ExcelUpload";            

            try
            {
                log.Info("Started Execution of ExcelUpload Process");
                foreach (string sqlSheetName in sqlSheets)
                {
                    #region GET PARAMS FROM CONFIG FILE BASED ON CURRENT SHEET
                    strExcelDataQry = "select " + ConfigurationManager.AppSettings[sqlSheetName + "ColumnNames"] + " from [" + sqlSheetName + "$]";
                    strSqlTable     = ConfigurationManager.AppSettings[sqlSheetName + "Table"];
                    intRows         = Convert.ToInt32(ConfigurationManager.AppSettings[sqlSheetName + "RowsToExclude"].ToString());
                    #endregion

                    #region TRUNCATE TABLE
                    SqlCommand SqlCommand = new SqlCommand("TRUNCATE TABLE " + strSqlTable, Sqlconn);
                    Sqlconn.Open();
                    SqlCommand.ExecuteNonQuery();
                    Sqlconn.Close();
                    log.Info("Table " + strSqlTable + " Truncated Successfully");
                    #endregion

                    #region BULK COPY DATA 
                    log.Info("Started processing the sheet " + sqlSheetName); 
                    OleDbCommand oledbcmd = new OleDbCommand(strExcelDataQry, oledbconn);
                    oledbconn.Open();
                    DataSet ds = new DataSet();
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(oledbcmd))
                    {
                        adapter.Fill(ds);
                    }

                    for (int iRow = 0; iRow < intRows; iRow++)
                    {
                        ds.Tables[0].Rows[iRow].Delete();
                    }                    
                    ds.Tables[0].AcceptChanges();

                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        foreach (DataColumn col in ds.Tables[0].Columns)
                        {
                            if (col.DataType == typeof(System.String))
                            {

                                    dr[col] = dr[col].ToString().Trim();
                            }
                        }
                    }

                    bulkcopy.DestinationTableName = strSqlTable;
                    bulkcopy.WriteToServer(ds.Tables[0]);
                    oledbconn.Close();
                    log.Info("Sheet " + sqlSheetName + " successfully loaded to the table " + strSqlTable); 
                    #endregion
                }
                log.Info("ExcelUpload Process completed successfully");
            }
            catch (System.Exception ex)
            {
                log.Info("Error while processing :- " + ex.Message);
            }
            finally
            {
                Sqlconn.Close();
                oledbconn.Close();
            }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果您希望货币列为Int,则无法在其中插入$符号和decimal,因为默认的整数数据类型将不允许它。 因此,要解决您的问题,请将当前值设为Varchar并将数据插入$100.00之类的内容 而显示它只是显示它。 在前端进行计算时,使用$关键字删除Contains符号  并将其转换为Double并进行计算。 希望它有所帮助

答案 1 :(得分:0)

好的尝试此链接用于存储从excel到gridview的数据(仅作为示例),您只需更改您的需求。 我还测试了该代码,以便在单元格中存储包含$符号的数据,并将其存储在数据库中,同时显示相同的data from the database with $ symbols in the same cells。 看看它,它可能会有用。

Load Gridview from Excel

只需根据需要更改代码。