• DataSet导出到EXCEL


    string physicPath = HttpContext.Current.Server.MapPath(this.Context.Request.Path);//獲取路徑
            string fileName = Guid.NewGuid() + ".Xls";
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + physicPath + fileName + ";Extended Properties=Excel 8.0;";
            ToExcel(ds.Tables[0], connString);
            Response.Clear();
            Response.WriteFile(physicPath + fileName);
            string httpHeader = "attachment;filename=" + "iniu因你服饰" + "_" + string.Format("{0:yyyyMMdd}", DateTime.Today) +

    string.Format("{0:00}", DateTime.Now.Hour) + string.Format("{0:00}", DateTime.Now.Minute) + ".xls";
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition", httpHeader);
            Response.Flush();
            System.IO.File.Delete(physicPath + fileName);//下載后刪除創建的EXCEL
            Response.End();

    public void ToExcel(DataTable dt, string connString)
        {
            int rows = dt.Rows.Count;
            int cols = dt.Columns.Count;
            StringBuilder sb = new StringBuilder();
            sb.Append("CREATE TABLE ");
            sb.Append(dt.TableName + " ( ");
            for (int i = 0; i < cols; i++)
            {           
                if (i < cols - 1)
                   
                    sb.Append(string.Format("{0} varchar,", dt.Columns[i].ColumnName));
                else
                    sb.Append(string.Format("{0} varchar)", dt.Columns[i].ColumnName));
            }
            using (OleDbConnection objConn = new OleDbConnection(connString))
            {
                OleDbCommand objCmd = new OleDbCommand();
                objCmd.Connection = objConn;
                objCmd.CommandText = sb.ToString();
                objConn.Open();
                objCmd.ExecuteNonQuery();

                #region 開始處理資料內容的新增
                sb.Remove(0, sb.Length);
                sb.Append("INSERT INTO ");
                sb.Append(dt.TableName + " ( ");
                for (int i = 0; i < cols; i++)
                {
                    if (i < cols - 1)
                        sb.Append(dt.Columns[i].ColumnName + ",");
                    else
                        sb.Append(dt.Columns[i].ColumnName + ") values (");
                }
                for (int i = 0; i < cols; i++)
                {
                    if (i < cols - 1)
                        sb.Append("@" + dt.Columns[i].ColumnName + ",");
                    else
                        sb.Append("@" + dt.Columns[i].ColumnName + ")");
                }
                #endregion
                objCmd.CommandText = sb.ToString();
                OleDbParameterCollection param = objCmd.Parameters;
                for (int i = 0; i < cols; i++)
                {
                    param.Add(new OleDbParameter("@" + dt.Columns[i].ColumnName, OleDbType.VarChar));
                }
                foreach (DataRow row in dt.Rows)
                {
                    for (int i = 0; i < param.Count; i++)
                    {
                        param[i].Value = row[i];
                    }
                    objCmd.ExecuteNonQuery();
                }
            }
        }

  • 相关阅读:
    资源合并fis-postpackager-simple插件的使用
    FIS3使用官方例子流程
    FIS常用命令
    SASS输出风格
    Webstorm实时编译SASS和LESS
    如何使用Less?
    DNS预解析dns-prefetch提升页面载入速度优化前端性能
    前端性能优化策略
    利用多域名存储静态资源进行性能优化:网站的静态资源为什么要使用独立域名
    Nginx多域名配置
  • 原文地址:https://www.cnblogs.com/gerryge/p/2105221.html
Copyright © 2020-2023  润新知