C#中将数据插入到Excel表中
来源:百度知道 编辑:UC知道 时间:2024/05/15 08:19:38
我感觉应该用读写文件的方式写,但是弄了半天没弄出来
求大侠们帮帮忙,根据我提供的字段,给段代码
谢谢了~~~~~~~
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 tabl