代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
完全无组件,不用调用Excel.exe 导出数据到Excel
作者:
CSDN
/ 发布于
2012/1/11
/
919
<div><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Text; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Data; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Data.OleDb; </span><span style="color: rgb(0,0,255)">namespace</span><span style="color: rgb(0,0,0)"> PowerAgent.DBUtility { </span><span style="color: rgb(0,128,0)">/**/</span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> DataToExcel 的摘要说明。 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> DataToExcel 的摘要说明。 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> DataToExcel { </span><span style="color: rgb(0,0,255)">const</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> ConnectionString </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> DataToExcel() { } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> DataTableToExcel(DataTable dt, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> excelPath) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (dt </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">DataTable不能为空</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> rows </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> dt.Rows.Count; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> cols </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> dt.Columns.Count; StringBuilder sb; </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> connString; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (rows </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">没有数据</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; } sb </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> StringBuilder(); connString </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.Format(ConnectionString, excelPath); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">生成创建表的脚本</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> sb.Append(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">CREATE TABLE </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); sb.Append(dt.TableName </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"> ( </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> cols; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> cols </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">) sb.Append(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.Format(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">{0} varchar,</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, dt.Columns.ColumnName)); </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)"> sb.Append(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.Format(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">{0} varchar)</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, dt.Columns.ColumnName)); } </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> (OleDbConnection objConn </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> OleDbConnection(connString)) { OleDbCommand objCmd </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> OleDbCommand(); objCmd.Connection </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> objConn; objCmd.CommandText </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> sb.ToString(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { objConn.Open(); objCmd.ExecuteNonQuery(); } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception e) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">在Excel中创建表失败,错误信息:</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> e.Message; } 生成插入数据脚本</span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> 生成插入数据脚本</span><span style="color: rgb(0,0,0)"> sb.Remove(</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, sb.Length); sb.Append(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">INSERT INTO </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); sb.Append(dt.TableName </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"> ( </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> cols; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> cols </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">) sb.Append(dt.Columns.ColumnName </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">,</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)"> sb.Append(dt.Columns.ColumnName </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">) values (</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); } </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> cols; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> cols </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">) sb.Append(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">@</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> dt.Columns.ColumnName </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">,</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)"> sb.Append(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">@</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> dt.Columns.ColumnName </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">)</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">建立插入动作的Command</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> objCmd.CommandText </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> sb.ToString(); OleDbParameterCollection param </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> objCmd.Parameters; </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> cols; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { param.Add(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> OleDbParameter(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">@</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> dt.Columns.ColumnName, OleDbType.VarChar)); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">遍历DataTable将数据插入新建的Excel文件中</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (DataRow row </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> dt.Rows) { </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> param.Count; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { param.Value </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> row; } objCmd.ExecuteNonQuery(); } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">数据已成功导入Excel</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; }</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">end using</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } }</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">end class</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">} </span></div>
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
CSDN
贡献的其它代码
(
27
)
.
xml操作类
.
完全无组件,不用调用Excel.exe 导出数据到Excel
.
DropDownList手动构造一个下拉树形
.
判断是否低于IE6的版本
.
javascript 鼠标拖拉画图
.
取汉字首字母
.
字符串格式化通用类
.
mssql海量高效分页存储过程
.
.net 获取远程IP 得到省市
.
javascript 模仿windows拖动 封装类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3