我目前正在尝试写入excel文件。我用这段代码:
public void UpdateExcelFile(string dir, string sheet, string sql)
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
MyConnection = new System.Data.OleDb.OleDbConnection(String.Format("provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0;MAXSCANROWS=0\";", dir));
MyConnection.Open();
myCommand.Connection = MyConnection;
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
if (myCommand != null)
{
myCommand.Dispose();
myCommand = null;
}
if (MyConnection != null)
MyConnection.Dispose();
}
我在条件表达式中得到的数据类型不匹配"例外。我的发言如下:
string cmdString = String.Format("Insert into [{0}] (Customer, Location, CalendarWeek, from, till, [agreed at], [agreed with], [{1}]) values('{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')"
, sheetName, getPackaging.Text, getCustomer.Text, getLocation.Text, getCalWeek, getFirstDayOfCalWeek,
getLastDayOfCalWeek, getDateOfAgreement.Value.ToString("dd/MM/yyyy"), getNameForPrice.Text, getPrice.Value.ToString());
所以一切都转换为字符串。在我的Excel文件中,我将每一列都转换为"标准"。为什么会这样?我甚至将MaxScanRows属性设置为0。
编辑: 这是cmdString
Insert into [Sheet1$] (Customer, Location, CalendarWeek, from, till, [agreed at], [agreed with], [10x500]) values('A','City','1','02.01.2016','08.01.2016','05.03.2016','Test','3')