C# 使用OleDB写数据到excel的插入数据语句!!!


double value = 10.215;
这个10.215数据写到E3这个单元格是否可以?就举这个例子( sql语句 ,就是sql写不出来 )就可以了。初学,insert很多次都还没有预期效果,谢谢!!!更新也写上最好!!!

第1个回答  推荐于2016-01-31
不能用sql语句将数值插入到指定的单元格。
操作Excel,常见的有两种方法:
一种直接把它作为数据库来处理,这时Excel的sheet就是数据库中的一张表。
一种用程序直接操纵Excel,可以对Excel文档做任何处理。例如:
Excel.Application oExcel = new Excel.Application();
Excel.Workbooks oBooks;
Excel.Workbook oBook;
Excel.Sheets oSheets;
Excel.Worksheet oSheet;
Excel.Range oCells;
..........

sTemplate = AppPath + @"templates\" + template;

oExcel.DisplayAlerts = false;
oBooks = oExcel.Workbooks;

oBooks.Add(sTemplate);

oBook = oBooks[1];

oSheets = oBook.Worksheets;
oSheet = (Excel.Worksheet)oSheets[1];
oCells = oSheet.Cells;

try
{
oCells[3, 5] =10.215;
..............

oSheet.SaveAs(sFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value,
Missing.Value);
oBook.Close(Missing.Value, Missing.Value, Missing.Value);
oBooks.Close();
}
catch(Exception ex)
{
return ex.Message;
}
finally
{
if (oCells != null)
{
Marshal.ReleaseComObject(oCells);
}
if (oSheet != null)
{
Marshal.ReleaseComObject(oSheet);
}
if (oSheets != null)
{
Marshal.ReleaseComObject(oSheets);
}
if (oBook != null)
{
Marshal.ReleaseComObject(oBook);
}
if (oBooks != null)
{
Marshal.ReleaseComObject(oBooks);
}
if (oExcel != null)
{
oExcel.Quit();
Marshal.ReleaseComObject(oExcel);
}

oCells = null;
oSheets = null;
oBook = null;
oBooks = null;
oExcel = null;
oSheet = null;

System.GC.Collect();
}本回答被提问者采纳
相似回答