0

我想将 excel 表数据上传到 sql sever 2008,我的所有数据,即我的 excel 表中的数字和字母表都将上传到数据库表中,但 $ 符号不会上传到那里,因为我必须使用货币作为一列,$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();
            }

        }
    }
}
4

2 回答 2

0

如果您想要货币列,因为Int您无法在其中插入$符号,decimal因为默认整数数据类型不允许它。因此,要解决您的问题,请将 curreny 设置为Varchar并插入数据,$100.00 并在显示时将其显示出来。在前端进行计算时,$使用关键字删除符号Contains并将其转换为Double并进行计算。希望能帮助到你

于 2013-11-14T09:15:12.060 回答
0

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

从 Excel 加载 Gridview

只需根据您的需要更改代码。

于 2013-11-15T10:48:39.453 回答