InsertIntoExcelEx
private bool InsertIntoExcelEx(System.Data.DataTable dt)
{
string baseFileName = "SignCustomers";
string mPath = System.IO.Path.GetTempPath() + System.IO.Path.GetTempFileName().Replace(".tmp", "") + ".xls";
mPath = Server.MapPath("~/Temp") + "\\" + baseFileName + "JJLTemp.xls";
System.IO.File.Copy(Server.MapPath("~/Temp") + "\\" + baseFileName + "JJL.xls", Server.MapPath("~/Temp") + "\\" + baseFileName + "JJLTemp.xls", true);
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + mPath + ";" + "Extended Properties=Excel 5.0;";
string strExcel = "insert into [签约客户$] (区域公司,部门,项目部,咨询,客户姓名,合同号,状态,签约日期,电话,籍贯,出生日期,媒体来源,注册方式) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')";
bool isSuccessful = false;
string tempID = "";
try
{
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
foreach (DataRow row in dt.Rows)
{
string appdate = "";
string sql = string.Format(strExcel, row["区域"], row["部门"], row["项目部"], row["咨询"]
, row["客户姓名"], row["合同号"], row["状态"], !string.IsNullOrEmpty(row["签约日期"].ToString()) ? DateTime.Parse(row["签约日期"].ToString()).ToString("yyyy-MM-dd") : "", row["电话"], row["籍贯"], !string.IsNullOrEmpty(row["出生日期"].ToString()) ? DateTime.Parse(row["出生日期"].ToString()).ToString("yyyy-MM-dd") : "", row["媒体来源"], row["注册方式"]);
tempID = sql;
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
int num = cmd.ExecuteNonQuery();
if (num == 1)
{
isSuccessful = true;
}
}
catch (Exception ex)
{
string kk = "";
}
}
conn.Close();
}
}
catch (Exception erorr)
{
throw;
}
Random r=new Random();
int rand = r.Next(10000, 99999);
string str = DateTime.Now.ToString("yyyyMMddHHmmssff") + rand.ToString();
string fileName = "JJL(" + str + ")";
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
this.Response.ContentType = "Accept-Encoding: x-compress; x-zip";
System.IO.FileStream fs = new FileStream(Server.MapPath("~/Temp") + "\\" + baseFileName + "JJLTemp.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Delete);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
fs.Close();
if (buffer != null)
{
Response.BinaryWrite(buffer);
}
Response.Flush();
Response.Close();
return isSuccessful;
}
本文来自博客园,作者:程序猿网友666,转载请注明原文链接:https://www.cnblogs.com/chenghu/archive/2013/06/04/3117291.html

浙公网安备 33010602011771号