.NET programming creates Access files and Excel files

xiaoxiao2021-03-05  25

Some systems may require data to export data to Access or Excel file format to facilitate data, printing, etc. Excel files or Access these two files that need to be exported may not exist in advance, which requires us to program themselves, and organize some of the methods of generating these two files, only Russen is most common. Not all.

First, first generate an Excel file. Solution 1. If you save it with Excel, just two-dimensional data, that is, use him as a database. The easiest, you don't have to reference any additional components, you only need to use OLEDB to complete the creation of an Excel file. Sample code is as follows.

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 = @ "Test Table";

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 displayComm = new oledbcommand ();

OcreateComm.connection = OCONN;

OcreateComm.commandtext = strcreatetablesql;

Oconn.open ();

OcreateComm.executenonQuery ();

Oconn.Close ();

}

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 = @ "Test Table";

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 displayComm = new oledbcommand ();

OcreateComm.connection = OCONN;

OcreateComm.commandtext = strcreatetablesql;

Oconn.open ();

OcreateComm.executenonQuery ();

Oconn.Close ();

}

While you perform the creation table, if you find that the Excel file does not exist, you will automatically create the creation of the Excel file. This may not be known if people who have never been touched.

As for the increase, modification operation, it is not described with the ordinary database. You can refer to the following article: http://www.cnblogs.com/meyer/archive/2004/2/08/6977.html

Solution 2, directly generate a plain text file that uses interval symbols, but the suffix of files is XLS.

Note: This time, if you open this file directly, no problem, everything is normal, but if you use ADO.NET to read this file, your link engine should not be Excel, but a text file (Microsoft Text driver. That is, the link string should not be "provider = microsoft.jet.OLEDB.4.0; Data Source = C: //aa2.xls; extended Properties = Excel 8.0;" should be the following method: OLEDB way connection string : Provider = microsoft.jet.OLEDb.4.0; data source = c: //11.txt; extended proties = 'text; hdr = no; fmt = tabdelimited'odbc method Read TXT string to write: driver = {Microsoft Text Driver (* .txt; * .csv)}; dbq = c: //11.txt; extensions = ASC, CSV, Tab, TXT; Please refer to the following article: http://www.codegurbu.com/cpp/cpp / crPp_managed/nfc/print.php/c8299/

Solution 3, you have to create an Excel file, there are some Excel's own features need to be created, which requires COM, namely: Microsoft Excel Object Library, please add Microsoft Excel 11.0 Object Library to it, according to your office The version, this component library version is different.

Sample code:

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 ();

}

I am just simply created an Excel file, there is no more operation Excel, if you want to see more operation methods, please refer to the following article: http://blog.9cbs.net/lt/thuiss/archive/2004/ 08/29 / 88341.aspxhttp: //support.microsoft.com/default.aspx? ScID = KB; EN-US; 306023 & product = VCSNET # 6HTTP: / /EXPERT.9CBS.NET/ExPERT/topic/3086/3086690. Xmlhttp: //expert.9cbs.net/expert/topic/3068/3068466.xml

2. Generating Access Database Access is a database, so the first method of Excel is unable to apply. Creating an Access Database File You can use Adox, Adox and OLEDB Differences: Adox is a Data API just an interface, OLEDB is a data provider, API to call the data provider. Sample code: Before use, add a reference Microsoft ADO Ext. 2.x for DDL and security According to your operating system, the version may be different.

Using

ADOX;

Using

System.IO;

public

Static

Void

CreateAccessFile

String

Filename)

...

{

IF (! file.exiss (filename))

... {

Adox.catalogclass cat = new adox.catalogclass ();

Cat.create ("provider = microsoft.jet.Oledb.4.0; data source =" filename ";");

Cat = NULL;

}

}

转载请注明原文地址:https://www.9cbs.com/read-35034.html

New Post(0)