代码语言
.
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
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
Access Excel Dbf转换的类
作者:
许华
/ 发布于
2015/3/24
/
751
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.IO; namespace TransDBFClass { public class TransDBFClass { /// <summary> /// 将Access数据库转换为Excel表格 /// </summary> /// <param name="DbPath">Access数据库所在文件路径</param> public void AccessToExcel(string DbPath) { //OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\project\TransDBF\db.mdb;"); OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath + ";"); try { conn.Open(); DataTable dt = conn.GetSchema("Tables");//获取ACCESS数据库中所有的表\查询\宏\窗体\模块 string p = "c:\\dbf"; if (!System.IO.Directory.Exists(p)) { System.IO.Directory.CreateDirectory(p); } string[] vFiles = Directory.GetFiles(p); foreach (string vFile in vFiles) File.Delete(vFile); for (int i = 0; i < dt.Rows.Count; i++) { try { string tablename = ""; if (dt.Rows[i].ItemArray[3].ToString() == "TABLE")//判断是否是用户表 { tablename = dt.Rows[i].ItemArray[2].ToString();//获取数据表名 } //转换Access为dbf格式数据sql语句 // String sql = "SELECT * INTO [dBASE 5.0; Database=C:\\dbf;]." + tablename + ".dbf FROM " + tablename; String sql = "SELECT * INTO [Excel 5.0;Database=C:\\dbf\\dbf.xls ]." + "[" + tablename + "] FROM " + tablename; OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); } catch (Exception ex) { } } } catch (Exception ex) { File.AppendAllText(@"c:\1.txt",ex.ToString()); } finally { conn.Close(); } } /// <summary> /// 将已经转换了的Excel表转换为Dbf格式方法 /// </summary> public void ExcelToDbf() { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\dbf\dbf.xls;Extended Properties=Excel 8.0;"); try { conn.Open(); DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); for (int i = 0; i < dt.Rows.Count; i++) { try { string tablename = ""; if (dt.Rows[i].ItemArray[3].ToString() == "TABLE")//判断是否是用户表 { tablename = dt.Rows[i].ItemArray[2].ToString();//获取数据表名 } //转换Excel为dbf格式数据sql语句 String sql = "SELECT * INTO [dBASE 5.0; Database=C:\\dbf;].DBF" + i + ".dbf FROM " + tablename; // String sql = "SELECT * INTO [Excel 5.0;Database=C:\\dbf\\dbf.xls ]." + "[" + tablename + "] FROM " + tablename; if (tablename.IndexOf('$') >= 0) { continue; } else { OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); } } catch (Exception ex) { } } } catch (Exception ex) { File.AppendAllText(@"c:\2.txt",ex.ToString()); } finally { conn.Close(); } } } } 还有一种带密码访问的 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.IO; namespace TransDBFClass { public class TransDBFClass { /// <summary> /// 将Access数据库转换为Excel表格 /// </summary> /// <param name="DbPath">Access数据库所在文件路径</param> public void AccessToExcel(string DbPath) { //OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\project\TransDBF\db.mdb;"); //OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath + ";Persist Security Info=False;Jet OLEDB:Database Password=sa;"); OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath + ";Jet OLEDB:Database Password=sa;"); try { conn.Open(); DataTable dt = conn.GetSchema("Tables");//获取ACCESS数据库中所有的表\查询\宏\窗体\模块 string p = "d:\\dbf"; if (!System.IO.Directory.Exists(p)) { System.IO.Directory.CreateDirectory(p); } string[] vFiles = Directory.GetFiles(p); foreach (string vFile in vFiles) File.Delete(vFile); for (int i = 0; i < dt.Rows.Count; i++) { try { string tablename = ""; if (dt.Rows[i].ItemArray[3].ToString() == "TABLE")//判断是否是用户表 { tablename = dt.Rows[i].ItemArray[2].ToString();//获取数据表名 } //转换Access为dbf格式数据sql语句 // String sql = "SELECT * INTO [dBASE 5.0; Database=C:\\dbf;]." + tablename + ".dbf FROM " + tablename; String sql = "SELECT * INTO [Excel 5.0;Database=d:\\dbf\\dbf.xls ]." + "[" + tablename + "] FROM " + tablename; OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); } catch (Exception ex) { } } } catch (Exception ex) { File.AppendAllText(@"c:\1.txt",ex.ToString()); } finally { conn.Close(); } } /// <summary> /// 将已经转换了的Excel表转换为Dbf格式方法 /// </summary> public void ExcelToDbf() { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\dbf\dbf.xls;Extended Properties=Excel 8.0;"); try { conn.Open(); DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); for (int i = 0; i < dt.Rows.Count; i++) { try { string tablename = ""; if (dt.Rows[i].ItemArray[3].ToString() == "TABLE")//判断是否是用户表 { tablename = dt.Rows[i].ItemArray[2].ToString();//获取数据表名 } //转换Excel为dbf格式数据sql语句 String sql = "SELECT * INTO [dBASE 5.0; Database=d:\\dbf;].DBF" + i + ".dbf FROM " + tablename; // String sql = "SELECT * INTO [Excel 5.0;Database=C:\\dbf\\dbf.xls ]." + "[" + tablename + "] FROM " + tablename; if (tablename.IndexOf('$') >= 0) { continue; } else { OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.ExecuteNonQuery(); } } catch (Exception ex) { } } } catch (Exception ex) { File.AppendAllText(@"c:\2.txt",ex.ToString()); } finally { conn.Close(); } } } }
试试其它关键字
转换类
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
许华
贡献的其它代码
(
7
)
.
DEV searchLookUpEdit 控件使用
.
Dev xtraControl 动态改变单元格、行背景色
.
Access转换为DBF
.
Access Excel Dbf转换的类
.
dbf 转换Access
.
修改dbf列名
.
获得Access数据库中的表名,以及根据表名获取字段名
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3