C#中将数据插入到Excel表中
想添加的数据就是下图表中的字段,在点击提交按钮的时候,将文本框中的值全部存进一个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 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();
}
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();
}
展开全部
//引用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();
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();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我反而觉得你可以把excel作为数据源,然后就能插入了,去看c#的强dataset类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以先去了解一下 如何用文本流把内容写到文本文档里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询