在 Excel 工作表中插入值时,无法向 OleDbCommand 提供任何提示(语法)以插入数值。打开工作表时,它不会在工作表中将数值显示为数字。
有一种解决方法,当在 excel 文件中创建第一条记录时,然后转到第一条记录并在已存在记录的单元格(需要数字数据的地方)中重新输入相同的值。
或者
您可以在该 Excel 表中放置带有数值的默认记录。
下面提到了示例代码,用于在工作表中创建第一条记录后将数据类型转换为数字。当任何记录插入工作表时调用一次 ChangeFormat 函数,进一步的数据将以正确的格式保存。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Data;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
System.Data.DataTable xlsData = new System.Data.DataTable();
string xlsConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\romil.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes;\"";
System.Data.OleDb.OleDbConnection xlsCon = new System.Data.OleDb.OleDbConnection(xlsConnectionString);
System.Data.OleDb.OleDbCommand xlsCommand;
int recUpdate;
int recordsinSheet;
xlsCommand = new System.Data.OleDb.OleDbCommand("Select count(*) as RecCount from [Sheet1$]");
xlsCommand.Connection = xlsCon;
xlsCon.Open();
recordsinSheet =Convert.ToInt32( xlsCommand.ExecuteScalar());
xlsCommand= new System.Data.OleDb.OleDbCommand("Insert into [Sheet1$] (Field1,Field2) values ('123',2)");
xlsCommand.Connection = xlsCon;
recUpdate = xlsCommand.ExecuteNonQuery();
xlsCon.Close();
if ((recordsinSheet + recUpdate) == 1)
ChangeFormat();
Console.ReadKey();
}
private static void ChangeFormat()
{
string filename = "d:\\romil.xlsx";
object missing = System.Reflection.Missing.Value ;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Microsoft.Office.Interop.Excel.Worksheet wsh=null;
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in wb.Sheets)
{
if (sheet.Name == "Sheet1")
{
wsh = sheet;
break;
}
}
for (int rCnt = 2; rCnt <= wsh.Rows.Count; rCnt++)
{
if ( wsh.Cells[rCnt, 2].Value== null)
break;
wsh.Cells[rCnt, 2] = wsh.Cells[rCnt, 2].Value;
}
wb.SaveAs(filename, missing,
missing, missing, missing, missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing,
missing, missing);
wb.Close();
}
}
}