不能用sql语句将数值插入到指定" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > C# 使用<em>OleDB</em>写数据到excel的插入数据语句!!!

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

作者:乔山办公网日期:

返回目录:excel表格制作


发我你的Excel表,邮箱 我应该能给你做出个例子来的

例子已发你邮箱了

不能用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 !e799bee5baa6e79fa5e98193e59b9ee7ad94361= 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();
}
现在工e79fa5e98193e59b9ee7ad94332程里添加引用Microsoft.Office.Interop.Excel,然后可以使用如下代码输出。

Dim oXl As Excel.Application = New Excel.Application()
Dim oWb As Excel.Workbook
Dim oWs As Excel.Worksheet
On Error GoTo Morn

oWb = oXl.Workbooks.Add()
oWs = oWb.Worksheets(1)

With oWs

.Cells._Default(1, 1).Value ="a"
.Cells._Default(1, 2).Value ="b"
.Cells._Default(1, 3).Value ="c"
.Cells._Default(1, 4).Value ="d"
.Cells._Default(2, 1).Value ="32"
.Cells._Default(2, 2).Value ="1"
.Cells._Default(2, 3).Value ="90"
.Cells._Default(2, 4).Value ="合格"
End With
oWs.Parent.Names.Add("CostRange", "=" & "A1:B39")
oWs.SaveAs(sFileName)
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
If MsgBox("已将数据输出到Excel文件中! 现在打开该文件?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "已完成") = MsgBoxResult.No Then
oXl.Quit()
Else
oXl.Visible = True
End If
oXl = Nothing
oWs = Nothing
oWb = Nothing

你最好是将插入的数据写入到dataset,或者datatable中,然后再写入excel
在sheet1中包含一个列表,有表头c1,c2,c3,即a1单元格的值是c1,a2单元格的值是c2,a1单元格的值是c3。现需要e799bee5baa6e58685e5aeb9338往这个列表中插入一条数据:
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName+ ";Extended Properties=\"Excel 8.0;\"";
OleDbConnection conn = new OleDbConnection(excelConnectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
string sql = "insert into [sheet1$] values('1','2','3')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

这是一种方法,当然也可以通过调用EXCEL的COM对象来完成。
如果你要插入的不是列表的话,如果你的excel中不是列表的话,需要在你sheet中特定的单元格中“插入名称”。
提供一个方法仅供参考哦
public static bool SaveDataTableToExcel(System.Data.DataTable excelTable, string filePath)
{
Microsoft.Office.Interop.Excel.Application app =
new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
app.Visible = false;
Workbook wBook = app.Workbooks.Add(true);
Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
if (excelTable.Rows.Count > 0)
{
int row = 0;
row = excelTable.Rows.Count;
int col = excelTable.Columns.Count;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
string str = excelTable.Rows[i][j].ToString();
wSheet.Cells[i + 2, j + 1] = str;
}
}
}

int size = excelTable.Columns.Count;
for (int i = 0; i < size; i++)
{
wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
}
//设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
//保存工作簿
wBook.Save();
//保存excel文件
app.Save(filePath);
app.SaveWorkspace(filePath);
app.Quit();
app = null;
return true;
}
catch (Exception err)
{
MessageBox.Show("导出Excel出错!错误原因:" + err.Message, "提示信息",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
finally
{
}
}

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网