Access 数据库和EXECL 的连接方法
一些系统可能需求把数据导出到Access或者Excel文件格式,以方便的传递数据、打印等。 Excel 文件或者 Access这两种需要导出的文件可能并不是事先就存在的,这就需要我们自己编程生成他们,下面整理一下生成这两个文件的一些 方法 ,只罗列最常用的。并不全。
一些系统可能需求把数据导出到Access或者Excel文件格式,以方便的传递数据、打印等。
Excel 文件或者 Access这两种需要导出的文件可能并不是事先就存在的,这就需要我们自己编程生成他们,下面整理一下生成这两个文件的一些方法,只罗列最常用的。并不全。
一、首先生成Excel文件。
方案一、如果用Excel保存的只是二维数据,也就是把他当数据库的来用。
最简单,你不用引用任何额外组件,只需要用 OLEDB 就可以完成创建Excel文件。 范例代码如下。
using System.Data.OleDb;
public static void CreateExcelFile2()
...{
string OLEDBConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\aa2.xls;";
OLEDBConnStr += " Extended Properties=Excel 8.0;";
string strCreateTableSQL = @" CREATE TABLE ";
strCreateTableSQL += @" 测试表 ";
strCreateTableSQL += @" ( ";
strCreateTableSQL += @" ID INTEGER, ";
strCreateTableSQL += @" UserID INTEGER, ";
strCreateTableSQL += @" UserIP VARCHAR , ";
strCreateTableSQL += @" PostTime DATETIME , ";
strCreateTableSQL += @" FromParm VARCHAR ";
strCreateTableSQL += @" ) ";
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = OLEDBConnStr;
OleDbCommand oCreateComm = new OleDbCommand();
oCreateComm.Connection = oConn;
oCreateComm.CommandText = strCreateTableSQL;
oConn.Open();
oCreateComm.ExecuteNonQuery();
oConn.Close();
}
在你执行创建表的同时,系统如果发现Excel文件不存在,就自动完成了Excel文件的创建。这点如果没接触过的人,可能会不知道的。
至于对其中的增加、修改操作, 跟普通数据库没啥两样,就不描述了。
可以参考以下文章:
http://www.cnblogs.com/meyer/archive/2004/12/08/6977.html
方案二、直接生成一个使用间隔符号隔开每一项数据的纯文本文件,但是文件的后缀是 XLS 。
注意:这时候,如果你直接用Excel打开这样的文件,没问题,一切正常,但是如果你用ADO.net 读取这个文件的时候,你的链接引擎不应该是Excel,而是文本文件(Microsoft Text Driver)。也就是链接字符串不应该是
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\aa2.xls;Extended Properties=Excel 8.0;"
而应该是下面的方式:
OLEDB的方式连接字符串:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\11.txt;Extended Properties='text;HDR=No;FMT=TabDelimited'
ODBC的方式读TXT字符串写法:
Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\\11.txt;Extensions=asc,csv,tab,txt;
请参考以下文章:
http://www.codeguru.com/Cpp/Cpp/cpp_managed/nfc/print.php/c8299/
方案三、你要创建的Excel文件,有一些Excel自己的特色需要创建,这就需要使用 Com 了,即:Microsoft Excel Object Library了
请添加 Microsoft Excel 11.0 Object Library 对它的引用,根据你装的Office的版本,这个组件库的版本也不一样。
范例代码:
public static void CreateExcelFile()
...{
string FileName = "c:\\aa.xls";
Missing miss = Missing.Value;
Excel.Application m_objExcel = new Excel.Application();
m_objExcel.Visible = false;
Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
Excel.Workbook m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
m_objBook.SaveAs(FileName, miss, miss, miss, miss,
miss, Excel.XlSaveAsAccessMode.xlNoChange, miss,
miss,miss, miss, miss);
m_objBook.Close(false, miss, miss);
m_objExcel.Quit();
}
我这里只是简单的创建了Excel文件,没有更多的操作Excel,如果希望看到更多的操作方法,请参考以下几篇文章:
http://blog.csdn.net/lluiss/archive/2004/08/29/88341.aspx
http://support.microsoft.com/default.aspx?scid=kb;en-us;306023&Product=vcSnet#6
http://expert.csdn.net/Expert/topic/3086/3086690.xml
http://expert.csdn.net/Expert/topic/3068/3068466.xml
二、生成Access 数据库
Access 毕竟是一个数据库,所以Excel上述第一种方法,无法适用。
创建Access 数据库文件可以使用 ADOX,
ADOX与OleDB的区别:ADOX是 data api 只是一个接口, OLEDB 是数据提供者,API 去调用 数据提供者。
范例代码:
使用前,请添加引用 Microsoft ADO Ext. 2.x for DDL and Security 根据你的操作系统,可能这里的版本也不一样。
using ADOX;
using System.IO;
public static void CreateAccessFile(string FileName)
...{
if(!File.Exists(FileName))
...{
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName +";");
cat = null;
}
}
上述代码只是生成了Access数据库,适用ADOX你也可以操作数据库,增加表等等操作,具体请参考以下文章:
http://blog.csdn.net/net_lover/archive/2004/06/08/6963.aspx
http://support.microsoft.com/kb/317881/EN-US/
http://study.99net.net/study/program/vb/1049955696.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

Apache server is a powerful web server software that acts as a bridge between browsers and website servers. 1. It handles HTTP requests and returns web page content based on requests; 2. Modular design allows extended functions, such as support for SSL encryption and dynamic web pages; 3. Configuration files (such as virtual host configurations) need to be carefully set to avoid security vulnerabilities, and optimize performance parameters, such as thread count and timeout time, in order to build high-performance and secure web applications.

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

Nginx performance monitoring and troubleshooting are mainly carried out through the following steps: 1. Use nginx-V to view version information, and enable the stub_status module to monitor the number of active connections, requests and cache hit rate; 2. Use top command to monitor system resource occupation, iostat and vmstat monitor disk I/O and memory usage respectively; 3. Use tcpdump to capture packets to analyze network traffic and troubleshoot network connection problems; 4. Properly configure the number of worker processes to avoid insufficient concurrent processing capabilities or excessive process context switching overhead; 5. Correctly configure Nginx cache to avoid improper cache size settings; 6. By analyzing Nginx logs, such as using awk and grep commands or ELK
