提问人:Laxman 提问时间:11/5/2015 最后编辑:pnutsLaxman 更新时间:11/18/2023 访问量:687
C# 到 Excel 将记录插入为十进制而不是文本
C# to Excel insert record as decimal instead of text
问:
我正在使用 OLEDB 连接将数据从我的 C# 程序插入到 Excel 文件中。所有字段都以文本形式插入,但我希望将字段作为十进制插入到 Excel 文件中。我的代码:AMOUNT
OleDbConnection conOledb;
OleDbCommand cmdOledb = new OleDbCommand();
conOledb = new OleDbConnection(" Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + destFile + "';Extended Properties='Excel 12.0 Xml;HDR=YES'");
string sqlQuery = "Insert into [Sheet1$] ([Record Identifier],[Payment Value Date],[Product Code],[Debit Account Number],[Beneficiary Name],[Instrument Amount],[Beneficiary Bank IFSC Code ],[Beneficiary Account Number],[Remarks1],[Remarks2]) " +
" VALUES(@MODE, @DATE, @RTGS, @OURBANKACC, @PARTYNAME, @AMOUNT, @RTGSCODE, @PARTYBANKACC, @PARTYNAME, @NARRATION1)";
conOledb.Open();
cmdOledb.Connection = conOledb;
cmdOledb.CommandText = sqlQuery;
cmdOledb.Parameters.AddWithValue("@MODE", 'P');
cmdOledb.Parameters.AddWithValue("@DATE", Convert.ToDateTime(dt_From.EditValue).ToString("dd-MM-yyyy"));
cmdOledb.Parameters.AddWithValue("@RTGS", (row["RTGS"] ?? string.Empty).ToString());
cmdOledb.Parameters.AddWithValue("@OURBANKACC", (row["OURBANKACC"] ?? string.Empty).ToString());
cmdOledb.Parameters.AddWithValue("@PARTYNAME", (row["PARTYNAME"] ?? string.Empty).ToString());
cmdOledb.Parameters.AddWithValue("@AMOUNT", Convert.ToDecimal(row["AMOUNT"] ?? 0));
cmdOledb.Parameters.AddWithValue("@RTGSCODE", (row["RTGSCODE"] ?? string.Empty).ToString());
cmdOledb.Parameters.AddWithValue("@PARTYBANKACC", (row["PARTYBANKACC"] ?? string.Empty).ToString());
cmdOledb.Parameters.AddWithValue("@PARTYNAME", (row["PARTYNAME"] ?? string.Empty).ToString());
cmdOledb.Parameters.AddWithValue("@NARRATION1", (row["NARRATION1"] ?? string.Empty).ToString());
cmdOledb.ExecuteNonQuery();
conOledb.Close();
答: 暂无答案
评论