Home Database Mysql Tutorial Access 数据库和EXECL 的连接方法

Access 数据库和EXECL 的连接方法

Jun 07, 2016 pm 03:02 PM
access Can database method system connect

一些系统可能需求把数据导出到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


 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Jul 30, 2024 pm 02:17 PM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

How to convert deepseek pdf How to convert deepseek pdf Feb 19, 2025 pm 05:24 PM

DeepSeek cannot convert files directly to PDF. Depending on the file type, you can use different methods: Common documents (Word, Excel, PowerPoint): Use Microsoft Office, LibreOffice and other software to export as PDF. Image: Save as PDF using image viewer or image processing software. Web pages: Use the browser's "Print into PDF" function or the dedicated web page to PDF tool. Uncommon formats: Find the right converter and convert it to PDF. It is crucial to choose the right tools and develop a plan based on the actual situation.

Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Jun 02, 2024 pm 02:58 PM

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

How to read dbf file in oracle How to read dbf file in oracle May 10, 2024 am 01:27 AM

Oracle can read dbf files through the following steps: create an external table and reference the dbf file; query the external table to retrieve data; import the data into the Oracle table.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Aug 29, 2024 pm 03:30 PM

Recently, Huawei announced that it will launch a new smart wearable product equipped with Xuanji sensing system in September, which is expected to be Huawei's latest smart watch. This new product will integrate advanced emotional health monitoring functions. The Xuanji Perception System provides users with a comprehensive health assessment with its six characteristics - accuracy, comprehensiveness, speed, flexibility, openness and scalability. The system uses a super-sensing module and optimizes the multi-channel optical path architecture technology, which greatly improves the monitoring accuracy of basic indicators such as heart rate, blood oxygen and respiration rate. In addition, the Xuanji Sensing System has also expanded the research on emotional states based on heart rate data. It is not limited to physiological indicators, but can also evaluate the user's emotional state and stress level. It supports the monitoring of more than 60 sports health indicators, covering cardiovascular, respiratory, neurological, endocrine,

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

See all articles