• 使用ADO.net将数据导出到Excel并提供下载


    参考Meyer的文章写的一个例子,原文地址
    http://www.cnblogs.com/meyer/articles/6977.html

    项目下载

    public string DataTableToExcel(DataTable dt,string excelPath)
            
    {
                
    if(dt == null)
                
    {
                    
    return "DataTable不能为空";
                }


                
    int rows = dt.Rows.Count;
                
    int cols = dt.Columns.Count;
                StringBuilder sb;
                
    string connString;

                
    if(rows == 0)
                
    {
                    
    return "没有数据";
                }


                sb 
    = new StringBuilder();
                connString 
    = string.Format(ConnectionString,excelPath);

                
    //生成创建表的脚本
                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();

                    
    try
                    
    {
                        objConn.Open();
                        objCmd.ExecuteNonQuery();
                    }

                    
    catch(Exception e)
                    
    {
                        
    return "在Excel中创建表失败,错误信息:" + e.Message;
                    }


                    
    生成插入数据脚本


                    
    //建立插入动作的Command
                    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));
                    }


                    
    //遍历DataTable将数据插入新建的Excel文件中
                    foreach (DataRow row in dt.Rows)
                    
    {    
                        
    for (int i=0; i<param.Count; i++)
                        
    {
                            param[i].Value 
    = row[i]; 
                        }


                        objCmd.ExecuteNonQuery();
                    }


                    
    return "数据已成功导入Excel";
                }
    //end using
            }
  • 相关阅读:
    Amazon EBS的功能更新
    ORA-03113: end-of-file on communication channel
    云serverlinux又一次挂载指定文件夹(非扩充)
    Binder对象死亡通知机制
    cocos2d::ui::TextField 调用setAttachWithIME和setDetachWithIME都无效
    shell linux基本命令实例、笔记
    降智严重——nowcoder练习赛46&&codeforces #561 Div2
    2018-2-13-win10-uwp-隐藏实时可视化
    2018-2-13-win10-uwp-隐藏实时可视化
    2018-2-13-win10-UWP-ListView-模仿开始菜单
  • 原文地址:https://www.cnblogs.com/lingyun_k/p/298112.html
Copyright © 2020-2023  润新知