C#通过OLDB连接Excel文件时,如果文件不存在,会自动创建文件,用起来非常方便。
Excel2003的xls格式,使用Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" + "Extended Properties='Excel 8.0'"
Excel2007以后的xlsx格式,使用"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties='Excel 12.0'"
要使用ACE.OLEDB.12.0,需要下载一个组件AccessDatabaseEngine
另外要特别说明一下,自动创建xlsx格式,使用上面的连接时,文件能创建,但是打开时,会提示"excel 无法打开文件 因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。"
解决方法就是在连接后面再加一个参数,如下:"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties='Excel 12.0 XML'"
添加,删除、修改时不用加。
View Code
private void button2_Click(object sender, EventArgs e)
{
Create("D:\\text.xlsx", "测试","你,好");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
MessageBox.Show("ok");
}
private bool Create(string sFilePath, string sSheetName, string sCols)
{
try
{
var sConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties='Excel 12.0 XML;HDR=YES'", sFilePath);
using (var conn = new OleDbConnection(sConn))
{
conn.Open();
var cmd = new OleDbCommand { Connection = conn };
if (sSheetName.LastIndexOf('$') > 0)
{
sSheetName = sSheetName.Substring(sSheetName.Length - 1);
}
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 3600;
var sql = new StringBuilder();
sql.Append("CREATE TABLE [" + sSheetName + "](");
var arrCols = sCols.Split(',');
for (var i = 0; i < arrCols.Length; i++)
{
sql.Append("[" + arrCols[i] + "] text,");
}
sql = sql.Remove(sql.Length - 1, 1);
sql.Append(")");
cmd.CommandText = sql.ToString();
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
}
catch (Exception ex)
{
return false;
}
}
private bool Insert(string sFilePath, string sTable, string sSendPhone, string sSendCount)
{
var sConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties='Excel 12.0 XML'", sFilePath);
try
{
using (var conn = new OleDbConnection(sConn))
{
conn.Open();
var cmd = new OleDbCommand { Connection = conn };
var sCmdTxt = string.Format("Insert Into {0} Values('{1}','{2}')", sTable, sSendPhone, sSendCount);
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 3600;
cmd.CommandText = sCmdTxt;
cmd.ExecuteNonQuery();
conn.Close();
}
return true;
}
catch (Exception ex)
{
return false;
}
}
{
Create("D:\\text.xlsx", "测试","你,好");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
Insert("D:\\text.xlsx", "测试", "123123", "4567");
MessageBox.Show("ok");
}
private bool Create(string sFilePath, string sSheetName, string sCols)
{
try
{
var sConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties='Excel 12.0 XML;HDR=YES'", sFilePath);
using (var conn = new OleDbConnection(sConn))
{
conn.Open();
var cmd = new OleDbCommand { Connection = conn };
if (sSheetName.LastIndexOf('$') > 0)
{
sSheetName = sSheetName.Substring(sSheetName.Length - 1);
}
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 3600;
var sql = new StringBuilder();
sql.Append("CREATE TABLE [" + sSheetName + "](");
var arrCols = sCols.Split(',');
for (var i = 0; i < arrCols.Length; i++)
{
sql.Append("[" + arrCols[i] + "] text,");
}
sql = sql.Remove(sql.Length - 1, 1);
sql.Append(")");
cmd.CommandText = sql.ToString();
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
}
catch (Exception ex)
{
return false;
}
}
private bool Insert(string sFilePath, string sTable, string sSendPhone, string sSendCount)
{
var sConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties='Excel 12.0 XML'", sFilePath);
try
{
using (var conn = new OleDbConnection(sConn))
{
conn.Open();
var cmd = new OleDbCommand { Connection = conn };
var sCmdTxt = string.Format("Insert Into {0} Values('{1}','{2}')", sTable, sSendPhone, sSendCount);
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 3600;
cmd.CommandText = sCmdTxt;
cmd.ExecuteNonQuery();
conn.Close();
}
return true;
}
catch (Exception ex)
{
return false;
}
}