zoukankan      html  css  js  c++  java
  • 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;
        }
    

      

  • 相关阅读:
    目前阻碍大数据成功的常见问题有哪些
    YII2 的授权(Authorization)
    CCBPM工作流引擎的消息机制与设计
    日志框架实现实时改动,实时生效,详细框架思路(2)
    manacher hihoCoder1032 最长回文子串
    C#.NEt-GDI+中的Pen測试
    Unity3D游戏开发最佳实践20技巧(一)
    Android init.rc文件浅析
    OBIEE开发手冊
    Android Jni层 创建 linux socket 出错问题解决
  • 原文地址:https://www.cnblogs.com/chenghu/p/3117291.html
Copyright © 2011-2022 走看看