Implementation method of importing Excel into SQL database using C programming language

PHPz
Release: 2024-01-17 12:30:15
forward
876 people have browsed it

Implementation method of importing Excel into SQL database using C programming language

How to import excel into sql database using code in C

///

/// Read data from Excel

///

/// Path

/// DataSet

public DataSet ImportFromExcel(string filePath)

{

DataSet ds = new DataSet();

string connString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " filePath ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""";

DataTable table = OleDbHelper.GetExcelTables(connString);

if(table == null || table.Rows.Count

{

return null;

}

foreach(DataRow dr in table.Rows)

{

string cmdText = "select * from [" dr["TABLE_NAME"].ToString() "]";

DataTable dt = OleDbHelper.FillDataTable(connString, cmdText);

dt.TableName = dr["TABLE_NAME"].ToString();

ds.Tables.Add(dt);

}

return ds;

}

Next, just write the DataSet into the database

How to import excel spreadsheet into visual foxpro database

There is no better way to import excel tables into vfp, but it can be achieved by reading excel.

The following is an example of vfp directly reading an EXCEL file:

M_File=GETFILE('xls','Order File')

IF M_File=""

=MESSAGEBOX ("Please select the order receipt file to be imported!", 0 48, "Prompt")

RETURN

ELSE

IF JUSTEXT(M_FILE)#"XLS" &Get file extension

=MESSAGEBOX ("The document you selected is not an EXCEL document!", 0 48, "Prompt")

RETURN

ENDIF

ENDIF

USE Book Order Form

ZAP

myexcel=createobject('excel.application') &Create an object

myexcel.visible=.f.&invisible

bookexcel=myexcel.workbooks.open(M_File) &Open the specified file

o_SheetName=myexcel.application.ActiveSheet.Name & Get the name of the currently active worksheet

UsedRange =bookexcel.worksheets(o_SheetName).UsedRange & Returns the area that can be used in the worksheet, the properties of the UsedRange table

o_rows=UsedRange.rows.count &summary rows

o_cols=UsedRange.columns.count&summary column

IF o_rows

=MESSAGEBOX ("The number of data rows to be imported is too few, please check!", 0 16, "Prompt")

ELSE

FOR i=2 TO o_rows

m1=myexcel.cells(i,1).value &ISBN

m2=myexcel.cells(i,2).value & book title

m3=myexcel.cells(i,3).value &Publisher

m4=myexcel.cells(i,4).value &author

m5=myexcel.cells(i,5).value &pricing

m6=myexcel.cells(i,6).value & quantity

APPEND BLANK

REPLACE ISBN WITH m1, book title WITH m2, publisher WITH m3, author WITH m4, price WITH m5, quantity WITH m6

ENDFOR

ENDIF

myexcel.workbooks.close &Close workspace

myexcel.quit &Close excel

brow

The above is the detailed content of Implementation method of importing Excel into SQL database using C programming language. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!