作者:乔山办公网日期:
返回目录:excel表格制作
我做了一个实例,希望可以帮助你,记得采纳哦。
我就截图你看吧
添加代码:
if(messagebox.show("请你核对资料","确认e799bee5baa6e59b9ee7ad94362",messagebotuton.okcancel,messageboxicon.asterisk)==dialogresult.cancel)
{
return;
}
else
{
string strcn=@"data source=HY-PC\SQLEXPRESS;initial catalog=cjgl;integrated security=true";
sqlconnection cn=new sqlconnection(strcn);
cn.open();
try
{
string sql="insert into KC values('"+textbox1.text+"','"+textbox2.text+"')";
sqlcommand cmd=new sqlcommand(sql,cn);
if(cmd.executenoquery()>0)
{
messagebox.show("添加成功","提示");
form_load(null,null);
}
else
{
messagebox.show("添加失败","提示");
}
}
finally
{
cn.close();
}
}
其余的更新,修改,都差不多
通过ADO.NET,可以写一个包含增删改的一个类函数(如下所示),要用的时候直接调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Load
{
class DBHelper
{
private static SqlCommand cmd = null;
private static SqlDataReader dr = null;
public int RowCount { get; private set; }
SqlConnection sqlCnn = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand();
//数据库e799bee5baa6e997aee7ad94e78988e69d83362连接字符串
private static string connectionString = "Server = 127.0.0.1; Database = DB; Integrated Security =SSPI";
//数据库连接Connection对象
public static SqlConnection connection = new SqlConnection(connectionString);
public DBHelper()
{ }
#region 返回结果集
public static SqlDataReader GetResult(string sql)
{
try
{
cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = connection;
cmd.Connection.Open();
dr = cmd.ExecuteReader();
return dr;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
}
}
#endregion
#region 对Select语句,返回int型结果集
public static int GetSqlResult(string sql)
{
try
{
cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = connection;
cmd.Connection.Open();
int a = (int)cmd.ExecuteScalar();
return a;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return -1;
}
finally
{
cmd.Connection.Close();
}
}
#endregion
#region 对Update,Insert和Delete语句,返回该命令所影响的行数
public static int GetDsqlResult(string sql)
{
try
{
cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = connection;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
return 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return -1;
}
finally
{
cmd.Connection.Close();
}
}
#endregion
}
}
string excelName= ExcelFileUrl();//返回Excel的路径
string fileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + excelName;//保存Excle的文件路径
object missing = Missing.Value;
Microsoft.Office.Interop.Excel.Application appExcel = null;//实例Excel类
try
{
appExcel = new Microsoft.Office.Interop.Excel.Application();
appExcel.DisplayAlerts = false;//DisplayAlerts 属性设置成 False,就不会出现这种警告。
Microsoft.Office.Interop.Excel.Workbook workbook = appExcel.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);//打开Excel
//Microsoft.Office.Interop.Excel.Workbooks workbooks = appExcel.Workbooks;//
//Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(missing);//工作簿
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets;//实例表格
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets[1];//第一个表格
}
catch
{}
类的传值如传e68a84e8a2ade799bee5baa6e79fa5e98193331的是第一个表格类型是Microsoft.Office.Interop.Excel.Worksheet 的函数
一、首先处理好数据库连接字串
Excel2000-2003: string connStr = "Microsoft.Jet.Oledb.4.0;Data Source='c:\test.xls';Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
Excel2007: string connStr = "Microsoft.Ace.OleDb.12.0;Data Source='c:\test.xlsx';Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
其中:
HDR ( Header Row )设置:
若指定值为e799bee5baa6e79fa5e98193e59b9ee7ad94338Yes,代表 Excel 档中的工作表第一行是栏位名称
若指定值为 No,代表 Excel 档中的工作表第一行就是资料了,没有栏位名称
IMEX ( IMport EXport mode )设置
当 IMEX=0 时为"汇出模式",这个模式开启的 Excel 档案只能用来做"写入"用途。
当 IMEX=1 时为"汇入模式",这个模式开启的 Excel 档案只能用来做"读取"用途。
当 IMEX=2 时为"连结模式",这个模式开启的 Excel 档案可同时支援"读取"与"写入"用途。
二、进行表格数据的查询、插入和更新:
(假设Excel文件text.xls中存在Excel表单tree,有2列分别为id,name)
1、查询
String sql = "select id, name from [tree$]";
或
String sql = "select id, name from `tree$`;
2、插入
String sql = "insert into [tree$] (id,name) values(1,'testname');
3、更新
String sql = "update [tree$] set name='name2' where id=1;
4、数据的删除
在OleDB的连接方式下,不可以使用delete from 语句来删除某表中的某一条记录。确切的说,在此模式下,将无法删除表中的记录。即使用update语句将所有的字段写成null,打开excel文件后依然会发现保留了该空行,而且在使用oleDB连接进行查询时,依然会查询到这条空数据。