c#检索出的数据写入execl中 各位高手请帮帮忙,我现在从数据库里查询出一组数据,在本地某一磁盘里有一个execl表格模板我想将数据插入到execl表格指定的列,用c#要如何实现啊,请帮帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string filename =Application.StartupPath+@"\信息.xls"; string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=Yes'";//这个链接字符串是excel2003的 OleDbConnection oleConn = new OleDbConnection(connstr); try { oleConn.Open(); string sqlStr; DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null); bool existTable = false; foreach (DataRow dr in dt.Rows)//检查是否有信息表 { if(dr["TABLE_NAME"].ToString()=="信息表$")//要加个$号 existTable = true; } if (!existTable) { sqlStr = @"create table 信息表(手机 char(15),姓名 nvarchar(10),生日 char(8),工作 nvarchar(20),邮箱 varchar(30),地址 nvarchar(50))"; OleDbCommand oleCmd = new OleDbCommand(sqlStr, oleConn); oleCmd.ExecuteNonQuery(); } string phone = textBox1.Text; string name = textBox2.Text; string birthday = comboBox1.Text + "/" + comboBox2.Text + "/" + comboBox3.Text; string workplace = textBox3.Text; string email = textBox4.Text; string address = textBox5.Text; //下面的代码用OleDbCommand的parameter添加参数 sqlStr = "insert into 信息表 values('"+phone+"','"+name+"','"+birthday+"','"+workplace+"','"+email+"','"+address+"')"; OleDbCommand Cmd = new OleDbCommand(sqlStr, oleConn); Cmd.ExecuteNonQuery(); } catch (Exception te) { MessageBox.Show(te.Message); } finally { oleConn.Close(); } http://www.cnblogs.com/pam/archive/2008/09/09/1287636.html c#操作excel有好几个类库,你可以下载。XlsDocument Xls = new XlsDocument();Xls.FileName = "";Worksheet sheet = Xls.Workbook.Worksheets.Add("");Cell cell = sheet.Cells.Add(1,1,"1"); 访问数据库 C#3.5: Linq组内按字段名称中的首数字进行拍序真令人头疼啊,请大家出出主意啊⊙﹏⊙b汗 如何向其他的应用程序发送复制粘贴的按键 如何使用BindingSource.AddNew添加數據 ASP.NET中怎样引用CSS文件??? c#反射问题 c#动态报表制作(winform)中 求开机自启动c#源码,谢谢了 谁有vs.net的下载地址,请给我一份, 万分感谢! c#到底是和物,它与vc,java有和区别呢? 监控进程发生异常 .net中%怎么用?
string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;
Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=Yes'";//这个链接字符串是excel2003的
OleDbConnection oleConn = new OleDbConnection(connstr);
try
{
oleConn.Open(); string sqlStr;
DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
bool existTable = false; foreach (DataRow dr in dt.Rows)//检查是否有信息表
{
if(dr["TABLE_NAME"].ToString()=="信息表$")//要加个$号
existTable = true;
}
if (!existTable)
{
sqlStr = @"create table 信息表(手机 char(15),姓名 nvarchar(10),生日 char(8),工作 nvarchar(20),邮箱 varchar(30),地址 nvarchar(50))";
OleDbCommand oleCmd = new OleDbCommand(sqlStr, oleConn);
oleCmd.ExecuteNonQuery();
}
string phone = textBox1.Text;
string name = textBox2.Text;
string birthday = comboBox1.Text + "/" + comboBox2.Text + "/" + comboBox3.Text;
string workplace = textBox3.Text;
string email = textBox4.Text;
string address = textBox5.Text;
//下面的代码用OleDbCommand的parameter添加参数
sqlStr = "insert into 信息表 values('"+phone+"','"+name+"','"+birthday+"','"+workplace+"','"+email+"','"+address+"')";
OleDbCommand Cmd = new OleDbCommand(sqlStr, oleConn);
Cmd.ExecuteNonQuery();
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
finally {
oleConn.Close();
}
Xls.FileName = "";
Worksheet sheet = Xls.Workbook.Worksheets.Add("");
Cell cell = sheet.Cells.Add(1,1,"1");