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

该博客给出参考Meyer文章所写的例子,提供了原文地址http://www.cnblogs.com/meyer/articles/6977.html ,还提及项目下载相关内容。

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

项目下载

None.gif public   string  DataTableToExcel(DataTable dt, string  excelPath)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(dt == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "DataTable不能为空";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int rows = dt.Rows.Count;
InBlock.gif            
int cols = dt.Columns.Count;
InBlock.gif            StringBuilder sb;
InBlock.gif            
string connString;
InBlock.gif
InBlock.gif            
if(rows == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "没有数据";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            sb 
= new StringBuilder();
InBlock.gif            connString 
= string.Format(ConnectionString,excelPath);
InBlock.gif
InBlock.gif            
//生成创建表的脚本
InBlock.gif
            sb.Append("CREATE TABLE ");
InBlock.gif            sb.Append(dt.TableName 
+ " ( ");
InBlock.gif
InBlock.gif            
for(int i=0;i<cols;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(i < cols - 1)
InBlock.gif                    sb.Append(
string.Format("{0} varchar,",dt.Columns[i].ColumnName));
InBlock.gif                
else
InBlock.gif                    sb.Append(
string.Format("{0} varchar)",dt.Columns[i].ColumnName));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
using(OleDbConnection objConn = new OleDbConnection(connString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OleDbCommand objCmd 
= new OleDbCommand();
InBlock.gif                objCmd.Connection 
= objConn;
InBlock.gif
InBlock.gif                objCmd.CommandText 
= sb.ToString();
InBlock.gif
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    objConn.Open();
InBlock.gif                    objCmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif                }

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

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                
生成插入数据脚本#region 生成插入数据脚本
InBlock.gif                sb.Remove(
0,sb.Length);
InBlock.gif                sb.Append(
"INSERT INTO ");
InBlock.gif                sb.Append(dt.TableName 
+ " ( ");
InBlock.gif
InBlock.gif                
for(int i=0;i<cols;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(i < cols - 1)
InBlock.gif                        sb.Append(dt.Columns[i].ColumnName 
+ ",");
InBlock.gif                    
else
InBlock.gif                        sb.Append(dt.Columns[i].ColumnName 
+ ") values (");
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
for(int i=0;i<cols;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(i < cols - 1)
InBlock.gif                        sb.Append(
"@" + dt.Columns[i].ColumnName + ",");
InBlock.gif                    
else
InBlock.gif                        sb.Append(
"@" + dt.Columns[i].ColumnName + ")");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
InBlock.gif                
//建立插入动作的Command
InBlock.gif
                objCmd.CommandText = sb.ToString();
InBlock.gif                OleDbParameterCollection param 
= objCmd.Parameters;
InBlock.gif
InBlock.gif                
for(int i=0;i<cols;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    param.Add(
new OleDbParameter("@" + dt.Columns[i].ColumnName, OleDbType.VarChar));
ExpandedSubBlockEnd.gif                }

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

InBlock.gif
InBlock.gif                    objCmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
return "数据已成功导入Excel";
ExpandedSubBlockEnd.gif            }
//end using
ExpandedBlockEnd.gif
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值