excel相关的代码

 

string SheetName = "sheet1";
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\guanxi.xls" +
@";Extended Properties=""Excel 8.0;HDR=YES;""";
DbConnection connection = null;
DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.OleDb");

using (connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;


using (DbCommand command = connection.CreateCommand())
{
connection.Open();
try
{
command.CommandText = "CREATE TABLE [" + SheetName + "] (" +
"[phone] text,[msg] text,[result] text,[start] text,[spname] text,[ids] text)";

command.ExecuteNonQuery();
}
catch (Exception)
{
}


string strPath = Application.StartupPath;

// 用于保存没有标注Poi的路
FileStream fileUnMarkedPois = new FileStream(strPath + @"\error.txt",
FileMode.Create | FileMode.Append, FileAccess.Write);
StreamWriter swUnMarkedPois = new StreamWriter(fileUnMarkedPois,
System.Text.Encoding.GetEncoding("GB2312"));

string str;
int n = 0; ClientResult result;
DateTime t1 = DateTime.Now;
DateTime t2 = DateTime.Now;
for (int i = start; i < cl.Count && Running; i++)
{
CaseInfo info = null;
try
{

info = cl.Get(i);
if (info == null)
continue;
string temp = info.msg;
//sms.RecvMsg(int.Parse(info.moid), info.phoneNum, info.msg);
t1 = DateTime.Now;

command.CommandText = "INSERT INTO [" + SheetName + "$] (phone,msg,result,start,spname,ids) Values(";
command.CommandText += "'" + info.phoneNum + "',";
command.CommandText += "'" + SQLEscape(temp) + "',";
command.CommandText += "'" + SQLEscape(Result) + "',";
command.CommandText += "'" + ts.TotalMilliseconds.ToString() + "',";
command.CommandText += "'" + SQLEscape(result.SpName) + "',";
command.CommandText += "'" + SQLEscape(result.IDs) + "')";

command.ExecuteNonQuery();
str = i.ToString() + "/" + cl.Count.ToString();

textBoxInfo.Invoke(new DelegateTestAll(UpdateInfo), new object[] { str });
//n++;
//if (n >= 2000)
//{
// break;
//}

}
catch (Exception exp )
{

swUnMarkedPois.WriteLine(info.msg + " " + info.phoneNum);
swUnMarkedPois.WriteLine(exp.Message + exp.Source);
swUnMarkedPois.Flush();

}
}
swUnMarkedPois.Close();
fileUnMarkedPois.Close();


connection.Close();
}

}
}
catch (Exception exp)
{
// MessageBox.Show("打开excel出错");
throw exp;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值