C#中将数据插入到Excel表中

想添加的数据就是下图表中的字段,在点击提交按钮的时候,将文本框中的值全部存进一个Excel表中,而且,没添加一次的时候,信息不覆盖,而是追加显示
我感觉应该用读写文件的方式写,但是弄了半天没弄出来
求大侠们帮帮忙,根据我提供的字段,给段代码
谢谢了~~~~~~~
string month = cboMonth.Text;
string day = cboDay.Text;
string bir = year+"年" + month+"月" + day+"日";

string job = txtJob.Text;
string area = txtAddress.Text;
string email = txtEmail.Text;

string content = tel+name+bir+job+area+email;
try
{
FileStream file = new FileStream("储存.xls", FileMode.Create,FileAccess.Write);
StreamWriter sw = new StreamWriter(file);
sw.WriteLine(content);
sw.Close();
file.Close();
}

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();
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-08-19
//引用COM中的 Microsoft.Excel 11.0
string excelFilePath = @"D:\1.xls"; //已经存在的Excel
Excel.Application myExcel = new Excel.ApplicationClass();
string excelFilePath = @"D:\1.xls";
Excel.Application myExcel = new Excel.ApplicationClass();
object oMissing = System.Reflection.Missing.Value;
Excel.Workbook myBook= myExcel.Application.Workbooks.Open(excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];
mySheet.Cells[5, 5] = "test"; //给excel中的第5行和第5列赋值 表头就是第1行
myBook.Close(true, oMissing, oMissing); // 保存修改后的excel
myExcel.Quit();

或者引用 .net 里的 Microsoft.Office.Excel

string excelFilePath = @"D:\1.xls";
Microsoft.Office.Interop.Excel.ApplicationClass myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Workbook myBook = myExcel.Workbooks.Open(excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
Microsoft.Office.Interop.Excel.Worksheet mysheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range r = (Microsoft.Office.Interop.Excel.Range)mysheet.Cells[5, 5];
r.Value2 = "luotest";
myBook.Close(true, oMissing, oMissing);
myExcel.Quit();
第2个回答  2009-08-19
我反而觉得你可以把excel作为数据源,然后就能插入了,去看c#的强dataset类
第3个回答  2009-08-19
你可以先去了解一下 如何用文本流把内容写到文本文档里
相似回答